7
7
the PyBadge. Feel free to customize it to your heart's content.
8
8
"""
9
9
10
- import time
11
10
from math import sqrt , cos , sin , radians
12
11
import board
13
12
from micropython import const
39
38
BACKGROUND_TEXT_COLOR = 0xFFFFFF
40
39
FOREGROUND_TEXT_COLOR = 0x000000
41
40
42
- # Default Values
43
- brightness = 0.2
44
- direction = 1
45
- speed = 1
41
+ settings = {"brightness" : 0.2 , "direction" : 1 , "speed" : 1 }
46
42
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
+ )
50
51
52
+ # Define Events and Shift Register
51
53
latest_event = Event ()
52
54
last_event = Event ()
53
55
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
+ )
61
65
62
66
# Make the Display Background
63
67
splash = displayio .Group ()
67
71
color_palette = displayio .Palette (1 )
68
72
color_palette [0 ] = BACKGROUND_COLOR
69
73
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 )
73
75
splash .append (bg_sprite )
74
76
75
77
# Draw a Foreground Rectangle where the name goes
79
81
# Load the Hello font
80
82
large_font_name = "/fonts/Verdana-Bold-18.bdf"
81
83
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" ))
83
85
84
86
# Load the "My Name Is" font
85
87
small_font_name = "/fonts/Arial-12.bdf"
86
88
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" ))
88
90
89
91
# Load the Name font
90
92
name_font_name = NAME_FONTNAME
91
93
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" ))
93
95
94
96
# 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
+ )
101
106
102
107
# 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
+ )
109
117
110
118
# 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
+ )
117
128
118
129
# Remap the calculated rotation to 0 - 255
119
130
def remap (vector ):
120
131
return int (((255 * vector + 85 ) * 0.75 ) + 0.5 )
121
132
133
+
122
134
# Calculate the Hue rotation starting with Red as 0 degrees
123
135
def rotate (degrees ):
124
136
cosA = cos (radians (degrees ))
125
137
sinA = sin (radians (degrees ))
126
138
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
129
141
return (remap (red ), remap (green ), remap (blue ))
130
142
143
+
131
144
palette = []
132
145
pixels = []
133
146
@@ -140,37 +153,39 @@ def rotate(degrees):
140
153
for x in range (0 , NEOPIXEL_COUNT ):
141
154
pixels .append (x * 360 // NEOPIXEL_COUNT )
142
155
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
+
143
172
# Main Loop
144
173
last_read = 0
145
174
while True :
146
- for color in range (0 , 360 , speed ):
175
+ for color in range (0 , 360 , settings [ " speed" ] ):
147
176
for index in range (0 , NEOPIXEL_COUNT ):
148
- palette_index = pixels [index ] + color * direction
177
+ palette_index = pixels [index ] + color * settings [ " direction" ]
149
178
if palette_index >= 360 :
150
179
palette_index -= 360
151
180
elif palette_index < 0 :
152
181
palette_index += 360
153
182
neopixels [index ] = palette [palette_index ]
154
183
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 )
160
186
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 )
174
188
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