Created
September 29, 2018 19:25
-
-
Save devries/995971d57858f5a4231c9d01c2a2cad6 to your computer and use it in GitHub Desktop.
Writing over SPI to TCL LEDs from python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import math | |
def main(): | |
#fout = open('spidev','w') | |
fout = open('/dev/spidev2.0', 'w') | |
ctr = 0 | |
while True: | |
outdata = [] | |
outdata.append('\x00\x00\x00\x00') | |
for j in range(1250): | |
h = 360.0/1250.0*((j+ctr)%1250) | |
pval = hsv_to_rgb(h, 1.0, 1.0) | |
t = [int(math.floor(255.0*p)) for p in pval] | |
flag = (t[0]&0xc0)>>6 | |
flag |= (t[1]&0xc0)>>4 | |
flag |= (t[2]&0xc0)>>2 | |
flag ^= 0xff | |
outdata.append('%c'%flag) | |
outdata.append('%c'%t[2]) | |
outdata.append('%c'%t[1]) | |
outdata.append('%c'%t[0]) | |
outdata.append('\x00\x00\x00\x00') | |
outdata.append('\x00\x00\x00\x00') | |
fout.write(''.join(outdata)) | |
fout.flush() | |
#sys.stdout.write(''.join(outdata)) | |
#sys.stdout.flush() | |
time.sleep(0.02) | |
ctr+=1 | |
fout.close() | |
def hsv_to_rgb(h, s, v): | |
if s < 1.0e-6: | |
return (v, v, v) | |
h = h/60.0 | |
i = int(math.floor(h)) | |
f = h - i | |
p = v*(1.0-s) | |
q = v*(1.0-s*f) | |
t = v*(1.0-s*(1.0-f)) | |
if i==0: | |
return (v,t,p) | |
elif i==1: | |
return (q,v,p) | |
elif i==2: | |
return (p,v,t) | |
elif i==3: | |
return (p,q,v) | |
elif i==4: | |
return (t,p,v) | |
else: | |
return (v,p,q) | |
return (0,0,0) | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment