Skip to content

Commit 79fe541

Browse files
committed
Change PyBadge_Conference_Badge example from gampadshift to keypad
1 parent 99a7e2d commit 79fe541

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

PyBadge_Conference_Badge/code.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
import displayio
1111
import digitalio
1212
import neopixel
13-
from gamepadshift import GamePadShift
13+
from keypad import ShiftRegisterKeys, Event
1414
from adafruit_display_shapes.rect import Rect
1515
from adafruit_display_text.label import Label
1616
from adafruit_bitmap_font import bitmap_font
1717

1818
# 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)
19+
BUTTON_LEFT = const(7)
20+
BUTTON_UP = const(6)
21+
BUTTON_DOWN = const(5)
22+
BUTTON_RIGHT = const(4)
23+
BUTTON_SEL = const(3)
24+
BUTTON_START = const(2)
25+
BUTTON_A = const(1)
26+
BUTTON_B = const(0)
2727

2828
# Customizations
2929
HELLO_STRING = "HELLO"
@@ -45,9 +45,15 @@
4545
neopixels = neopixel.NeoPixel(board.NEOPIXEL, NEOPIXEL_COUNT, brightness=brightness,
4646
auto_write=False, pixel_order=neopixel.GRB)
4747

48-
pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
49-
digitalio.DigitalInOut(board.BUTTON_OUT),
50-
digitalio.DigitalInOut(board.BUTTON_LATCH))
48+
latest_event = Event()
49+
last_press = None
50+
51+
pad = ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
52+
data=board.BUTTON_OUT,
53+
latch=board.BUTTON_LATCH,
54+
key_count=8,
55+
value_when_pressed=True,
56+
max_events=1)
5157

5258
# Make the Display Background
5359
splash = displayio.Group()
@@ -131,7 +137,6 @@ def rotate(degrees):
131137
pixels.append(x * 360 // NEOPIXEL_COUNT)
132138

133139
# Main Loop
134-
current_buttons = pad.get_pressed()
135140
last_read = 0
136141
while True:
137142
for color in range(0, 360, speed):
@@ -146,20 +151,24 @@ def rotate(degrees):
146151
neopixels.brightness = brightness
147152
# Reading buttons too fast returns 0
148153
if (last_read + 0.1) < time.monotonic():
149-
buttons = pad.get_pressed()
154+
pad.events.get_into(latest_event)
150155
last_read = time.monotonic()
151-
if current_buttons != buttons:
156+
#print()
157+
#print("latest keynumber:", latest_event.key_number)
158+
if latest_event is not None and latest_event.key_number != last_press:
152159
# Respond to the buttons
153-
if (buttons & BUTTON_RIGHT) > 0:
160+
if (latest_event.key_number == BUTTON_RIGHT):
154161
direction = -1
155-
elif (buttons & BUTTON_LEFT) > 0:
162+
elif (latest_event.key_number == BUTTON_LEFT):
156163
direction = 1
157-
elif (buttons & BUTTON_UP) > 0 and speed < 10:
164+
elif (latest_event.key_number == BUTTON_UP) and speed < 10:
158165
speed += 1
159-
elif (buttons & BUTTON_DOWN) > 0 and speed > 1:
166+
elif (latest_event.key_number == BUTTON_DOWN) and speed > 1:
160167
speed -= 1
161-
elif (buttons & BUTTON_A) > 0 and brightness < 0.5:
168+
elif (latest_event.key_number == BUTTON_A) and brightness < 0.5:
162169
brightness += 0.025
163-
elif (buttons & BUTTON_B) > 0 and brightness > 0.025:
170+
elif (latest_event.key_number == BUTTON_B) and brightness > 0.025:
164171
brightness -= 0.025
165-
current_buttons = buttons
172+
last_press = latest_event.key_number
173+
else:
174+
last_press = None

0 commit comments

Comments
 (0)