Skip to content

Commit bda0c97

Browse files
committed
scrolling label
1 parent 5283f39 commit bda0c97

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# SPDX-FileCopyrightText: 2019 Scott Shawcroft for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
"""
6+
`adafruit_display_text.scrolling_label`
7+
====================================================
8+
9+
Displays text into a fixed-width label that scrolls leftward
10+
if the full_text is large enough to need it.
11+
12+
* Author(s): Tim Cocks
13+
14+
Implementation Notes
15+
--------------------
16+
17+
**Hardware:**
18+
19+
**Software and Dependencies:**
20+
21+
* Adafruit CircuitPython firmware for the supported boards:
22+
https://circuitpython.org/downloads
23+
24+
"""
25+
26+
__version__ = "0.0.0-auto.0"
27+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
28+
29+
import time
30+
from adafruit_display_text import bitmap_label
31+
32+
33+
class ScrollingLabel(bitmap_label.Label):
34+
35+
"""
36+
ScrollingLabel - A fixed-width label that will scroll to the left
37+
in order to show the full text if it's larger than the fixed-width.
38+
39+
:param font: The font to use for the label.
40+
:param max_characters: The number of characters that sets the fixed-width. Default is 10.
41+
:param full_text: The full text to show in the label. If this is longer than
42+
`max_characters` then the label will scroll to show everything.
43+
:param animate_time: The number of seconds in between scrolling animation
44+
frames. Default is 0.3 seconds.
45+
:param current_index: The index of the first visible character in the label.
46+
Default is 0, the first character. Will increase while scrolling.
47+
"""
48+
49+
# pylint: disable=too-many-arguments
50+
def __init__(
51+
self,
52+
font,
53+
max_characters=10,
54+
full_text="",
55+
animate_time=0.3,
56+
current_index=0,
57+
**kwargs
58+
):
59+
60+
super().__init__(font, **kwargs)
61+
self.animate_time = animate_time
62+
self._current_index = current_index
63+
self._last_animate_time = -1
64+
self.max_characters = max_characters
65+
if "text" in kwargs:
66+
raise ValueError(
67+
"User code should not set the text argument on ScrollingText. "
68+
"Use full_text instead."
69+
)
70+
71+
if full_text[-1] != " ":
72+
full_text = "{} ".format(full_text)
73+
self._full_text = full_text
74+
75+
self.update()
76+
77+
def update(self, force=False):
78+
"""
79+
Attempt to update the display. If `animate_time` has elapsed since
80+
previews animation frame then move the characters over by 1 index.
81+
Must be called in the main loop of user code.
82+
83+
:param force: whether to ignore `animation_time` and force the update. Default is False.
84+
:return: None
85+
"""
86+
_now = time.monotonic()
87+
if force or self._last_animate_time + self.animate_time <= _now:
88+
89+
if len(self.full_text) <= self.max_characters:
90+
self.text = self.full_text
91+
self._last_animate_time = _now
92+
return
93+
94+
if self.current_index + self.max_characters <= len(self.full_text):
95+
_showing_string = self.full_text[
96+
self.current_index : self.current_index + self.max_characters
97+
]
98+
else:
99+
_showing_string_start = self.full_text[self.current_index :]
100+
_showing_string_end = "{}".format(
101+
self.full_text[
102+
: (self.current_index + self.max_characters)
103+
% len(self.full_text)
104+
]
105+
)
106+
107+
_showing_string = "{}{}".format(
108+
_showing_string_start, _showing_string_end
109+
)
110+
self.text = _showing_string
111+
112+
self.current_index += 1
113+
self._last_animate_time = _now
114+
115+
return
116+
117+
@property
118+
def current_index(self):
119+
"""
120+
Index of the first visible character.
121+
122+
:return int: the current index
123+
"""
124+
return self._current_index
125+
126+
@current_index.setter
127+
def current_index(self, new_index):
128+
if new_index < len(self.full_text):
129+
self._current_index = new_index
130+
else:
131+
self._current_index = new_index % len(self.full_text)
132+
133+
@property
134+
def full_text(self):
135+
"""
136+
The full text to be shown. If it's longer than `max_characters` then
137+
scrolling will occur as needed.
138+
139+
:return string: The full text of this label.
140+
"""
141+
return self._full_text
142+
143+
@full_text.setter
144+
def full_text(self, new_text):
145+
if new_text[-1] != " ":
146+
new_text = "{} ".format(new_text)
147+
self._full_text = new_text
148+
self.current_index = 0
149+
self.update()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPDX-FileCopyrightText: 2022 Tim Cocks for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
import board
5+
import terminalio
6+
from adafruit_display_text.scrolling_label import ScrollingLabel
7+
8+
9+
text = "Hello world CircuitPython scrolling label"
10+
my_scrolling_label = ScrollingLabel(
11+
terminalio.FONT, full_text=text, max_characters=20, animate_time=0.3
12+
)
13+
my_scrolling_label.x = 10
14+
my_scrolling_label.y = 10
15+
board.DISPLAY.show(my_scrolling_label)
16+
while True:
17+
my_scrolling_label.update()

0 commit comments

Comments
 (0)