Code for CircuitPython RGBMatrix Guide

This commit is contained in:
Jeff Epler 2020-04-14 14:17:59 -05:00
parent 16ef7443a1
commit af074b1b9a
4 changed files with 271 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

View file

@ -0,0 +1,97 @@
import random
import time
import board
import displayio
import framebufferio
import rgbmatrix
displayio.release_displays()
matrix = rgbmatrix.RGBMatrix(
width=64, height=32, bit_depth=3,
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
addr_pins=[board.A5, board.A4, board.A3, board.A2],
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
bitmap_file = open("emoji.bmp", 'rb')
bitmap = displayio.OnDiskBitmap(bitmap_file)
STOPPED, RUNNING, BRAKING = range(3)
def shuffled(seq):
return sorted(seq, key=lambda _: random.random())
class Strip(displayio.TileGrid):
def __init__(self):
super().__init__(bitmap=bitmap, pixel_shader=displayio.ColorConverter(),
width=1, height=4, tile_width=20, tile_height=24)
self.order = shuffled(range(20))
self.state = STOPPED
self.pos = 0
self.vel = 0
self.y = 0
self.x = 0
self.stop_time = time.monotonic_ns()
def step(self):
if self.state == RUNNING:
self.vel = max(self.vel * 9 // 10, 64)
if time.monotonic_ns() > self.stop_time:
self.state = BRAKING
elif self.state == BRAKING:
self.vel = max(self.vel * 85 // 100, 7)
self.pos = (self.pos + self.vel) % 7680
yy = round(self.pos / 16)
yyy = yy % 24
off = yy // 24
if self.state == BRAKING and self.vel == 7 and yyy < 4:
self.pos = off * 24 * 16
self.vel = 0
yy = 0
self.state = STOPPED
self.y = yy % 24 - 20
for i in range(4):
self[i] = self.order[(19 - i + off) % 20]
def kick(self, i):
self.state = RUNNING
self.vel = random.randint(256, 320)
self.stop_time = time.monotonic_ns() + 3000000000 + i * 350000000
def brake(self):
self.state = BRAKING
g = displayio.Group(max_size=3)
strips = []
for idx in range(3):
strip = Strip()
strip.x = idx * 22
strip.y = -20
g.append(strip)
strips.append(strip)
display.show(g)
orders = [shuffled(range(20)), shuffled(range(20)), shuffled(range(20))]
for si, oi in zip(strips, orders):
for idx in range(4):
si[idx] = oi[idx]
def all_stopped():
return all(si.state == STOPPED for si in strips)
for idx, si in enumerate(strips):
si.kick(idx)
while True:
display.refresh(minimum_frames_per_second=0)
if all_stopped():
for idx in range(100):
display.refresh(minimum_frames_per_second=0)
for idx, si in enumerate(strips):
si.kick(idx)
for idx, si in enumerate(strips):
si.step()

View file

@ -0,0 +1,92 @@
import random
import time
import board
import displayio
import framebufferio
import rgbmatrix
displayio.release_displays()
def apply_life_rule(old, new):
width = old.width
height = old.height
for y in range(height):
yyy = y * width
ym1 = ((y + height - 1) % height) * width
yp1 = ((y + 1) % height) * width
xm1 = width - 1
for x in range(width):
xp1 = (x + 1) % width
neighbors = (
old[xm1 + ym1] + old[xm1 + yyy] + old[xm1 + yp1] +
old[x + ym1] + old[x + yp1] +
old[xp1 + ym1] + old[xp1 + yyy] + old[xp1 + yp1])
new[x+yyy] = neighbors == 3 or (neighbors == 2 and old[x+yyy])
xm1 = x
def randomize(output, fraction=0.50):
for i in range(output.height * output.width):
output[i] = random.random() < fraction
# after xkcd's tribute to John Conway (1937-2020) https://xkcd.com/2293/
conway_data = [
b' +++ ',
b' + + ',
b' + + ',
b' + ',
b'+ +++ ',
b' + + + ',
b' + + ',
b' + + ',
b' + + ',
]
def conway(output):
for i in range(output.height * output.width):
output[i] = 0
for i, si in enumerate(conway_data):
y = output.height - len(conway_data) - 2 + i
for j, cj in enumerate(si):
output[(output.width - 8)//2 + j, y] = cj & 1
matrix = rgbmatrix.RGBMatrix(
width=64, height=32, bit_depth=1,
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
addr_pins=[board.A5, board.A4, board.A3, board.A2],
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
SCALE = 1
b1 = displayio.Bitmap(display.width//SCALE, display.height//SCALE, 2)
b2 = displayio.Bitmap(display.width//SCALE, display.height//SCALE, 2)
palette = displayio.Palette(2)
tg1 = displayio.TileGrid(b1, pixel_shader=palette)
tg2 = displayio.TileGrid(b2, pixel_shader=palette)
g1 = displayio.Group(max_size=3, scale=SCALE)
g1.append(tg1)
display.show(g1)
g2 = displayio.Group(max_size=3, scale=SCALE)
g2.append(tg2)
palette[1] = 0xffffff
conway(b1)
display.auto_refresh = True
time.sleep(3)
n = 40
while True:
for i in range(n):
display.show(g1)
apply_life_rule(b1, b2)
display.show(g2)
apply_life_rule(b2, b1)
randomize(b1)
palette[0] = 0
palette[1] = (
(0x0000ff if random.random() > .33 else 0) |
(0x00ff00 if random.random() > .33 else 0) |
(0xff0000 if random.random() > .33 else 0)) or 0xffffff
n = 200

View file

@ -0,0 +1,82 @@
import array
from _pixelbuf import wheel
import board
import displayio
import framebufferio
import rgbmatrix
import terminalio
displayio.release_displays()
matrix = rgbmatrix.RGBMatrix(
width=64, height=32, bit_depth=3,
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
addr_pins=[board.A5, board.A4, board.A3, board.A2],
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
def tilegrid(palette):
return displayio.TileGrid(
bitmap=terminalio.FONT.bitmap, pixel_shader=palette,
width=1, height=1, tile_width=6, tile_height=14, default_tile=32)
g = displayio.Group(max_size=2)
linelen = (64//7)+2
l1 = displayio.Group(max_size=linelen)
l2 = displayio.Group(max_size=linelen)
g.append(l1)
g.append(l2)
display.show(g)
l1.y = 1
l2.y = 16
sh = [displayio.Palette(2) for _ in range(linelen)]
tg1 = [tilegrid(shi) for shi in sh]
tg2 = [tilegrid(shi) for shi in sh]
charmap = array.array('b', [terminalio.FONT.get_glyph(32).tile_index]) * 256
for ch in range(256):
glyph = terminalio.FONT.get_glyph(ch)
if glyph is not None:
charmap[ch] = glyph.tile_index
for idx, gi in enumerate(tg1):
gi.x = 7 * idx
l1.append(gi)
for idx, gi in enumerate(tg2):
gi.x = 7 * idx
l2.append(gi)
lines = [
b"This scroller is brought to you by CircuitPython & PROTOMATTER",
b" .... . .-.. .-.. --- / .--. .-. --- - --- -- .- - - . .-.",
b"Greetz to ... @PaintYourDragon @v923z @adafruit ",
b" @danh @ladyada @kattni @tannewt all showers & tellers",
b"New York Strong Wash Your Hands ",
b" Flatten the curve Stronger Together",
]
even_lines = lines[0::2]
odd_lines = lines[1::2]
def scroll(t, b):
sp = b' ' * linelen
t = sp + t + sp
b = sp + b + sp
maxlen = max(len(t), len(b))
for i in range(maxlen-linelen):
for j in range(linelen):
sh[j][1] = wheel(3 * (2*i+j))
tg1[j][0] = charmap[t[i+j]]
tg2[j][0] = charmap[b[i+j]]
for j in range(7):
l1.x = -j
l2.x = -j
display.refresh(minimum_frames_per_second=0)
#display.refresh(minimum_frames_per_second=0)
while True:
for e, o in zip(even_lines, odd_lines):
scroll(e, o)