Skip to content

Commit

Permalink
Fix ugame.py for the ugame10 console
Browse files Browse the repository at this point in the history
  • Loading branch information
deshipu committed Aug 26, 2022
1 parent 3020d92 commit 4124dfb
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions ugame10/ugame.py
@@ -1,13 +1,10 @@
"""
A helper module that initializes the display and buttons for the uGame
game console. See https://hackaday.io/project/27629-game
"""

import board
import digitalio
import analogio
import gamepad
import keypad
import stage
import audioio
import audiocore


K_X = 0x01
Expand All @@ -20,14 +17,56 @@
K_SELECT = 0x00


class _Buttons:
def __init__(self):
self.keys = keypad.Keys((board.X, board.DOWN,
board.LEFT, board.RIGHT, board.UP,
board.O), value_when_pressed=False,
interval=0.05)
self.last_state = 0
self.event = keypad.Event(0, False)
self.last_z_press = None

def get_pressed(self):
buttons = self.last_state
events = self.keys.events
while events:
if events.get_into(self.event):
bit = 1 << self.event.key_number
if self.event.pressed:
buttons |= bit
self.last_state |= bit
else:
self.last_state &= ~bit
return buttons


class _Audio:
last_audio = None

def __init__(self, speaker_pin, mute_pin):
self.muted = True
self.buffer = bytearray(256)
self.audio = audioio.AudioOut(speaker_pin)
self.mute_pin = digitalio.DigitalInOut(mute_pin)
self.mute_pin.switch_to_output(value=False)

def play(self, audio_file, loop=False):
if self.muted:
return
self.stop()
wave = audiocore.WaveFile(audio_file, self.buffer)
self.audio.play(wave, loop=loop)

def stop(self):
self.audio.stop()

def mute(self, value=True):
self.muted = value
self.mute_pin.value = not value


display = board.DISPLAY
buttons = gamepad.GamePad(
digitalio.DigitalInOut(board.X),
digitalio.DigitalInOut(board.DOWN),
digitalio.DigitalInOut(board.LEFT),
digitalio.DigitalInOut(board.RIGHT),
digitalio.DigitalInOut(board.UP),
digitalio.DigitalInOut(board.O),
)
audio = stage.Audio(board.SPEAKER, board.MUTE)
audio = _Audio(board.SPEAKER, board.MUTE)
buttons = _Buttons()
battery = analogio.AnalogIn(board.BATTERY)

0 comments on commit 4124dfb

Please sign in to comment.