Skip to content

Commit 5dc8f89

Browse files
committed
Reannotate segments.py
1 parent 07a94f0 commit 5dc8f89

File tree

1 file changed

+65
-56
lines changed

1 file changed

+65
-56
lines changed

adafruit_ht16k33/segments.py

Lines changed: 65 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
from time import sleep
1212
from adafruit_ht16k33.ht16k33 import HT16K33
1313

14+
try:
15+
from typing import Union, List, Tuple, Optional, Dict
16+
from busio import I2C
17+
except ImportError:
18+
pass
19+
1420

1521
__version__ = "0.0.0+auto.0"
1622
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
@@ -149,11 +155,11 @@ class Seg14x4(HT16K33):
149155

150156
def __init__(
151157
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:
157163
super().__init__(i2c, address, auto_write)
158164
if not 1 <= chars_per_display <= 8:
159165
raise ValueError(
@@ -163,7 +169,7 @@ def __init__(
163169
self._chars = chars_per_display * len(self.i2c_device)
164170
self._bytes_per_char = 2
165171

166-
def print(self, value, decimal = 0):
172+
def print(self, value: Union[str, float], decimal: int = 0) -> None:
167173
"""Print the value to the display.
168174
169175
:param value: The value to print
@@ -173,7 +179,7 @@ def print(self, value, decimal = 0):
173179
integer if decimal is zero.
174180
"""
175181

176-
if isinstance(value, (str)):
182+
if isinstance(value, str):
177183
self._text(value)
178184
elif isinstance(value, (int, float)):
179185
self._number(value, decimal)
@@ -182,7 +188,7 @@ def print(self, value, decimal = 0):
182188
if self._auto_write:
183189
self.show()
184190

185-
def print_hex(self, value):
191+
def print_hex(self, value: Union[int, str]) -> None:
186192
"""Print the value as a hexidecimal string to the display.
187193
188194
:param int value: The number to print
@@ -198,7 +204,7 @@ def __setitem__(self, key, value):
198204
if self._auto_write:
199205
self.show()
200206

201-
def scroll(self, count = 1):
207+
def scroll(self, count: int = 1) -> None:
202208
"""Scroll the display by specified number of places.
203209
204210
:param int count: The number of places to scroll
@@ -214,7 +220,7 @@ def scroll(self, count = 1):
214220
self._get_buffer(self._adjusted_index(i + 2 * count)),
215221
)
216222

217-
def _put(self, char, index = 0):
223+
def _put(self, char: str, index: int = 0) -> None:
218224
"""Put a character at the specified place."""
219225
if not 0 <= index < self._chars:
220226
return
@@ -230,7 +236,7 @@ def _put(self, char, index = 0):
230236
self._set_buffer(self._adjusted_index(index * 2), CHARS[1 + character])
231237
self._set_buffer(self._adjusted_index(index * 2 + 1), CHARS[character])
232238

233-
def _push(self, char):
239+
def _push(self, char: str) -> None:
234240
"""Scroll the display and add a character at the end."""
235241
if (
236242
char != "."
@@ -241,12 +247,12 @@ def _push(self, char):
241247
self._put(" ", self._chars - 1)
242248
self._put(char, self._chars - 1)
243249

244-
def _text(self, text):
250+
def _text(self, text: str) -> None:
245251
"""Display the specified text."""
246252
for character in text:
247253
self._push(character)
248254

249-
def _number(self, number, decimal = 0):
255+
def _number(self, number: float, decimal: int = 0) -> str:
250256
"""
251257
Display a floating point or integer number on the Adafruit HT16K33 based displays
252258
@@ -284,12 +290,13 @@ def _number(self, number, decimal = 0):
284290
places += 1
285291

286292
# Set decimal places, if number of decimal places is specified (decimal > 0)
293+
txt = None
287294
if places > 0 < decimal < len(stnum[places:]) and dot > 0:
288295
txt = stnum[: dot + decimal + 1]
289296
elif places > 0:
290297
txt = stnum[:places]
291298

292-
if len(txt) > self._chars + 1:
299+
if txt is None or len(txt) > self._chars + 1:
293300
self._auto_write = auto_write
294301
raise ValueError("Output string ('{0}') is too long!".format(txt))
295302

@@ -298,22 +305,24 @@ def _number(self, number, decimal = 0):
298305

299306
return txt
300307

301-
def _adjusted_index(self, index):
308+
def _adjusted_index(self, index: int) -> int:
302309
# Determine which part of the buffer to use and adjust index
303310
offset = (index // self._bytes_per_buffer()) * self._buffer_size
304311
return offset + index % self._bytes_per_buffer()
305312

306-
def _chars_per_buffer(self):
313+
def _chars_per_buffer(self) -> int:
307314
return self._chars // len(self.i2c_device)
308315

309-
def _bytes_per_buffer(self):
316+
def _bytes_per_buffer(self) -> int:
310317
return self._bytes_per_char * self._chars_per_buffer()
311318

312-
def _char_buffer_index(self, char_pos):
319+
def _char_buffer_index(self, char_pos: int) -> int:
313320
offset = (char_pos // self._chars_per_buffer()) * self._buffer_size
314321
return offset + (char_pos % self._chars_per_buffer()) * self._bytes_per_char
315322

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:
317326
"""Set digit at position to raw bitmask value. Position should be a value
318327
of 0 to 3 with 0 being the left most character on the display.
319328
@@ -339,7 +348,7 @@ def set_digit_raw(self, index, bitmask):
339348
if self._auto_write:
340349
self.show()
341350

342-
def marquee(self, text, delay = 0.25, loop = True):
351+
def marquee(self, text: str, delay: float = 0.25, loop: bool = True) -> None:
343352
"""
344353
Automatically scroll the text at the specified delay between characters
345354
@@ -357,7 +366,7 @@ def marquee(self, text, delay = 0.25, loop = True):
357366
else:
358367
self._scroll_marquee(text, delay)
359368

360-
def _scroll_marquee(self, text, delay):
369+
def _scroll_marquee(self, text: str, delay: float):
361370
"""Scroll through the text string once using the delay"""
362371
char_is_dot = False
363372
for character in text:
@@ -374,22 +383,22 @@ class _AbstractSeg7x4(Seg14x4):
374383

375384
def __init__( # pylint: disable=too-many-arguments
376385
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:
383392
super().__init__(i2c, address, auto_write, chars_per_display)
384393
self._chardict = char_dict
385394
self._bytes_per_char = 1
386395

387-
def _adjusted_index(self, index):
396+
def _adjusted_index(self, index: int) -> int:
388397
# Determine which part of the buffer to use and adjust index
389398
offset = (index // self._bytes_per_buffer()) * self._buffer_size
390399
return offset + self.POSITIONS[index % self._bytes_per_buffer()]
391400

392-
def scroll(self, count = 1):
401+
def scroll(self, count: int = 1) -> None:
393402
"""Scroll the display by specified number of places.
394403
395404
:param int count: The number of places to scroll
@@ -405,7 +414,7 @@ def scroll(self, count = 1):
405414
self._get_buffer(self._adjusted_index(i + count)),
406415
)
407416

408-
def _push(self, char):
417+
def _push(self, char: str) -> None:
409418
"""Scroll the display and add a character at the end."""
410419
if char in ":;":
411420
self._put(char)
@@ -418,7 +427,7 @@ def _push(self, char):
418427
self._put(" ", self._chars - 1)
419428
self._put(char, self._chars - 1)
420429

421-
def _put(self, char, index = 0):
430+
def _put(self, char: str, index: int = 0) -> None:
422431
"""Put a character at the specified place."""
423432
# pylint: disable=too-many-return-statements
424433
if not 0 <= index < self._chars:
@@ -456,7 +465,7 @@ def _put(self, char, index = 0):
456465
return
457466
self._set_buffer(index, NUMBERS[character])
458467

459-
def set_digit_raw(self, index, bitmask):
468+
def set_digit_raw(self, index: int, bitmask: int) -> None:
460469
"""Set digit at position to raw bitmask value. Position should be a value
461470
of 0 to 3 with 0 being the left most digit on the display.
462471
@@ -490,23 +499,23 @@ class Seg7x4(_AbstractSeg7x4):
490499

491500
def __init__( # pylint: disable=too-many-arguments
492501
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:
499508
super().__init__(i2c, address, auto_write, char_dict, chars_per_display)
500509
# Use colon for controling two-dots indicator at the center (index 0)
501510
self._colon = Colon(self)
502511

503512
@property
504-
def colon(self):
513+
def colon(self) -> bool:
505514
"""Simplified colon accessor"""
506515
return self._colon[0]
507516

508517
@colon.setter
509-
def colon(self, turn_on):
518+
def colon(self, turn_on: bool) -> None:
510519
self._colon[0] = turn_on
511520

512521

@@ -522,17 +531,17 @@ class BigSeg7x4(_AbstractSeg7x4):
522531

523532
def __init__(
524533
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:
530539
super().__init__(i2c, address, auto_write, char_dict)
531540
# Use colon for controling two-dots indicator at the center (index 0)
532541
# or the two-dots indicators at the left (index 1)
533542
self.colons = Colon(self, 2)
534543

535-
def _setindicator(self, index, value):
544+
def _setindicator(self, index: int, value: bool) -> None:
536545
"""Set side LEDs (dots)
537546
Index is as follow :
538547
* 0 : two dots at the center
@@ -549,38 +558,38 @@ def _setindicator(self, index, value):
549558
if self._auto_write:
550559
self.show()
551560

552-
def _getindicator(self, index):
561+
def _getindicator(self, index: int) -> int:
553562
"""Get side LEDs (dots)
554563
See setindicator() for indexes
555564
"""
556565
bitmask = 1 << (index + 1)
557566
return self._get_buffer(0x04) & bitmask
558567

559568
@property
560-
def top_left_dot(self):
569+
def top_left_dot(self) -> bool:
561570
"""The top-left dot indicator."""
562571
return bool(self._getindicator(1))
563572

564573
@top_left_dot.setter
565-
def top_left_dot(self, value):
574+
def top_left_dot(self, value: bool) -> None:
566575
self._setindicator(1, value)
567576

568577
@property
569-
def bottom_left_dot(self):
578+
def bottom_left_dot(self) -> bool:
570579
"""The bottom-left dot indicator."""
571580
return bool(self._getindicator(2))
572581

573582
@bottom_left_dot.setter
574-
def bottom_left_dot(self, value):
583+
def bottom_left_dot(self, value: bool) -> None:
575584
self._setindicator(2, value)
576585

577586
@property
578-
def ampm(self):
587+
def ampm(self) -> bool:
579588
"""The AM/PM indicator."""
580589
return bool(self._getindicator(3))
581590

582591
@ampm.setter
583-
def ampm(self, value):
592+
def ampm(self, value: bool) -> None:
584593
self._setindicator(3, value)
585594

586595

@@ -591,11 +600,11 @@ class Colon:
591600

592601
MASKS = (0x02, 0x0C)
593602

594-
def __init__(self, disp, num_of_colons = 1):
603+
def __init__(self, disp: _AbstractSeg7x4, num_of_colons: int = 1) -> None:
595604
self._disp = disp
596605
self._num_of_colons = num_of_colons
597606

598-
def __setitem__(self, key, value):
607+
def __setitem__(self, key: int, value: bool) -> None:
599608
if key > self._num_of_colons - 1:
600609
raise ValueError("Trying to set a non-existent colon.")
601610
current = self._disp._get_buffer(0x04)
@@ -606,7 +615,7 @@ def __setitem__(self, key, value):
606615
if self._disp.auto_write:
607616
self._disp.show()
608617

609-
def __getitem__(self, key):
618+
def __getitem__(self, key: int) -> bool:
610619
if key > self._num_of_colons - 1:
611620
raise ValueError("Trying to access a non-existent colon.")
612621
return bool(self._disp._get_buffer(0x04) & self.MASKS[key])

0 commit comments

Comments
 (0)