Skip to content

Commit 8a16497

Browse files
author
Margaret Matocha
committed
Add more complex example with alternating display of label and bitmap_label
1 parent accdb87 commit 8a16497

File tree

1 file changed

+345
-0
lines changed

1 file changed

+345
-0
lines changed
Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,345 @@
1+
# Sample code using the textMap library and the "textBox" wrapper class
2+
# Creates four textBox instances
3+
# Inserts each textBox into a tileGrid group
4+
# Writes text into the box one character at a time
5+
# Moves the position of the textBox around the display
6+
# Clears each textBox after the full string is written (even if the text is outside of the box)
7+
8+
#import textmap
9+
#from textmap import textBox
10+
11+
import board
12+
import displayio
13+
import terminalio
14+
import fontio
15+
import sys
16+
import time
17+
import busio
18+
19+
from adafruit_display_text import bitmap_label
20+
#from adafruit_display_text import bitmap_label as Label
21+
22+
from adafruit_display_text import label
23+
24+
# Setup the SPI display
25+
##########
26+
# Use this Boolean variables to select which font style to use
27+
##########
28+
use_builtin_font=True # Set True to use the terminalio.FONT BuiltinFont,
29+
# Set False to use a BDF loaded font, see "fontFiles" below
30+
##########
31+
32+
# Set scaling factor for display text
33+
my_scale=1
34+
35+
# Setup the SPI display
36+
37+
if "DISPLAY" not in dir(board):
38+
# Setup the LCD display with driver
39+
# You may need to change this to match the display driver for the chipset
40+
# used on your display
41+
from adafruit_ili9341 import ILI9341
42+
43+
displayio.release_displays()
44+
45+
# setup the SPI bus
46+
spi = board.SPI()
47+
tft_cs = board.D9 # arbitrary, pin not used
48+
tft_dc = board.D10
49+
tft_backlight = board.D12
50+
tft_reset = board.D11
51+
52+
while not spi.try_lock():
53+
spi.configure(baudrate=32000000)
54+
spi.unlock()
55+
56+
display_bus = displayio.FourWire(
57+
spi,
58+
command=tft_dc,
59+
chip_select=tft_cs,
60+
reset=tft_reset,
61+
baudrate=32000000,
62+
polarity=1,
63+
phase=1,
64+
)
65+
66+
# Number of pixels in the display
67+
DISPLAY_WIDTH = 320
68+
DISPLAY_HEIGHT = 240
69+
70+
# create the display
71+
display = ILI9341(
72+
display_bus,
73+
width=DISPLAY_WIDTH,
74+
height=DISPLAY_HEIGHT,
75+
rotation=180, # The rotation can be adjusted to match your configuration.
76+
auto_refresh=True,
77+
native_frames_per_second=90,
78+
)
79+
80+
# reset the display to show nothing.
81+
display.show(None)
82+
else:
83+
# built-in display
84+
display = board.DISPLAY
85+
86+
print ('Display is started')
87+
88+
89+
# load all the fonts
90+
print('loading fonts...')
91+
92+
fontList = []
93+
94+
# Load some proportional fonts
95+
fontFiles = [
96+
'fonts/Helvetica-Bold-16.bdf',
97+
# 'fonts/BitstreamVeraSans-Roman-24.bdf', # Header2
98+
# 'fonts/BitstreamVeraSans-Roman-16.bdf', # mainText
99+
]
100+
101+
from adafruit_bitmap_font import bitmap_font
102+
103+
for i, fontFile in enumerate(fontFiles):
104+
105+
if use_builtin_font:
106+
thisFont=terminalio.FONT # comment this out to switch back to BDF loaded fonts
107+
else:
108+
thisFont = bitmap_font.load_font(fontFile)
109+
110+
fontList.append(thisFont)
111+
112+
113+
preloadTheGlyphs= True # set this to True if you want to preload the font glyphs into memory
114+
# preloading the glyphs will help speed up the rendering of text but will use more RAM
115+
116+
if preloadTheGlyphs:
117+
118+
# identify the glyphs to load into memory -> increases rendering speed
119+
glyphs = b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-,.:?! '
120+
121+
print('loading glyphs...')
122+
for font in fontList:
123+
if font is not terminalio.FONT:
124+
font.load_glyphs(glyphs)
125+
126+
print('Glyphs are loaded.')
127+
128+
129+
print('Fonts completed loading.')
130+
131+
# create group
132+
import gc
133+
134+
135+
myString1=('This is a label.py and\nbitmap_label.py comparison.')
136+
myString23='none'
137+
myString_bitmap_label='bitmap_label'
138+
myString_label='label bitmap_label'
139+
140+
141+
#####
142+
# Create the "bitmap_label.py" versions of the text labels.
143+
144+
gc.collect()
145+
bitmap_label_start=gc.mem_free()
146+
147+
bmap_label1 = bitmap_label.Label(font=fontList[0], text=myString1, color=0xFFFFFF, max_glyphs=len(myString1),
148+
background_color=0xFF0000,
149+
padding_bottom=0,
150+
padding_left=0,
151+
padding_right=0,
152+
padding_top=0,
153+
background_tight=True,
154+
x=10,
155+
y=60,
156+
line_spacing=1.25,
157+
scale=my_scale,
158+
anchor_point=(0.5,0),
159+
anchored_position=(160, 50),
160+
)
161+
label2_padding=10
162+
bmap_label2 = bitmap_label.Label(font=fontList[0], text=myString23, color=0x000000, max_glyphs=len(myString23),
163+
background_color=0xFFFF00,
164+
padding_bottom=label2_padding,
165+
padding_left=0,
166+
padding_right=0,
167+
padding_top=label2_padding,
168+
background_tight=False,
169+
x=10,
170+
y=100,
171+
line_spacing=1.25,
172+
scale=my_scale,
173+
anchor_point=(0,0),
174+
anchored_position=(200,150),
175+
)
176+
177+
bmap_label3 = bitmap_label.Label(font=fontList[0], text=myString23, color=0x000000, max_glyphs=len(myString23),
178+
background_color=0xFFFF00,
179+
padding_bottom=0,
180+
padding_left=0,
181+
padding_right=0,
182+
padding_top=0,
183+
background_tight=True,
184+
x=10,
185+
y=150,
186+
line_spacing=1.25,
187+
scale=my_scale,
188+
)
189+
190+
bmap_label4 = bitmap_label.Label(font=fontList[0], text=myString_label, color=0x000000, max_glyphs=len(myString_label),
191+
background_color=0xFFFF00,
192+
padding_bottom=0,
193+
padding_left=0,
194+
padding_right=0,
195+
padding_top=0,
196+
background_tight=False,
197+
x=10,
198+
y=200,
199+
line_spacing=1.25,
200+
scale=my_scale,
201+
)
202+
203+
myString5='bitmap_label -->'
204+
bmap_label5 = bitmap_label.Label(font=fontList[0], text=myString5, color=0xFFFFFF, max_glyphs=len(myString5),
205+
background_color=0x000000,
206+
padding_bottom=0,
207+
padding_left=0,
208+
padding_right=0,
209+
padding_top=0,
210+
background_tight=True,
211+
x=10,
212+
y=200,
213+
line_spacing=1.25,
214+
anchor_point=(1,0.5),
215+
anchored_position=(200,200),
216+
scale=my_scale,
217+
)
218+
219+
220+
221+
gc.collect()
222+
bitmap_label_end=gc.mem_free()
223+
224+
bmap_group = displayio.Group( max_size=5 ) # Create a group for displaying
225+
bmap_group.append(bmap_label1)
226+
bmap_group.append(bmap_label2)
227+
bmap_group.append(bmap_label3)
228+
bmap_group.append(bmap_label4)
229+
bmap_group.append(bmap_label5)
230+
231+
232+
233+
#####
234+
# Create the "label.py" versions of the text labels.
235+
236+
gc.collect()
237+
label_start=gc.mem_free()
238+
239+
label1 = label.Label(font=fontList[0], text=myString1, color=0xFFFFFF, max_glyphs=len(myString1),
240+
background_color=0xFF0000,
241+
padding_bottom=0,
242+
padding_left=0,
243+
padding_right=0,
244+
padding_top=0,
245+
background_tight=True,
246+
x=10,
247+
y=60,
248+
line_spacing=1.25,
249+
scale=my_scale,
250+
anchor_point=(0.5,0),
251+
anchored_position=(160, 50),
252+
)
253+
254+
label2 = label.Label(font=fontList[0], text=myString23, color=0x000000, max_glyphs=len(myString23),
255+
background_color=0xFFFF00,
256+
padding_bottom=label2_padding,
257+
padding_left=0,
258+
padding_right=0,
259+
padding_top=label2_padding,
260+
background_tight=False,
261+
x=10,
262+
y=100,
263+
line_spacing=1.25,
264+
scale=my_scale,
265+
anchor_point=(0,0),
266+
anchored_position=(200,150),
267+
)
268+
269+
label3 = label.Label(font=fontList[0], text=myString23, color=0x000000, max_glyphs=len(myString23),
270+
background_color=0xFFFF00,
271+
padding_bottom=0,
272+
padding_left=0,
273+
padding_right=0,
274+
padding_top=0,
275+
background_tight=True,
276+
x=10,
277+
y=150,
278+
line_spacing=1.25,
279+
scale=my_scale,
280+
)
281+
282+
label4 = label.Label(font=fontList[0], text=myString_label, color=0x000000, max_glyphs=len(myString_label),
283+
background_color=0xFFFF00,
284+
padding_bottom=0,
285+
padding_left=0,
286+
padding_right=0,
287+
padding_top=0,
288+
background_tight=True,
289+
x=10,
290+
y=200,
291+
line_spacing=1.25,
292+
scale=my_scale,
293+
)
294+
295+
296+
myString5 = '<-- label'
297+
label5 = label.Label(font=fontList[0], text=myString5, color=0xFFFFFF, max_glyphs=len(myString5),
298+
background_color=0x000000,
299+
padding_bottom=0,
300+
padding_left=0,
301+
padding_right=0,
302+
padding_top=0,
303+
background_tight=False,
304+
x=10,
305+
y=200,
306+
line_spacing=1.25,
307+
anchor_point=(0,0.5),
308+
anchored_position=(50,200),
309+
scale=my_scale,
310+
)
311+
312+
gc.collect()
313+
label_end=gc.mem_free()
314+
315+
label_group = displayio.Group( max_size=5 ) # Create a group for displaying
316+
label_group.append(label1)
317+
label_group.append(label2)
318+
label_group.append(label3)
319+
label_group.append(label4)
320+
label_group.append(label5)
321+
322+
323+
324+
325+
print('***')
326+
327+
display.auto_refresh=True
328+
329+
while True:
330+
print('bitmap_label')
331+
time.sleep(0.1)
332+
display.show(bmap_group)
333+
334+
time.sleep(2)
335+
336+
print('label')
337+
time.sleep(0.1)
338+
display.show(label_group)
339+
time.sleep(2)
340+
341+
342+
343+
344+
345+

0 commit comments

Comments
 (0)