Skip to content

Instantly share code, notes, and snippets.

@jepler
Created February 15, 2021 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jepler/681480e07739b56319eb9e8c1fd86be6 to your computer and use it in GitHub Desktop.
Save jepler/681480e07739b56319eb9e8c1fd86be6 to your computer and use it in GitHub Desktop.
import time
import random
import board
import busio
import digitalio
import gc
import rp2pio
import adafruit_pioasm
program = """
.program piopixl8
.side_set 2
;; out bit 1: '595 "ser"
;; side-set bit 1: '595 "srclk" (shift clock)
;; side-set bit 2: '595 "rclk" (latch clock)
;; 1 iteration = 2 cycles
;; 8 iterations + setup = 17 cycles
;; 1 loop = 51 cycles + 1 delay cycle = 52 cycles
;; 800kHz * 52 = 41.60MHz
;; 125MHz / 3 = 41.67MHz
.wrap_target
set x, 7 side 2
pull
bitloop0:
set pins, 1 side 0
jmp x--, bitloop0 side 1
set x, 7 side 2
bitloop1:
out pins, 1 side 0
jmp x--, bitloop1 side 1
set x, 7 side 2
bitloop2:
set pins, 0 side 0
jmp x--, bitloop2 side 1
.wrap
"""
assembled = adafruit_pioasm.assemble(program)
sm = rp2pio.StateMachine(
assembled,
frequency=800_000 * 52,
init=adafruit_pioasm.assemble("set pindirs 7"),
first_out_pin=board.GP0,
out_pin_count=1,
first_set_pin=board.GP0,
set_pin_count=3,
first_sideset_pin=board.GP1,
sideset_pin_count=2,
auto_pull=True,
out_shift_right=False,
pull_threshold=8,
)
def wheel(pos):
pos = round(pos % 256)
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
def shade(seq, frac):
f2 = frac * frac
f3 = frac * f2
return [round(si * f3) for si in seq]
def ramp(base, count):
result = [None] * 3 * count
for i in range(count//2):
result[3*i:3*i+3] = shade(base, 1-i / (count // 2))
j = (count // 2) * 3
count -= count//2
for i in range(count):
result[j+3*i:j+3*i+3] = shade(base, i / count)
return result + result
sn = 32
strand_data = [
ramp((255,255,255), sn),
ramp(wheel(32), sn),
ramp(wheel(64), sn),
ramp(wheel(92), sn),
ramp(wheel(128), sn),
ramp(wheel(160), sn),
ramp(wheel(192), sn),
ramp(wheel(224), sn)]
#strand_data = 8 * [strand_data[0]]
#print(strand_data)
print(strand_data[0])
leds = [
[0xff, 0x00, 0x00,],
[0xff, 0xff, 0x00,],
[0x00, 0xff, 0x00,],
[0x00, 0xff, 0xff,],
[0x00, 0x00, 0xff,],
[0xff, 0x00, 0xff,],
[0xff, 0xff, 0xff,],
[0xaa, 0x55, 0xaa,],
]
n = len(strand_data[0])
buf = bytearray(n*8)
for output_idx in range(n*8): # For each output byte, ...
input_idx = output_idx >> 3
#input_bit = 1 << (output_idx & 0x7)
input_bit = 0x80 >> (output_idx & 0x7)
b = 0
for strand_idx in range(8): # Find 8 input bits
if strand_data[strand_idx][input_idx] & input_bit:
b |= 1<<strand_idx
#print(f"{input_idx:3d} {input_bit:3d} {b:08b}")
buf[output_idx] = b
print(strand_data[0])
#print(buf)
i = 0
nn = 30*24
while True:
sm.write(buf[i*24:i*24+nn])
i = (i + 1) % sn
time.sleep(.001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment