Skip to content

Commit a3ba5af

Browse files
authored
Merge pull request #1977 from tekktrik/feature/remove-gampadshift
Remove gampadshift from PyBadge_Conference_Badge example
2 parents 4c37152 + dbfacc9 commit a3ba5af

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed

PyBadge_Conference_Badge/code.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,21 @@
1212
import board
1313
from micropython import const
1414
import displayio
15-
import digitalio
1615
import neopixel
17-
from gamepadshift import GamePadShift
16+
from keypad import ShiftRegisterKeys, Event
1817
from adafruit_display_shapes.rect import Rect
1918
from adafruit_display_text.label import Label
2019
from adafruit_bitmap_font import bitmap_font
2120

2221
# 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)
3130

3231
# Customizations
3332
HELLO_STRING = "HELLO"
@@ -49,9 +48,16 @@
4948
neopixels = neopixel.NeoPixel(board.NEOPIXEL, NEOPIXEL_COUNT, brightness=brightness,
5049
auto_write=False, pixel_order=neopixel.GRB)
5150

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)
5561

5662
# Make the Display Background
5763
splash = displayio.Group()
@@ -135,7 +141,6 @@ def rotate(degrees):
135141
pixels.append(x * 360 // NEOPIXEL_COUNT)
136142

137143
# Main Loop
138-
current_buttons = pad.get_pressed()
139144
last_read = 0
140145
while True:
141146
for color in range(0, 360, speed):
@@ -150,20 +155,22 @@ def rotate(degrees):
150155
neopixels.brightness = brightness
151156
# Reading buttons too fast returns 0
152157
if (last_read + 0.1) < time.monotonic():
153-
buttons = pad.get_pressed()
158+
pad.events.get_into(latest_event)
154159
last_read = time.monotonic()
155-
if current_buttons != buttons:
160+
if latest_event.pressed and latest_event.key_number != last_event.key_number:
156161
# Respond to the buttons
157-
if (buttons & BUTTON_RIGHT) > 0:
162+
if latest_event.key_number == BUTTON_RIGHT:
158163
direction = -1
159-
elif (buttons & BUTTON_LEFT) > 0:
164+
elif latest_event.key_number == BUTTON_LEFT:
160165
direction = 1
161-
elif (buttons & BUTTON_UP) > 0 and speed < 10:
166+
elif (latest_event.key_number == BUTTON_UP) and speed < 10:
162167
speed += 1
163-
elif (buttons & BUTTON_DOWN) > 0 and speed > 1:
168+
elif (latest_event.key_number == BUTTON_DOWN) and speed > 1:
164169
speed -= 1
165-
elif (buttons & BUTTON_A) > 0 and brightness < 0.5:
170+
elif (latest_event.key_number == BUTTON_A) and brightness < 0.5:
166171
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:
168173
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

Comments
 (0)