2222import logging
2323import subprocess
2424
25- import smbus
26-
2725import Adafruit_GPIO .Platform as Platform
2826
2927
@@ -56,14 +54,14 @@ def get_default_bus():
5654 else :
5755 raise RuntimeError ('Could not determine default I2C bus for platform.' )
5856
59- def get_i2c_device (address , busnum = None , ** kwargs ):
57+ def get_i2c_device (address , busnum = None , i2c_interface = None , ** kwargs ):
6058 """Return an I2C device for the specified address and on the specified bus.
6159 If busnum isn't specified, the default I2C bus for the platform will attempt
6260 to be detected.
6361 """
6462 if busnum is None :
6563 busnum = get_default_bus ()
66- return Device (address , busnum , ** kwargs )
64+ return Device (address , busnum , i2c_interface , ** kwargs )
6765
6866def require_repeated_start ():
6967 """Enable repeated start conditions for I2C register reads. This is the
@@ -85,14 +83,21 @@ def require_repeated_start():
8583
8684
8785class Device (object ):
88- """Class for communicating with an I2C device using the smbus library.
89- Allows reading and writing 8-bit, 16-bit, and byte array values to registers
86+ """Class for communicating with an I2C device using the adafruit-pureio pure
87+ python smbus library, or other smbus compatible I2C interface. Allows reading
88+ and writing 8-bit, 16-bit, and byte array values to registers
9089 on the device."""
91- def __init__ (self , address , busnum ):
90+ def __init__ (self , address , busnum , i2c_interface = None ):
9291 """Create an instance of the I2C device at the specified address on the
9392 specified I2C bus number."""
9493 self ._address = address
95- self ._bus = smbus .SMBus (busnum )
94+ if i2c_interface is None :
95+ # Use pure python I2C interface if none is specified.
96+ import Adafruit_PureIO .smbus
97+ self ._bus = Adafruit_PureIO .smbus .SMBus (busnum )
98+ else :
99+ # Otherwise use the provided class to create an smbus interface.
100+ self ._bus = i2c_interface (busnum )
96101 self ._logger = logging .getLogger ('Adafruit_I2C.Device.Bus.{0}.Address.{1:#0X}' \
97102 .format (busnum , address ))
98103
0 commit comments