11
11
from time import sleep
12
12
from adafruit_ht16k33 .ht16k33 import HT16K33
13
13
14
+ try :
15
+ from typing import Union , List , Tuple , Optional , Dict
16
+ from busio import I2C
17
+ except ImportError :
18
+ pass
19
+
14
20
15
21
__version__ = "0.0.0+auto.0"
16
22
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
@@ -149,11 +155,11 @@ class Seg14x4(HT16K33):
149
155
150
156
def __init__ (
151
157
self ,
152
- i2c ,
153
- address = 0x70 ,
154
- auto_write = True ,
155
- chars_per_display = 4 ,
156
- ):
158
+ i2c : I2C ,
159
+ address = 0x70 ,
160
+ auto_write : Union [ int , List [ int ], Tuple [ int , ...]] = True ,
161
+ chars_per_display : int = 4 ,
162
+ ) -> None :
157
163
super ().__init__ (i2c , address , auto_write )
158
164
if not 1 <= chars_per_display <= 8 :
159
165
raise ValueError (
@@ -163,7 +169,7 @@ def __init__(
163
169
self ._chars = chars_per_display * len (self .i2c_device )
164
170
self ._bytes_per_char = 2
165
171
166
- def print (self , value , decimal = 0 ):
172
+ def print (self , value : Union [ str , float ], decimal : int = 0 ) -> None :
167
173
"""Print the value to the display.
168
174
169
175
:param value: The value to print
@@ -173,7 +179,7 @@ def print(self, value, decimal = 0):
173
179
integer if decimal is zero.
174
180
"""
175
181
176
- if isinstance (value , ( str ) ):
182
+ if isinstance (value , str ):
177
183
self ._text (value )
178
184
elif isinstance (value , (int , float )):
179
185
self ._number (value , decimal )
@@ -182,7 +188,7 @@ def print(self, value, decimal = 0):
182
188
if self ._auto_write :
183
189
self .show ()
184
190
185
- def print_hex (self , value ) :
191
+ def print_hex (self , value : Union [ int , str ]) -> None :
186
192
"""Print the value as a hexidecimal string to the display.
187
193
188
194
:param int value: The number to print
@@ -198,7 +204,7 @@ def __setitem__(self, key, value):
198
204
if self ._auto_write :
199
205
self .show ()
200
206
201
- def scroll (self , count = 1 ):
207
+ def scroll (self , count : int = 1 ) -> None :
202
208
"""Scroll the display by specified number of places.
203
209
204
210
:param int count: The number of places to scroll
@@ -214,7 +220,7 @@ def scroll(self, count = 1):
214
220
self ._get_buffer (self ._adjusted_index (i + 2 * count )),
215
221
)
216
222
217
- def _put (self , char , index = 0 ):
223
+ def _put (self , char : str , index : int = 0 ) -> None :
218
224
"""Put a character at the specified place."""
219
225
if not 0 <= index < self ._chars :
220
226
return
@@ -230,7 +236,7 @@ def _put(self, char, index = 0):
230
236
self ._set_buffer (self ._adjusted_index (index * 2 ), CHARS [1 + character ])
231
237
self ._set_buffer (self ._adjusted_index (index * 2 + 1 ), CHARS [character ])
232
238
233
- def _push (self , char ) :
239
+ def _push (self , char : str ) -> None :
234
240
"""Scroll the display and add a character at the end."""
235
241
if (
236
242
char != "."
@@ -241,12 +247,12 @@ def _push(self, char):
241
247
self ._put (" " , self ._chars - 1 )
242
248
self ._put (char , self ._chars - 1 )
243
249
244
- def _text (self , text ) :
250
+ def _text (self , text : str ) -> None :
245
251
"""Display the specified text."""
246
252
for character in text :
247
253
self ._push (character )
248
254
249
- def _number (self , number , decimal = 0 ):
255
+ def _number (self , number : float , decimal : int = 0 ) -> str :
250
256
"""
251
257
Display a floating point or integer number on the Adafruit HT16K33 based displays
252
258
@@ -284,12 +290,13 @@ def _number(self, number, decimal = 0):
284
290
places += 1
285
291
286
292
# Set decimal places, if number of decimal places is specified (decimal > 0)
293
+ txt = None
287
294
if places > 0 < decimal < len (stnum [places :]) and dot > 0 :
288
295
txt = stnum [: dot + decimal + 1 ]
289
296
elif places > 0 :
290
297
txt = stnum [:places ]
291
298
292
- if len (txt ) > self ._chars + 1 :
299
+ if txt is None or len (txt ) > self ._chars + 1 :
293
300
self ._auto_write = auto_write
294
301
raise ValueError ("Output string ('{0}') is too long!" .format (txt ))
295
302
@@ -298,22 +305,24 @@ def _number(self, number, decimal = 0):
298
305
299
306
return txt
300
307
301
- def _adjusted_index (self , index ) :
308
+ def _adjusted_index (self , index : int ) -> int :
302
309
# Determine which part of the buffer to use and adjust index
303
310
offset = (index // self ._bytes_per_buffer ()) * self ._buffer_size
304
311
return offset + index % self ._bytes_per_buffer ()
305
312
306
- def _chars_per_buffer (self ):
313
+ def _chars_per_buffer (self ) -> int :
307
314
return self ._chars // len (self .i2c_device )
308
315
309
- def _bytes_per_buffer (self ):
316
+ def _bytes_per_buffer (self ) -> int :
310
317
return self ._bytes_per_char * self ._chars_per_buffer ()
311
318
312
- def _char_buffer_index (self , char_pos ) :
319
+ def _char_buffer_index (self , char_pos : int ) -> int :
313
320
offset = (char_pos // self ._chars_per_buffer ()) * self ._buffer_size
314
321
return offset + (char_pos % self ._chars_per_buffer ()) * self ._bytes_per_char
315
322
316
- def set_digit_raw (self , index , bitmask ):
323
+ def set_digit_raw (
324
+ self , index : int , bitmask : Union [int , List [int , int ], Tuple [int , int ]]
325
+ ) -> None :
317
326
"""Set digit at position to raw bitmask value. Position should be a value
318
327
of 0 to 3 with 0 being the left most character on the display.
319
328
@@ -339,7 +348,7 @@ def set_digit_raw(self, index, bitmask):
339
348
if self ._auto_write :
340
349
self .show ()
341
350
342
- def marquee (self , text , delay = 0.25 , loop = True ):
351
+ def marquee (self , text : str , delay : float = 0.25 , loop : bool = True ) -> None :
343
352
"""
344
353
Automatically scroll the text at the specified delay between characters
345
354
@@ -357,7 +366,7 @@ def marquee(self, text, delay = 0.25, loop = True):
357
366
else :
358
367
self ._scroll_marquee (text , delay )
359
368
360
- def _scroll_marquee (self , text , delay ):
369
+ def _scroll_marquee (self , text : str , delay : float ):
361
370
"""Scroll through the text string once using the delay"""
362
371
char_is_dot = False
363
372
for character in text :
@@ -374,22 +383,22 @@ class _AbstractSeg7x4(Seg14x4):
374
383
375
384
def __init__ ( # pylint: disable=too-many-arguments
376
385
self ,
377
- i2c ,
378
- address = 0x70 ,
379
- auto_write = True ,
380
- char_dict = None ,
381
- chars_per_display = 4 ,
382
- ):
386
+ i2c : I2C ,
387
+ address : Union [ int , List [ int ], Tuple [ int , ...]] = 0x70 ,
388
+ auto_write : bool = True ,
389
+ char_dict : Optional [ Dict [ str , int ]] = None ,
390
+ chars_per_display : int = 4 ,
391
+ ) -> None :
383
392
super ().__init__ (i2c , address , auto_write , chars_per_display )
384
393
self ._chardict = char_dict
385
394
self ._bytes_per_char = 1
386
395
387
- def _adjusted_index (self , index ) :
396
+ def _adjusted_index (self , index : int ) -> int :
388
397
# Determine which part of the buffer to use and adjust index
389
398
offset = (index // self ._bytes_per_buffer ()) * self ._buffer_size
390
399
return offset + self .POSITIONS [index % self ._bytes_per_buffer ()]
391
400
392
- def scroll (self , count = 1 ):
401
+ def scroll (self , count : int = 1 ) -> None :
393
402
"""Scroll the display by specified number of places.
394
403
395
404
:param int count: The number of places to scroll
@@ -405,7 +414,7 @@ def scroll(self, count = 1):
405
414
self ._get_buffer (self ._adjusted_index (i + count )),
406
415
)
407
416
408
- def _push (self , char ) :
417
+ def _push (self , char : str ) -> None :
409
418
"""Scroll the display and add a character at the end."""
410
419
if char in ":;" :
411
420
self ._put (char )
@@ -418,7 +427,7 @@ def _push(self, char):
418
427
self ._put (" " , self ._chars - 1 )
419
428
self ._put (char , self ._chars - 1 )
420
429
421
- def _put (self , char , index = 0 ):
430
+ def _put (self , char : str , index : int = 0 ) -> None :
422
431
"""Put a character at the specified place."""
423
432
# pylint: disable=too-many-return-statements
424
433
if not 0 <= index < self ._chars :
@@ -456,7 +465,7 @@ def _put(self, char, index = 0):
456
465
return
457
466
self ._set_buffer (index , NUMBERS [character ])
458
467
459
- def set_digit_raw (self , index , bitmask ) :
468
+ def set_digit_raw (self , index : int , bitmask : int ) -> None :
460
469
"""Set digit at position to raw bitmask value. Position should be a value
461
470
of 0 to 3 with 0 being the left most digit on the display.
462
471
@@ -490,23 +499,23 @@ class Seg7x4(_AbstractSeg7x4):
490
499
491
500
def __init__ ( # pylint: disable=too-many-arguments
492
501
self ,
493
- i2c ,
494
- address = 0x70 ,
495
- auto_write = True ,
496
- char_dict = None ,
497
- chars_per_display = 4 ,
498
- ):
502
+ i2c : I2C ,
503
+ address : Union [ int , List [ int ], Tuple [ int , ...]] = 0x70 ,
504
+ auto_write : bool = True ,
505
+ char_dict : Optional [ Dict [ str , int ]] = None ,
506
+ chars_per_display : int = 4 ,
507
+ ) -> None :
499
508
super ().__init__ (i2c , address , auto_write , char_dict , chars_per_display )
500
509
# Use colon for controling two-dots indicator at the center (index 0)
501
510
self ._colon = Colon (self )
502
511
503
512
@property
504
- def colon (self ):
513
+ def colon (self ) -> bool :
505
514
"""Simplified colon accessor"""
506
515
return self ._colon [0 ]
507
516
508
517
@colon .setter
509
- def colon (self , turn_on ) :
518
+ def colon (self , turn_on : bool ) -> None :
510
519
self ._colon [0 ] = turn_on
511
520
512
521
@@ -522,17 +531,17 @@ class BigSeg7x4(_AbstractSeg7x4):
522
531
523
532
def __init__ (
524
533
self ,
525
- i2c ,
526
- address = 0x70 ,
527
- auto_write = True ,
528
- char_dict = None ,
529
- ):
534
+ i2c : I2C ,
535
+ address : Union [ int , List [ int ], Tuple [ int , ...]] = 0x70 ,
536
+ auto_write : bool = True ,
537
+ char_dict : Optional [ Dict [ str , int ]] = None ,
538
+ ) -> None :
530
539
super ().__init__ (i2c , address , auto_write , char_dict )
531
540
# Use colon for controling two-dots indicator at the center (index 0)
532
541
# or the two-dots indicators at the left (index 1)
533
542
self .colons = Colon (self , 2 )
534
543
535
- def _setindicator (self , index , value ) :
544
+ def _setindicator (self , index : int , value : bool ) -> None :
536
545
"""Set side LEDs (dots)
537
546
Index is as follow :
538
547
* 0 : two dots at the center
@@ -549,38 +558,38 @@ def _setindicator(self, index, value):
549
558
if self ._auto_write :
550
559
self .show ()
551
560
552
- def _getindicator (self , index ) :
561
+ def _getindicator (self , index : int ) -> int :
553
562
"""Get side LEDs (dots)
554
563
See setindicator() for indexes
555
564
"""
556
565
bitmask = 1 << (index + 1 )
557
566
return self ._get_buffer (0x04 ) & bitmask
558
567
559
568
@property
560
- def top_left_dot (self ):
569
+ def top_left_dot (self ) -> bool :
561
570
"""The top-left dot indicator."""
562
571
return bool (self ._getindicator (1 ))
563
572
564
573
@top_left_dot .setter
565
- def top_left_dot (self , value ) :
574
+ def top_left_dot (self , value : bool ) -> None :
566
575
self ._setindicator (1 , value )
567
576
568
577
@property
569
- def bottom_left_dot (self ):
578
+ def bottom_left_dot (self ) -> bool :
570
579
"""The bottom-left dot indicator."""
571
580
return bool (self ._getindicator (2 ))
572
581
573
582
@bottom_left_dot .setter
574
- def bottom_left_dot (self , value ) :
583
+ def bottom_left_dot (self , value : bool ) -> None :
575
584
self ._setindicator (2 , value )
576
585
577
586
@property
578
- def ampm (self ):
587
+ def ampm (self ) -> bool :
579
588
"""The AM/PM indicator."""
580
589
return bool (self ._getindicator (3 ))
581
590
582
591
@ampm .setter
583
- def ampm (self , value ) :
592
+ def ampm (self , value : bool ) -> None :
584
593
self ._setindicator (3 , value )
585
594
586
595
@@ -591,11 +600,11 @@ class Colon:
591
600
592
601
MASKS = (0x02 , 0x0C )
593
602
594
- def __init__ (self , disp , num_of_colons = 1 ):
603
+ def __init__ (self , disp : _AbstractSeg7x4 , num_of_colons : int = 1 ) -> None :
595
604
self ._disp = disp
596
605
self ._num_of_colons = num_of_colons
597
606
598
- def __setitem__ (self , key , value ) :
607
+ def __setitem__ (self , key : int , value : bool ) -> None :
599
608
if key > self ._num_of_colons - 1 :
600
609
raise ValueError ("Trying to set a non-existent colon." )
601
610
current = self ._disp ._get_buffer (0x04 )
@@ -606,7 +615,7 @@ def __setitem__(self, key, value):
606
615
if self ._disp .auto_write :
607
616
self ._disp .show ()
608
617
609
- def __getitem__ (self , key ) :
618
+ def __getitem__ (self , key : int ) -> bool :
610
619
if key > self ._num_of_colons - 1 :
611
620
raise ValueError ("Trying to access a non-existent colon." )
612
621
return bool (self ._disp ._get_buffer (0x04 ) & self .MASKS [key ])
0 commit comments