@@ -104,12 +104,19 @@ class IS31FL3731:
104
104
width : int = 16
105
105
height : int = 9
106
106
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
+ ):
108
113
self .i2c_device = I2CDevice (i2c , address )
109
114
self ._frame = None
110
115
self ._init (frames = frames )
111
116
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 ]:
113
120
# Read a buffer of data from the specified 8-bit I2C register address.
114
121
# The provided result parameter will be filled to capacity with bytes
115
122
# of data read from the register.
@@ -118,40 +125,44 @@ def _i2c_read_reg(self, reg: int = None, result: bytes = None) -> bytes:
118
125
return result
119
126
return None
120
127
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 :
122
131
# Write a contiguous block of data (bytearray) starting at the
123
132
# specified I2C register address (register passed as argument).
124
133
self ._i2c_write_block (bytes ([reg ]) + data )
125
134
126
- def _i2c_write_block (self , data : bytes = None ) -> bytes :
135
+ def _i2c_write_block (self , data : Optional [ ReadableBuffer ] ) -> None :
127
136
# Write a buffer of data (byte array) to the specified I2C register
128
137
# address.
129
138
with self .i2c_device as i2c :
130
139
i2c .write (data )
131
140
132
- def _bank (self , bank : int = None ) -> int :
141
+ def _bank (self , bank : Optional [ int ] = None ) -> Optional [ int ] :
133
142
if bank is None :
134
143
result = bytearray (1 )
135
144
return self ._i2c_read_reg (_BANK_ADDRESS , result )[0 ]
136
145
self ._i2c_write_reg (_BANK_ADDRESS , bytearray ([bank ]))
137
146
return None
138
147
139
148
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 ]:
142
154
self ._bank (bank )
143
155
if value is None :
144
156
result = bytearray (1 )
145
- print (f"Register: { result } " )
146
157
return self ._i2c_read_reg (register , result )[0 ]
147
158
self ._i2c_write_reg (register , bytearray ([value ]))
148
159
return None
149
160
150
- def _mode (self , mode = None ):
161
+ def _mode (self , mode : Optional [ int ] = None ) -> int :
151
162
"""Function for setting _register mode"""
152
163
return self ._register (_CONFIG_BANK , _MODE_REGISTER , mode )
153
164
154
- def _init (self , frames : int = 0 ) -> int :
165
+ def _init (self , frames : Iterable ) -> None :
155
166
self .sleep (True )
156
167
# Clear config; sets to Picture Mode, no audio sync, maintains sleep
157
168
self ._bank (_CONFIG_BANK )
@@ -174,15 +185,20 @@ def reset(self):
174
185
time .sleep (0.01 ) # 10 MS pause to reset.
175
186
self .sleep (False )
176
187
177
- def sleep (self , value : bool = False ):
188
+ def sleep (self , value ):
178
189
"""
179
190
Set the Software Shutdown Register bit
180
191
181
192
:param value: True to set software shutdown bit; False unset
182
193
"""
183
194
return self ._register (_CONFIG_BANK , _SHUTDOWN_REGISTER , not value )
184
195
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 :
186
202
"""
187
203
Start autoplay
188
204
@@ -204,7 +220,12 @@ def autoplay(self, delay: float = 0.0, loops: int = 0, frames: int = 0) -> int:
204
220
self ._register (_CONFIG_BANK , _AUTOPLAY2_REGISTER , delay % 64 )
205
221
self ._mode (_AUTOPLAY_MODE | self ._frame )
206
222
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 :
208
229
"""
209
230
Start and stop the fade feature. If both fade_in and fade_out are None (the
210
231
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
237
258
self ._register (_CONFIG_BANK , _BREATH1_REGISTER , fade_out << 4 | fade_in )
238
259
self ._register (_CONFIG_BANK , _BREATH2_REGISTER , 1 << 4 | pause )
239
260
240
- def frame (self , frame : int = None , show : bool = True ) -> int :
261
+ def frame (self , frame : Optional [ int ] = None , show : bool = True ) -> Optional [ int ] :
241
262
"""
242
263
Set the current frame
243
264
@@ -253,17 +274,17 @@ def frame(self, frame: int = None, show: bool = True) -> int:
253
274
self ._register (_CONFIG_BANK , _FRAME_REGISTER , frame )
254
275
return None
255
276
256
- def audio_sync (self , value : int = None ) -> int :
277
+ def audio_sync (self , value : Optional [ int ] ) -> Optional [ int ] :
257
278
"""Set the audio sync feature register"""
258
279
return self ._register (_CONFIG_BANK , _AUDIOSYNC_REGISTER , value )
259
280
260
281
def audio_play (
261
282
self ,
262
- sample_rate : int = 0 ,
283
+ sample_rate : int ,
263
284
audio_gain : int = 0 ,
264
285
agc_enable : bool = False ,
265
286
agc_fast : bool = False ,
266
- ) -> int :
287
+ ) -> None :
267
288
"""Controls the audio play feature"""
268
289
if sample_rate == 0 :
269
290
self ._mode (_PICTURE_MODE )
@@ -282,7 +303,7 @@ def audio_play(
282
303
)
283
304
self ._mode (_AUDIOPLAY_MODE )
284
305
285
- def blink (self , rate : int = None ) -> int :
306
+ def blink (self , rate : Optional [ int ] ) -> Optional [ int ] :
286
307
"""Updates the blink register"""
287
308
# pylint: disable=no-else-return
288
309
# This needs to be refactored when it can be tested
@@ -295,7 +316,12 @@ def blink(self, rate: int = None) -> int:
295
316
self ._register (_CONFIG_BANK , _BLINK_REGISTER , rate & 0x07 | 0x08 )
296
317
return None
297
318
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
+ ):
299
325
"""
300
326
Fill the display with a brightness level
301
327
@@ -321,20 +347,20 @@ def fill(self, color: int = None, blink: bool = False, frame: int = 0) -> int:
321
347
322
348
# This function must be replaced for each board
323
349
@staticmethod
324
- def pixel_addr (x , y ) :
350
+ def pixel_addr (x : int , y : int ) -> int :
325
351
"""Calulate the offset into the device array for x,y pixel"""
326
352
return x + y * 16
327
353
328
- # pylint: disable-msg=too-many-arguments, too-many-branches
354
+ # pylint: disable-msg=too-many-arguments
329
355
def pixel (
330
356
self ,
331
357
x : int ,
332
358
y : int ,
333
- color : int = 255 ,
359
+ color : Optional [int ] = None ,
360
+ frame : Optional [int ] = None ,
334
361
blink : bool = False ,
335
- frame : int = 0 ,
336
362
rotate : int = 0 ,
337
- ):
363
+ ) -> Optional [ int ] :
338
364
"""
339
365
Matrix display configuration
340
366
@@ -397,7 +423,7 @@ def pixel(
397
423
398
424
# pylint: enable-msg=too-many-arguments
399
425
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 ) :
401
427
"""Set buffer to value of Python Imaging Library image. The image should
402
428
be in 8-bit mode (L) and a size equal to the display size.
403
429
@@ -410,7 +436,9 @@ def image(self, img: bytes = None, blink: bool = False, frame: int = 0) -> bytes
410
436
imwidth , imheight = img .size
411
437
if imwidth != self .width or imheight != self .height :
412
438
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
+ )
414
442
)
415
443
# Grab all the pixels from the image, faster than getpixel.
416
444
pixels = img .load ()
0 commit comments