Skip to content

Commit 2407c6e

Browse files
committed
make non-blocking behavior match blocking, refactor to re-use non-blocking from blocking and delete now unused code.
1 parent 4f74c59 commit 2407c6e

File tree

1 file changed

+45
-40
lines changed

1 file changed

+45
-40
lines changed

adafruit_ht16k33/segments.py

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
=========================
99
"""
1010
import time
11-
from time import sleep
1211
from adafruit_ht16k33.ht16k33 import HT16K33
1312

1413
try:
@@ -191,6 +190,7 @@ def __init__(
191190
self._last_nb_scroll_time = -1
192191
self._nb_scroll_text = None
193192
self._nb_scroll_index = -1
193+
self._nb_prev_char_is_dot = False
194194

195195
def print(self, value: Union[str, float], decimal: int = 0) -> None:
196196
"""Print the value to the display.
@@ -369,7 +369,13 @@ def set_digit_raw(
369369
if self._auto_write:
370370
self.show()
371371

372-
def non_blocking_marquee(self, text: str, delay: float = 0.25, loop: bool = True):
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:
373379
"""
374380
Scroll the text at the specified delay between characters. Must be called
375381
repeatedly from main loop faster than delay time.
@@ -378,66 +384,65 @@ def non_blocking_marquee(self, text: str, delay: float = 0.25, loop: bool = True
378384
:param float delay: (optional) The delay in seconds to pause before scrolling
379385
to the next character (default=0.25)
380386
:param bool loop: (optional) Whether to endlessly loop the text (default=True)
387+
:param bool space_between: (optional) Whether to seperate the end and beginning of
388+
the text with a space. (default=False)
381389
"""
382390
# pylint: disable=too-many-nested-blocks
383391
if isinstance(text, str):
384392
now = time.monotonic()
385393
# if text is the same
386394
if text == self._nb_scroll_text:
387-
# if the text is 4 or less chars we don't need scrolling.
388-
if len(text) > 4:
389-
# if we delayed long enough, and it's time to scroll
390-
if now >= self._last_nb_scroll_time + delay:
391-
self._last_nb_scroll_time = now
392-
# if there are chars left in the text
393-
if self._nb_scroll_index + 1 < len(text):
394-
self._nb_scroll_index += 1
395-
self._push(text[self._nb_scroll_index])
396-
self.show()
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(" ")
397414
else:
398-
if loop:
399-
self._nb_scroll_index = -1
400-
self._push(" ")
401-
self.show()
402-
415+
return True
403416
else:
404417
# different text
418+
self._nb_scroll_index = 0
419+
self.fill(False)
405420
self._nb_scroll_text = text
406421
self._last_nb_scroll_time = now
407-
if len(text) <= 4:
408-
self.print(f"{text}{' ' * (4 - len(text))}")
409-
else:
410-
self._nb_scroll_index = 3
411-
self.print(text[0:4])
422+
self.print(text[0])
423+
424+
return False
412425

413-
def marquee(self, text: str, delay: float = 0.25, loop: bool = True) -> None:
426+
def marquee(
427+
self, text: str, delay: float = 0.25, loop: bool = True, space_between=False
428+
) -> None:
414429
"""
415430
Automatically scroll the text at the specified delay between characters
416431
417432
:param str text: The text to display
418433
:param float delay: (optional) The delay in seconds to pause before scrolling
419434
to the next character (default=0.25)
420435
:param bool loop: (optional) Whether to endlessly loop the text (default=True)
421-
436+
:param bool space_between: (optional) Whether to seperate the end and beginning of
437+
the text with a space. (default=False)
422438
"""
423439
if isinstance(text, str):
424440
self.fill(False)
425-
if loop:
426-
while True:
427-
self._scroll_marquee(text, delay)
428-
else:
429-
self._scroll_marquee(text, delay)
430-
431-
def _scroll_marquee(self, text: str, delay: float) -> None:
432-
"""Scroll through the text string once using the delay"""
433-
char_is_dot = False
434-
for character in text:
435-
self.print(character)
436-
# Add delay if character is not a dot or more than 2 in a row
437-
if character != "." or char_is_dot:
438-
sleep(delay)
439-
char_is_dot = character == "."
440-
self.show()
441+
while True:
442+
if self.non_blocking_marquee(
443+
text=text, delay=delay, loop=loop, space_between=space_between
444+
):
445+
return
441446

442447

443448
class _AbstractSeg7x4(Seg14x4):

0 commit comments

Comments
 (0)