# 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
PWM_FREQUENCY = 50  # Hz


def _calculate_value_in_range(min_val, max_val, percentage):
    """Get the value within a range based on percentage.

    Example:
        A percentage 0.0 maps to min_val
        A percentage of 1.0 maps to max_val
    """
    value_range = max_val - min_val
    return min_val + int(percentage * value_range)


class Motor:
    """An interface class to allow simple acces to motor functions"""

    def __init__(self, name, rc_channel, motor_type=T100):
        self.name = name
        self.rc_channel = rc_channel
        if motor_type == T100:
            self.pwm_map = T100_PWM_MAP
        elif motor_type == SERVO:
            self.pwm_map = SERVO_PWM_MAP
        else:
            raise ValueError('Unknown motor_type')

        self._speed = 0
        self.duty_cycle_ms = self.pwm_map['stopped'] / 1000


# 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

PWM_FREQUENCY = 50  # Hz


def _calculate_value_in_range(min_val, max_val, percentage):
    """Get the value within a range based on percentage.

    Example:
        A percentage 0.0 maps to min_val
        A percentage of 1.0 maps to max_val
    """
    value_range = max_val - min_val
    return min_val + int(percentage * value_range)


class Motor:
    """An interface class to allow simple acces to motor functions"""

    def __init__(self, name, rc_channel, motor_type=T100):
        self.name = name
        self.rc_channel = rc_channel
        if motor_type == T100:
            self.pwm_map = T100_PWM_MAP
        elif motor_type == SERVO:
            self.pwm_map = SERVO_PWM_MAP
        else:
            raise ValueError('Unknown motor_type')

        self._speed = 0
        self.duty_cycle_ms = self.pwm_map['stopped'] / 1000


        self.pwm = PWM(self.rc_channel - 1)