Skip to content

Commit a5061b4

Browse files
authored
Merge pull request #58 from adafruit/updates
Updates
2 parents b5c03fa + d15c90b commit a5061b4

File tree

4 files changed

+154
-56
lines changed

4 files changed

+154
-56
lines changed

adafruit_pybadger/pybadger_base.py

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
2828
"""
2929

30+
from __future__ import annotations
31+
32+
3033
import time
3134
import array
3235
import math
@@ -36,7 +39,7 @@
3639
from adafruit_bitmap_font import bitmap_font
3740
import displayio
3841
from adafruit_display_shapes.rect import Rect
39-
from adafruit_display_text import label
42+
from adafruit_display_text import bitmap_label as label
4043
import terminalio
4144
import adafruit_miniqr
4245

@@ -55,16 +58,17 @@
5558
pass
5659

5760
try:
61+
from typing import TYPE_CHECKING
62+
except ImportError:
63+
TYPE_CHECKING = const(0)
64+
65+
if TYPE_CHECKING:
5866
from typing import Union, Tuple, Optional, Generator
5967
from adafruit_bitmap_font.bdf import BDF # pylint: disable=ungrouped-imports
6068
from adafruit_bitmap_font.pcf import PCF # pylint: disable=ungrouped-imports
6169
from fontio import BuiltinFont
6270
from keypad import Keys, ShiftRegisterKeys
6371
from neopixel import NeoPixel
64-
from adafruit_lsm6ds.lsm6ds33 import LSM6DS33
65-
from adafruit_lis3dh import LIS3DH_I2C
66-
except ImportError:
67-
pass
6872

6973

7074
__version__ = "0.0.0-auto.0"
@@ -161,27 +165,20 @@ def _create_badge_background(self) -> None:
161165
if self._background_group is None:
162166
self._background_group = displayio.Group()
163167

164-
self.display.show(self._background_group)
168+
self.show(self._background_group)
165169

166170
if self._background_image_filename:
167-
with open(self._background_image_filename, "rb") as file_handle:
168-
on_disk_bitmap = displayio.OnDiskBitmap(file_handle)
169-
background_image = displayio.TileGrid(
170-
on_disk_bitmap,
171-
pixel_shader=getattr(
172-
on_disk_bitmap, "pixel_shader", displayio.ColorConverter()
173-
),
174-
# TODO: Once CP6 is no longer supported, replace the above line with below
175-
# pixel_shader=on_disk_background.pixel_shader,
176-
)
177-
self._background_group.append(background_image)
178-
for image_label in self._lines:
179-
self._background_group.append(image_label)
180-
181-
self.display.refresh()
182-
else:
183-
for background_label in self._lines:
184-
self._background_group.append(background_label)
171+
file_handle = open( # pylint: disable=consider-using-with
172+
self._background_image_filename, "rb"
173+
)
174+
on_disk_bitmap = displayio.OnDiskBitmap(file_handle)
175+
background_image = displayio.TileGrid(
176+
on_disk_bitmap,
177+
pixel_shader=on_disk_bitmap.pixel_shader,
178+
)
179+
self._background_group.append(background_image)
180+
for image_label in self._lines:
181+
self._background_group.append(image_label)
185182

186183
def badge_background(
187184
self,
@@ -274,7 +271,7 @@ def badge_line(
274271
scale: int = 1,
275272
font: Union[BuiltinFont, BDF, PCF] = terminalio.FONT,
276273
left_justify: bool = False,
277-
padding_above: int = 0,
274+
padding_above: float = 0,
278275
) -> None:
279276
"""Add a line of text to the display. Designed to work with ``badge_background`` for a
280277
color-block style badge, or with ``image_background`` for a badge with a background image.
@@ -331,7 +328,7 @@ def badge_line(
331328
trim_padding = 0
332329
if font is terminalio.FONT:
333330
trim_y = 4 * scale
334-
trim_padding = 4 * padding_above
331+
trim_padding = round(4 * padding_above)
335332

336333
if not padding_above:
337334
text_label.y = self._y_position + ((height // 2) * scale) - trim_y
@@ -342,14 +339,14 @@ def badge_line(
342339
self._y_position += height * scale + 4
343340

344341
else:
345-
text_label.y = (
342+
text_label.y = round(
346343
self._y_position
347344
+ (((height // 2) * scale) - trim_y)
348345
+ ((height * padding_above) - trim_padding)
349346
)
350347

351348
if font is terminalio.FONT:
352-
self._y_position += (height * scale - trim_y) + (
349+
self._y_position += (height * scale - trim_y) + round(
353350
(height * padding_above) - trim_padding
354351
)
355352
else:
@@ -362,7 +359,7 @@ def show_custom_badge(self) -> None:
362359
if not self._created_background:
363360
self._create_badge_background()
364361

365-
self.display.show(self._background_group)
362+
self.show(self._background_group)
366363

367364
# pylint: disable=too-many-arguments
368365
def _create_label_group(
@@ -424,14 +421,23 @@ def auto_dim_display(self, delay: float = 5.0, movement_threshold: int = 10):
424421
while True:
425422
pybadger.auto_dim_display(delay=10)
426423
"""
427-
if not self._check_for_movement(movement_threshold=movement_threshold):
428-
current_time = time.monotonic()
429-
if current_time - self._start_time > delay:
430-
self.display.brightness = 0.1
431-
self._start_time = current_time
424+
if not hasattr(self.display, "brightness"):
425+
return
426+
current_time = time.monotonic()
427+
if self._check_for_movement(movement_threshold=movement_threshold):
428+
self.activity(current_time)
429+
if current_time - self._start_time > delay:
430+
self.display.brightness = 0.1
432431
else:
433432
self.display.brightness = self._display_brightness
434433

434+
def activity(self, current_time=None):
435+
"""Turn postpone dimming of the screen"""
436+
if not hasattr(self.display, "brightness"):
437+
return
438+
self.display.brightness = self._display_brightness
439+
self._start_time = current_time or time.monotonic()
440+
435441
@property
436442
def pixels(self) -> NeoPixel:
437443
"""Sequence like object representing the NeoPixels on the board."""
@@ -443,12 +449,12 @@ def light(self) -> bool:
443449
return self._light_sensor.value
444450

445451
@property
446-
def acceleration(self) -> Union[LSM6DS33, LIS3DH_I2C]:
452+
def acceleration(self) -> Tuple[int, int, int]:
447453
"""Accelerometer data, +/- 2G sensitivity."""
448454
return (
449455
self._accelerometer.acceleration
450456
if self._accelerometer is not None
451-
else None
457+
else (0, 0, 0)
452458
)
453459

454460
@property
@@ -476,7 +482,7 @@ def show_business_card(
476482
email_font_one: Union[BuiltinFont, BDF, PCF] = terminalio.FONT,
477483
email_string_two: Optional[str] = None,
478484
email_scale_two: int = 1,
479-
email_font_two: Union[BuiltinFont, BDF, PCF] = terminalio.FONT
485+
email_font_two: Union[BuiltinFont, BDF, PCF] = terminalio.FONT,
480486
) -> None:
481487
"""Display a bitmap image and a text string, such as a personal image and email address.
482488
@@ -547,22 +553,15 @@ def show_business_card(
547553
business_card_label_groups.append(email_two_group)
548554

549555
business_card_splash = displayio.Group()
550-
self.display.show(business_card_splash)
551-
with open(image_name, "rb") as file_name:
552-
on_disk_bitmap = displayio.OnDiskBitmap(file_name)
553-
face_image = displayio.TileGrid(
554-
on_disk_bitmap,
555-
pixel_shader=getattr(
556-
on_disk_bitmap, "pixel_shader", displayio.ColorConverter()
557-
),
558-
# TODO: Once CP6 is no longer supported, replace the above line with below
559-
# pixel_shader=on_disk_bitmap.pixel_shader,
560-
)
561-
business_card_splash.append(face_image)
562-
for group in business_card_label_groups:
563-
business_card_splash.append(group)
564-
565-
self.display.refresh()
556+
image_file = open(image_name, "rb") # pylint: disable=consider-using-with
557+
on_disk_bitmap = displayio.OnDiskBitmap(image_file)
558+
face_image = displayio.TileGrid(
559+
on_disk_bitmap, pixel_shader=on_disk_bitmap.pixel_shader
560+
)
561+
business_card_splash.append(face_image)
562+
for group in business_card_label_groups:
563+
business_card_splash.append(group)
564+
self.show(business_card_splash)
566565

567566
# pylint: disable=too-many-locals
568567
def show_badge(
@@ -580,7 +579,7 @@ def show_badge(
580579
my_name_is_string: str = "MY NAME IS",
581580
name_font: Union[BuiltinFont, BDF, PCF] = terminalio.FONT,
582581
name_scale: int = 1,
583-
name_string: str = "Blinka"
582+
name_string: str = "Blinka",
584583
) -> None:
585584
"""Create a "Hello My Name is"-style badge.
586585
@@ -647,11 +646,19 @@ def show_badge(
647646
group.append(hello_group)
648647
group.append(my_name_is_group)
649648
group.append(name_group)
649+
self.show(group)
650+
651+
def show(self, group) -> None:
652+
"""Show the given group, refreshing the screen immediately"""
653+
self.activity()
654+
self.display.auto_refresh = False
650655
self.display.show(group)
656+
self.display.refresh()
657+
self.display.auto_refresh = True
651658

652659
def show_terminal(self) -> None:
653660
"""Revert to terminalio screen."""
654-
self.display.show(None)
661+
self.show(None)
655662

656663
@staticmethod
657664
def bitmap_qr(matrix: adafruit_miniqr.QRBitMatrix) -> displayio.Bitmap:
@@ -703,7 +710,7 @@ def show_qr_code(self, data: str = "https://circuitpython.org") -> None:
703710
)
704711
qr_code = displayio.Group(scale=qr_code_scale)
705712
qr_code.append(qr_img)
706-
self.display.show(qr_code)
713+
self.show(qr_code)
707714

708715
@staticmethod
709716
def _sine_sample(length: int) -> Generator[int, None, None]:
37.6 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: CC-BY-SA-4.0

examples/pybadger_pygamer_asyncio.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# SPDX-FileCopyrightText: 2022 Jeff Epler for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
# pylint: disable=consider-using-with
5+
6+
import asyncio
7+
from displayio import TileGrid, OnDiskBitmap, Group
8+
from rainbowio import colorwheel
9+
from adafruit_pybadger import pybadger
10+
11+
# If you choose to enter a pronoun it's shown on the "business card" page
12+
pronoun = ""
13+
custom_line1 = "FIRST"
14+
custom_line2 = "LAST" # also a great place to show a pronoun
15+
16+
# Set up the custom image
17+
qr_image = OnDiskBitmap(open("/QR_Blinka_CircuitPythonOrg.bmp", "rb"))
18+
qr_tg = TileGrid(qr_image, pixel_shader=qr_image.pixel_shader)
19+
qr_gp = Group()
20+
qr_gp.append(qr_tg)
21+
22+
pybadger.badge_background(
23+
background_color=pybadger.WHITE,
24+
rectangle_color=pybadger.PURPLE,
25+
rectangle_drop=0.25,
26+
rectangle_height=0.55,
27+
)
28+
29+
pybadger.badge_line(
30+
text="HELLO I'M", color=pybadger.BLINKA_PURPLE, scale=2, padding_above=1
31+
)
32+
pybadger.badge_line(text=custom_line1, color=pybadger.WHITE, scale=6, padding_above=1)
33+
pybadger.badge_line(
34+
text=custom_line2, color=pybadger.BLINKA_PURPLE, scale=2, padding_above=0.25
35+
)
36+
37+
# Start with the custom badge page
38+
pybadger.show_custom_badge()
39+
40+
# This task responds to buttons and changes the visible page
41+
async def ui_task():
42+
while True:
43+
if pybadger.button.a:
44+
pybadger.show_business_card(
45+
image_name="Blinka.bmp",
46+
name_string="Jeff Epler",
47+
name_scale=2,
48+
email_string_one="[email protected]",
49+
email_string_two=pronoun,
50+
)
51+
elif pybadger.button.b:
52+
pybadger.show(qr_gp)
53+
elif pybadger.button.start:
54+
pybadger.show_custom_badge()
55+
elif pybadger.button.select:
56+
pybadger.activity()
57+
else:
58+
pybadger.auto_dim_display(
59+
delay=0.5
60+
) # Remove or comment out this line if you have the PyBadge LC
61+
await asyncio.sleep(0.02)
62+
63+
64+
# This task animates the LEDs
65+
async def led_task():
66+
pixels = pybadger.pixels
67+
pixels.auto_write = False
68+
num_pixels = len(pixels)
69+
j = 0
70+
while True:
71+
bright = pybadger.display.brightness > 0.5
72+
j = (j + (7 if bright else 3)) & 255
73+
b = 31 / 255.0 if bright else 5 / 255.0
74+
if pixels.brightness != b:
75+
pixels.brightness = b
76+
for i in range(num_pixels):
77+
rc_index = i * 97 + j
78+
pixels[i] = colorwheel(rc_index & 255)
79+
pixels.show()
80+
await asyncio.sleep(0.02)
81+
82+
83+
# Run both tasks via asyncio!
84+
async def main():
85+
await asyncio.gather(ui_task(), led_task())
86+
87+
88+
asyncio.run(main())

0 commit comments

Comments
 (0)