Skip to content

Commit baed2ee

Browse files
committed
Add support for PyGamer
1 parent 397bada commit baed2ee

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

pygamer/stage.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../stage.py

pygamer/ugame.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
import board
7+
import digitalio
8+
import gamepadshift
9+
import stage
10+
import displayio
11+
import busio
12+
import time
13+
14+
15+
K_X = 0x01
16+
K_O = 0x02
17+
K_START = 0x04
18+
K_SELECT = 0x08
19+
K_DOWN = 0x10
20+
K_LEFT = 0x20
21+
K_RIGHT = 0x40
22+
K_UP = 0x80
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
29+
b"\xb1\x03\x01\x2C\x2D" # _FRMCTR1
30+
b"\xb2\x03\x01\x2C\x2D" # _FRMCTR2
31+
b"\xb3\x06\x01\x2C\x2D\x01\x2C\x2D" # _FRMCTR3
32+
b"\xb4\x01\x07" # _INVCTR line inversion
33+
b"\xc0\x03\xa2\x02\x84" # _PWCTR1 GVDD = 4.7V, 1.0uA
34+
b"\xc1\x01\xc5" # _PWCTR2 VGH=14.7V, VGL=-7.35V
35+
b"\xc2\x02\x0a\x00" # _PWCTR3 Opamp current small, Boost frequency
36+
b"\xc3\x02\x8a\x2a"
37+
b"\xc4\x02\x8a\xee"
38+
b"\xc5\x01\x0e" # _VMCTR1 VCOMH = 4V, VOML = -1.1V
39+
b"\x20\x00" # _INVOFF
40+
b"\x36\x01\xa8" # _MADCTL
41+
# 1 clk cycle nonoverlap, 2 cycle gate rise, 3 sycle osc equalie,
42+
# fix on VTL
43+
b"\x3a\x01\x05" # COLMOD - 16bit color
44+
b"\xe0\x10\x02\x1c\x07\x12\x37\x32\x29\x2d\x29\x25\x2B\x39\x00\x01\x03\x10" # _GMCTRP1 Gamma
45+
b"\xe1\x10\x03\x1d\x07\x06\x2E\x2C\x29\x2D\x2E\x2E\x37\x3F\x00\x00\x02\x10" # _GMCTRN1
46+
b"\x13\x80\x0a" # _NORON
47+
b"\x29\x80\x64" # _DISPON
48+
)
49+
displayio.release_displays()
50+
_tft_spi = busio.SPI(clock=board.TFT_SCK, MOSI=board.TFT_MOSI)
51+
_tft_spi.try_lock()
52+
_tft_spi.configure(baudrate=24000000)
53+
_tft_spi.unlock()
54+
_fourwire = displayio.FourWire(_tft_spi, command=board.TFT_DC,
55+
chip_select=board.TFT_CS)
56+
_reset = digitalio.DigitalInOut(board.TFT_RST)
57+
_reset.switch_to_output(value=0)
58+
time.sleep(0.05)
59+
_reset.value = 1
60+
time.sleep(0.05)
61+
display = displayio.Display(_fourwire, _TFT_INIT, width=160, height=128,
62+
rotation=0, backlight_pin=board.TFT_LITE)
63+
del _TFT_INIT
64+
display.auto_brightness = True
65+
66+
67+
class Buttons:
68+
def __init__(self):
69+
self.buttons = gamepadshift.GamePadShift(
70+
digitalio.DigitalInOut(board.BUTTON_CLOCK),
71+
digitalio.DigitalInOut(board.BUTTON_OUT),
72+
digitalio.DigitalInOut(board.BUTTON_LATCH),
73+
)
74+
self.joy_x = analogio.AnalogIn(board.JOYSTICK_X)
75+
self.joy_y = analogio.AnalogIn(board.JOYSTICK_Y)
76+
77+
def get_pressed(self):
78+
pressed = self.buttons.get_pressed()
79+
dead = 500
80+
center = 32767
81+
x = self.joy_x.value
82+
if x < center - dead:
83+
pressed |= K_RIGHT
84+
elif x > center + dead:
85+
pressed |= K_LEFT
86+
y = self.joy_y.value
87+
if y < center - dead:
88+
pressed |= K_UP
89+
elif y > center + dead:
90+
pressed |= K_DOWN
91+
return pressed
92+
93+
94+
buttons = Buttons()
95+
audio = stage.Audio(board.SPEAKER, board.SPEAKER_ENABLE)

0 commit comments

Comments
 (0)