Skip to content

Commit ed65a07

Browse files
committed
Fix building espboy
1 parent c1cee39 commit ed65a07

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed

espboy/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
make USER_C_MODULES=../../../micropython-stage/modules CFLAGS_EXTRA=-DMODULE_STAGE_ENABLED=1 FROZEN_MANIFEST=../../../micropython-stage/espboy/manifest.py PORT=/dev/ttyUSB0 FLASH_MODE=dio FLASH_SIZE=4m erase clean deploy

espboy/freeze/st7735r.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import ustruct
2+
import utime
3+
4+
5+
class ST7735R:
6+
width = 128
7+
height = 128
8+
9+
def __init__(self, spi, dc, cs, rotation=0x06):
10+
self.spi = spi
11+
self.dc = dc
12+
self.dc(1)
13+
self.cs = cs
14+
self.cs(0)
15+
self.rotation = rotation
16+
utime.sleep_ms(100)
17+
for command, data, delay in (
18+
(b'\x01', b'', 120),
19+
(b'\x11', b'', 120),
20+
(b'\x36', bytes(((rotation & 0x07) << 5,)), 0),
21+
(b'\x3a', b'\x05', 0),
22+
(b'\xb4', b'\x07', 0),
23+
(b'\xb1', b'\x01\x2c\x2d', 0),
24+
(b'\xb2', b'\x01\x2c\x2d', 0),
25+
(b'\xb3', b'\x01\x2c\x2d\x01\x2c\x2d', 0),
26+
(b'\xc0', b'\x02\x02\x84', 0),
27+
(b'\xc1', b'\xc5', 0),
28+
(b'\xc2', b'\x0a\x00', 0),
29+
(b'\xc3', b'\x8a\x2a', 0),
30+
(b'\xc4', b'\x8a\xee', 0),
31+
(b'\xc5', b'\x0e', 0),
32+
(b'\x20', b'', 0),
33+
(b'\xe0', b'\x02\x1c\x07\x12\x37\x32\x29\x2d'
34+
b'\x29\x25\x2B\x39\x00\x01\x03\x10', 0),
35+
(b'\xe1', b'\x03\x1d\x07\x06\x2E\x2C\x29\x2D'
36+
b'\x2E\x2E\x37\x3F\x00\x00\x02\x10', 0),
37+
(b'\x13', b'', 10),
38+
(b'\x29', b'', 120),
39+
):
40+
self.write(command, data)
41+
utime.sleep_ms(delay)
42+
self.dc(0)
43+
self.cs(1)
44+
45+
def block(self, x0, y0, x1, y1):
46+
if self.rotation & 0x01:
47+
x0 += 3 # 32 # alternate st7735 display
48+
x1 += 3 # 32
49+
y0 += 2 # 0
50+
y1 += 2 # 0
51+
else:
52+
x0 += 2 # 0
53+
x1 += 2 # 0
54+
y0 += 3 # 32
55+
y1 += 3 # 32
56+
xpos = ustruct.pack('>HH', x0, x1)
57+
ypos = ustruct.pack('>HH', y0, y1)
58+
self.write(b'\x2a', xpos)
59+
self.write(b'\x2b', ypos)
60+
self.write(b'\x2c')
61+
self.dc(1)
62+
63+
def write(self, command=None, data=None):
64+
if command is not None:
65+
self.dc(0)
66+
self.spi.write(command)
67+
if data:
68+
self.dc(1)
69+
self.spi.write(data)
70+
71+
def clear(self, color=0x00):
72+
self.block(0, 0, self.width - 1, self.height - 1)
73+
pixel = color.to_bytes(2, 'big')
74+
data = pixel * 256
75+
for count in range(self.width * self.height // 256):
76+
self.write(None, data)
77+
78+
def __enter__(self):
79+
self.cs(0)
80+
return self
81+
82+
def __exit__(self, exc_type, exc_val, exc_tb):
83+
self.cs(1)

espboy/freeze/stage.py

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

espboy/freeze/ugame.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
from machine import SPI, I2C, Pin
7+
import st7735r
8+
9+
10+
K_LEFT = 0x01
11+
K_UP = 0x02
12+
K_DOWN = 0x04
13+
K_RIGHT = 0x08
14+
K_X = 0x10
15+
K_O = 0x20
16+
K_L = 0x40
17+
K_R = 0x80
18+
19+
20+
class Audio:
21+
def __init__(self):
22+
pass
23+
24+
def play(self, audio_file):
25+
pass
26+
27+
def stop(self):
28+
pass
29+
30+
def mute(self, value=True):
31+
pass
32+
33+
34+
class Buttons: # mcp23017
35+
def __init__(self, i2c, address=0x20):
36+
self._i2c = i2c
37+
self._address = address
38+
for register, value in (
39+
(0x00, b'\xff'), # port A input
40+
(0x01, b'\xff'), # port A polarity invert
41+
(0x06, b'\xff'), # port A pull-up
42+
(0x10, b'\xfe'), # port B input on all pins except B0
43+
):
44+
self._i2c.writeto_mem(self._address, register, value)
45+
46+
def _get_pressed(self):
47+
return self._i2c.readfrom_mem(self._address, 0x09, 1)
48+
49+
def cs(self, value):
50+
# toggle the B0 pin for the display's CS
51+
self._i2c.writeto_mem(self._address, 0x19,
52+
b'\x01' if value else b'\x00')
53+
54+
55+
i2c = I2C(sda=Pin(4), scl=Pin(5))
56+
buttons = Buttons(i2c)
57+
spi = SPI(1, baudrate=40000000)
58+
display = st7735r.ST7735R(spi, Pin(16, Pin.OUT), buttons.cs)
59+
audio = Audio()

espboy/manifest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include("$(PORT_DIR)/boards/manifest.py")
2+
freeze("../../micropython/modules")
3+
freeze("freeze")

0 commit comments

Comments
 (0)