1+ import board
2+ import analogio
3+ import stage
4+ import displayio
5+ import busio
6+ import time
7+ import keypad
8+ import audiocore
9+ import audiopwmio
10+ import supervisor
11+ import digitalio
12+
13+
14+ K_X = 0x01
15+ K_O = 0x02
16+ K_START = 0x04 # Y
17+ K_SELECT = 0x08 # X
18+ K_DOWN = 0x10
19+ K_LEFT = 0x20
20+ K_RIGHT = 0x40
21+ K_UP = 0x80
22+
23+
24+ # re-initialize the display for correct rotation and RGB mode
25+
26+ _TFT_INIT = (
27+ b"\x01 \x80 \x96 " # _SWRESET and Delay 150ms
28+ b"\x11 \x80 \xFF " # _SLPOUT and Delay 500ms
29+ b"\x3A \x81 \x55 \x0A " # _COLMOD and Delay 10ms
30+ b"\x36 \x01 \x08 " # _MADCTL
31+ b"\x21 \x80 \x0A " # _INVON Hack and Delay 10ms
32+ b"\x13 \x80 \x0A " # _NORON and Delay 10ms
33+ b"\x36 \x01 \xC0 " # _MADCTL
34+ b"\x29 \x80 \xFF " # _DISPON and Delay 500ms
35+ b"\x36 \xA0 " # 反转屏幕Y轴
36+ )
37+
38+
39+ class _Buttons :
40+ def __init__ (self ):
41+ self .keys = keypad .Keys ((
42+ board .GP6 ,
43+ board .GP5 ,
44+ board .GP8 ,
45+ board .GP7
46+ ), value_when_pressed = False , pull = True , interval = 0.05 )
47+ self .last_state = 0
48+ self .event = keypad .Event (0 , False )
49+ self .last_z_press = None
50+ self .joy_x = analogio .AnalogIn (board .A3 )
51+ self .joy_y = analogio .AnalogIn (board .A2 )
52+
53+ def get_pressed (self ):
54+ buttons = self .last_state
55+ events = self .keys .events
56+ while events :
57+ if events .get_into (self .event ):
58+ bit = 1 << self .event .key_number
59+ if self .event .pressed :
60+ buttons |= bit
61+ self .last_state |= bit
62+ else :
63+ self .last_state &= ~ bit
64+ if buttons & K_START :
65+ now = time .monotonic ()
66+ if self .last_z_press :
67+ if now - self .last_z_press > 2 :
68+ supervisor .set_next_code_file (None )
69+ supervisor .reload ()
70+ else :
71+ self .last_z_press = now
72+ else :
73+ self .last_z_press = None
74+ dead = 15000
75+ x = self .joy_x .value - 32767
76+ if x < - dead :
77+ buttons |= K_LEFT
78+ elif x > dead :
79+ buttons |= K_RIGHT
80+ y = self .joy_y .value - 32767
81+ if y < - dead :
82+ buttons |= K_UP
83+ elif y > dead :
84+ buttons |= K_DOWN
85+ return buttons
86+
87+
88+ class _Audio :
89+ last_audio = None
90+
91+ def __init__ (self ):
92+ self .muted = True
93+ self .buffer = bytearray (128 )
94+ self .audio = audiopwmio .PWMAudioOut (board .GP23 )
95+
96+ def play (self , audio_file , loop = False ):
97+ if self .muted :
98+ return
99+ self .stop ()
100+ wave = audiocore .WaveFile (audio_file , self .buffer )
101+ self .audio .play (wave , loop = loop )
102+
103+ def stop (self ):
104+ self .audio .stop ()
105+
106+ def mute (self , value = True ):
107+ self .muted = value
108+
109+
110+ displayio .release_displays ()
111+ spi = busio .SPI (clock = board .GP2 , MOSI = board .GP3 )
112+ tft_cs = board .GP18
113+ tft_dc = board .GP1
114+ display_bus = displayio .FourWire (spi , command = tft_dc , chip_select = tft_cs , reset = board .GP0 )
115+ display = displayio .Display (display_bus , _TFT_INIT , width = 240 , height = 240 ,
116+ rotation = 180 , auto_refresh = False , rowstart = 0 )
117+ audio = _Audio ()
118+ buttons = _Buttons ()
0 commit comments