8000 Fix ugame.py for the ugame10 console · python-ugame/circuitpython-stage@4124dfb · GitHub
[go: up one dir, main page]

Skip to content

Commit 4124dfb

Browse files
committed
Fix ugame.py for the ugame10 console
1 parent 3020d92 commit 4124dfb

File tree

1 file changed

+54
-15
lines changed

1 file changed

+54
-15
lines changed

ugame10/ugame.py

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +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
72
import digitalio
83
import analogio
9-
import gamepad
4+
import keypad
105
import stage
6+
import audioio
7+
import audiocore
118

129

1310
K_X = 0x01
@@ -20,14 +17,56 @@
2017
K_SELECT = 0x00
2118

2219

20+
class _Buttons:
21+
def __init__(self):
22+
self.keys = keypad.Keys((board.X, board.DOWN,
23+
board.LEFT, board.RIGHT, board.UP,
24+
board.O), value_when_pressed=False,
25+
interval=0.05)
26+
self.last_state = 0
27+
self.event = keypad.Event(0, False)
28+
self.last_z_press = None
29+
30+
def get_pressed(self):
31+
buttons = self.last_state
32+
events = self.keys.events
33+
while events:
34+
if events.get_into(self.event):
35+
bit = 1 << self.event.key_number
36+
if self.event.pressed:
37+
buttons |= bit
38+
self.last_state |= bit
39+
else:
40+
self.last_state &= ~bit
41+
return buttons
42+
43+
44+
class _Audio:
45+
last_audio = None
46+
47+
def __init__(self, speaker_pin, mute_pin):
48+
self.muted = True
49+
self.buffer = bytearray(256)
50+
self.audio = audioio.AudioOut(speaker_pin)
51+
self.mute_pin = digitalio.DigitalInOut(mute_pin)
52+
self.mute_pin.switch_to_output(value=False)
53+
54+
def play(self, audio_file, loop=False):
55+
if self.muted:
56+
return
57+
self.stop()
58+
wave = audiocore.WaveFile(audio_file, self.buffer)
59+
self.audio.play(wave, loop=loop)
60+
61+
def stop(self):
62+
self.audio.stop()
63+
64+
def mute(self, value=True):
65+
self.muted = value
66+
self.mute_pin.value = not value
67+
68+
2369
display = board.DISPLAY
24-
buttons = gamepad.GamePad(
25-
digitalio.DigitalInOut(board.X),
26-
digitalio.DigitalInOut(board.DOWN),
27-
digitalio.DigitalInOut(board.LEFT),
28-
digitalio.DigitalInOut(board.RIGHT),
29-
digitalio.DigitalInOut(board.UP),
30-
digitalio.DigitalInOut(board.O),
31-
)
32-
audio = stage.Audio(board.SPEAKER, board.MUTE)
70+
audio = _Audio(board.SPEAKER, board.MUTE)
71+
buttons = _Buttons()
3372
battery = analogio.AnalogIn(board.BATTERY)

0 commit comments

Comments
 (0)
0