Skip to content

Commit 35b4ac8

Browse files
committed
Attempt adafruit#2 also ran black formatter
I'm pretty sure I won't even get it on my 2nd attempt so might as well number them.
1 parent 3232d28 commit 35b4ac8

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

adafruit_is31fl3731/__init__.py

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,19 @@ class IS31FL3731:
104104
width: int = 16
105105
height: int = 9
106106

107-
def __init__(self, i2c: None, address: int = 0x74, frames: int = None) -> None:
107+
def __init__(
108+
self,
109+
i2c: busio.I2C,
110+
frames: Optional[int] = None,
111+
address: int = 0x74,
112+
):
108113
self.i2c_device = I2CDevice(i2c, address)
109114
self._frame = None
110115
self._init(frames=frames)
111116

112-
def _i2c_read_reg(self, reg: int = None, result: bytes = None) -> bytes:
117+
def _i2c_read_reg(
118+
self, reg: Optional[int] = None, result: Optional[ReadableBuffer] = None
119+
) -> Optional[ReadableBuffer]:
113120
# Read a buffer of data from the specified 8-bit I2C register address.
114121
# The provided result parameter will be filled to capacity with bytes
115122
# of data read from the register.
@@ -118,40 +125,44 @@ def _i2c_read_reg(self, reg: int = None, result: bytes = None) -> bytes:
118125
return result
119126
return None
120127

121-
def _i2c_write_reg(self, reg: int = None, data: bytes = None) -> bytes:
128+
def _i2c_write_reg(
129+
self, reg: Optional[int] = None, data: Optional[ReadableBuffer] = None
130+
) -> None:
122131
# Write a contiguous block of data (bytearray) starting at the
123132
# specified I2C register address (register passed as argument).
124133
self._i2c_write_block(bytes([reg]) + data)
125134

126-
def _i2c_write_block(self, data: bytes = None) -> bytes:
135+
def _i2c_write_block(self, data: Optional[ReadableBuffer]) -> None:
127136
# Write a buffer of data (byte array) to the specified I2C register
128137
# address.
129138
with self.i2c_device as i2c:
130139
i2c.write(data)
131140

132-
def _bank(self, bank: int = None) -> int:
141+
def _bank(self, bank: Optional[int] = None) -> Optional[int]:
133142
if bank is None:
134143
result = bytearray(1)
135144
return self._i2c_read_reg(_BANK_ADDRESS, result)[0]
136145
self._i2c_write_reg(_BANK_ADDRESS, bytearray([bank]))
137146
return None
138147

139148
def _register(
140-
self, bank: int = None, register: int = None, value: int = None
141-
) -> int:
149+
self,
150+
bank: Optional[int] = None,
151+
register: Optional[int] = None,
152+
value: Optional[int] = None,
153+
) -> Optional[int]:
142154
self._bank(bank)
143155
if value is None:
144156
result = bytearray(1)
145-
print(f"Register: {result}")
146157
return self._i2c_read_reg(register, result)[0]
147158
self._i2c_write_reg(register, bytearray([value]))
148159
return None
149160

150-
def _mode(self, mode=None):
161+
def _mode(self, mode: Optional[int] = None) -> int:
151162
"""Function for setting _register mode"""
152163
return self._register(_CONFIG_BANK, _MODE_REGISTER, mode)
153164

154-
def _init(self, frames: int = 0) -> int:
165+
def _init(self, frames: Iterable) -> None:
155166
self.sleep(True)
156167
# Clear config; sets to Picture Mode, no audio sync, maintains sleep
157168
self._bank(_CONFIG_BANK)
@@ -174,15 +185,20 @@ def reset(self):
174185
time.sleep(0.01) # 10 MS pause to reset.
175186
self.sleep(False)
176187

177-
def sleep(self, value: bool = False):
188+
def sleep(self, value):
178189
"""
179190
Set the Software Shutdown Register bit
180191
181192
:param value: True to set software shutdown bit; False unset
182193
"""
183194
return self._register(_CONFIG_BANK, _SHUTDOWN_REGISTER, not value)
184195

185-
def autoplay(self, delay: float = 0.0, loops: int = 0, frames: int = 0) -> int:
196+
def autoplay(
197+
self,
198+
delay: Optional[int] = None,
199+
loops: Optional[Iterable] = None,
200+
frames: Optional[int] = None,
201+
) -> int:
186202
"""
187203
Start autoplay
188204
@@ -204,7 +220,12 @@ def autoplay(self, delay: float = 0.0, loops: int = 0, frames: int = 0) -> int:
204220
self._register(_CONFIG_BANK, _AUTOPLAY2_REGISTER, delay % 64)
205221
self._mode(_AUTOPLAY_MODE | self._frame)
206222

207-
def fade(self, fade_in: int = None, fade_out: int = None, pause: int = 0) -> int:
223+
def fade(
224+
self,
225+
fade_in: Optional[int] = None,
226+
fade_out: Optional[int] = None,
227+
pause: Optional[int] = None,
228+
) -> int:
208229
"""
209230
Start and stop the fade feature. If both fade_in and fade_out are None (the
210231
default), the breath feature is used for fading. if fade_in is None, then
@@ -237,7 +258,7 @@ def fade(self, fade_in: int = None, fade_out: int = None, pause: int = 0) -> int
237258
self._register(_CONFIG_BANK, _BREATH1_REGISTER, fade_out << 4 | fade_in)
238259
self._register(_CONFIG_BANK, _BREATH2_REGISTER, 1 << 4 | pause)
239260

240-
def frame(self, frame: int = None, show: bool = True) -> int:
261+
def frame(self, frame: Optional[int] = None, show: bool = True) -> Optional[int]:
241262
"""
242263
Set the current frame
243264
@@ -253,17 +274,17 @@ def frame(self, frame: int = None, show: bool = True) -> int:
253274
self._register(_CONFIG_BANK, _FRAME_REGISTER, frame)
254275
return None
255276

256-
def audio_sync(self, value: int = None) -> int:
277+
def audio_sync(self, value: Optional[int]) -> Optional[int]:
257278
"""Set the audio sync feature register"""
258279
return self._register(_CONFIG_BANK, _AUDIOSYNC_REGISTER, value)
259280

260281
def audio_play(
261282
self,
262-
sample_rate: int = 0,
283+
sample_rate: int,
263284
audio_gain: int = 0,
264285
agc_enable: bool = False,
265286
agc_fast: bool = False,
266-
) -> int:
287+
) -> None:
267288
"""Controls the audio play feature"""
268289
if sample_rate == 0:
269290
self._mode(_PICTURE_MODE)
@@ -282,7 +303,7 @@ def audio_play(
282303
)
283304
self._mode(_AUDIOPLAY_MODE)
284305

285-
def blink(self, rate: int = None) -> int:
306+
def blink(self, rate: Optional[int]) -> Optional[int]:
286307
"""Updates the blink register"""
287308
# pylint: disable=no-else-return
288309
# This needs to be refactored when it can be tested
@@ -295,7 +316,12 @@ def blink(self, rate: int = None) -> int:
295316
self._register(_CONFIG_BANK, _BLINK_REGISTER, rate & 0x07 | 0x08)
296317
return None
297318

298-
def fill(self, color: int = None, blink: bool = False, frame: int = 0) -> int:
319+
def fill(
320+
self,
321+
color: Optional[int] = None,
322+
frame: Optional[int] = None,
323+
blink: bool = False,
324+
):
299325
"""
300326
Fill the display with a brightness level
301327
@@ -321,20 +347,20 @@ def fill(self, color: int = None, blink: bool = False, frame: int = 0) -> int:
321347

322348
# This function must be replaced for each board
323349
@staticmethod
324-
def pixel_addr(x, y):
350+
def pixel_addr(x: int, y: int) -> int:
325351
"""Calulate the offset into the device array for x,y pixel"""
326352
return x + y * 16
327353

328-
# pylint: disable-msg=too-many-arguments, too-many-branches
354+
# pylint: disable-msg=too-many-arguments
329355
def pixel(
330356
self,
331357
x: int,
332358
y: int,
333-
color: int = 255,
359+
color: Optional[int] = None,
360+
frame: Optional[int] = None,
334361
blink: bool = False,
335-
frame: int = 0,
336362
rotate: int = 0,
337-
):
363+
) -> Optional[int]:
338364
"""
339365
Matrix display configuration
340366
@@ -397,7 +423,7 @@ def pixel(
397423

398424
# pylint: enable-msg=too-many-arguments
399425

400-
def image(self, img: bytes = None, blink: bool = False, frame: int = 0) -> bytes:
426+
def image(self, img: Optional[str], frame: Optional[int], blink: bool = False):
401427
"""Set buffer to value of Python Imaging Library image. The image should
402428
be in 8-bit mode (L) and a size equal to the display size.
403429
@@ -410,7 +436,9 @@ def image(self, img: bytes = None, blink: bool = False, frame: int = 0) -> bytes
410436
imwidth, imheight = img.size
411437
if imwidth != self.width or imheight != self.height:
412438
raise ValueError(
413-
f"Image must be same dimensions as display {self.width}x{self.height}"
439+
"Image must be same dimensions as display ({0}x{1}).".format(
440+
self.width, self.height
441+
)
414442
)
415443
# Grab all the pixels from the image, faster than getpixel.
416444
pixels = img.load()

adafruit_is31fl3731/matrix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def pixel_addr(x: int, y: int) -> int:
5656
# for animation. Buffering the full matrix for a quick write is not a
5757
# memory concern here, as by definition this method is used with PIL
5858
# images; we're not running on a RAM-constrained microcontroller.
59-
def image(self, img: str = None, blink: bool = False, frame: int = 0):
59+
def image(self, img: Optional[Image], frame: Optional[int], blink: bool = False):
6060
"""Set buffer to value of Python Imaging Library image.
6161
The image should be in 8-bit mode (L) and a size equal to the
6262
display size.
@@ -73,7 +73,7 @@ def image(self, img: str = None, blink: bool = False, frame: int = 0):
7373
)
7474

7575
# Frame-select and then write pixel data in one big operation
76-
if frame != 0:
76+
if frame is not None:
7777
self._bank(frame)
7878
# We can safely reduce the image to a "flat" byte sequence because
7979
# the matrix layout is known linear; no need to go through a 2D

0 commit comments

Comments
 (0)