@@ -76,63 +76,6 @@ def load_font(fontname, text):
76
76
font .load_glyphs (text .encode ('utf-8' ))
77
77
return font
78
78
79
- class _PyBadgerCustomBadge :
80
- """Easily display lines of text on the display."""
81
- def __init__ (self , background_group = None , background_filename = None ):
82
- self ._label = label
83
- self .display = board .DISPLAY
84
- self ._background_filename = background_filename
85
- self ._background_group = background_group
86
-
87
- self .custom_badge_group = displayio .Group (max_size = 20 )
88
-
89
- self ._lines = []
90
- for _ in range (1 ):
91
- self ._lines .append (self ._add_text_line ())
92
-
93
- def __getitem__ (self , item ):
94
- """Fetch the Nth text line Group"""
95
- if len (self ._lines ) - 1 < item :
96
- for _ in range (item - (len (self ._lines ) - 1 )):
97
- self ._lines .append (self ._add_text_line ())
98
- return self ._lines [item ]
99
-
100
- @staticmethod
101
- def _add_image_background (file_handle ):
102
- on_disk_bitmap = displayio .OnDiskBitmap (file_handle )
103
- background_image = displayio .TileGrid (on_disk_bitmap ,
104
- pixel_shader = displayio .ColorConverter ())
105
- return background_image
106
-
107
- def _add_text_line (self , color = 0xFFFFFF ):
108
- """Adds a line on the display of the specified color and returns the label object."""
109
- text_label = self ._label .Label (font = terminalio .FONT , text = "" , max_glyphs = 45 , color = color )
110
- return text_label
111
-
112
- def show (self ):
113
- """Call show() to display the badge lines."""
114
-
115
- self .display .show (self .custom_badge_group )
116
- if self ._background_filename :
117
- with open (self ._background_filename , "rb" ) as file_handle :
118
- background_image = self ._add_image_background (file_handle )
119
- self .custom_badge_group .append (background_image )
120
- for image_label in self ._lines :
121
- self .custom_badge_group .append (image_label )
122
-
123
- try :
124
- # Refresh display in CircuitPython 5
125
- self .display .refresh ()
126
- except AttributeError :
127
- # Refresh display in CircuitPython 4
128
- self .display .wait_for_frame ()
129
- else :
130
- self .custom_badge_group .append (self ._background_group )
131
- for background_label in self ._lines :
132
- self .custom_badge_group .append (background_label )
133
-
134
- self .display .show (self .custom_badge_group )
135
-
136
79
# pylint: disable=too-many-instance-attributes
137
80
class PyBadgerBase :
138
81
"""PyBadger base class."""
@@ -182,11 +125,11 @@ def __init__(self, *, pixels_brightness=1.0):
182
125
self ._light_sensor = None
183
126
self ._accelerometer = None
184
127
self ._label = label
185
- self ._custom_badge_object = None
186
128
self ._y_position = 1
187
- self ._line_number = 0
188
129
self ._background_group = None
189
- self ._image_filename = None
130
+ self ._background_image_filename = None
131
+ self ._lines = []
132
+ self ._created_background = False
190
133
191
134
# Display
192
135
self .display = board .DISPLAY
@@ -208,13 +151,32 @@ def __init__(self, *, pixels_brightness=1.0):
208
151
self ._sine_wave = None
209
152
self ._sine_wave_sample = None
210
153
211
- def _custom_badge (self ):
212
- if not self ._background_group and not self ._image_filename :
213
- raise ValueError ("You must provide a bitmap image filename." )
214
- if not self ._background_group :
215
- self ._background_group = self ._badge_background ()
216
- return _PyBadgerCustomBadge (background_group = self ._background_group ,
217
- background_filename = self ._image_filename )
154
+ def _create_badge_background (self ):
155
+ self ._created_background = True
156
+
157
+ if self ._background_group is None :
158
+ self ._background_group = displayio .Group (max_size = 30 )
159
+
160
+ self .display .show (self ._background_group )
161
+
162
+ if self ._background_image_filename :
163
+ with open (self ._background_image_filename , "rb" ) as file_handle :
164
+ on_disk_bitmap = displayio .OnDiskBitmap (file_handle )
165
+ background_image = displayio .TileGrid (on_disk_bitmap ,
166
+ pixel_shader = displayio .ColorConverter ())
167
+ self ._background_group .append (background_image )
168
+ for image_label in self ._lines :
169
+ self ._background_group .append (image_label )
170
+
171
+ try :
172
+ # Refresh display in CircuitPython 5
173
+ self .display .refresh ()
174
+ except AttributeError :
175
+ # Refresh display in CircuitPython 4
176
+ self .display .wait_for_frame ()
177
+ else :
178
+ for background_label in self ._lines :
179
+ self ._background_group .append (background_label )
218
180
219
181
def badge_background (self , background_color = (255 , 0 , 0 ), rectangle_color = (255 , 255 , 255 ),
220
182
rectangle_drop = 0.4 , rectangle_height = 0.5 ):
@@ -247,7 +209,7 @@ def _badge_background(cls, background_color=(255, 0, 0), rectangle_color=(255, 2
247
209
rectangle_drop = 0.4 , rectangle_height = 0.5 ):
248
210
"""Populate the background color with a rectangle color block over it as the background for
249
211
a name badge."""
250
- background_group = displayio .Group (max_size = 2 )
212
+ background_group = displayio .Group (max_size = 30 )
251
213
color_bitmap = displayio .Bitmap (board .DISPLAY .width , board .DISPLAY .height , 1 )
252
214
color_palette = displayio .Palette (1 )
253
215
color_palette [0 ] = background_color
@@ -272,7 +234,7 @@ def image_background(self, image_name=None):
272
234
273
235
pybadger.image_background("Blinka.bmp")
274
236
"""
275
- self ._image_filename = image_name
237
+ self ._background_image_filename = image_name
276
238
277
239
# pylint: disable=too-many-arguments
278
240
def badge_line (self , text = " " , color = (0 , 0 , 0 ), scale = 1 , font = terminalio .FONT ,
@@ -317,20 +279,15 @@ def badge_line(self, text=" ", color=(0, 0, 0), scale=1, font=terminalio.FONT,
317
279
if isinstance (font , str ):
318
280
font = load_font (font , text )
319
281
320
- if self ._custom_badge_object is None :
321
- self ._custom_badge_object = self ._custom_badge ()
322
-
323
- self ._custom_badge_object [self ._line_number ].font = font
324
- self ._custom_badge_object [self ._line_number ].scale = scale
325
- self ._custom_badge_object [self ._line_number ].text = text
326
- self ._custom_badge_object [self ._line_number ].color = color
282
+ text_label = self ._label .Label (font = font , text = text , max_glyphs = 45 , color = color ,
283
+ scale = scale )
284
+ self ._lines .append (text_label )
327
285
328
- _ , _ , width , height = self . _custom_badge_object [ self . _line_number ] .bounding_box
286
+ _ , _ , width , height = text_label .bounding_box
329
287
if not left_justify :
330
- self ._custom_badge_object [self ._line_number ].x = (self .display .width // 2 ) - \
331
- ((width * scale ) // 2 )
288
+ text_label .x = (self .display .width // 2 ) - ((width * scale ) // 2 )
332
289
else :
333
- self . _custom_badge_object [ self . _line_number ] .x = 0
290
+ text_label .x = 0
334
291
335
292
trim_y = 0
336
293
trim_padding = 0
@@ -339,31 +296,30 @@ def badge_line(self, text=" ", color=(0, 0, 0), scale=1, font=terminalio.FONT,
339
296
trim_padding = 4 * padding_above
340
297
341
298
if not padding_above :
342
- self ._custom_badge_object [self ._line_number ].y = self ._y_position + ((height // 2 ) *
343
- scale ) - trim_y
299
+ text_label .y = self ._y_position + ((height // 2 ) * scale ) - trim_y
344
300
345
301
if font is terminalio .FONT :
346
302
self ._y_position += height * scale - trim_y
347
303
else :
348
304
self ._y_position += height * scale + 4
349
305
350
306
else :
351
- self ._custom_badge_object [self ._line_number ].y = self ._y_position + \
352
- (((height // 2 ) * scale ) - trim_y ) + \
353
- ((height * padding_above ) -
354
- trim_padding )
307
+ text_label .y = self ._y_position + (((height // 2 ) * scale ) - trim_y ) + \
308
+ ((height * padding_above ) - trim_padding )
355
309
356
310
if font is terminalio .FONT :
357
311
self ._y_position += ((height * scale - trim_y ) + ((height * padding_above ) -
358
312
trim_padding ))
359
313
else :
360
314
self ._y_position += height * scale + 4
361
315
362
- self ._line_number += 1
363
-
364
316
def show (self ):
365
317
"""Call ``show()`` to display the badge lines of text."""
366
- return self ._custom_badge_object .show ()
318
+
319
+ if not self ._created_background :
320
+ self ._create_badge_background ()
321
+
322
+ self .display .show (self ._background_group )
367
323
368
324
# pylint: disable=too-many-arguments
369
325
def _create_label_group (self , text , font ,
0 commit comments