# Path: IoTPy/errors.py
# class IoTPy_APIError(Exception):
#     pass
# 
# Path: IoTPy/pinmaps.py
# CAP_GPIO     = 0x1
# 
# 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.errors import IoTPy_APIError
from IoTPy.pinmaps import CAP_GPIO
from IoTPy.sfp import encode_sfp


class OneWire(object):

    def __init__(self, board, pin):
        self.board = board
        if self.board.pinout[pin].capabilities & CAP_GPIO:
            self.logical_pin = self.board.pinout[pin].pinID
        else:
            raise IoTPy_APIError("Trying to assign OneWire function to non GPIO pin.")

        #self.board.low_level_io(0, encode_sfp(1, [self.logical_pin]))  # set primary
        #self.board.low_level_io(0, encode_sfp(3, [self.logical_pin, 1])) # gpio mode output
        self.board.low_level_io(0, encode_sfp(100, [self.logical_pin]))

    def __enter__(self):
        return self

    def trans(self, data):

# Path: IoTPy/errors.py
# class IoTPy_APIError(Exception):
#     pass
# 
# Path: IoTPy/pinmaps.py
# CAP_GPIO     = 0x1
# 
# 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/onewire.py
from IoTPy.errors import IoTPy_APIError
from IoTPy.pinmaps import CAP_GPIO
from IoTPy.sfp import encode_sfp



class OneWire(object):

    def __init__(self, board, pin):
        self.board = board
        if self.board.pinout[pin].capabilities & CAP_GPIO:
            self.logical_pin = self.board.pinout[pin].pinID
        else:
            raise IoTPy_APIError("Trying to assign OneWire function to non GPIO pin.")

        #self.board.low_level_io(0, encode_sfp(1, [self.logical_pin]))  # set primary
        #self.board.low_level_io(0, encode_sfp(3, [self.logical_pin, 1])) # gpio mode output
        self.board.low_level_io(0, encode_sfp(100, [self.logical_pin]))

    def __enter__(self):
        return self

    def trans(self, data):

        self.board.low_level_io(0, encode_sfp(101, [data]))