Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

Commit fdf0fca

Browse files
committed
Switch to use pure python I2C interface with python 2 & 3 compatibility vs. older python-smbus library.
1 parent cdf7a7b commit fdf0fca

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Adafruit_GPIO/I2C.py

+13-8
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import logging
2323
import subprocess
2424

25-
import smbus
26-
2725
import 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

6866
def 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

8785
class 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

setup.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import sys
66

77
# Define required packages.
8-
requires = []
8+
requires = ['adafruit-pureio']
99
# Assume spidev is required on non-windows & non-mac platforms (i.e. linux).
1010
if sys.platform != 'win32' and sys.platform != 'darwin':
1111
requires.append('spidev')
1212

1313
setup(name = 'Adafruit_GPIO',
14-
version = '0.9.3',
14+
version = '1.0.0',
1515
author = 'Tony DiCola',
1616
author_email = 'tdicola@adafruit.com',
1717
description = 'Library to provide a cross-platform GPIO interface on the Raspberry Pi and Beaglebone Black using the RPi.GPIO and Adafruit_BBIO libraries.',

0 commit comments

Comments
 (0)