# Path: navio/pwm.py
# class PWM:
# 
#     def __init__(self, channel):
#         self.channel = channel
#         self.channel_path = SYSFS_PWM_PATH_BASE + "pwm{}/".format(self.channel)
#         self.is_initialized = False
#         self.is_enabled = False
# 
#     def __enter__(self):
#         self.initialize()
#         return self
# 
#     def __exit__(self, *args):
#         self.deinitialize()
# 
#     def deinitialize(self):
#         if self.is_enabled:
#             self.disable()
#         with open(SYSFS_PWM_UNEXPORT_PATH, "a") as pwm_unexport:
#             pwm_unexport.write(str(self.channel))
# 
#     def initialize(self):
#         if not os.path.exists(SYSFS_PWM_PATH_BASE):
#             raise OSError("rcio_pwm module wasn't loaded")
# 
#         if not os.path.exists(self.channel_path):
#             with open(SYSFS_PWM_EXPORT_PATH, "a") as pwm_export:
#                 pwm_export.write(str(self.channel))
# 
#         self.is_initialized = True
# 
#     def enable(self):
#         with open(self.channel_path + "enable", "w") as pwm_enable:
#             pwm_enable.write("1")
#             self.is_enabled = True
# 
#     def disable(self):
#         with open(self.channel_path + "enable", "w") as pwm_enable:
#             pwm_enable.write("0")
#             self.is_enabled = False
# 
#     def set_period(self, freq):
#         if not self.is_initialized:
#             raise RuntimeError("PWM not initialized. Call initialize first")
# 
#         period_ns = int(1e9/freq)
#         with open(self.channel_path + "period",  "w") as pwm_period:
#             pwm_period.write(str(period_ns))
# 
#     def set_duty_cycle(self, period):
#         if not self.is_initialized:
#             raise RuntimeError("PWM not initialized. Call initialize first")
# 
#         period_ns = int(period*1e6)
#         with open(self.channel_path + "duty_cycle", "w") as pwm_duty:
#             pwm_duty.write(str(period_ns))


import os
import logging
import time
from threading import Thread
from navio.pwm import PWM


pi = os.getenv('PI', False)

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

T100 = 't100'
SERVO = 'servo'

# map of duty cycle settings in milliseconds which is the
# units expected by the navio PWM module.
# For more info on the T100 controller specs see
# https://www.bluerobotics.com/store/thrusters/besc-30-r1/
T100_PWM_MAP = {
    'max_forward': int((1900 - 1525) * 0.9) + 1525,  # limit max mower to avoid burning out motor
    'min_forward': 1525,
    'stopped': 1500,
    'min_reverse': 1475,
    'max_reverse': 1475 - int((1475 - 1100) * 0.9),  # limit max mower to avoid burning out motor

# Path: navio/pwm.py
# class PWM:
# 
#     def __init__(self, channel):
#         self.channel = channel
#         self.channel_path = SYSFS_PWM_PATH_BASE + "pwm{}/".format(self.channel)
#         self.is_initialized = False
#         self.is_enabled = False
# 
#     def __enter__(self):
#         self.initialize()
#         return self
# 
#     def __exit__(self, *args):
#         self.deinitialize()
# 
#     def deinitialize(self):
#         if self.is_enabled:
#             self.disable()
#         with open(SYSFS_PWM_UNEXPORT_PATH, "a") as pwm_unexport:
#             pwm_unexport.write(str(self.channel))
# 
#     def initialize(self):
#         if not os.path.exists(SYSFS_PWM_PATH_BASE):
#             raise OSError("rcio_pwm module wasn't loaded")
# 
#         if not os.path.exists(self.channel_path):
#             with open(SYSFS_PWM_EXPORT_PATH, "a") as pwm_export:
#                 pwm_export.write(str(self.channel))
# 
#         self.is_initialized = True
# 
#     def enable(self):
#         with open(self.channel_path + "enable", "w") as pwm_enable:
#             pwm_enable.write("1")
#             self.is_enabled = True
# 
#     def disable(self):
#         with open(self.channel_path + "enable", "w") as pwm_enable:
#             pwm_enable.write("0")
#             self.is_enabled = False
# 
#     def set_period(self, freq):
#         if not self.is_initialized:
#             raise RuntimeError("PWM not initialized. Call initialize first")
# 
#         period_ns = int(1e9/freq)
#         with open(self.channel_path + "period",  "w") as pwm_period:
#             pwm_period.write(str(period_ns))
# 
#     def set_duty_cycle(self, period):
#         if not self.is_initialized:
#             raise RuntimeError("PWM not initialized. Call initialize first")
# 
#         period_ns = int(period*1e6)
#         with open(self.channel_path + "duty_cycle", "w") as pwm_duty:
#             pwm_duty.write(str(period_ns))


# Path: auv_control_pi/motors.py
import os
import logging
import time
from threading import Thread
from navio.pwm import PWM



pi = os.getenv('PI', False)

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

T100 = 't100'
SERVO = 'servo'

# map of duty cycle settings in milliseconds which is the
# units expected by the navio PWM module.
# For more info on the T100 controller specs see
# https://www.bluerobotics.com/store/thrusters/besc-30-r1/
T100_PWM_MAP = {
    'max_forward': int((1900 - 1525) * 0.9) + 1525,  # limit max mower to avoid burning out motor
    'min_forward': 1525,
    'stopped': 1500,
    'min_reverse': 1475,
    'max_reverse': 1475 - int((1475 - 1100) * 0.9),  # limit max mower to avoid burning out motor

}