Skip to content

Commit 9789a71

Browse files
authored
Merge pull request #19 from nnja/refactor_to_reduce_code_dupe
Refactor to improve readability and reduce code duplication
2 parents 346c848 + de254d0 commit 9789a71

File tree

1 file changed

+50
-60
lines changed

1 file changed

+50
-60
lines changed

adafruit_pybadger.py

+50-60
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ def __init__(self, i2c=None, *, pixels_brightness=1.0):
141141
self._sine_wave = None
142142
self._sine_wave_sample = None
143143

144+
# pylint: disable=too-many-arguments
145+
def _create_label_group(self, text, font,
146+
scale, height_adjustment,
147+
color=0xFFFFFF, width_adjustment=2, line_spacing=0.75):
148+
"""Create a label group with the given text, font, and spacing"""
149+
# If the given font is a string, treat it as a file path and try to load it
150+
if isinstance(font, str):
151+
font = load_font(font, text)
152+
153+
group = displayio.Group(scale=scale)
154+
label = Label(font, text=text, line_spacing=line_spacing)
155+
_, _, width, _ = label.bounding_box
156+
label.x = ((self.display.width // (width_adjustment * scale)) - width // 2)
157+
label.y = int(self.display.height * (height_adjustment / scale))
158+
label.color = color
159+
group.append(label)
160+
return group
161+
144162
def _check_for_movement(self, movement_threshold=10):
145163
"""Checks to see if board is moving. Used to auto-dim display when not moving."""
146164
current_accelerometer = self.acceleration
@@ -281,41 +299,25 @@ def show_business_card(self, *, image_name=None, name_string=None, name_scale=1,
281299
self.display.refresh(target_frames_per_second=60)
282300
except AttributeError:
283301
self.display.wait_for_frame()
302+
284303
if name_string:
285-
if isinstance(name_font, str):
286-
name_font = load_font(name_font, name_string)
287-
name_group = displayio.Group(scale=name_scale)
288-
name_label = Label(name_font, text=name_string, line_spacing=0.75)
289-
(_, _, width, _) = name_label.bounding_box
290-
name_label.x = ((self.display.width // (2 * name_scale)) - width // 2)
291-
name_label.y = int(self.display.height * (0.73 / name_scale))
292-
name_label.color = 0xFFFFFF
293-
name_group.append(name_label)
304+
name_group = self._create_label_group(text=name_string,
305+
font=name_font,
306+
scale=name_scale,
307+
height_adjustment=0.73)
294308
business_card_splash.append(name_group)
295309
if email_string_one:
296-
if isinstance(email_font_one, str):
297-
email_font_one = load_font(email_font_one, email_string_one)
298-
email_group_one = displayio.Group(scale=email_scale_one)
299-
email_label_one = Label(email_font_one, text=email_string_one, line_spacing=0.75)
300-
(_, _, width, _) = email_label_one.bounding_box
301-
email_label_one.width = self.display.width
302-
email_label_one.x = ((self.display.width // (2 * email_scale_one)) - width // 2)
303-
email_label_one.y = int(self.display.height * (0.84 / email_scale_one))
304-
email_label_one.color = 0xFFFFFF
305-
email_group_one.append(email_label_one)
306-
business_card_splash.append(email_group_one)
310+
email_one_group = self._create_label_group(text=email_string_one,
311+
font=email_font_one,
312+
scale=email_scale_one,
313+
height_adjustment=0.84)
314+
business_card_splash.append(email_one_group)
307315
if email_string_two:
308-
if isinstance(email_font_two, str):
309-
email_font_two = load_font(email_font_two, email_string_two)
310-
email_group_two = displayio.Group(scale=email_scale_two)
311-
email_label_two = Label(email_font_two, text=email_string_two, line_spacing=0.75)
312-
(_, _, width, _) = email_label_two.bounding_box
313-
email_label_two.width = self.display.width
314-
email_label_two.x = ((self.display.width // (2 * email_scale_two)) - width // 2)
315-
email_label_two.y = int(self.display.height * (0.91 / email_scale_two))
316-
email_label_two.color = 0xFFFFFF
317-
email_group_two.append(email_label_two)
318-
business_card_splash.append(email_group_two)
316+
email_two_group = self._create_label_group(text=email_string_two,
317+
font=email_font_two,
318+
scale=email_scale_two,
319+
height_adjustment=0.91)
320+
business_card_splash.append(email_two_group)
319321

320322
# pylint: disable=too-many-locals
321323
def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,
@@ -359,35 +361,23 @@ def show_badge(self, *, background_color=0xFF0000, foreground_color=0xFFFFFF,
359361
(int(self.display.height * 0.5)), fill=foreground_color)
360362
splash.append(rect)
361363

362-
if isinstance(hello_font, str):
363-
hello_font = load_font(hello_font, hello_string)
364-
hello_group = displayio.Group(scale=hello_scale)
365-
hello_label = Label(font=hello_font, text=hello_string, line_spacing=0.75)
366-
(_, _, width, _) = hello_label.bounding_box
367-
hello_label.x = ((self.display.width // (2 * hello_scale)) - width // 2)
368-
hello_label.y = int(self.display.height * (0.117 / hello_scale))
369-
hello_label.color = background_text_color
370-
hello_group.append(hello_label)
371-
372-
if isinstance(my_name_is_font, str):
373-
my_name_is_font = load_font(my_name_is_font, my_name_is_string)
374-
my_name_is_group = displayio.Group(scale=my_name_is_scale)
375-
my_name_is_label = Label(font=my_name_is_font, text=my_name_is_string, line_spacing=0.75)
376-
(_, _, width, _) = my_name_is_label.bounding_box
377-
my_name_is_label.x = ((self.display.width // (2 * my_name_is_scale)) - width // 2)
378-
my_name_is_label.y = int(self.display.height * (0.28 / my_name_is_scale))
379-
my_name_is_label.color = background_text_color
380-
my_name_is_group.append(my_name_is_label)
381-
382-
if isinstance(name_font, str):
383-
name_font = load_font(name_font, name_string)
384-
name_group = displayio.Group(scale=name_scale)
385-
name_label = Label(font=name_font, text=name_string, line_spacing=0.75)
386-
(_, _, width, _) = name_label.bounding_box
387-
name_label.x = ((self.display.width // (2 * name_scale)) - width // 2)
388-
name_label.y = int(self.display.height * (0.65 / name_scale))
389-
name_label.color = foreground_text_color
390-
name_group.append(name_label)
364+
hello_group = self._create_label_group(text=hello_string,
365+
font=hello_font,
366+
scale=hello_scale,
367+
height_adjustment=0.117,
368+
color=background_text_color)
369+
370+
my_name_is_group = self._create_label_group(text=my_name_is_string,
371+
font=my_name_is_font,
372+
scale=my_name_is_scale,
373+
height_adjustment=0.28,
374+
color=background_text_color)
375+
376+
name_group = self._create_label_group(text=name_string,
377+
font=name_font,
378+
scale=name_scale,
379+
height_adjustment=0.65,
380+
color=foreground_text_color)
391381

392382
group = displayio.Group()
393383
group.append(splash)

0 commit comments

Comments
 (0)