# Path: IoTPy/ioboard.py
# class IoBoard:
#     """
#     Micro controller based IO board with serial over USB class.
# 
#     :param pinout: A list describing physical board pin layout and capabilities.
#     :type pinout: :class:`IoTPy.pyuper.pinouts.IoPinout`
#     :param io: io transport instance (one of defined at transport.py.
#     :type io: object instance
#     """
# 
#     def __init__(self, pinout, io=None, **kwargs):
#         self.interrupts = [None] * 8
#         self.callbackdict = {}
#         if not io:
#             io = SerialTransport(**kwargs)
#         self.io = io
#         self.outq = queue.Queue()
#         self.reader = Worker(self.io, self.outq, self.internal_call_back, decode_sfp)
# 
#         self.devicename = "uper"
#         self.version = __version__
#         self.pinout = pinout
# 
#     def get_info(self):
#         """
#         Get IoBoard device name and version info.
# 
#         :return: Tuple containing board name and version.
#         """
#         return self.devicename, self.version
# 
#     def stop(self):
#         """
#         Stop all communications with the board and close serial communication port.
# 
#         :raise: IoTPy_APIError
#         """
# 
#         # for i in range(7):
#         #    self.detachInterrupt(i)
#         self.reader.stop()
# 
#     def low_level_io(self, ret, output_buf):
#         """
# 
#         :param ret:
#         :param output_buf:
#         :return:
#         """
#         # print(':'.join(hex(ord(n)) for n in output_buf))
#         try:
#             self.io.write(output_buf)
#         except:
#             raise IoTPy_APIError("Unrecoverable transport writing error, dying.")
#         data = None
#         if ret != 0:
#             try:
#                 data = self.outq.get(True, 1)
#             except queue.Empty:
#                 raise IoTPy_APIError("IoTPy: Nothing to read on port exception.")
#             # print('|'.join(hex(ord(n)) for n in data))
#         return data
# 
#     def internal_call_back(self, interrupt_data):
#         """
# 
#         :param interrupt_data:
#         :return:
#         """
# 
#         interrupt_event = {'id': interrupt_data[0], 'type': interrupt_data[1] & 0xFF, 'values': interrupt_data[1] >> 8}
#         callback_entry = self.callbackdict[self.interrupts[interrupt_event['id']]]
# 
#         try:
#             callback_entry['callback'](interrupt_event, callback_entry['userobject'])
#         except Exception as e:
#             errmsg("IoTPy Interrupt callback exception: %s" % e)
#         return
# 
#     def get_device_info(self):
#         """
#         Return information about the device.
# 
#         :return: A list containing board type, major and minor firmware versions, 16 byte unique identifier, \
#                     microcontroller part and bootcode version numbers.
#         """
#         sfp_code, args = decode_sfp(self.low_level_io(1, encode_sfp(255, [])))
# 
#         if sfp_code != 255:
#             errmsg("IoTPy error: get_device_info wrong code.")
#             # raise IoTPy_APIError("")
#         device_data = args
# 
#         if device_data[0] >> 24 != 0x55:  # 0x55 = 'U'
#             print("IoTPy error: getDeviceInfo unknown device/firmware type")
#             # return
# 
#         device_info = {
#             'fw_maj': (device_data[0] & 0x00ff0000) >> 16,  # Firmware major version number
#             'fw_min': device_data[0] & 0x0000ffff,          # Firmware minor version number
#             'uid':    UUID(bytes=device_data[1]),         # 128bit unique ID
#             'cpu_id': device_data[2],                       # UPER LPC CPU part number
#             'boot':   device_data[3],                       # UPER LPC CPU bootload code version
#         }
# 
#         return device_info
# 
#     def reset(self):
#         """
#         Perform software restart.
#         """
#         self.low_level_io(0, encode_sfp(251, []))
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exc_type, exc_value, traceback):
#         self.stop()
# 
#     def analog(self, pin):
#         return ADC(self, pin)
# 
#     def digital(self, name):
#         if not (isinstance(name, int) or isinstance(name, string_types)):
#             raise IoTPy_APIError("GPIO name must be an integer.")
#         return GPIO(self, name)
# 
#     def digital_port(self, names):
#         for name in names:
#             if not isinstance(name, int):
#                 raise IoTPy_APIError("GPIO name must be an integer.")
#         return GPIOPort(self, names)
# 
#     def i2c(self, name):
#         return I2C(self)
# 
#     def pwm(self, pin, freq=100, polarity=1):
#         return PWM(self, pin, freq, polarity)
# 
#     def spi(self, name, clock=1000000, mode=SPI.MODE_0):
#         _names = {"SPI0": 0, "SPI1": 1}
#         if isinstance(name, int):
#             port = name
#         elif isinstance(name, str):
#             if _names.has_key(name):
#                 port = _names[name]
#             else:
#                 raise IoTPy_APIError("Invalid SPI name %s. Must be one of %s." % (name, ", ".join(sorted(_names.keys()))))
#         else:
#             raise IoTPy_APIError("PWM name must be an integer or a string")
# 
#         divider = int(round(2.0e6/clock))
# 
#         return SPI(self, port, divider, mode)
# 
#     def onewire(self, pin):
#         return OneWire(self, pin)


from IoTPy.ioboard import IoBoard
from IoTPy.pinmaps import *

UPER1_PINOUT = IoPinout({
    1: P0_20,
    2: P0_2,
    3: P1_26,
    4: P1_27,
    5: P1_20,
    6: P_I2C,
    7: P_I2C,
    8: P0_21,

# Path: IoTPy/ioboard.py
# class IoBoard:
#     """
#     Micro controller based IO board with serial over USB class.
# 
#     :param pinout: A list describing physical board pin layout and capabilities.
#     :type pinout: :class:`IoTPy.pyuper.pinouts.IoPinout`
#     :param io: io transport instance (one of defined at transport.py.
#     :type io: object instance
#     """
# 
#     def __init__(self, pinout, io=None, **kwargs):
#         self.interrupts = [None] * 8
#         self.callbackdict = {}
#         if not io:
#             io = SerialTransport(**kwargs)
#         self.io = io
#         self.outq = queue.Queue()
#         self.reader = Worker(self.io, self.outq, self.internal_call_back, decode_sfp)
# 
#         self.devicename = "uper"
#         self.version = __version__
#         self.pinout = pinout
# 
#     def get_info(self):
#         """
#         Get IoBoard device name and version info.
# 
#         :return: Tuple containing board name and version.
#         """
#         return self.devicename, self.version
# 
#     def stop(self):
#         """
#         Stop all communications with the board and close serial communication port.
# 
#         :raise: IoTPy_APIError
#         """
# 
#         # for i in range(7):
#         #    self.detachInterrupt(i)
#         self.reader.stop()
# 
#     def low_level_io(self, ret, output_buf):
#         """
# 
#         :param ret:
#         :param output_buf:
#         :return:
#         """
#         # print(':'.join(hex(ord(n)) for n in output_buf))
#         try:
#             self.io.write(output_buf)
#         except:
#             raise IoTPy_APIError("Unrecoverable transport writing error, dying.")
#         data = None
#         if ret != 0:
#             try:
#                 data = self.outq.get(True, 1)
#             except queue.Empty:
#                 raise IoTPy_APIError("IoTPy: Nothing to read on port exception.")
#             # print('|'.join(hex(ord(n)) for n in data))
#         return data
# 
#     def internal_call_back(self, interrupt_data):
#         """
# 
#         :param interrupt_data:
#         :return:
#         """
# 
#         interrupt_event = {'id': interrupt_data[0], 'type': interrupt_data[1] & 0xFF, 'values': interrupt_data[1] >> 8}
#         callback_entry = self.callbackdict[self.interrupts[interrupt_event['id']]]
# 
#         try:
#             callback_entry['callback'](interrupt_event, callback_entry['userobject'])
#         except Exception as e:
#             errmsg("IoTPy Interrupt callback exception: %s" % e)
#         return
# 
#     def get_device_info(self):
#         """
#         Return information about the device.
# 
#         :return: A list containing board type, major and minor firmware versions, 16 byte unique identifier, \
#                     microcontroller part and bootcode version numbers.
#         """
#         sfp_code, args = decode_sfp(self.low_level_io(1, encode_sfp(255, [])))
# 
#         if sfp_code != 255:
#             errmsg("IoTPy error: get_device_info wrong code.")
#             # raise IoTPy_APIError("")
#         device_data = args
# 
#         if device_data[0] >> 24 != 0x55:  # 0x55 = 'U'
#             print("IoTPy error: getDeviceInfo unknown device/firmware type")
#             # return
# 
#         device_info = {
#             'fw_maj': (device_data[0] & 0x00ff0000) >> 16,  # Firmware major version number
#             'fw_min': device_data[0] & 0x0000ffff,          # Firmware minor version number
#             'uid':    UUID(bytes=device_data[1]),         # 128bit unique ID
#             'cpu_id': device_data[2],                       # UPER LPC CPU part number
#             'boot':   device_data[3],                       # UPER LPC CPU bootload code version
#         }
# 
#         return device_info
# 
#     def reset(self):
#         """
#         Perform software restart.
#         """
#         self.low_level_io(0, encode_sfp(251, []))
# 
#     def __enter__(self):
#         return self
# 
#     def __exit__(self, exc_type, exc_value, traceback):
#         self.stop()
# 
#     def analog(self, pin):
#         return ADC(self, pin)
# 
#     def digital(self, name):
#         if not (isinstance(name, int) or isinstance(name, string_types)):
#             raise IoTPy_APIError("GPIO name must be an integer.")
#         return GPIO(self, name)
# 
#     def digital_port(self, names):
#         for name in names:
#             if not isinstance(name, int):
#                 raise IoTPy_APIError("GPIO name must be an integer.")
#         return GPIOPort(self, names)
# 
#     def i2c(self, name):
#         return I2C(self)
# 
#     def pwm(self, pin, freq=100, polarity=1):
#         return PWM(self, pin, freq, polarity)
# 
#     def spi(self, name, clock=1000000, mode=SPI.MODE_0):
#         _names = {"SPI0": 0, "SPI1": 1}
#         if isinstance(name, int):
#             port = name
#         elif isinstance(name, str):
#             if _names.has_key(name):
#                 port = _names[name]
#             else:
#                 raise IoTPy_APIError("Invalid SPI name %s. Must be one of %s." % (name, ", ".join(sorted(_names.keys()))))
#         else:
#             raise IoTPy_APIError("PWM name must be an integer or a string")
# 
#         divider = int(round(2.0e6/clock))
# 
#         return SPI(self, port, divider, mode)
# 
#     def onewire(self, pin):
#         return OneWire(self, pin)


# Path: IoTPy/boards/uper.py
from IoTPy.ioboard import IoBoard
from IoTPy.pinmaps import *


UPER1_PINOUT = IoPinout({
    1: P0_20,
    2: P0_2,
    3: P1_26,
    4: P1_27,
    5: P1_20,
    6: P_I2C,
    7: P_I2C,
    8: P0_21,

    9: P1_23,