Skip to content

Commit 18374b0

Browse files
authored
Merge pull request #705 from makermelissa/master
Add PyBadge Conference Badge Code
2 parents 41cf9c4 + bdbd778 commit 18374b0

File tree

5 files changed

+104371
-0
lines changed

5 files changed

+104371
-0
lines changed

PyBadge_Conference_Badge/code.py

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
"""
2+
This is a Conference Badge type Name Tag that is intended to be displayed on
3+
the PyBadge. Feel free to customize it to your heart's content.
4+
"""
5+
6+
import time
7+
from math import sqrt, cos, sin, radians
8+
import board
9+
from micropython import const
10+
import displayio
11+
import digitalio
12+
import neopixel
13+
from gamepadshift import GamePadShift
14+
from adafruit_display_shapes.rect import Rect
15+
from adafruit_display_text.label import Label
16+
from adafruit_bitmap_font import bitmap_font
17+
18+
# Button Constants
19+
BUTTON_LEFT = const(128)
20+
BUTTON_UP = const(64)
21+
BUTTON_DOWN = const(32)
22+
BUTTON_RIGHT = const(16)
23+
BUTTON_SEL = const(8)
24+
BUTTON_START = const(4)
25+
BUTTON_A = const(2)
26+
BUTTON_B = const(1)
27+
28+
# Customizations
29+
HELLO_STRING = "HELLO"
30+
MY_NAME_STRING = "MY NAME IS"
31+
NAME_STRING = "Blinka"
32+
NAME_FONTNAME = "/fonts/Noto-18.bdf"
33+
NEOPIXEL_COUNT = 5
34+
BACKGROUND_COLOR = 0xFF0000
35+
FOREGROUND_COLOR = 0xFFFFFF
36+
BACKGROUND_TEXT_COLOR = 0xFFFFFF
37+
FOREGROUND_TEXT_COLOR = 0x000000
38+
39+
# Default Values
40+
brightness = 0.2
41+
direction = 1
42+
speed = 1
43+
44+
# Define the NeoPixel and Game Pad Objects
45+
neopixels = neopixel.NeoPixel(board.NEOPIXEL, NEOPIXEL_COUNT, brightness=brightness,
46+
auto_write=False, pixel_order=neopixel.GRB)
47+
48+
pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
49+
digitalio.DigitalInOut(board.BUTTON_OUT),
50+
digitalio.DigitalInOut(board.BUTTON_LATCH))
51+
52+
# Make the Display Background
53+
splash = displayio.Group(max_size=20)
54+
board.DISPLAY.show(splash)
55+
56+
color_bitmap = displayio.Bitmap(160, 128, 1)
57+
color_palette = displayio.Palette(1)
58+
color_palette[0] = BACKGROUND_COLOR
59+
60+
bg_sprite = displayio.TileGrid(color_bitmap,
61+
pixel_shader=color_palette,
62+
x=0, y=0)
63+
splash.append(bg_sprite)
64+
65+
# Draw a Foreground Rectangle where the name goes
66+
rect = Rect(0, 50, 160, 70, fill=FOREGROUND_COLOR)
67+
splash.append(rect)
68+
69+
# Load the Hello font
70+
large_font_name = "/fonts/Verdana-Bold-18.bdf"
71+
large_font = bitmap_font.load_font(large_font_name)
72+
large_font.load_glyphs(HELLO_STRING.encode('utf-8'))
73+
74+
# Load the "My Name Is" font
75+
small_font_name = "/fonts/Arial-12.bdf"
76+
small_font = bitmap_font.load_font(small_font_name)
77+
small_font.load_glyphs(MY_NAME_STRING.encode('utf-8'))
78+
79+
# Load the Name font
80+
name_font_name = NAME_FONTNAME
81+
name_font = bitmap_font.load_font(name_font_name)
82+
name_font.load_glyphs(NAME_STRING.encode('utf-8'))
83+
84+
# Setup and Center the Hello Label
85+
hello_label = Label(large_font, text=HELLO_STRING)
86+
(x, y, w, h) = hello_label.bounding_box
87+
hello_label.x = (80 - w // 2)
88+
hello_label.y = 15
89+
hello_label.color = BACKGROUND_TEXT_COLOR
90+
splash.append(hello_label)
91+
92+
# Setup and Center the "My Name Is" Label
93+
mni_label = Label(small_font, text=MY_NAME_STRING)
94+
(x, y, w, h) = mni_label.bounding_box
95+
mni_label.x = (80 - w // 2)
96+
mni_label.y = 35
97+
mni_label.color = BACKGROUND_TEXT_COLOR
98+
splash.append(mni_label)
99+
100+
# Setup and Center the Name Label
101+
name_label = Label(name_font, text=NAME_STRING, line_spacing=0.75)
102+
(x, y, w, h) = name_label.bounding_box
103+
name_label.x = (80 - w // 2)
104+
name_label.y = 85
105+
name_label.color = FOREGROUND_TEXT_COLOR
106+
splash.append(name_label)
107+
108+
# Remap the calculated rotation to 0 - 255
109+
def remap(vector):
110+
return int(((255 * vector + 85) * 0.75) + 0.5)
111+
112+
# Calculate the Hue rotation starting with Red as 0 degrees
113+
def rotate(degrees):
114+
cosA = cos(radians(degrees))
115+
sinA = sin(radians(degrees))
116+
red = cosA + (1.0 - cosA) / 3.0
117+
green = 1./3. * (1.0 - cosA) + sqrt(1./3.) * sinA
118+
blue = 1./3. * (1.0 - cosA) - sqrt(1./3.) * sinA
119+
return (remap(red), remap(green), remap(blue))
120+
121+
palette = []
122+
pixels = []
123+
124+
# Generate a rainbow palette
125+
for degree in range(0, 360):
126+
color = rotate(degree)
127+
palette.append(color[0] << 16 | color[1] << 8 | color[2])
128+
129+
# Create the Pattern
130+
for x in range(0, NEOPIXEL_COUNT):
131+
pixels.append(x * 360 // NEOPIXEL_COUNT)
132+
133+
# Main Loop
134+
current_buttons = pad.get_pressed()
135+
last_read = 0
136+
while True:
137+
for color in range(0, 360, speed):
138+
for index in range(0, NEOPIXEL_COUNT):
139+
palette_index = pixels[index] + color * direction
140+
if palette_index >= 360:
141+
palette_index -= 360
142+
elif palette_index < 0:
143+
palette_index += 360
144+
neopixels[index] = palette[palette_index]
145+
neopixels.show()
146+
neopixels.brightness = brightness
147+
# Reading buttons too fast returns 0
148+
if (last_read + 0.1) < time.monotonic():
149+
buttons = pad.get_pressed()
150+
last_read = time.monotonic()
151+
if current_buttons != buttons:
152+
# Respond to the buttons
153+
if (buttons & BUTTON_RIGHT) > 0:
154+
direction = -1
155+
elif (buttons & BUTTON_LEFT) > 0:
156+
direction = 1
157+
elif (buttons & BUTTON_UP) > 0 and speed < 10:
158+
speed += 1
159+
elif (buttons & BUTTON_DOWN) > 0 and speed > 1:
160+
speed -= 1
161+
elif (buttons & BUTTON_A) > 0 and brightness < 0.5:
162+
brightness += 0.025
163+
elif (buttons & BUTTON_B) > 0 and brightness > 0.025:
164+
brightness -= 0.025
165+
current_buttons = buttons

0 commit comments

Comments
 (0)