Skip to content

Commit b712572

Browse files
committed
Initial commit
0 parents  commit b712572

File tree

11 files changed

+1101
-0
lines changed

11 files changed

+1101
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Radomir Dopieralski
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Stage – a Tile and Sprite Engine
2+
********************************
3+
4+
Stage is a library that lets you display tile grids and sprites on SPI-based
5+
RGB displays in MicroPython. It is mostly made with video games in mind, but it
6+
can be useful in making any kind of graphical user interface too.
7+
8+
For performance reasons, a part of this library has been written in C and has
9+
to be compiled as part of the MicroPython firmware as the ``_stage`` module.
10+
For memory saving reasons, it's best if the Python library is also included in
11+
the firmware, as a frozen module.
12+
13+
14+
API Reference
15+
*************
16+
17+
The API reference is available at `<http://circuitpython-stage.readthedocs.io>`_.
18+
19+
20+
Compiling
21+
*********
22+
23+
Please refer to the official guide on compiling external modules available
24+
at `<http://docs.micropython.org/en/latest/develop/cmodules.html>`_.
25+
26+
The sub-directories contain example library initialization files for different
27+
platforms. You should freeze the files for the platform you target into your
28+
firmware.
29+
30+
Example::
31+
make USER_C_MODULES=../../../micropython-stage/modules CFLAGS_EXTRA=-DMODULE_STAGE_ENABLE FROZEN_MPY_DIR=../../../micropython-stage/m5stack

esp8266st7735/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)

esp8266st7735/stage.py

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

esp8266st7735/ugame.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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_X = 0x20
11+
K_DOWN = 0x01
12+
K_LEFT = 0x08
13+
K_RIGHT = 0x04
14+
K_UP = 0x02
15+
K_O = 0x10
16+
17+
18+
class Audio:
19+
def __init__(self):
20+
pass
21+
22+
def play(self, audio_file):
23+
pass
24+
25+
def stop(self):
26+
pass
27+
28+
def mute(self, value=True):
29+
pass
30+
31+
32+
class Buttons:
33+
def __init__(self, i2c, address=0x10):
34+
self._i2c = i2c
35+
self._address = address
36+
37+
def get_pressed(self):
38+
return self._i2c.readfrom(self._address, 1)[0]
39+
40+
41+
spi = SPI(1, baudrate=40000000)
42+
display = st7735r.ST7735R(spi, Pin(12, Pin.OUT), Pin(15, Pin.OUT))
43+
i2c = I2C(sda=Pin(4), scl=Pin(5))
44+
buttons = Buttons(i2c)
45+
audio = Audio()

m5stack/ili9341.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import ustruct
2+
import utime
3+
4+
5+
class Display(object):
6+
_BUF = bytearray(4)
7+
8+
width = 320
9+
height = 240
10+
11+
def __init__(self, spi, dc, cs=None, rst=None):
12+
self.spi = spi
13+
self.dc = dc
14+
self.cs = cs or (lambda x: x)
15+
self.rst = rst or (lambda x: x)
16+
self.reset()
17+
18+
def reset(self):
19+
self.rst(0)
20+
utime.sleep_ms(50)
21+
self.rst(1)
22+
utime.sleep_ms(50)
23+
self.cs(0)
24+
for command, data in (
25+
(b'\xef', b'\x03\x80\x02'),
26+
(b'\xcf', b'\x00\xc1\x30'),
27+
(b'\xed', b'\x64\x03\x12\x81'),
28+
(b'\xe8', b'\x85\x00\x78'),
29+
(b'\xcb', b'\x39\x2c\x00\x34\x02'),
30+
(b'\xf7', b'\x20'),
31+
(b'\xea', b'\x00\x00'),
32+
(b'\xc0', b'\x23'), # Power Control 1, VRH[5:0]
33+
(b'\xc1', b'\x10'), # Power Control 2, SAP[2:0], BT[3:0]
34+
(b'\xc5', b'\x3e\x28'), # VCM Control 1
35+
(b'\xc7', b'\x86'), # VCM Control 2
36+
(b'\x36', b'\x10'), # Memory Access Control
37+
(b'\x3a', b'\x55'), # Pixel Format
38+
(b'\xb1', b'\x00\x18'), # FRMCTR1
39+
(b'\xb6', b'\x08\x82\x27'), # Display Function Control
40+
(b'\xf2', b'\x00'), # 3Gamma Function Disable
41+
(b'\x26', b'\x01'), # Gamma Curve Selected
42+
(b'\xe0', # Set Gamma
43+
b'\x0f\x31\x2b\x0c\x0e\x08\x4e\xf1\x37\x07\x10\x03\x0e\x09\x00'),
44+
(b'\xe1', # Set Gamma
45+
b'\x00\x0e\x14\x03\x11\x07\x31\xc1\x48\x08\x0f\x0c\x31\x36\x0f'),
46+
(b'\x11', None),
47+
(b'\x29', None),
48+
):
49+
self.write(command, data)
50+
self.cs(1)
51+
utime.sleep_ms(50)
52+
53+
def write(self, command=None, data=None):
54+
if command is not None:
55+
self.dc(0)
56+
self.spi.write(command)
57+
if data:
58+
self.dc(1)
59+
self.spi.write(data)
60+
61+
def block(self, x0, y0, x1, y1):
62+
ustruct.pack_into('>HH', self._BUF, 0, x0, x1)
63+
self.write(b'\x2a', self._BUF)
64+
ustruct.pack_into('>HH', self._BUF, 0, y0, y1)
65+
self.write(b'\x2b', self._BUF)
66+
self.write(b'\x2c')
67+
self.dc(1)
68+
69+
def clear(self, color=0x00):
70+
self.cs(0)
71+
self.block(0, 0, self.width, self.height)
72+
chunks, rest = divmod(self.width * self.height, 512)
73+
pixel = ustruct.pack('>H', color)
74+
if chunks:
75+
data = pixel * 512
76+
for count in range(chunks):
77+
self.spi.write(data)
78+
if rest:
79+
self.spi.write(pixel * rest)
80+
self.cs(1)
81+
82+
def __enter__(self):
83+
self.cs(0)
84+
return self
85+
86+
def __exit__(self, exc_type, exc_val, exc_tb):
87+
self.cs(1)

m5stack/stage.py

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

m5stack/ugame.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 ili9341
8+
9+
10+
K_X = 0x20
11+
K_DOWN = 0x01
12+
K_LEFT = 0x08
13+
K_RIGHT = 0x04
14+
K_UP = 0x02
15+
K_O = 0x10
16+
17+
18+
class Audio:
19+
def __init__(self):
20+
pass
21+
22+
def play(self, audio_file):
23+
pass
24+
25+
def stop(self):
26+
pass
27+
28+
def mute(self, value=True):
29+
pass
30+
31+
32+
class Buttons:
33+
def __init__(self, i2c, address=0x10):
34+
self._i2c = i2c
35+
self._address = address
36+
37+
def get_pressed(self):
38+
return self._i2c.readfrom(self._address, 1)[0]
39+
40+
41+
spi = SPI(2, baudrate=40000000, sck=Pin(18), mosi=Pin(23))
42+
display = ili9341.Display(spi, Pin(27, Pin.OUT), Pin(14, Pin.OUT),
43+
Pin(33, Pin.OUT))
44+
lite = Pin(32, Pin.OUT)
45+
lite(1)
46+
i2c = I2C(sda=Pin(21), scl=Pin(22))
47+
buttons = Buttons(i2c)
48+
audio = Audio()

modules/stage/micropython.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
STAGE_MOD_DIR := $(USERMOD_DIR)
2+
3+
SRC_USERMOD += $(STAGE_MOD_DIR)/mod_stage.c

0 commit comments

Comments
 (0)