Skip to content

Commit 3dab0b2

Browse files
authored
Merge pull request #2123 from makermelissa/main
Update PyBadge Conference Badge to be more readable
2 parents ec03d18 + fe94b1b commit 3dab0b2

File tree

1 file changed

+78
-63
lines changed

1 file changed

+78
-63
lines changed

PyBadge_Conference_Badge/code.py

Lines changed: 78 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
the PyBadge. Feel free to customize it to your heart's content.
88
"""
99

10-
import time
1110
from math import sqrt, cos, sin, radians
1211
import board
1312
from micropython import const
@@ -39,25 +38,30 @@
3938
BACKGROUND_TEXT_COLOR = 0xFFFFFF
4039
FOREGROUND_TEXT_COLOR = 0x000000
4140

42-
# Default Values
43-
brightness = 0.2
44-
direction = 1
45-
speed = 1
41+
settings = {"brightness": 0.2, "direction": 1, "speed": 1}
4642

47-
# Define the NeoPixel and Game Pad Objects
48-
neopixels = neopixel.NeoPixel(board.NEOPIXEL, NEOPIXEL_COUNT, brightness=brightness,
49-
auto_write=False, pixel_order=neopixel.GRB)
43+
# Define the NeoPixel
44+
neopixels = neopixel.NeoPixel(
45+
board.NEOPIXEL,
46+
NEOPIXEL_COUNT,
47+
brightness=settings["brightness"],
48+
auto_write=False,
49+
pixel_order=neopixel.GRB,
50+
)
5051

52+
# Define Events and Shift Register
5153
latest_event = Event()
5254
last_event = Event()
5355

54-
pad = ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
55-
data=board.BUTTON_OUT,
56-
latch=board.BUTTON_LATCH,
57-
key_count=8,
58-
value_when_pressed=True,
59-
interval=0.1,
60-
max_events=1)
56+
pad = ShiftRegisterKeys(
57+
clock=board.BUTTON_CLOCK,
58+
data=board.BUTTON_OUT,
59+
latch=board.BUTTON_LATCH,
60+
key_count=8,
61+
value_when_pressed=True,
62+
interval=0.1,
63+
max_events=1,
64+
)
6165

6266
# Make the Display Background
6367
splash = displayio.Group()
@@ -67,9 +71,7 @@
6771
color_palette = displayio.Palette(1)
6872
color_palette[0] = BACKGROUND_COLOR
6973

70-
bg_sprite = displayio.TileGrid(color_bitmap,
71-
pixel_shader=color_palette,
72-
x=0, y=0)
74+
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
7375
splash.append(bg_sprite)
7476

7577
# Draw a Foreground Rectangle where the name goes
@@ -79,55 +81,66 @@
7981
# Load the Hello font
8082
large_font_name = "/fonts/Verdana-Bold-18.bdf"
8183
large_font = bitmap_font.load_font(large_font_name)
82-
large_font.load_glyphs(HELLO_STRING.encode('utf-8'))
84+
large_font.load_glyphs(HELLO_STRING.encode("utf-8"))
8385

8486
# Load the "My Name Is" font
8587
small_font_name = "/fonts/Arial-12.bdf"
8688
small_font = bitmap_font.load_font(small_font_name)
87-
small_font.load_glyphs(MY_NAME_STRING.encode('utf-8'))
89+
small_font.load_glyphs(MY_NAME_STRING.encode("utf-8"))
8890

8991
# Load the Name font
9092
name_font_name = NAME_FONTNAME
9193
name_font = bitmap_font.load_font(name_font_name)
92-
name_font.load_glyphs(NAME_STRING.encode('utf-8'))
94+
name_font.load_glyphs(NAME_STRING.encode("utf-8"))
9395

9496
# Setup and Center the Hello Label
95-
hello_label = Label(large_font, text=HELLO_STRING)
96-
(x, y, w, h) = hello_label.bounding_box
97-
hello_label.x = (80 - w // 2)
98-
hello_label.y = 15
99-
hello_label.color = BACKGROUND_TEXT_COLOR
100-
splash.append(hello_label)
97+
splash.append(
98+
Label(
99+
large_font,
100+
anchor_point=(0.5, 0.5),
101+
anchored_position=(board.DISPLAY.width // 2, 15),
102+
text=HELLO_STRING,
103+
color=BACKGROUND_TEXT_COLOR,
104+
)
105+
)
101106

102107
# Setup and Center the "My Name Is" Label
103-
mni_label = Label(small_font, text=MY_NAME_STRING)
104-
(x, y, w, h) = mni_label.bounding_box
105-
mni_label.x = (80 - w // 2)
106-
mni_label.y = 35
107-
mni_label.color = BACKGROUND_TEXT_COLOR
108-
splash.append(mni_label)
108+
splash.append(
109+
Label(
110+
small_font,
111+
anchor_point=(0.5, 0.5),
112+
anchored_position=(board.DISPLAY.width // 2, 35),
113+
text=MY_NAME_STRING,
114+
color=BACKGROUND_TEXT_COLOR,
115+
)
116+
)
109117

110118
# Setup and Center the Name Label
111-
name_label = Label(name_font, text=NAME_STRING, line_spacing=0.75)
112-
(x, y, w, h) = name_label.bounding_box
113-
name_label.x = (80 - w // 2)
114-
name_label.y = 85
115-
name_label.color = FOREGROUND_TEXT_COLOR
116-
splash.append(name_label)
119+
splash.append(
120+
Label(
121+
name_font,
122+
anchor_point=(0.5, 0.5),
123+
anchored_position=(board.DISPLAY.width // 2, 85),
124+
text=NAME_STRING,
125+
color=FOREGROUND_TEXT_COLOR,
126+
)
127+
)
117128

118129
# Remap the calculated rotation to 0 - 255
119130
def remap(vector):
120131
return int(((255 * vector + 85) * 0.75) + 0.5)
121132

133+
122134
# Calculate the Hue rotation starting with Red as 0 degrees
123135
def rotate(degrees):
124136
cosA = cos(radians(degrees))
125137
sinA = sin(radians(degrees))
126138
red = cosA + (1.0 - cosA) / 3.0
127-
green = 1./3. * (1.0 - cosA) + sqrt(1./3.) * sinA
128-
blue = 1./3. * (1.0 - cosA) - sqrt(1./3.) * sinA
139+
green = 1.0 / 3.0 * (1.0 - cosA) + sqrt(1.0 / 3.0) * sinA
140+
blue = 1.0 / 3.0 * (1.0 - cosA) - sqrt(1.0 / 3.0) * sinA
129141
return (remap(red), remap(green), remap(blue))
130142

143+
131144
palette = []
132145
pixels = []
133146

@@ -140,37 +153,39 @@ def rotate(degrees):
140153
for x in range(0, NEOPIXEL_COUNT):
141154
pixels.append(x * 360 // NEOPIXEL_COUNT)
142155

156+
157+
def check_buttons(event):
158+
if event.key_number == BUTTON_RIGHT:
159+
settings["direction"] = -1
160+
elif event.key_number == BUTTON_LEFT:
161+
settings["direction"] = 1
162+
elif (event.key_number == BUTTON_UP) and settings["speed"] < 10:
163+
settings["speed"] += 1
164+
elif (event.key_number == BUTTON_DOWN) and settings["speed"] > 1:
165+
settings["speed"] -= 1
166+
elif (event.key_number == BUTTON_A) and settings["brightness"] < 0.5:
167+
settings["brightness"] += 0.025
168+
elif (event.key_number == BUTTON_B) and settings["brightness"] > 0.025:
169+
settings["brightness"] -= 0.025
170+
171+
143172
# Main Loop
144173
last_read = 0
145174
while True:
146-
for color in range(0, 360, speed):
175+
for color in range(0, 360, settings["speed"]):
147176
for index in range(0, NEOPIXEL_COUNT):
148-
palette_index = pixels[index] + color * direction
177+
palette_index = pixels[index] + color * settings["direction"]
149178
if palette_index >= 360:
150179
palette_index -= 360
151180
elif palette_index < 0:
152181
palette_index += 360
153182
neopixels[index] = palette[palette_index]
154183
neopixels.show()
155-
neopixels.brightness = brightness
156-
# Reading buttons too fast returns 0
157-
if (last_read + 0.1) < time.monotonic():
158-
pad.events.get_into(latest_event)
159-
last_read = time.monotonic()
184+
neopixels.brightness = settings["brightness"]
185+
pad.events.get_into(latest_event)
160186
if latest_event.pressed and latest_event.key_number != last_event.key_number:
161-
# Respond to the buttons
162-
if latest_event.key_number == BUTTON_RIGHT:
163-
direction = -1
164-
elif latest_event.key_number == BUTTON_LEFT:
165-
direction = 1
166-
elif (latest_event.key_number == BUTTON_UP) and speed < 10:
167-
speed += 1
168-
elif (latest_event.key_number == BUTTON_DOWN) and speed > 1:
169-
speed -= 1
170-
elif (latest_event.key_number == BUTTON_A) and brightness < 0.5:
171-
brightness += 0.025
172-
elif (latest_event.key_number == BUTTON_B) and brightness > 0.025:
173-
brightness -= 0.025
187+
check_buttons(latest_event)
174188
last_event = latest_event
175-
latest_event = Event(key_number=8) # An imaginary key number that doesn't exist!
176-
189+
latest_event = Event(
190+
key_number=8
191+
) # An imaginary key number that doesn't exist!

0 commit comments

Comments
 (0)