# 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/i2c.py
# class I2C_Master(I2C_Base):
# 
#     """High level interface for I2C Master."""
# 
#     def __init__(self, wire):
#         self.wire = wire
# 
#     def request(self, address, quantity):
#         """Used by the master to request bytes from a slave device.
# 
#         :param address: the 7-bit address of the device to request bytes from
#         :param quantity: the number of bytes to request
#         :param stop: boolean. true will send a stop message after the request, releasing the bus. false will continually send a restart after the request, keeping the connection active.
#         :returns: list : the list of bytes returned from the slave device
# 
#         http://arduino.cc/en/Reference/WireRequestFrom
# 
#         """
#         self._begin()
#         n = self.wire.requestFrom(address, quantity, stop=True)
#         if n < quantity:
#             log.info('slave sent less bytes than requested')
#         ls = n * [None]
#         for i in range(n):
#             ls[i] = self.wire.read()
#         return ls
# 
#     def send(self, address, data):
#         """"""
#         self._begin()
#         self.wire.beginTransmission(address)
#         for b in data:
#             self.wire.write(b)
#         error_code = self.wire.endTransmission()
#         if error_code != 0:
#             raise I2C_CommunicationError(error_code)
# 
#     def scan(self):
#         """The i2c_scanner uses the return value of the Write.endTransmisstion
#         to see if a device did acknowledge to the address.
# 
#         original source: http://playground.arduino.cc/Main/I2cScanner#.Uxs4Wdt4iJM
# 
#         """
#         self._begin()
#         ls = []
#         for address in range(128):
#             try:
#                 self.send(address, [])
#                 ls.append(address)
#             except I2C_CommunicationError:
#                 pass
# 
#         return ls


from nanpy.arduinotree import ArduinoTree
from nanpy.i2c import I2C_Master
from nanpy.serialmanager import SerialManager
import logging
#!/usr/bin/env python

# Description: sending a byte to PCF8574

log = logging.getLogger(__name__)

I2C_ADDRESS = 0x27
 
def main():
    connection = SerialManager(sleep_after_connect=2)
    connection.open()
    print (connection.device)
    a = ArduinoTree(connection=connection)

# 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/i2c.py
# class I2C_Master(I2C_Base):
# 
#     """High level interface for I2C Master."""
# 
#     def __init__(self, wire):
#         self.wire = wire
# 
#     def request(self, address, quantity):
#         """Used by the master to request bytes from a slave device.
# 
#         :param address: the 7-bit address of the device to request bytes from
#         :param quantity: the number of bytes to request
#         :param stop: boolean. true will send a stop message after the request, releasing the bus. false will continually send a restart after the request, keeping the connection active.
#         :returns: list : the list of bytes returned from the slave device
# 
#         http://arduino.cc/en/Reference/WireRequestFrom
# 
#         """
#         self._begin()
#         n = self.wire.requestFrom(address, quantity, stop=True)
#         if n < quantity:
#             log.info('slave sent less bytes than requested')
#         ls = n * [None]
#         for i in range(n):
#             ls[i] = self.wire.read()
#         return ls
# 
#     def send(self, address, data):
#         """"""
#         self._begin()
#         self.wire.beginTransmission(address)
#         for b in data:
#             self.wire.write(b)
#         error_code = self.wire.endTransmission()
#         if error_code != 0:
#             raise I2C_CommunicationError(error_code)
# 
#     def scan(self):
#         """The i2c_scanner uses the return value of the Write.endTransmisstion
#         to see if a device did acknowledge to the address.
# 
#         original source: http://playground.arduino.cc/Main/I2cScanner#.Uxs4Wdt4iJM
# 
#         """
#         self._begin()
#         ls = []
#         for address in range(128):
#             try:
#                 self.send(address, [])
#                 ls.append(address)
#             except I2C_CommunicationError:
#                 pass
# 
#         return ls


# Path: nanpy/examples/i2c_send.py
from nanpy.arduinotree import ArduinoTree
from nanpy.i2c import I2C_Master
from nanpy.serialmanager import SerialManager
import logging

#!/usr/bin/env python

# Description: sending a byte to PCF8574

log = logging.getLogger(__name__)

I2C_ADDRESS = 0x27
 
def main():
    connection = SerialManager(sleep_after_connect=2)
    connection.open()
    print (connection.device)
    a = ArduinoTree(connection=connection)

    master = I2C_Master(a.wire)