|
53 | 53 | import time
|
54 | 54 | from micropython import const
|
55 | 55 |
|
| 56 | +from adafruit_bus_device.i2c_device import I2CDevice |
| 57 | + |
56 | 58 | __version__ = "0.0.0-auto.0"
|
57 | 59 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_IS31FL3731.git"
|
58 | 60 |
|
@@ -85,47 +87,38 @@ class IS31FL3731:
|
85 | 87 | The IS31FL3731 is an abstract class contain the main function related to this chip.
|
86 | 88 | Each board needs to define width, height and pixel_addr.
|
87 | 89 |
|
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 |
90 | 92 | """
|
91 | 93 |
|
92 | 94 | width = 16
|
93 | 95 | height = 9
|
94 | 96 |
|
95 | 97 | def __init__(self, i2c, address=0x74, frames=None):
|
96 |
| - self.i2c = i2c |
97 |
| - self.address = address |
| 98 | + self.i2c_device = I2CDevice(i2c, address) |
98 | 99 | self._frame = None
|
99 | 100 | self._init(frames=frames)
|
100 | 101 |
|
101 | 102 | def _i2c_read_reg(self, reg, result):
|
102 | 103 | # Read a buffer of data from the specified 8-bit I2C register address.
|
103 | 104 | # The provided result parameter will be filled to capacity with bytes
|
104 | 105 | # 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) |
109 | 108 | return result
|
110 |
| - finally: |
111 |
| - self.i2c.unlock() |
112 | 109 | return None
|
113 | 110 |
|
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 |
| - |
124 | 111 | def _i2c_write_reg(self, reg, data):
|
125 | 112 | # Write a contiguous block of data (bytearray) starting at the
|
126 | 113 | # specified I2C register address (register passed as argument).
|
127 | 114 | self._i2c_write_block(bytes([reg]) + data)
|
128 | 115 |
|
| 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 | + |
129 | 122 | def _bank(self, bank=None):
|
130 | 123 | if bank is None:
|
131 | 124 | result = bytearray(1)
|
@@ -292,14 +285,10 @@ def fill(self, color=None, blink=None, frame=None):
|
292 | 285 | if not 0 <= color <= 255:
|
293 | 286 | raise ValueError("Color out of range")
|
294 | 287 | 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: |
298 | 289 | for row in range(6):
|
299 | 290 | data[0] = _COLOR_OFFSET + row * 24
|
300 |
| - self.i2c.writeto(self.address, data) |
301 |
| - finally: |
302 |
| - self.i2c.unlock() |
| 291 | + i2c.write(data) |
303 | 292 | if blink is not None:
|
304 | 293 | data = bool(blink) * 0xFF
|
305 | 294 | for col in range(18):
|
|
0 commit comments