Skip to content

Commit 1f8f520

Browse files
authored
Merge pull request #50 from tekktrik/dev/use-bus-device
Add adafruit_bus_device dependency
2 parents 4a0f055 + 6e747f6 commit 1f8f520

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

adafruit_is31fl3731/__init__.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import time
5454
from micropython import const
5555

56+
from adafruit_bus_device.i2c_device import I2CDevice
57+
5658
__version__ = "0.0.0-auto.0"
5759
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3731.git"
5860

@@ -85,47 +87,38 @@ class IS31FL3731:
8587
The IS31FL3731 is an abstract class contain the main function related to this chip.
8688
Each board needs to define width, height and pixel_addr.
8789
88-
:param ~adafruit_bus_device.i2c_device i2c_device: the connected i2c bus i2c_device
89-
:param address: the device address; defaults to 0x74
90+
:param ~busio.I2C i2c: the connected i2c bus i2c_device
91+
:param int address: the device address; defaults to 0x74
9092
"""
9193

9294
width = 16
9395
height = 9
9496

9597
def __init__(self, i2c, address=0x74, frames=None):
96-
self.i2c = i2c
97-
self.address = address
98+
self.i2c_device = I2CDevice(i2c, address)
9899
self._frame = None
99100
self._init(frames=frames)
100101

101102
def _i2c_read_reg(self, reg, result):
102103
# Read a buffer of data from the specified 8-bit I2C register address.
103104
# The provided result parameter will be filled to capacity with bytes
104105
# of data read from the register.
105-
while not self.i2c.try_lock():
106-
pass
107-
try:
108-
self.i2c.writeto_then_readfrom(self.address, bytes([reg]), result)
106+
with self.i2c_device as i2c:
107+
i2c.write_then_readinto(bytes([reg]), result)
109108
return result
110-
finally:
111-
self.i2c.unlock()
112109
return None
113110

114-
def _i2c_write_block(self, data):
115-
# Writes a contiguous block of data (bytearray) where the first byte
116-
# is the starting I2C register address (register is not an argument).
117-
while not self.i2c.try_lock():
118-
pass
119-
try:
120-
self.i2c.writeto(self.address, data)
121-
finally:
122-
self.i2c.unlock()
123-
124111
def _i2c_write_reg(self, reg, data):
125112
# Write a contiguous block of data (bytearray) starting at the
126113
# specified I2C register address (register passed as argument).
127114
self._i2c_write_block(bytes([reg]) + data)
128115

116+
def _i2c_write_block(self, data):
117+
# Write a buffer of data (byte array) to the specified I2C register
118+
# address.
119+
with self.i2c_device as i2c:
120+
i2c.write(data)
121+
129122
def _bank(self, bank=None):
130123
if bank is None:
131124
result = bytearray(1)
@@ -292,14 +285,10 @@ def fill(self, color=None, blink=None, frame=None):
292285
if not 0 <= color <= 255:
293286
raise ValueError("Color out of range")
294287
data = bytearray([color] * 25) # Extra byte at front for address.
295-
while not self.i2c.try_lock():
296-
pass
297-
try:
288+
with self.i2c_device as i2c:
298289
for row in range(6):
299290
data[0] = _COLOR_OFFSET + row * 24
300-
self.i2c.writeto(self.address, data)
301-
finally:
302-
self.i2c.unlock()
291+
i2c.write(data)
303292
if blink is not None:
304293
data = bool(blink) * 0xFF
305294
for col in range(18):

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44

55
Adafruit-Blinka
66
adafruit-circuitpython-framebuf
7+
adafruit-circuitpython-busdevice

setup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
# Author details
3535
author="Adafruit Industries",
3636
author_email="[email protected]",
37-
install_requires=["Adafruit-Blinka"],
37+
install_requires=[
38+
"Adafruit-Blinka",
39+
"adafruit-circuitpython-busdevice",
40+
],
3841
# Choose your license
3942
license="MIT",
4043
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers

0 commit comments

Comments
 (0)