Skip to content

Commit a895b3a

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

File tree

4 files changed

+88
-69
lines changed

4 files changed

+88
-69
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: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
"""
2-
A helper module that initializes the display and buttons for the uGame
3-
game console. See https://hackaday.io/project/27629-game
4-
"""
5-
61
import board
7-
import digitalio
8-
import gamepadshift
92
import stage
103
import displayio
114
import busio
125
import time
6+
import keypad
137

148

159
K_X = 0x01
@@ -46,27 +40,47 @@
4640
b"\x13\x80\x0a" # _NORON
4741
b"\x29\x80\x64" # _DISPON
4842
)
43+
44+
45+
class _Buttons:
46+
def __init__(self):
47+
self.keys = keypad.ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
48+
data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=8,
49+
interval=0.05)
50+
self.last_state = 0
51+
self.event = keypad.Event(0, False)
52+
self.last_z_press = None
53+
54+
def get_pressed(self):
55+
buttons = self.last_state
56+
events = self.keys.events
57+
while events:
58+
if events.get_into(self.event):
59+
bit = 1 << self.event.key_number
60+
if self.event.pressed:
61+
buttons |= bit
62+
self.last_state |= bit
63+
else:
64+
self.last_state &= ~bit
65+
if buttons & K_START:
66+
now = time.monotonic()
67+
if self.last_z_press:
68+
if now - self.last_z_press > 2:
69+
supervisor.reload()
70+
else:
71+
self.last_z_press = now
72+
else:
73+
self.last_z_press = None
74+
return buttons
75+
76+
4977
displayio.release_displays()
5078
_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()
5479
_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)
80+
chip_select=board.TFT_CS, reset=board.TFT_RST)
6181
display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128,
62-
rotation=0, backlight_pin=board.TFT_LITE)
82+
rotation=0, backlight_pin=board.TFT_LITE,
83+
auto_refresh=False, auto_brightness=True)
6384
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-
85+
buttons = _Buttons()
7286
audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE)

pygamer/ugame.py

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
"""
2-
A helper module that initializes the display and buttons for the uGame
3-
game console. See https://hackaday.io/project/27629-game
4-
"""
5-
61
import board
7-
import digitalio
82
import analogio
9-
import gamepadshift
103
import stage
114
import displayio
125
import busio
136
import time
7+
import keypad
148

159

1610
K_X = 0x01
@@ -47,49 +41,60 @@
4741
b"\x13\x80\x0a" # _NORON
4842
b"\x29\x80\x64" # _DISPON
4943
)
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
6644

6745

68-
class Buttons:
46+
class _Buttons:
6947
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-
)
48+
self.keys = keypad.ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
49+
data=board.BUTTON_OUT, latch=board.BUTTON_LATCH, key_count=4,
50+
interval=0.05)
51+
self.last_state = 0
52+
self.event = keypad.Event(0, False)
53+
self.last_z_press = None
7554
self.joy_x = analogio.AnalogIn(board.JOYSTICK_X)
7655
self.joy_y = analogio.AnalogIn(board.JOYSTICK_Y)
7756

7857
def get_pressed(self):
79-
pressed = self.buttons.get_pressed()
58+
buttons = self.last_state
59+
events = self.keys.events
60+
while events:
61+
if events.get_into(self.event):
62+
bit = 1 << self.event.key_number
63+
if self.event.pressed:
64+
buttons |= bit
65+
self.last_state |= bit
66+
else:
67+
self.last_state &= ~bit
68+
if buttons & K_START:
69+
now = time.monotonic()
70+
if self.last_z_press:
71+
if now - self.last_z_press > 2:
72+
supervisor.reload()
73+
else:
74+
self.last_z_press = now
75+
else:
76+
self.last_z_press = None
8077
dead = 15000
8178
x = self.joy_x.value - 32767
8279
if x < -dead:
83-
pressed |= K_LEFT
80+
buttons |= K_LEFT
8481
elif x > dead:
85-
pressed |= K_RIGHT
82+
buttons |= K_RIGHT
8683
y = self.joy_y.value - 32767
8784
if y < -dead:
88-
pressed |= K_UP
85+
buttons |= K_UP
8986
elif y > dead:
90-
pressed |= K_DOWN
91-
return pressed
87+
buttons |= K_DOWN
88+
return buttons
9289

9390

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

0 commit comments

Comments
 (0)