8
8
=========================
9
9
"""
10
10
import time
11
- from time import sleep
12
11
from adafruit_ht16k33 .ht16k33 import HT16K33
13
12
14
13
try :
@@ -191,6 +190,7 @@ def __init__(
191
190
self ._last_nb_scroll_time = - 1
192
191
self ._nb_scroll_text = None
193
192
self ._nb_scroll_index = - 1
193
+ self ._nb_prev_char_is_dot = False
194
194
195
195
def print (self , value : Union [str , float ], decimal : int = 0 ) -> None :
196
196
"""Print the value to the display.
@@ -369,7 +369,13 @@ def set_digit_raw(
369
369
if self ._auto_write :
370
370
self .show ()
371
371
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 :
373
379
"""
374
380
Scroll the text at the specified delay between characters. Must be called
375
381
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
378
384
:param float delay: (optional) The delay in seconds to pause before scrolling
379
385
to the next character (default=0.25)
380
386
: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)
381
389
"""
382
390
# pylint: disable=too-many-nested-blocks
383
391
if isinstance (text , str ):
384
392
now = time .monotonic ()
385
393
# if text is the same
386
394
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 (" " )
397
414
else :
398
- if loop :
399
- self ._nb_scroll_index = - 1
400
- self ._push (" " )
401
- self .show ()
402
-
415
+ return True
403
416
else :
404
417
# different text
418
+ self ._nb_scroll_index = 0
419
+ self .fill (False )
405
420
self ._nb_scroll_text = text
406
421
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
412
425
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 :
414
429
"""
415
430
Automatically scroll the text at the specified delay between characters
416
431
417
432
:param str text: The text to display
418
433
:param float delay: (optional) The delay in seconds to pause before scrolling
419
434
to the next character (default=0.25)
420
435
: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)
422
438
"""
423
439
if isinstance (text , str ):
424
440
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
441
446
442
447
443
448
class _AbstractSeg7x4 (Seg14x4 ):
0 commit comments