Skip to content

Commit 0fa39f1

Browse files
authored
Merge pull request #164 from FoamyGuy/scrolling_label
Adding ScrollingLabel
2 parents 5283f39 + 5437378 commit 0fa39f1

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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 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+
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+
66+
if text[-1] != " ":
67+
text = "{} ".format(text)
68+
self._full_text = text
69+
70+
self.update()
71+
72+
def update(self, force=False):
73+
"""
74+
Attempt to update the display. If `animate_time` has elapsed since
75+
previews animation frame then move the characters over by 1 index.
76+
Must be called in the main loop of user code.
77+
78+
:param force: whether to ignore `animation_time` and force the update. Default is False.
79+
:return: None
80+
"""
81+
_now = time.monotonic()
82+
if force or self._last_animate_time + self.animate_time <= _now:
83+
84+
if len(self.full_text) <= self.max_characters:
85+
self.text = self.full_text
86+
self._last_animate_time = _now
87+
return
88+
89+
if self.current_index + self.max_characters <= len(self.full_text):
90+
_showing_string = self.full_text[
91+
self.current_index : self.current_index + self.max_characters
92+
]
93+
else:
94+
_showing_string_start = self.full_text[self.current_index :]
95+
_showing_string_end = "{}".format(
96+
self.full_text[
97+
: (self.current_index + self.max_characters)
98+
% len(self.full_text)
99+
]
100+
)
101+
102+
_showing_string = "{}{}".format(
103+
_showing_string_start, _showing_string_end
104+
)
105+
self.text = _showing_string
106+
107+
self.current_index += 1
108+
self._last_animate_time = _now
109+
110+
return
111+
112+
@property
113+
def current_index(self):
114+
"""
115+
Index of the first visible character.
116+
117+
:return int: the current index
118+
"""
119+
return self._current_index
120+
121+
@current_index.setter
122+
def current_index(self, new_index):
123+
if new_index < len(self.full_text):
124+
self._current_index = new_index
125+
else:
126+
self._current_index = new_index % len(self.full_text)
127+
128+
@property
129+
def full_text(self):
130+
"""
131+
The full text to be shown. If it's longer than `max_characters` then
132+
scrolling will occur as needed.
133+
134+
:return string: The full text of this label.
135+
"""
136+
return self._full_text
137+
138+
@full_text.setter
139+
def full_text(self, new_text):
140+
if new_text[-1] != " ":
141+
new_text = "{} ".format(new_text)
142+
self._full_text = new_text
143+
self.current_index = 0
144+
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, 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)