|
7 | 7 | adafruit_ht16k33.segments
|
8 | 8 | =========================
|
9 | 9 | """
|
10 |
| - |
11 |
| -from time import sleep |
| 10 | +import time |
12 | 11 | from adafruit_ht16k33.ht16k33 import HT16K33
|
13 | 12 |
|
14 | 13 | try:
|
|
17 | 16 | except ImportError:
|
18 | 17 | pass
|
19 | 18 |
|
20 |
| - |
21 | 19 | __version__ = "0.0.0+auto.0"
|
22 | 20 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HT16K33.git"
|
23 | 21 |
|
24 | 22 | # fmt: off
|
25 | 23 | CHARS = (
|
26 |
| - 0b00000000, 0b00000000, # |
27 |
| - 0b01000000, 0b00000110, # ! |
28 |
| - 0b00000010, 0b00100000, # " |
29 |
| - 0b00010010, 0b11001110, # # |
30 |
| - 0b00010010, 0b11101101, # $ |
31 |
| - 0b00001100, 0b00100100, # % |
32 |
| - 0b00100011, 0b01011101, # & |
33 |
| - 0b00000100, 0b00000000, # ' |
34 |
| - 0b00100100, 0b00000000, # ( |
35 |
| - 0b00001001, 0b00000000, # ) |
36 |
| - 0b00111111, 0b11000000, # * |
37 |
| - 0b00010010, 0b11000000, # + |
38 |
| - 0b00001000, 0b00000000, # , |
39 |
| - 0b00000000, 0b11000000, # - |
40 |
| - 0b00000000, 0b00000000, # . |
41 |
| - 0b00001100, 0b00000000, # / |
42 |
| - 0b00001100, 0b00111111, # 0 |
43 |
| - 0b00000000, 0b00000110, # 1 |
44 |
| - 0b00000000, 0b11011011, # 2 |
45 |
| - 0b00000000, 0b10001111, # 3 |
46 |
| - 0b00000000, 0b11100110, # 4 |
47 |
| - 0b00100000, 0b01101001, # 5 |
48 |
| - 0b00000000, 0b11111101, # 6 |
49 |
| - 0b00000000, 0b00000111, # 7 |
50 |
| - 0b00000000, 0b11111111, # 8 |
51 |
| - 0b00000000, 0b11101111, # 9 |
52 |
| - 0b00010010, 0b00000000, # : |
53 |
| - 0b00001010, 0b00000000, # ; |
54 |
| - 0b00100100, 0b01000000, # < |
55 |
| - 0b00000000, 0b11001000, # = |
56 |
| - 0b00001001, 0b10000000, # > |
57 |
| - 0b01100000, 0b10100011, # ? |
58 |
| - 0b00000010, 0b10111011, # @ |
59 |
| - 0b00000000, 0b11110111, # A |
60 |
| - 0b00010010, 0b10001111, # B |
61 |
| - 0b00000000, 0b00111001, # C |
62 |
| - 0b00010010, 0b00001111, # D |
63 |
| - 0b00000000, 0b11111001, # E |
64 |
| - 0b00000000, 0b01110001, # F |
65 |
| - 0b00000000, 0b10111101, # G |
66 |
| - 0b00000000, 0b11110110, # H |
67 |
| - 0b00010010, 0b00000000, # I |
68 |
| - 0b00000000, 0b00011110, # J |
69 |
| - 0b00100100, 0b01110000, # K |
70 |
| - 0b00000000, 0b00111000, # L |
71 |
| - 0b00000101, 0b00110110, # M |
72 |
| - 0b00100001, 0b00110110, # N |
73 |
| - 0b00000000, 0b00111111, # O |
74 |
| - 0b00000000, 0b11110011, # P |
75 |
| - 0b00100000, 0b00111111, # Q |
76 |
| - 0b00100000, 0b11110011, # R |
77 |
| - 0b00000000, 0b11101101, # S |
78 |
| - 0b00010010, 0b00000001, # T |
79 |
| - 0b00000000, 0b00111110, # U |
80 |
| - 0b00001100, 0b00110000, # V |
81 |
| - 0b00101000, 0b00110110, # W |
82 |
| - 0b00101101, 0b00000000, # X |
83 |
| - 0b00010101, 0b00000000, # Y |
84 |
| - 0b00001100, 0b00001001, # Z |
85 |
| - 0b00000000, 0b00111001, # [ |
86 |
| - 0b00100001, 0b00000000, # \ |
87 |
| - 0b00000000, 0b00001111, # ] |
88 |
| - 0b00001100, 0b00000011, # ^ |
89 |
| - 0b00000000, 0b00001000, # _ |
90 |
| - 0b00000001, 0b00000000, # ` |
91 |
| - 0b00010000, 0b01011000, # a |
92 |
| - 0b00100000, 0b01111000, # b |
93 |
| - 0b00000000, 0b11011000, # c |
94 |
| - 0b00001000, 0b10001110, # d |
95 |
| - 0b00001000, 0b01011000, # e |
96 |
| - 0b00000000, 0b01110001, # f |
97 |
| - 0b00000100, 0b10001110, # g |
98 |
| - 0b00010000, 0b01110000, # h |
99 |
| - 0b00010000, 0b00000000, # i |
100 |
| - 0b00000000, 0b00001110, # j |
101 |
| - 0b00110110, 0b00000000, # k |
102 |
| - 0b00000000, 0b00110000, # l |
103 |
| - 0b00010000, 0b11010100, # m |
104 |
| - 0b00010000, 0b01010000, # n |
105 |
| - 0b00000000, 0b11011100, # o |
106 |
| - 0b00000001, 0b01110000, # p |
107 |
| - 0b00000100, 0b10000110, # q |
108 |
| - 0b00000000, 0b01010000, # r |
109 |
| - 0b00100000, 0b10001000, # s |
110 |
| - 0b00000000, 0b01111000, # t |
111 |
| - 0b00000000, 0b00011100, # u |
112 |
| - 0b00100000, 0b00000100, # v |
113 |
| - 0b00101000, 0b00010100, # w |
114 |
| - 0b00101000, 0b11000000, # x |
115 |
| - 0b00100000, 0b00001100, # y |
116 |
| - 0b00001000, 0b01001000, # z |
117 |
| - 0b00001001, 0b01001001, # { |
118 |
| - 0b00010010, 0b00000000, # | |
119 |
| - 0b00100100, 0b10001001, # } |
120 |
| - 0b00000101, 0b00100000, # ~ |
| 24 | + 0b00000000, 0b00000000, # |
| 25 | + 0b01000000, 0b00000110, # ! |
| 26 | + 0b00000010, 0b00100000, # " |
| 27 | + 0b00010010, 0b11001110, # # |
| 28 | + 0b00010010, 0b11101101, # $ |
| 29 | + 0b00001100, 0b00100100, # % |
| 30 | + 0b00100011, 0b01011101, # & |
| 31 | + 0b00000100, 0b00000000, # ' |
| 32 | + 0b00100100, 0b00000000, # ( |
| 33 | + 0b00001001, 0b00000000, # ) |
| 34 | + 0b00111111, 0b11000000, # * |
| 35 | + 0b00010010, 0b11000000, # + |
| 36 | + 0b00001000, 0b00000000, # , |
| 37 | + 0b00000000, 0b11000000, # - |
| 38 | + 0b00000000, 0b00000000, # . |
| 39 | + 0b00001100, 0b00000000, # / |
| 40 | + 0b00001100, 0b00111111, # 0 |
| 41 | + 0b00000000, 0b00000110, # 1 |
| 42 | + 0b00000000, 0b11011011, # 2 |
| 43 | + 0b00000000, 0b10001111, # 3 |
| 44 | + 0b00000000, 0b11100110, # 4 |
| 45 | + 0b00100000, 0b01101001, # 5 |
| 46 | + 0b00000000, 0b11111101, # 6 |
| 47 | + 0b00000000, 0b00000111, # 7 |
| 48 | + 0b00000000, 0b11111111, # 8 |
| 49 | + 0b00000000, 0b11101111, # 9 |
| 50 | + 0b00010010, 0b00000000, # : |
| 51 | + 0b00001010, 0b00000000, # ; |
| 52 | + 0b00100100, 0b01000000, # < |
| 53 | + 0b00000000, 0b11001000, # = |
| 54 | + 0b00001001, 0b10000000, # > |
| 55 | + 0b01100000, 0b10100011, # ? |
| 56 | + 0b00000010, 0b10111011, # @ |
| 57 | + 0b00000000, 0b11110111, # A |
| 58 | + 0b00010010, 0b10001111, # B |
| 59 | + 0b00000000, 0b00111001, # C |
| 60 | + 0b00010010, 0b00001111, # D |
| 61 | + 0b00000000, 0b11111001, # E |
| 62 | + 0b00000000, 0b01110001, # F |
| 63 | + 0b00000000, 0b10111101, # G |
| 64 | + 0b00000000, 0b11110110, # H |
| 65 | + 0b00010010, 0b00000000, # I |
| 66 | + 0b00000000, 0b00011110, # J |
| 67 | + 0b00100100, 0b01110000, # K |
| 68 | + 0b00000000, 0b00111000, # L |
| 69 | + 0b00000101, 0b00110110, # M |
| 70 | + 0b00100001, 0b00110110, # N |
| 71 | + 0b00000000, 0b00111111, # O |
| 72 | + 0b00000000, 0b11110011, # P |
| 73 | + 0b00100000, 0b00111111, # Q |
| 74 | + 0b00100000, 0b11110011, # R |
| 75 | + 0b00000000, 0b11101101, # S |
| 76 | + 0b00010010, 0b00000001, # T |
| 77 | + 0b00000000, 0b00111110, # U |
| 78 | + 0b00001100, 0b00110000, # V |
| 79 | + 0b00101000, 0b00110110, # W |
| 80 | + 0b00101101, 0b00000000, # X |
| 81 | + 0b00010101, 0b00000000, # Y |
| 82 | + 0b00001100, 0b00001001, # Z |
| 83 | + 0b00000000, 0b00111001, # [ |
| 84 | + 0b00100001, 0b00000000, # \ |
| 85 | + 0b00000000, 0b00001111, # ] |
| 86 | + 0b00001100, 0b00000011, # ^ |
| 87 | + 0b00000000, 0b00001000, # _ |
| 88 | + 0b00000001, 0b00000000, # ` |
| 89 | + 0b00010000, 0b01011000, # a |
| 90 | + 0b00100000, 0b01111000, # b |
| 91 | + 0b00000000, 0b11011000, # c |
| 92 | + 0b00001000, 0b10001110, # d |
| 93 | + 0b00001000, 0b01011000, # e |
| 94 | + 0b00000000, 0b01110001, # f |
| 95 | + 0b00000100, 0b10001110, # g |
| 96 | + 0b00010000, 0b01110000, # h |
| 97 | + 0b00010000, 0b00000000, # i |
| 98 | + 0b00000000, 0b00001110, # j |
| 99 | + 0b00110110, 0b00000000, # k |
| 100 | + 0b00000000, 0b00110000, # l |
| 101 | + 0b00010000, 0b11010100, # m |
| 102 | + 0b00010000, 0b01010000, # n |
| 103 | + 0b00000000, 0b11011100, # o |
| 104 | + 0b00000001, 0b01110000, # p |
| 105 | + 0b00000100, 0b10000110, # q |
| 106 | + 0b00000000, 0b01010000, # r |
| 107 | + 0b00100000, 0b10001000, # s |
| 108 | + 0b00000000, 0b01111000, # t |
| 109 | + 0b00000000, 0b00011100, # u |
| 110 | + 0b00100000, 0b00000100, # v |
| 111 | + 0b00101000, 0b00010100, # w |
| 112 | + 0b00101000, 0b11000000, # x |
| 113 | + 0b00100000, 0b00001100, # y |
| 114 | + 0b00001000, 0b01001000, # z |
| 115 | + 0b00001001, 0b01001001, # { |
| 116 | + 0b00010010, 0b00000000, # | |
| 117 | + 0b00100100, 0b10001001, # } |
| 118 | + 0b00000101, 0b00100000, # ~ |
121 | 119 | 0b00111111, 0b11111111,
|
122 | 120 | )
|
123 | 121 | # fmt: on
|
@@ -189,6 +187,10 @@ def __init__(
|
189 | 187 |
|
190 | 188 | self._chars = chars_per_display * len(self.i2c_device)
|
191 | 189 | self._bytes_per_char = 2
|
| 190 | + self._last_nb_scroll_time = -1 |
| 191 | + self._nb_scroll_text = None |
| 192 | + self._nb_scroll_index = -1 |
| 193 | + self._nb_prev_char_is_dot = False |
192 | 194 |
|
193 | 195 | def print(self, value: Union[str, float], decimal: int = 0) -> None:
|
194 | 196 | """Print the value to the display.
|
@@ -367,38 +369,84 @@ def set_digit_raw(
|
367 | 369 | if self._auto_write:
|
368 | 370 | self.show()
|
369 | 371 |
|
370 |
| - def marquee(self, text: str, delay: float = 0.25, loop: bool = True) -> None: |
| 372 | + def non_blocking_marquee( |
| 373 | + self, |
| 374 | + text: str, |
| 375 | + delay: float = 0.25, |
| 376 | + loop: bool = True, |
| 377 | + space_between: bool = False, |
| 378 | + ) -> bool: |
371 | 379 | """
|
372 |
| - Automatically scroll the text at the specified delay between characters |
| 380 | + Scroll the text at the specified delay between characters. Must be called |
| 381 | + repeatedly from main loop faster than delay time. |
373 | 382 |
|
374 | 383 | :param str text: The text to display
|
375 | 384 | :param float delay: (optional) The delay in seconds to pause before scrolling
|
376 | 385 | to the next character (default=0.25)
|
377 | 386 | :param bool loop: (optional) Whether to endlessly loop the text (default=True)
|
378 |
| -
|
| 387 | + :param bool space_between: (optional) Whether to seperate the end and beginning of |
| 388 | + the text with a space. (default=False) |
379 | 389 | """
|
| 390 | + # pylint: disable=too-many-nested-blocks |
380 | 391 | if isinstance(text, str):
|
381 |
| - self.fill(False) |
382 |
| - if loop: |
383 |
| - while True: |
384 |
| - self._scroll_marquee(text, delay) |
| 392 | + now = time.monotonic() |
| 393 | + # if text is the same |
| 394 | + if text == self._nb_scroll_text: |
| 395 | + # if we delayed long enough, and it's time to scroll |
| 396 | + if now >= self._last_nb_scroll_time + delay: |
| 397 | + # if there are chars left in the text |
| 398 | + if self._nb_scroll_index + 1 < len(text): |
| 399 | + self._nb_scroll_index += 1 |
| 400 | + |
| 401 | + _character = text[self._nb_scroll_index] |
| 402 | + |
| 403 | + if _character != "." or self._nb_prev_char_is_dot: |
| 404 | + self._last_nb_scroll_time = now |
| 405 | + |
| 406 | + self.print(text[self._nb_scroll_index]) |
| 407 | + self._nb_prev_char_is_dot = text[self._nb_scroll_index] == "." |
| 408 | + else: |
| 409 | + self._nb_scroll_index = -1 |
| 410 | + if loop: |
| 411 | + if space_between: |
| 412 | + self._last_nb_scroll_time = now |
| 413 | + self.print(" ") |
| 414 | + else: |
| 415 | + return True |
385 | 416 | else:
|
386 |
| - self._scroll_marquee(text, delay) |
| 417 | + # different text |
| 418 | + self._nb_scroll_index = 0 |
| 419 | + self.fill(False) |
| 420 | + self._nb_scroll_text = text |
| 421 | + self._last_nb_scroll_time = now |
| 422 | + self.print(text[0]) |
387 | 423 |
|
388 |
| - def _scroll_marquee(self, text: str, delay: float) -> None: |
389 |
| - """Scroll through the text string once using the delay""" |
390 |
| - char_is_dot = False |
391 |
| - for character in text: |
392 |
| - self.print(character) |
393 |
| - # Add delay if character is not a dot or more than 2 in a row |
394 |
| - if character != "." or char_is_dot: |
395 |
| - sleep(delay) |
396 |
| - char_is_dot = character == "." |
397 |
| - self.show() |
| 424 | + return False |
| 425 | + |
| 426 | + def marquee( |
| 427 | + self, text: str, delay: float = 0.25, loop: bool = True, space_between=False |
| 428 | + ) -> None: |
| 429 | + """ |
| 430 | + Automatically scroll the text at the specified delay between characters |
| 431 | +
|
| 432 | + :param str text: The text to display |
| 433 | + :param float delay: (optional) The delay in seconds to pause before scrolling |
| 434 | + to the next character (default=0.25) |
| 435 | + :param bool loop: (optional) Whether to endlessly loop the text (default=True) |
| 436 | + :param bool space_between: (optional) Whether to seperate the end and beginning of |
| 437 | + the text with a space. (default=False) |
| 438 | + """ |
| 439 | + if isinstance(text, str): |
| 440 | + self.fill(False) |
| 441 | + while True: |
| 442 | + if self.non_blocking_marquee( |
| 443 | + text=text, delay=delay, loop=loop, space_between=space_between |
| 444 | + ): |
| 445 | + return |
398 | 446 |
|
399 | 447 |
|
400 | 448 | class _AbstractSeg7x4(Seg14x4):
|
401 |
| - POSITIONS = (0, 2, 6, 8) # The positions of characters. |
| 449 | + POSITIONS = (0, 2, 6, 8) # The positions of characters. |
402 | 450 |
|
403 | 451 | def __init__( # pylint: disable=too-many-arguments
|
404 | 452 | self,
|
|
0 commit comments