12
12
import board
13
13
from micropython import const
14
14
import displayio
15
- import digitalio
16
15
import neopixel
17
- from gamepadshift import GamePadShift
16
+ from keypad import ShiftRegisterKeys , Event
18
17
from adafruit_display_shapes .rect import Rect
19
18
from adafruit_display_text .label import Label
20
19
from adafruit_bitmap_font import bitmap_font
21
20
22
21
# Button Constants
23
- BUTTON_LEFT = const (128 )
24
- BUTTON_UP = const (64 )
25
- BUTTON_DOWN = const (32 )
26
- BUTTON_RIGHT = const (16 )
27
- BUTTON_SEL = const (8 )
28
- BUTTON_START = const (4 )
29
- BUTTON_A = const (2 )
30
- BUTTON_B = const (1 )
22
+ BUTTON_LEFT = const (7 )
23
+ BUTTON_UP = const (6 )
24
+ BUTTON_DOWN = const (5 )
25
+ BUTTON_RIGHT = const (4 )
26
+ BUTTON_SEL = const (3 )
27
+ BUTTON_START = const (2 )
28
+ BUTTON_A = const (1 )
29
+ BUTTON_B = const (0 )
31
30
32
31
# Customizations
33
32
HELLO_STRING = "HELLO"
49
48
neopixels = neopixel .NeoPixel (board .NEOPIXEL , NEOPIXEL_COUNT , brightness = brightness ,
50
49
auto_write = False , pixel_order = neopixel .GRB )
51
50
52
- pad = GamePadShift (digitalio .DigitalInOut (board .BUTTON_CLOCK ),
53
- digitalio .DigitalInOut (board .BUTTON_OUT ),
54
- digitalio .DigitalInOut (board .BUTTON_LATCH ))
51
+ latest_event = Event ()
52
+ last_event = Event ()
53
+
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 )
55
61
56
62
# Make the Display Background
57
63
splash = displayio .Group ()
@@ -135,7 +141,6 @@ def rotate(degrees):
135
141
pixels .append (x * 360 // NEOPIXEL_COUNT )
136
142
137
143
# Main Loop
138
- current_buttons = pad .get_pressed ()
139
144
last_read = 0
140
145
while True :
141
146
for color in range (0 , 360 , speed ):
@@ -150,20 +155,22 @@ def rotate(degrees):
150
155
neopixels .brightness = brightness
151
156
# Reading buttons too fast returns 0
152
157
if (last_read + 0.1 ) < time .monotonic ():
153
- buttons = pad .get_pressed ( )
158
+ pad .events . get_into ( latest_event )
154
159
last_read = time .monotonic ()
155
- if current_buttons != buttons :
160
+ if latest_event . pressed and latest_event . key_number != last_event . key_number :
156
161
# Respond to the buttons
157
- if ( buttons & BUTTON_RIGHT ) > 0 :
162
+ if latest_event . key_number == BUTTON_RIGHT :
158
163
direction = - 1
159
- elif ( buttons & BUTTON_LEFT ) > 0 :
164
+ elif latest_event . key_number == BUTTON_LEFT :
160
165
direction = 1
161
- elif (buttons & BUTTON_UP ) > 0 and speed < 10 :
166
+ elif (latest_event . key_number == BUTTON_UP ) and speed < 10 :
162
167
speed += 1
163
- elif (buttons & BUTTON_DOWN ) > 0 and speed > 1 :
168
+ elif (latest_event . key_number == BUTTON_DOWN ) and speed > 1 :
164
169
speed -= 1
165
- elif (buttons & BUTTON_A ) > 0 and brightness < 0.5 :
170
+ elif (latest_event . key_number == BUTTON_A ) and brightness < 0.5 :
166
171
brightness += 0.025
167
- elif (buttons & BUTTON_B ) > 0 and brightness > 0.025 :
172
+ elif (latest_event . key_number == BUTTON_B ) and brightness > 0.025 :
168
173
brightness -= 0.025
169
- current_buttons = buttons
174
+ last_event = latest_event
175
+ latest_event = Event (key_number = 8 ) # An imaginary key number that doesn't exist!
176
+
0 commit comments