|
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 |
| - |
6 | 1 | import board
|
7 | 2 | import digitalio
|
8 | 3 | import analogio
|
9 |
| -import gamepad |
| 4 | +import keypad |
10 | 5 | import stage
|
| 6 | +import audioio |
| 7 | +import audiocore |
11 | 8 |
|
12 | 9 |
|
13 | 10 | K_X = 0x01
|
|
20 | 17 | K_SELECT = 0x00
|
21 | 18 |
|
22 | 19 |
|
| 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 | + |
23 | 69 | 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() |
33 | 72 | battery = analogio.AnalogIn(board.BATTERY)
|
0 commit comments