Skip to content

Commit f9d5117

Browse files
committed
Refactor into PyBadgerBase class. Now works.
1 parent 2252e3f commit f9d5117

File tree

1 file changed

+45
-89
lines changed

1 file changed

+45
-89
lines changed

adafruit_pybadger/pybadger_base.py

Lines changed: 45 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -76,63 +76,6 @@ def load_font(fontname, text):
7676
font.load_glyphs(text.encode('utf-8'))
7777
return font
7878

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-
13679
# pylint: disable=too-many-instance-attributes
13780
class PyBadgerBase:
13881
"""PyBadger base class."""
@@ -182,11 +125,11 @@ def __init__(self, *, pixels_brightness=1.0):
182125
self._light_sensor = None
183126
self._accelerometer = None
184127
self._label = label
185-
self._custom_badge_object = None
186128
self._y_position = 1
187-
self._line_number = 0
188129
self._background_group = None
189-
self._image_filename = None
130+
self._background_image_filename = None
131+
self._lines = []
132+
self._created_background = False
190133

191134
# Display
192135
self.display = board.DISPLAY
@@ -208,13 +151,32 @@ def __init__(self, *, pixels_brightness=1.0):
208151
self._sine_wave = None
209152
self._sine_wave_sample = None
210153

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)
218180

219181
def badge_background(self, background_color=(255, 0, 0), rectangle_color=(255, 255, 255),
220182
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
247209
rectangle_drop=0.4, rectangle_height=0.5):
248210
"""Populate the background color with a rectangle color block over it as the background for
249211
a name badge."""
250-
background_group = displayio.Group(max_size=2)
212+
background_group = displayio.Group(max_size=30)
251213
color_bitmap = displayio.Bitmap(board.DISPLAY.width, board.DISPLAY.height, 1)
252214
color_palette = displayio.Palette(1)
253215
color_palette[0] = background_color
@@ -272,7 +234,7 @@ def image_background(self, image_name=None):
272234
273235
pybadger.image_background("Blinka.bmp")
274236
"""
275-
self._image_filename = image_name
237+
self._background_image_filename = image_name
276238

277239
# pylint: disable=too-many-arguments
278240
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,
317279
if isinstance(font, str):
318280
font = load_font(font, text)
319281

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)
327285

328-
_, _, width, height = self._custom_badge_object[self._line_number].bounding_box
286+
_, _, width, height = text_label.bounding_box
329287
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)
332289
else:
333-
self._custom_badge_object[self._line_number].x = 0
290+
text_label.x = 0
334291

335292
trim_y = 0
336293
trim_padding = 0
@@ -339,31 +296,30 @@ def badge_line(self, text=" ", color=(0, 0, 0), scale=1, font=terminalio.FONT,
339296
trim_padding = 4 * padding_above
340297

341298
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
344300

345301
if font is terminalio.FONT:
346302
self._y_position += height * scale - trim_y
347303
else:
348304
self._y_position += height * scale + 4
349305

350306
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)
355309

356310
if font is terminalio.FONT:
357311
self._y_position += ((height * scale - trim_y) + ((height * padding_above) -
358312
trim_padding))
359313
else:
360314
self._y_position += height * scale + 4
361315

362-
self._line_number += 1
363-
364316
def show(self):
365317
"""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)
367323

368324
# pylint: disable=too-many-arguments
369325
def _create_label_group(self, text, font,

0 commit comments

Comments
 (0)