Skip to content

Commit b014aa0

Browse files
committed
Make PewPew M4 use the keypad module instead of gamepad
1 parent 64aaa13 commit b014aa0

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

pewpew_m4/ugame.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import board
2-
import digitalio
3-
import gamepad
42
import stage
53
import supervisor
64
import time
5+
import keypad
76

87

98
K_X = 0x01
@@ -14,24 +13,30 @@
1413
K_O = 0x20
1514
K_START = 0x40
1615
K_Z = 0x40
17-
K_SELECT = 0x00
16+
K_SELECT = 0x80
1817

1918

2019
class _Buttons:
2120
def __init__(self):
22-
self.buttons = gamepad.GamePad(
23-
digitalio.DigitalInOut(board.BUTTON_X),
24-
digitalio.DigitalInOut(board.BUTTON_DOWN),
25-
digitalio.DigitalInOut(board.BUTTON_LEFT),
26-
digitalio.DigitalInOut(board.BUTTON_RIGHT),
27-
digitalio.DigitalInOut(board.BUTTON_UP),
28-
digitalio.DigitalInOut(board.BUTTON_O),
29-
digitalio.DigitalInOut(board.BUTTON_Z),
30-
)
21+
self.keys = keypad.Keys((board.BUTTON_X, board.BUTTON_DOWN,
22+
board.BUTTON_LEFT, board.BUTTON_RIGHT, board.BUTTON_UP,
23+
board.BUTTON_O, board.BUTTON_Z), value_when_pressed=False,
24+
interval=0.05)
25+
self.last_state = 0
26+
self.event = keypad.Event(0, False)
3127
self.last_z_press = None
3228

3329
def get_pressed(self):
34-
buttons = self.buttons.get_pressed()
30+
buttons = self.last_state
31+
events = self.keys.events
32+
while events:
33+
if events.get_into(self.event):
34+
bit = 1 << self.event.key_number
35+
if self.event.pressed:
36+
buttons |= bit
37+
self.last_state |= bit
38+
else:
39+
self.last_state &= ~bit
3540
if buttons & K_Z:
3641
now = time.monotonic()
3742
if self.last_z_press:

0 commit comments

Comments
 (0)