Skip to content

Instantly share code, notes, and snippets.

@boochow
Last active November 23, 2018 10:27
Show Gist options
  • Save boochow/6a1efb67e812acf573ff19493ceaa6f3 to your computer and use it in GitHub Desktop.
Save boochow/6a1efb67e812acf573ff19493ceaa6f3 to your computer and use it in GitHub Desktop.
from machine import Clock, PWM, Pin
import time
Clock(Clock.PWM, enable=False, source=Clock.OSC, divi=6, divf=0, mash=0)
Clock(Clock.PWM, enable=True)
# set PWM to serializer mode, inverted (1 = L, 0 = H), FIFO enabled
p0 = PWM(Pin(18), mode=PWM.MODE_SERIALIZER, active=0, tick_hz=3200000, duty_ticks=0, period=32, ms=PWM.MS_ENABLE, polarity=1, use_fifo=1, fifo_repeat=0)
p1 = PWM(1, active=0, period=32, use_fifo=1) # both two PWMs must be fifo enabled (chip bug?)
# convert unsigned 32bit int to 32bit signed int
def uint32(x):
if x & 0x8000000:
x = x - 0xffffffff - 1
return x
def byte2pwm(b):
bits = "{:08b}".format(b)
data = 0
for c in bits:
data = (data << 4) | (0b0111 if c == '0' else 0b0001)
return data
pat=[2,0,0]*16+[0,2,0]*16+[1,1,0]*16+[0,1,1]*16+[0,0,2]*16+[1,0,1]*16
pat = pat + pat[0:192]
buf=bytearray()
for i in pat:
buf.extend(uint32(byte2pwm(i)).to_bytes(4, 'little'))
mv=memoryview(buf)
reset = bytearray([0xff]*24)
tail = bytearray([0] * 996)
while True:
for r in range(0,1152,12):
p0.fifo_active(False)
p0.fifo_clear()
p0.fifo_active(True)
sent=p0.fifo_write(reset+mv[r:r+768]+tail,bytearray(0),timeout=10000)
time.sleep_ms(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment