# Path: IoTPy/pinmaps.py
# CAP_PWM      = 0x4
# 
# Path: IoTPy/errors.py
# class IoTPy_APIError(Exception):
#     pass
# 
# def errmsg(fmt, *args):
#     if not fmt.endswith('\n'):
#         fmt += '\n'
#     sys.stderr.write(os.path.basename(sys.argv[0]))
#     sys.stderr.write(': ')
#     if len(args):
#         sys.stderr.write(fmt % args)
#     else:
#         sys.stderr.write(fmt)
#     sys.stderr.flush()
# 
# Path: IoTPy/sfp.py
# def encode_sfp(command, args):
#     """
#     Construct binary SFP command.
# 
#     :param command: SFP command ID.
#     :type command: int
#     :param args: A list of SFP arguments, which can be either an integer or a byte collection (string).
#     :type args: list
#     :return: Binary SFP command.
#     :rtype: str
#     """
#     functions = {
#         binary_type: _encode_bytes,
#         integer_types[0]: _encode_int   # [0] - kinda hack to get class int
#     }
# 
#     sfp_command = pack('B', command) + b''.join(functions[type(arg)](arg) for arg in args)
#     sfp_command = b'\xd4' + pack('>H', len(sfp_command)) + sfp_command
#     return sfp_command


from IoTPy.pinmaps import CAP_PWM
from IoTPy.errors import IoTPy_APIError, errmsg
from IoTPy.sfp import encode_sfp
    """

    _PWM_PORT_FUNCTIONS = [[50,51,52],[60,61,62]]
    _PWM_PORT_MAX = [0xffff, 0xffffffff]

    def __init__(self, board, pin, freq=100, polarity=1):
        self.board = board
        if self.board.pinout[pin].capabilities & CAP_PWM:
            self.logical_pin = self.board.pinout[pin].pinID
        else:
            errmsg("UPER API: Pin No:%d is not a PWM pin.", pin)
            raise IoTPy_APIError("Trying to assign PWM function to non PWM pin.")
        self.pwm_port = self.board.pinout[pin].extra[0]
        self.pwm_pin = self.board.pinout[pin].extra[1]
        self.primary = True
        self.pulse_time = 0
        self.polarity = polarity
        PWM_PORT_RUNNING[self.pwm_port]['channels'] += 1
        if PWM_PORT_RUNNING[self.pwm_port]['channels'] == 1:
            self.set_frequency(freq)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        PWM_PORT_RUNNING[self.pwm_port]['channels'] -= 1
        self.primary = True
        self.board.low_level_io(0, encode_sfp(1, [self.logical_pin]))  # set pin primary function
        if PWM_PORT_RUNNING[self.pwm_port]['channels'] == 0:
            PWM_PORT_RUNNING[self.pwm_port]['period'] = 0

# Path: IoTPy/pinmaps.py
# CAP_PWM      = 0x4
# 
# Path: IoTPy/errors.py
# class IoTPy_APIError(Exception):
#     pass
# 
# def errmsg(fmt, *args):
#     if not fmt.endswith('\n'):
#         fmt += '\n'
#     sys.stderr.write(os.path.basename(sys.argv[0]))
#     sys.stderr.write(': ')
#     if len(args):
#         sys.stderr.write(fmt % args)
#     else:
#         sys.stderr.write(fmt)
#     sys.stderr.flush()
# 
# Path: IoTPy/sfp.py
# def encode_sfp(command, args):
#     """
#     Construct binary SFP command.
# 
#     :param command: SFP command ID.
#     :type command: int
#     :param args: A list of SFP arguments, which can be either an integer or a byte collection (string).
#     :type args: list
#     :return: Binary SFP command.
#     :rtype: str
#     """
#     functions = {
#         binary_type: _encode_bytes,
#         integer_types[0]: _encode_int   # [0] - kinda hack to get class int
#     }
# 
#     sfp_command = pack('B', command) + b''.join(functions[type(arg)](arg) for arg in args)
#     sfp_command = b'\xd4' + pack('>H', len(sfp_command)) + sfp_command
#     return sfp_command


# Path: IoTPy/interfaces/pwm.py
from IoTPy.pinmaps import CAP_PWM
from IoTPy.errors import IoTPy_APIError, errmsg
from IoTPy.sfp import encode_sfp

    """

    _PWM_PORT_FUNCTIONS = [[50,51,52],[60,61,62]]
    _PWM_PORT_MAX = [0xffff, 0xffffffff]

    def __init__(self, board, pin, freq=100, polarity=1):
        self.board = board
        if self.board.pinout[pin].capabilities & CAP_PWM:
            self.logical_pin = self.board.pinout[pin].pinID
        else:
            errmsg("UPER API: Pin No:%d is not a PWM pin.", pin)
            raise IoTPy_APIError("Trying to assign PWM function to non PWM pin.")
        self.pwm_port = self.board.pinout[pin].extra[0]
        self.pwm_pin = self.board.pinout[pin].extra[1]
        self.primary = True
        self.pulse_time = 0
        self.polarity = polarity
        PWM_PORT_RUNNING[self.pwm_port]['channels'] += 1
        if PWM_PORT_RUNNING[self.pwm_port]['channels'] == 1:
            self.set_frequency(freq)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        PWM_PORT_RUNNING[self.pwm_port]['channels'] -= 1
        self.primary = True
        self.board.low_level_io(0, encode_sfp(1, [self.logical_pin]))  # set pin primary function
        if PWM_PORT_RUNNING[self.pwm_port]['channels'] == 0:
            PWM_PORT_RUNNING[self.pwm_port]['period'] = 0

            self.board.low_level_io(0, encode_sfp(PWM._PWM_PORT_FUNCTIONS[self.pwm_port][2], [self.pwm_pin]))