Skip to content

Commit 2aff72d

Browse files
authored
Merge pull request adafruit#1080 from jepler/cpy-rgbmatrix
CircuitPython RGBMatrix demo programs
2 parents 2f2fd55 + 2d06fa0 commit 2aff72d

File tree

5 files changed

+274
-2
lines changed

5 files changed

+274
-2
lines changed

CircuitPython_RGBMatrix/emoji.bmp

28.3 KB
Binary file not shown.

CircuitPython_RGBMatrix/fruit.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import random
2+
import time
3+
4+
import board
5+
import displayio
6+
import framebufferio
7+
import rgbmatrix
8+
9+
displayio.release_displays()
10+
11+
matrix = rgbmatrix.RGBMatrix(
12+
width=64, height=32, bit_depth=3,
13+
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
14+
addr_pins=[board.A5, board.A4, board.A3, board.A2],
15+
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
16+
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
17+
18+
bitmap_file = open("emoji.bmp", 'rb')
19+
bitmap = displayio.OnDiskBitmap(bitmap_file)
20+
21+
STOPPED, RUNNING, BRAKING = range(3)
22+
23+
def shuffled(seq):
24+
return sorted(seq, key=lambda _: random.random())
25+
26+
class Strip(displayio.TileGrid):
27+
def __init__(self):
28+
super().__init__(bitmap=bitmap, pixel_shader=displayio.ColorConverter(),
29+
width=1, height=4, tile_width=20, tile_height=24)
30+
self.order = shuffled(range(20))
31+
self.state = STOPPED
32+
self.pos = 0
33+
self.vel = 0
34+
self.y = 0
35+
self.x = 0
36+
self.stop_time = time.monotonic_ns()
37+
38+
def step(self):
39+
if self.state == RUNNING:
40+
self.vel = max(self.vel * 9 // 10, 64)
41+
if time.monotonic_ns() > self.stop_time:
42+
self.state = BRAKING
43+
elif self.state == BRAKING:
44+
self.vel = max(self.vel * 85 // 100, 7)
45+
self.pos = (self.pos + self.vel) % 7680
46+
yy = round(self.pos / 16)
47+
yyy = yy % 24
48+
off = yy // 24
49+
if self.state == BRAKING and self.vel == 7 and yyy < 4:
50+
self.pos = off * 24 * 16
51+
self.vel = 0
52+
yy = 0
53+
self.state = STOPPED
54+
self.y = yy % 24 - 20
55+
for i in range(4):
56+
self[i] = self.order[(19 - i + off) % 20]
57+
58+
def kick(self, i):
59+
self.state = RUNNING
60+
self.vel = random.randint(256, 320)
61+
self.stop_time = time.monotonic_ns() + 3000000000 + i * 350000000
62+
63+
def brake(self):
64+
self.state = BRAKING
65+
66+
g = displayio.Group(max_size=3)
67+
strips = []
68+
for idx in range(3):
69+
strip = Strip()
70+
strip.x = idx * 22
71+
strip.y = -20
72+
g.append(strip)
73+
strips.append(strip)
74+
display.show(g)
75+
76+
orders = [shuffled(range(20)), shuffled(range(20)), shuffled(range(20))]
77+
78+
for si, oi in zip(strips, orders):
79+
for idx in range(4):
80+
si[idx] = oi[idx]
81+
82+
def all_stopped():
83+
return all(si.state == STOPPED for si in strips)
84+
85+
for idx, si in enumerate(strips):
86+
si.kick(idx)
87+
88+
while True:
89+
display.refresh(minimum_frames_per_second=0)
90+
if all_stopped():
91+
for idx in range(100):
92+
display.refresh(minimum_frames_per_second=0)
93+
for idx, si in enumerate(strips):
94+
si.kick(idx)
95+
96+
for idx, si in enumerate(strips):
97+
si.step()

CircuitPython_RGBMatrix/life.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import random
2+
import time
3+
4+
import board
5+
import displayio
6+
import framebufferio
7+
import rgbmatrix
8+
9+
displayio.release_displays()
10+
11+
def apply_life_rule(old, new):
12+
width = old.width
13+
height = old.height
14+
for y in range(height):
15+
yyy = y * width
16+
ym1 = ((y + height - 1) % height) * width
17+
yp1 = ((y + 1) % height) * width
18+
xm1 = width - 1
19+
for x in range(width):
20+
xp1 = (x + 1) % width
21+
neighbors = (
22+
old[xm1 + ym1] + old[xm1 + yyy] + old[xm1 + yp1] +
23+
old[x + ym1] + old[x + yp1] +
24+
old[xp1 + ym1] + old[xp1 + yyy] + old[xp1 + yp1])
25+
new[x+yyy] = neighbors == 3 or (neighbors == 2 and old[x+yyy])
26+
xm1 = x
27+
28+
def randomize(output, fraction=0.50):
29+
for i in range(output.height * output.width):
30+
output[i] = random.random() < fraction
31+
32+
# after xkcd's tribute to John Conway (1937-2020) https://xkcd.com/2293/
33+
conway_data = [
34+
b' +++ ',
35+
b' + + ',
36+
b' + + ',
37+
b' + ',
38+
b'+ +++ ',
39+
b' + + + ',
40+
b' + + ',
41+
b' + + ',
42+
b' + + ',
43+
]
44+
45+
def conway(output):
46+
for i in range(output.height * output.width):
47+
output[i] = 0
48+
for i, si in enumerate(conway_data):
49+
y = output.height - len(conway_data) - 2 + i
50+
for j, cj in enumerate(si):
51+
output[(output.width - 8)//2 + j, y] = cj & 1
52+
53+
matrix = rgbmatrix.RGBMatrix(
54+
width=64, height=32, bit_depth=1,
55+
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
56+
addr_pins=[board.A5, board.A4, board.A3, board.A2],
57+
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
58+
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
59+
SCALE = 1
60+
b1 = displayio.Bitmap(display.width//SCALE, display.height//SCALE, 2)
61+
b2 = displayio.Bitmap(display.width//SCALE, display.height//SCALE, 2)
62+
palette = displayio.Palette(2)
63+
tg1 = displayio.TileGrid(b1, pixel_shader=palette)
64+
tg2 = displayio.TileGrid(b2, pixel_shader=palette)
65+
g1 = displayio.Group(max_size=3, scale=SCALE)
66+
g1.append(tg1)
67+
display.show(g1)
68+
g2 = displayio.Group(max_size=3, scale=SCALE)
69+
g2.append(tg2)
70+
71+
palette[1] = 0xffffff
72+
conway(b1)
73+
display.auto_refresh = True
74+
time.sleep(3)
75+
n = 40
76+
77+
while True:
78+
for _ in range(n):
79+
display.show(g1)
80+
apply_life_rule(b1, b2)
81+
display.show(g2)
82+
apply_life_rule(b2, b1)
83+
84+
randomize(b1)
85+
palette[0] = 0
86+
palette[1] = (
87+
(0x0000ff if random.random() > .33 else 0) |
88+
(0x00ff00 if random.random() > .33 else 0) |
89+
(0xff0000 if random.random() > .33 else 0)) or 0xffffff
90+
n = 200

CircuitPython_RGBMatrix/scroller.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import array
2+
3+
from _pixelbuf import wheel
4+
import board
5+
import displayio
6+
import framebufferio
7+
import rgbmatrix
8+
import terminalio
9+
displayio.release_displays()
10+
11+
matrix = rgbmatrix.RGBMatrix(
12+
width=64, height=32, bit_depth=3,
13+
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
14+
addr_pins=[board.A5, board.A4, board.A3, board.A2],
15+
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
16+
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
17+
18+
def tilegrid(palette):
19+
return displayio.TileGrid(
20+
bitmap=terminalio.FONT.bitmap, pixel_shader=palette,
21+
width=1, height=1, tile_width=6, tile_height=14, default_tile=32)
22+
23+
g = displayio.Group(max_size=2)
24+
linelen = (64//7)+2
25+
l1 = displayio.Group(max_size=linelen)
26+
l2 = displayio.Group(max_size=linelen)
27+
g.append(l1)
28+
g.append(l2)
29+
display.show(g)
30+
31+
l1.y = 1
32+
l2.y = 16
33+
34+
sh = [displayio.Palette(2) for _ in range(linelen)]
35+
tg1 = [tilegrid(shi) for shi in sh]
36+
tg2 = [tilegrid(shi) for shi in sh]
37+
38+
charmap = array.array('b', [terminalio.FONT.get_glyph(32).tile_index]) * 256
39+
for ch in range(256):
40+
glyph = terminalio.FONT.get_glyph(ch)
41+
if glyph is not None:
42+
charmap[ch] = glyph.tile_index
43+
44+
for idx, gi in enumerate(tg1):
45+
gi.x = 7 * idx
46+
l1.append(gi)
47+
48+
for idx, gi in enumerate(tg2):
49+
gi.x = 7 * idx
50+
l2.append(gi)
51+
52+
lines = [
53+
b"This scroller is brought to you by CircuitPython & PROTOMATTER",
54+
b" .... . .-.. .-.. --- / .--. .-. --- - --- -- .- - - . .-.",
55+
b"Greetz to ... @PaintYourDragon @v923z @adafruit ",
56+
b" @danh @ladyada @kattni @tannewt all showers & tellers",
57+
b"New York Strong Wash Your Hands ",
58+
b" Flatten the curve Stronger Together",
59+
]
60+
61+
even_lines = lines[0::2]
62+
odd_lines = lines[1::2]
63+
64+
def scroll(t, b):
65+
sp = b' ' * linelen
66+
t = sp + t + sp
67+
b = sp + b + sp
68+
maxlen = max(len(t), len(b))
69+
for i in range(maxlen-linelen):
70+
for j in range(linelen):
71+
sh[j][1] = wheel(3 * (2*i+j))
72+
tg1[j][0] = charmap[t[i+j]]
73+
tg2[j][0] = charmap[b[i+j]]
74+
for j in range(7):
75+
l1.x = -j
76+
l2.x = -j
77+
display.refresh(minimum_frames_per_second=0)
78+
#display.refresh(minimum_frames_per_second=0)
79+
80+
while True:
81+
for e, o in zip(even_lines, odd_lines):
82+
scroll(e, o)

pylint_check

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#!/bin/bash
22

3+
PYLINT="`type -p pylint3 2>/dev/null || type -p pylint`"
4+
35
function find_pyfiles() {
4-
for f in $(find . -type f -iname '*.py'); do
6+
if [ $# -eq 0 ]; then set -- .; fi
7+
for f in $(find "$@" -type f -iname '*.py'); do
58
if [ ! -e "$(dirname $f)/.circuitpython.skip" ]; then
69
echo "$f"
710
fi
811
done
912
}
1013

11-
find_pyfiles | xargs pylint
14+
find_pyfiles "$@" | xargs "$PYLINT"

0 commit comments

Comments
 (0)