Skip to content

Commit 596d77d

Browse files
authored
Merge branch 'master' into pulse-ox-log
2 parents 8cd6b07 + b9505c6 commit 596d77d

File tree

6 files changed

+454
-2
lines changed

6 files changed

+454
-2
lines changed

CircuitPython_RGBMatrix/emoji.bmp

28.3 KB
Binary file not shown.

CircuitPython_RGBMatrix/fruit.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
# This bitmap contains the emoji we're going to use. It is assumed
19+
# to contain 20 icons, each 20x24 pixels. This fits nicely on the 64x32
20+
# RGB matrix display.
21+
bitmap_file = open("emoji.bmp", 'rb')
22+
bitmap = displayio.OnDiskBitmap(bitmap_file)
23+
24+
# Each wheel can be in one of three states:
25+
STOPPED, RUNNING, BRAKING = range(3)
26+
27+
# Return a duplicate of the input list in a random (shuffled) order.
28+
def shuffled(seq):
29+
return sorted(seq, key=lambda _: random.random())
30+
31+
# The Wheel class manages the state of one wheel. "pos" is a position in
32+
# scaled integer coordinates, with one revolution being 7680 positions
33+
# and 1 pixel being 16 positions. The wheel also has a velocity (in positions
34+
# per tick) and a state (one of the above constants)
35+
class Wheel(displayio.TileGrid):
36+
def __init__(self):
37+
# Portions of up to 3 tiles are visible.
38+
super().__init__(bitmap=bitmap, pixel_shader=displayio.ColorConverter(),
39+
width=1, height=3, tile_width=20)
40+
self.order = shuffled(range(20))
41+
self.state = STOPPED
42+
self.pos = 0
43+
self.vel = 0
44+
self.y = 0
45+
self.x = 0
46+
self.stop_time = time.monotonic_ns()
47+
48+
def step(self):
49+
# Update each wheel for one time step
50+
if self.state == RUNNING:
51+
# Slowly lose speed when running, but go at least speed 64
52+
self.vel = max(self.vel * 9 // 10, 64)
53+
if time.monotonic_ns() > self.stop_time:
54+
self.state = BRAKING
55+
elif self.state == BRAKING:
56+
# More quickly lose speed when baking, down to speed 7
57+
self.vel = max(self.vel * 85 // 100, 7)
58+
59+
# Advance the wheel according to the velocity, and wrap it around
60+
# after 7680 positions
61+
self.pos = (self.pos + self.vel) % 7680
62+
63+
# Compute the rounded Y coordinate
64+
yy = round(self.pos / 16)
65+
# Compute the offset of the tile (tiles are 24 pixels tall)
66+
yyy = yy % 24
67+
# Find out which tile is the top tile
68+
off = yy // 24
69+
70+
# If we're braking and a tile is close to midscreen,
71+
# then stop and make sure that tile is exactly centered
72+
if self.state == BRAKING and self.vel == 7 and yyy < 4:
73+
self.pos = off * 24 * 16
74+
self.vel = 0
75+
yy = 0
76+
self.state = STOPPED
77+
78+
# Move the displayed tiles to the correct height and make sure the
79+
# correct tiles are displayed.
80+
self.y = yyy - 20
81+
for i in range(3):
82+
self[i] = self.order[(19 - i + off) % 20]
83+
84+
# Set the wheel running again, using a slight bit of randomness.
85+
# The 'i' value makes sure the first wheel brakes first, the second
86+
# brakes second, and the third brakes third.
87+
def kick(self, i):
88+
self.state = RUNNING
89+
self.vel = random.randint(256, 320)
90+
self.stop_time = time.monotonic_ns() + 3000000000 + i * 350000000
91+
92+
# Our fruit machine has 3 wheels, let's create them with a correct horizontal
93+
# (x) offset and arbitrary vertical (y) offset.
94+
g = displayio.Group(max_size=3)
95+
wheels = []
96+
for idx in range(3):
97+
wheel = Wheel()
98+
wheel.x = idx * 22
99+
wheel.y = -20
100+
g.append(wheel)
101+
wheels.append(wheel)
102+
display.show(g)
103+
104+
# Make a unique order of the emoji on each wheel
105+
orders = [shuffled(range(20)), shuffled(range(20)), shuffled(range(20))]
106+
107+
# And put up some images to start with
108+
for si, oi in zip(wheels, orders):
109+
for idx in range(3):
110+
si[idx] = oi[idx]
111+
112+
# We want a way to check if all the wheels are stopped
113+
def all_stopped():
114+
return all(si.state == STOPPED for si in wheels)
115+
116+
# To start with, though, they're all in motion
117+
for idx, si in enumerate(wheels):
118+
si.kick(idx)
119+
120+
# Here's the main loop
121+
while True:
122+
# Refresh the dislpay (doing this manually ensures the wheels move
123+
# together, not at different times)
124+
display.refresh(minimum_frames_per_second=0)
125+
if all_stopped():
126+
# Once everything comes to a stop, wait a little bit and then
127+
# start everything over again. Maybe you want to check if the
128+
# combination is a "winner" and add a light show or something.
129+
for idx in range(100):
130+
display.refresh(minimum_frames_per_second=0)
131+
for idx, si in enumerate(wheels):
132+
si.kick(idx)
133+
134+
# Otherwise, let the wheels keep spinning...
135+
for idx, si in enumerate(wheels):
136+
si.step()

CircuitPython_RGBMatrix/life.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
# Conway's "Game of Life" is played on a grid with simple rules, based
12+
# on the number of filled neighbors each cell has and whether the cell itself
13+
# is filled.
14+
# * If the cell is filled, and 2 or 3 neighbors are filled, the cell stays
15+
# filled
16+
# * If the cell is empty, and exactly 3 neighbors are filled, a new cell
17+
# becomes filled
18+
# * Otherwise, the cell becomes or remains empty
19+
#
20+
# The complicated way that the "m1" (minus 1) and "p1" (plus one) offsets are
21+
# calculated is due to the way the grid "wraps around", with the left and right
22+
# sides being connected, as well as the top and bottom sides being connected.
23+
#
24+
# This function has been somewhat optimized, so that when it indexes the bitmap
25+
# a single number [x + width * y] is used instead of indexing with [x, y].
26+
# This makes the animation run faster with some loss of clarity. More
27+
# optimizations are probably possible.
28+
29+
def apply_life_rule(old, new):
30+
width = old.width
31+
height = old.height
32+
for y in range(height):
33+
yyy = y * width
34+
ym1 = ((y + height - 1) % height) * width
35+
yp1 = ((y + 1) % height) * width
36+
xm1 = width - 1
37+
for x in range(width):
38+
xp1 = (x + 1) % width
39+
neighbors = (
40+
old[xm1 + ym1] + old[xm1 + yyy] + old[xm1 + yp1] +
41+
old[x + ym1] + old[x + yp1] +
42+
old[xp1 + ym1] + old[xp1 + yyy] + old[xp1 + yp1])
43+
new[x+yyy] = neighbors == 3 or (neighbors == 2 and old[x+yyy])
44+
xm1 = x
45+
46+
# Fill 'fraction' out of all the cells.
47+
def randomize(output, fraction=0.33):
48+
for i in range(output.height * output.width):
49+
output[i] = random.random() < fraction
50+
51+
52+
# Fill the grid with a tribute to John Conway
53+
def conway(output):
54+
# based on xkcd's tribute to John Conway (1937-2020) https://xkcd.com/2293/
55+
conway_data = [
56+
b' +++ ',
57+
b' + + ',
58+
b' + + ',
59+
b' + ',
60+
b'+ +++ ',
61+
b' + + + ',
62+
b' + + ',
63+
b' + + ',
64+
b' + + ',
65+
]
66+
for i in range(output.height * output.width):
67+
output[i] = 0
68+
for i, si in enumerate(conway_data):
69+
y = output.height - len(conway_data) - 2 + i
70+
for j, cj in enumerate(si):
71+
output[(output.width - 8)//2 + j, y] = cj & 1
72+
73+
# bit_depth=1 is used here because we only use primary colors, and it makes
74+
# the animation run a bit faster because RGBMatrix isn't taking over the CPU
75+
# as often.
76+
matrix = rgbmatrix.RGBMatrix(
77+
width=64, height=32, bit_depth=1,
78+
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
79+
addr_pins=[board.A5, board.A4, board.A3, board.A2],
80+
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
81+
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
82+
SCALE = 1
83+
b1 = displayio.Bitmap(display.width//SCALE, display.height//SCALE, 2)
84+
b2 = displayio.Bitmap(display.width//SCALE, display.height//SCALE, 2)
85+
palette = displayio.Palette(2)
86+
tg1 = displayio.TileGrid(b1, pixel_shader=palette)
87+
tg2 = displayio.TileGrid(b2, pixel_shader=palette)
88+
g1 = displayio.Group(max_size=3, scale=SCALE)
89+
g1.append(tg1)
90+
display.show(g1)
91+
g2 = displayio.Group(max_size=3, scale=SCALE)
92+
g2.append(tg2)
93+
94+
# First time, show the Conway tribute
95+
palette[1] = 0xffffff
96+
conway(b1)
97+
display.auto_refresh = True
98+
time.sleep(3)
99+
n = 40
100+
101+
while True:
102+
# run 2*n generations.
103+
# For the Conway tribute on 64x32, 80 frames is appropriate. For random
104+
# values, 400 frames seems like a good number. Working in this way, with
105+
# two bitmaps, reduces copying data and makes the animation a bit faster
106+
for _ in range(n):
107+
display.show(g1)
108+
apply_life_rule(b1, b2)
109+
display.show(g2)
110+
apply_life_rule(b2, b1)
111+
112+
# After 2*n generations, fill the board with random values and
113+
# start over with a new color.
114+
randomize(b1)
115+
# Pick a random color out of 6 primary colors or white.
116+
palette[1] = (
117+
(0x0000ff if random.random() > .33 else 0) |
118+
(0x00ff00 if random.random() > .33 else 0) |
119+
(0xff0000 if random.random() > .33 else 0)) or 0xffffff
120+
n = 200

CircuitPython_RGBMatrix/scroller.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# This example implements a rainbow colored scroller, in which each letter
2+
# has a different color. This is not possible with
3+
# Adafruit_Circuitpython_Display_Text, where each letter in a label has the
4+
# same color
5+
#
6+
# This demo also supports only ASCII characters and the built-in font.
7+
# See the simple_scroller example for one that supports alternative fonts
8+
# and characters, but only has a single color per label.
9+
10+
import array
11+
12+
from _pixelbuf import wheel
13+
import board
14+
import displayio
15+
import framebufferio
16+
import rgbmatrix
17+
import terminalio
18+
displayio.release_displays()
19+
20+
matrix = rgbmatrix.RGBMatrix(
21+
width=64, height=32, bit_depth=3,
22+
rgb_pins=[board.D6, board.D5, board.D9, board.D11, board.D10, board.D12],
23+
addr_pins=[board.A5, board.A4, board.A3, board.A2],
24+
clock_pin=board.D13, latch_pin=board.D0, output_enable_pin=board.D1)
25+
display = framebufferio.FramebufferDisplay(matrix, auto_refresh=False)
26+
27+
# Create a tilegrid with a bunch of common settings
28+
def tilegrid(palette):
29+
return displayio.TileGrid(
30+
bitmap=terminalio.FONT.bitmap, pixel_shader=palette,
31+
width=1, height=1, tile_width=6, tile_height=14, default_tile=32)
32+
33+
g = displayio.Group(max_size=2)
34+
35+
# We only use the built in font which we treat as being 7x14 pixels
36+
linelen = (64//7)+2
37+
38+
# prepare the main groups
39+
l1 = displayio.Group(max_size=linelen)
40+
l2 = displayio.Group(max_size=linelen)
41+
g.append(l1)
42+
g.append(l2)
43+
display.show(g)
44+
45+
l1.y = 1
46+
l2.y = 16
47+
48+
# Prepare the palettes and the individual characters' tiles
49+
sh = [displayio.Palette(2) for _ in range(linelen)]
50+
tg1 = [tilegrid(shi) for shi in sh]
51+
tg2 = [tilegrid(shi) for shi in sh]
52+
53+
# Prepare a fast map from byte values to
54+
charmap = array.array('b', [terminalio.FONT.get_glyph(32).tile_index]) * 256
55+
for ch in range(256):
56+
glyph = terminalio.FONT.get_glyph(ch)
57+
if glyph is not None:
58+
charmap[ch] = glyph.tile_index
59+
60+
# Set the X coordinates of each character in label 1, and add it to its group
61+
for idx, gi in enumerate(tg1):
62+
gi.x = 7 * idx
63+
l1.append(gi)
64+
65+
# Set the X coordinates of each character in label 2, and add it to its group
66+
for idx, gi in enumerate(tg2):
67+
gi.x = 7 * idx
68+
l2.append(gi)
69+
70+
# These pairs of lines should be the same length
71+
lines = [
72+
b"This scroller is brought to you by CircuitPython & PROTOMATTER",
73+
b" .... . .-.. .-.. --- / .--. .-. --- - --- -- .- - - . .-.",
74+
b"Greetz to ... @PaintYourDragon @v923z @adafruit ",
75+
b" @danh @ladyada @kattni @tannewt all showers & tellers",
76+
b"New York Strong Wash Your Hands ",
77+
b" Flatten the curve Stronger Together",
78+
]
79+
80+
even_lines = lines[0::2]
81+
odd_lines = lines[1::2]
82+
83+
# Scroll a top text and a bottom text
84+
def scroll(t, b):
85+
# Add spaces to the start and end of each label so that it goes from
86+
# the far right all the way off the left
87+
sp = b' ' * linelen
88+
t = sp + t + sp
89+
b = sp + b + sp
90+
maxlen = max(len(t), len(b))
91+
# For each whole character position...
92+
for i in range(maxlen-linelen):
93+
# Set the letter displayed at each position, and its color
94+
for j in range(linelen):
95+
sh[j][1] = wheel(3 * (2*i+j))
96+
tg1[j][0] = charmap[t[i+j]]
97+
tg2[j][0] = charmap[b[i+j]]
98+
# And then for each pixel position, move the two labels
99+
# and then refresh the display.
100+
for j in range(7):
101+
l1.x = -j
102+
l2.x = -j
103+
display.refresh(minimum_frames_per_second=0)
104+
#display.refresh(minimum_frames_per_second=0)
105+
106+
# Repeatedly scroll all the pairs of lines
107+
while True:
108+
for e, o in zip(even_lines, odd_lines):
109+
scroll(e, o)

0 commit comments

Comments
 (0)