Skip to content

Commit a2e540e

Browse files
committed
Update pybadge and pygamer to use keypad module instead of gamepadshift
1 parent b014aa0 commit a2e540e

File tree

4 files changed

+92
-63
lines changed

4 files changed

+92
-63
lines changed

pewpew_m4/pew.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
0xf8, 0xfe, 0x125b, 0xcffb, 0xe0cf, 0xffff,
2525
0x1ff8, 0xdbff, 0xffff))
2626

27-
K_X = 0x01
28-
K_DOWN = 0x02
29-
K_LEFT = 0x04
30-
K_RIGHT = 0x08
31-
K_UP = 0x10
32-
K_O = 0x20
27+
K_X = ugame.K_X
28+
K_DOWN = ugame.K_DOWN
29+
K_LEFT = ugame.K_LEFT
30+
K_RIGHT = ugame.K_RIGHT
31+
K_UP = ugame.K_UP
32+
K_O = ugame.K_O
3333

3434
_tick = None
3535
_display = None
@@ -178,7 +178,7 @@ def __str__(self):
178178

179179

180180
def init():
181-
global _tick, _display, _gamepad, _bitmap, _grid, _game
181+
global _tick, _display, _bitmap, _grid, _game
182182

183183
if _tick is not None:
184184
return

pewpew_m4/ugame.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ def get_pressed(self):
5151

5252
display = board.DISPLAY
5353
buttons = _Buttons()
54-
audio = stage.Audio(board.P5)
54+
audio = stage.Audio(board.SPEAKER)

pybadge/ugame.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"""
2-
A helper module that initializes the display and buttons for the uGame
3-
game console. See https://hackaday.io/project/27629-game
2+
A helper module that initializes the display and buttons for the
3+
Adafruit PyBadge game console.
44
"""
55

66
import board
7-
import digitalio
8-
import gamepadshift
97
import stage
108
import displayio
119
import busio
1210
import time
11+
import keypad
1312

1413

1514
K_X = 0x01
@@ -46,27 +45,47 @@
4645
b"\x13\x80\x0a" # _NORON
4746
b"\x29\x80\x64" # _DISPON
4847
)
48+
49+
50+
class _Buttons:
51+
def __init__(self):
52+
self.keys = keypad.ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
53+
data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=8,
54+
interval=0.05)
55+
self.last_state = 0
56+
self.event = keypad.Event(0, False)
57+
self.last_z_press = None
58+
59+
def get_pressed(self):
60+
buttons = self.last_state
61+
events = self.keys.events
62+
while events:
63+
if events.get_into(self.event):
64+
bit = 1 << self.event.key_number
65+
if self.event.pressed:
66+
buttons |= bit
67+
self.last_state |= bit
68+
else:
69+
self.last_state &= ~bit
70+
if buttons & K_START:
71+
now = time.monotonic()
72+
if self.last_z_press:
73+
if now - self.last_z_press > 2:
74+
supervisor.reload()
75+
else:
76+
self.last_z_press = now
77+
else:
78+
self.last_z_press = None
79+
return buttons
80+
81+
4982
displayio.release_displays()
5083
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
51-
_tft_spi.try_lock()
52-
_tft_spi.configure(baudrate=24000000)
53-
_tft_spi.unlock()
5484
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
55-
chip_select=board.TFT_CS)
56-
_reset = digitalio.DigitalInOut(board.TFT_RST)
57-
_reset.switch_to_output(value=0)
58-
time.sleep(0.05)
59-
_reset.value = 1
60-
time.sleep(0.05)
85+
chip_select=board.TFT_CS, reset=board.TFT_RST)
6186
display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128,
62-
rotation=0, backlight_pin=board.TFT_LITE)
87+
rotation=0, backlight_pin=board.TFT_LITE,
88+
auto_refresh=False, auto_brightness=True)
6389
del _TFT_INIT
64-
display.auto_brightness = True
65-
66-
buttons = gamepadshift.GamePadShift(
67-
digitalio.DigitalInOut(board.BUTTON_CLOCK),
68-
digitalio.DigitalInOut(board.BUTTON_OUT),
69-
digitalio.DigitalInOut(board.BUTTON_LATCH),
70-
)
71-
90+
buttons = _Buttons()
7291
audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE)

pygamer/ugame.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
"""
2-
A helper module that initializes the display and buttons for the uGame
3-
game console. See https://hackaday.io/project/27629-game
2+
A helper module that initializes the display and buttons for the
3+
Adafruit PyGamer game console.
44
"""
55

66
import board
7-
import digitalio
87
import analogio
9-
import gamepadshift
108
import stage
119
import displayio
1210
import busio
1311
import time
12+
import keypad
1413

1514

1615
K_X = 0x01
@@ -47,49 +46,60 @@
4746
b"\x13\x80\x0a" # _NORON
4847
b"\x29\x80\x64" # _DISPON
4948
)
50-
displayio.release_displays()
51-
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
52-
_tft_spi.try_lock()
53-
_tft_spi.configure(baudrate=24000000)
54-
_tft_spi.unlock()
55-
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
56-
chip_select=board.TFT_CS)
57-
_reset = digitalio.DigitalInOut(board.TFT_RST)
58-
_reset.switch_to_output(value=0)
59-
time.sleep(0.05)
60-
_reset.value = 1
61-
time.sleep(0.05)
62-
display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128,
63-
rotation=0, backlight_pin=board.TFT_LITE)
64-
del _TFT_INIT
65-
display.auto_brightness = True
6649

6750

68-
class Buttons:
51+
class _Buttons:
6952
def __init__(self):
70-
self.buttons = gamepadshift.GamePadShift(
71-
digitalio.DigitalInOut(board.BUTTON_CLOCK),
72-
digitalio.DigitalInOut(board.BUTTON_OUT),
73-
digitalio.DigitalInOut(board.BUTTON_LATCH),
74-
)
53+
self.keys = keypad.ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
54+
data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=4,
55+
interval=0.05)
56+
self.last_state = 0
57+
self.event = keypad.Event(0, False)
58+
self.last_z_press = None
7559
self.joy_x = analogio.AnalogIn(board.JOYSTICK_X)
7660
self.joy_y = analogio.AnalogIn(board.JOYSTICK_Y)
7761

7862
def get_pressed(self):
79-
pressed = self.buttons.get_pressed()
63+
buttons = self.last_state
64+
events = self.keys.events
65+
while events:
66+
if events.get_into(self.event):
67+
bit = 1 << self.event.key_number
68+
if self.event.pressed:
69+
buttons |= bit
70+
self.last_state |= bit
71+
else:
72+
self.last_state &= ~bit
73+
if buttons & K_START:
74+
now = time.monotonic()
75+
if self.last_z_press:
76+
if now - self.last_z_press > 2:
77+
supervisor.reload()
78+
else:
79+
self.last_z_press = now
80+
else:
81+
self.last_z_press = None
8082
dead = 15000
8183
x = self.joy_x.value - 32767
8284
if x < -dead:
83-
pressed |= K_LEFT
85+
buttons |= K_LEFT
8486
elif x > dead:
85-
pressed |= K_RIGHT
87+
buttons |= K_RIGHT
8688
y = self.joy_y.value - 32767
8789
if y < -dead:
88-
pressed |= K_UP
90+
buttons |= K_UP
8991
elif y > dead:
90-
pressed |= K_DOWN
91-
return pressed
92+
buttons |= K_DOWN
93+
return buttons
9294

9395

94-
buttons = Buttons()
96+
displayio.release_displays()
97+
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
98+
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
99+
chip_select=board.TFT_CS, reset=board.TFT_RST)
100+
display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128,
101+
rotation=0, backlight_pin=board.TFT_LITE,
102+
auto_refresh=False, auto_brightness=True)
103+
del _TFT_INIT
104+
buttons = _Buttons()
95105
audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE)

0 commit comments

Comments
 (0)