Skip to content

Commit 0de69f7

Browse files
authored
Merge branch 'master' into master
2 parents 8729cc5 + fc3d004 commit 0de69f7

20 files changed

Lines changed: 126419 additions & 9 deletions

File tree

CLUE_I_Ching/christopher_done_24.bdf

Lines changed: 1619 additions & 0 deletions
Large diffs are not rendered by default.

CLUE_I_Ching/clue_iching.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import time
2+
import random
3+
import displayio
4+
from adafruit_bitmap_font import bitmap_font
5+
from adafruit_display_text import label
6+
from adafruit_clue import clue
7+
8+
#--| User Config |-------------------------------
9+
BACKGROUND_COLOR = 0xCFBC17
10+
HEXAGRAM_COLOR = 0xBB0000
11+
FONT_COLOR = 0x005500
12+
SHAKE_THRESHOLD = 20
13+
MELODY = ( (1000, 0.1), # (freq, duration)
14+
(1200, 0.1),
15+
(1400, 0.1),
16+
(1600, 0.2))
17+
#--| User Config |-------------------------------
18+
19+
# Defined in order treating each hexagram as a 6 bit value.
20+
HEXAGRAMS = (
21+
"EARTH", "RETURN", "THE ARMY", "PREVAILING", "MODESTY", " CRYING\nPHEASANT",
22+
"ASCENDANCE", "PEACE", "WEARINESS", "THUNDER", "LETTING\n LOOSE",
23+
"MARRYING\n MAIDEN", " SMALL\nEXCESS", "ABUNDANCE", "STEADFASTNESS",
24+
" GREAT\nINJURY", "SUPPORT", "RETRENCHMENT", "WATER", "FRUGALITY",
25+
"ADMONISHMENT", "FULFILLMENT", "THE WELL", "WAITING", "ILLNESS",
26+
"THE CHASE", "TRAPPED", "LAKE", "CUTTING", "REVOLUTION", " GREAT\nEXCESS",
27+
"STRIDE", "LOSS", "THE CHEEKS", "BLINDNESS", "DECREASE", "MOUNTAIN",
28+
"DECORATION", "WORK", " BIG\nCATTLE", "ADVANCE", "BITING", "UNFULFILLMENT",
29+
"ABANDONED", "TRAVELER", "FIRE", " THE\nCAULDRON", " GREAT\nHARVEST",
30+
"VIEW", "INCREASE", "FLOWING", "SINCERITY", "PROGRESS", "FAMILY", "WIND",
31+
" SMALL\nCATTLE", "OBSTRUCTION", "PROPRIETY", "THE COURT", "TREADING",
32+
"LITTLE\n PIG", "GATHERING", "RENDEZVOUS", "HEAVEN",
33+
)
34+
35+
# Grab the CLUE's display
36+
display = clue.display
37+
38+
# Background fill
39+
bg_bitmap = displayio.Bitmap(display.width, display.height, 1)
40+
bg_palette = displayio.Palette(1)
41+
bg_palette[0] = BACKGROUND_COLOR
42+
background = displayio.TileGrid(bg_bitmap, pixel_shader=bg_palette)
43+
44+
# Hexagram setup
45+
sprite_sheet = displayio.Bitmap(11, 4, 2)
46+
palette = displayio.Palette(2)
47+
palette.make_transparent(0)
48+
palette[0] = 0x000000
49+
palette[1] = HEXAGRAM_COLOR
50+
51+
for x in range(11):
52+
sprite_sheet[x, 0] = 1 # - - 0 YIN
53+
sprite_sheet[x, 1] = 0
54+
sprite_sheet[x, 2] = 1 # --- 1 YANG
55+
sprite_sheet[x, 3] = 0
56+
sprite_sheet[5, 0] = 0
57+
58+
tile_grid = displayio.TileGrid(sprite_sheet, pixel_shader=palette,
59+
width = 1,
60+
height = 6,
61+
tile_width = 11,
62+
tile_height = 2)
63+
64+
hexagram = displayio.Group(max_size=1, x=60, y=15, scale=10)
65+
hexagram.append(tile_grid)
66+
67+
# Hexagram name label
68+
# font credit: https://www.instagram.com/cove703/
69+
font = bitmap_font.load_font("/christopher_done_24.bdf")
70+
font.load_glyphs(b'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
71+
hexname = label.Label(font, text=" "*40, color=FONT_COLOR)
72+
# this will initially hold the "shake for reading" message
73+
hexname.text = " SHAKE\n FOR\nREADING"
74+
hexname.anchor_point = (0.5, 0.0)
75+
hexname.anchored_position = (120, 120)
76+
77+
# Set up main display group (splash)
78+
splash = displayio.Group()
79+
display.show(splash)
80+
81+
# Add background and text label
82+
splash.append(background)
83+
splash.append(hexname)
84+
85+
def show_hexagram(number):
86+
for i in range(6):
87+
tile_grid[5-i] = (number >> i) & 0x01
88+
89+
def show_name(number):
90+
hexname.text = HEXAGRAMS[number]
91+
hexname.anchored_position = (120, 180)
92+
93+
#===================================
94+
# MAIN CODE
95+
#===================================
96+
print("shake")
97+
# wait for shake
98+
while not clue.shake(shake_threshold=SHAKE_THRESHOLD):
99+
pass
100+
101+
# calibrate the mystic universe
102+
x, y, z = clue.acceleration
103+
random.seed(int(time.monotonic() + abs(x) + abs(y) + abs(z)))
104+
105+
# cast a reading
106+
reading = random.randrange(64)
107+
print("reading = ", reading, HEXAGRAMS[reading])
108+
109+
# play a melody
110+
for note, duration in MELODY:
111+
clue.play_tone(note, duration)
112+
113+
# prompt to show
114+
display.auto_refresh = False
115+
hexname.text = " GOT IT\n\nPRESS BUTTON\n TO SEE"
116+
hexname.anchored_position = (120, 120)
117+
display.auto_refresh = True
118+
while not clue.button_a and not clue.button_b:
119+
pass
120+
121+
# and then show it
122+
display.auto_refresh = False
123+
splash.append(hexagram)
124+
show_hexagram(reading)
125+
show_name(reading)
126+
display.auto_refresh = True
127+
128+
# hold here until reset
129+
while True:
130+
pass

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

0 commit comments

Comments
 (0)