# Path: nanpy/arduinotree.py
# class ArduinoTree(object):
# 
#     """Object tree model of an Arduino.
# 
#     Examples:
# 
#         a=ArduinoTree()
#         a.pin.get(9).read_digital_value()
# 
#     """
# 
#     def __init__(self, connection=None):
#         ''
#         self.connection = connection
#         if not connection:
#             self.connection = serial_manager
#         if not hasattr(self.connection, 'classinfo'):
#             self.connection.classinfo = ClassInfo(self.connection)
# 
#     @property
#     @memoized
#     def api(self):
#         """Access to Arduino API."""
#         return ArduinoApi(self.connection)
# 
#     @property
#     @memoized
#     def pin(self):
#         """Object-oriented representation of an Arduino pin"""
# #         if not self.register:
# #             return None
# #         if not self.define:
# #             return None
# #         if not self.core:
# #             return None
# #         if not self.ram:
# #             return None
#         return PinFeature(self.define, self.register, self.core, self.ram, self.api)
# 
#     @property
#     @memoized
#     def define(self):
#         """Access to firmware constants."""
#         return DefineFeature(self.connection)
# 
#     @property
#     @memoized
#     def register(self):
#         """Direct access to AVR registers."""
#         try:
#             return RegisterFeature(self.connection)
#         except FirmwareMissingFeatureError:
#             return None
# 
#     @property
#     @memoized
#     def watchdog(self):
#         """Direct access to watchdog timer."""
#         return Watchdog(self.connection)
# 
#     @property
#     @memoized
#     def eeprom(self):
#         """Access to EEPROM."""
#         return EEPROM(self.connection)
# 
#     @property
#     @memoized
#     def ram(self):
#         """Access to RAM."""
#         try:
#             return RAM(self.connection)
#         except FirmwareMissingFeatureError:
#             return None
# 
#     @property
#     @memoized
#     def counter(self):
#         """Access to counter."""
#         return Counter(self.connection, F_CPU=self.define.get('F_CPU'))
# 
#     @property
#     @memoized
#     def core(self):
#         """Access to Arduino functions which are not part of the public API."""
#         try:
#             return ArduinoCore(self.connection)
#         except FirmwareMissingFeatureError:
#             return None
# 
#     @property
#     @memoized
#     def vcc(self):
#         """Access to VCC."""
#         if not self.register:
#             return None
#         if not self.define:
#             return None
#         return Vcc(self.register, MCU=self.define.get('MCU'))
# 
#     def soft_reset(self):
#         """Resets the AVR, the registers will be reset to their known, default
#         settings.
# 
#         Details: http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_softreset
# 
#         """
#         self.watchdog.enable(self.watchdog.WDTO_60MS)
#         time.sleep(0.2)
# 
#         # TODO: after restart the first read is not correct (why?)
#         # This command helps cleaning the read buffer
#         self.connection.flush_input()
# 
#     @property
#     @memoized
#     def wire(self):
#         """Access to Wire."""
#         return Wire(self.connection)
# 
#     @property
#     @memoized
#     def firmware_info(self):
#         """"""
#         return firmware_info(self.define.as_dict)
# 
#     @property
#     @memoized
#     def esp(self):
#         try:
#             return Esp(self.connection)
#         except FirmwareMissingFeatureError:
#             return None
# 
# Path: nanpy/PCF8574.py
# class PCF8574(object):
# 
#     def __init__(self, wire, address):
#         self._address = address
#         self.master = I2C_Master(wire)
# 
#     def read8(self):
#         ls = self.master.request(self._address, 1)
# 
#         # HACK: without beginTransmission all second read is empty
#         self.master.wire.beginTransmission(self._address)
#         error_code = self.master.wire.endTransmission()
# 
#         if len(ls) >= 1:
#             return ls[0]
#         else:
#             raise PCF8574_Error('no data received from PCF8574')
# 
#     def write8(self, value):
#         self.master.send(self._address, [value])
# 
#     def readPin(self, pin):
#         d = self.read8()
#         return (d & (1 << pin)) > 0
# 
#     def writePin(self, pin, value):
#         d = self.read8()
#         if value:
#             d |= (1 << pin)
#         else:
#             d &= ~(1 << pin)
#         self.write8(d)


from nanpy.arduinotree import ArduinoTree
from nanpy.serialmanager import SerialManager
from nanpy.PCF8574 import PCF8574


I2C_ADDRESS = 0x27


def main():
    print ('PCF8574 demo')
    print ('address: 0x%X' % I2C_ADDRESS)
    connection = SerialManager(sleep_after_connect=2)
    connection.open()

# Path: nanpy/arduinotree.py
# class ArduinoTree(object):
# 
#     """Object tree model of an Arduino.
# 
#     Examples:
# 
#         a=ArduinoTree()
#         a.pin.get(9).read_digital_value()
# 
#     """
# 
#     def __init__(self, connection=None):
#         ''
#         self.connection = connection
#         if not connection:
#             self.connection = serial_manager
#         if not hasattr(self.connection, 'classinfo'):
#             self.connection.classinfo = ClassInfo(self.connection)
# 
#     @property
#     @memoized
#     def api(self):
#         """Access to Arduino API."""
#         return ArduinoApi(self.connection)
# 
#     @property
#     @memoized
#     def pin(self):
#         """Object-oriented representation of an Arduino pin"""
# #         if not self.register:
# #             return None
# #         if not self.define:
# #             return None
# #         if not self.core:
# #             return None
# #         if not self.ram:
# #             return None
#         return PinFeature(self.define, self.register, self.core, self.ram, self.api)
# 
#     @property
#     @memoized
#     def define(self):
#         """Access to firmware constants."""
#         return DefineFeature(self.connection)
# 
#     @property
#     @memoized
#     def register(self):
#         """Direct access to AVR registers."""
#         try:
#             return RegisterFeature(self.connection)
#         except FirmwareMissingFeatureError:
#             return None
# 
#     @property
#     @memoized
#     def watchdog(self):
#         """Direct access to watchdog timer."""
#         return Watchdog(self.connection)
# 
#     @property
#     @memoized
#     def eeprom(self):
#         """Access to EEPROM."""
#         return EEPROM(self.connection)
# 
#     @property
#     @memoized
#     def ram(self):
#         """Access to RAM."""
#         try:
#             return RAM(self.connection)
#         except FirmwareMissingFeatureError:
#             return None
# 
#     @property
#     @memoized
#     def counter(self):
#         """Access to counter."""
#         return Counter(self.connection, F_CPU=self.define.get('F_CPU'))
# 
#     @property
#     @memoized
#     def core(self):
#         """Access to Arduino functions which are not part of the public API."""
#         try:
#             return ArduinoCore(self.connection)
#         except FirmwareMissingFeatureError:
#             return None
# 
#     @property
#     @memoized
#     def vcc(self):
#         """Access to VCC."""
#         if not self.register:
#             return None
#         if not self.define:
#             return None
#         return Vcc(self.register, MCU=self.define.get('MCU'))
# 
#     def soft_reset(self):
#         """Resets the AVR, the registers will be reset to their known, default
#         settings.
# 
#         Details: http://www.nongnu.org/avr-libc/user-manual/FAQ.html#faq_softreset
# 
#         """
#         self.watchdog.enable(self.watchdog.WDTO_60MS)
#         time.sleep(0.2)
# 
#         # TODO: after restart the first read is not correct (why?)
#         # This command helps cleaning the read buffer
#         self.connection.flush_input()
# 
#     @property
#     @memoized
#     def wire(self):
#         """Access to Wire."""
#         return Wire(self.connection)
# 
#     @property
#     @memoized
#     def firmware_info(self):
#         """"""
#         return firmware_info(self.define.as_dict)
# 
#     @property
#     @memoized
#     def esp(self):
#         try:
#             return Esp(self.connection)
#         except FirmwareMissingFeatureError:
#             return None
# 
# Path: nanpy/PCF8574.py
# class PCF8574(object):
# 
#     def __init__(self, wire, address):
#         self._address = address
#         self.master = I2C_Master(wire)
# 
#     def read8(self):
#         ls = self.master.request(self._address, 1)
# 
#         # HACK: without beginTransmission all second read is empty
#         self.master.wire.beginTransmission(self._address)
#         error_code = self.master.wire.endTransmission()
# 
#         if len(ls) >= 1:
#             return ls[0]
#         else:
#             raise PCF8574_Error('no data received from PCF8574')
# 
#     def write8(self, value):
#         self.master.send(self._address, [value])
# 
#     def readPin(self, pin):
#         d = self.read8()
#         return (d & (1 << pin)) > 0
# 
#     def writePin(self, pin, value):
#         d = self.read8()
#         if value:
#             d |= (1 << pin)
#         else:
#             d &= ~(1 << pin)
#         self.write8(d)


# Path: nanpy/examples/PCF8574.py
from nanpy.arduinotree import ArduinoTree
from nanpy.serialmanager import SerialManager
from nanpy.PCF8574 import PCF8574



I2C_ADDRESS = 0x27


def main():
    print ('PCF8574 demo')
    print ('address: 0x%X' % I2C_ADDRESS)
    connection = SerialManager(sleep_after_connect=2)
    connection.open()

    a = ArduinoTree(connection=connection)