aboutsummaryrefslogtreecommitdiff
path: root/circuitpython/frozen/circuitpython-stage/picosystem/ugame.py
blob: b0b2fd1b9ebcf35eadd90c4f5c560bdc1ef3582f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import board
import analogio
import stage
import keypad
import audiocore
import audiopwmio
import time
import supervisor


K_O = 0x01  # A
K_X = 0x02  # B
K_SELECT = 0x04  # X
K_START = 0x08   # Y
K_Z = 0x08   # Y
K_DOWN = 0x10
K_LEFT = 0x20
K_RIGHT = 0x40
K_UP = 0x80


class _Buttons:
    def __init__(self):
        self.keys = keypad.Keys((
            board.SW_A,
            board.SW_B,
            board.SW_X,
            board.SW_Y,
            board.SW_DOWN,
            board.SW_LEFT,
            board.SW_RIGHT,
            board.SW_UP
        ), value_when_pressed=False, pull=True, 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
        if buttons & K_Z:
            now = time.monotonic()
            if self.last_z_press:
                if now - self.last_z_press > 2:
                    supervisor.set_next_code_file(None)
                    supervisor.reload()
            else:
                self.last_z_press = now
        else:
            self.last_z_press = None
        return buttons

class _Audio:
    last_audio = None

    def __init__(self):
        self.muted = True
        self.buffer = bytearray(128)
        self.audio = audiopwmio.PWMAudioOut(board.AUDIO)

    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


audio = _Audio()
display = board.DISPLAY
buttons = _Buttons()
battery = analogio.AnalogIn(board.BAT_SENSE)