close-circle
Close

Update UDP broadcast for individual pixel control

A project log for A Hacker's Wedding Centerpiece(s)

Im getting married :D. I've been put incharge of making the coolest* centerpieces ever! (*coolest acording to me)

bveinabveina 01/12/2018 at 22:240 Comments

now that ive had the breakout boards populated and wired ten in a chain, i need to have some kind of test code. having more than one led means that now i want to have more than one color displayed.

the first prototype code had 4 bytes per data-gram "RR BB GG VV" red, blue, green, intensity value. and that color would be displayed on all leds. now, ive just duplicated that project. every group of 4 is one led, therefore 10 leds should take a 40 byte data-gram. not an earth-shattering bandwidth.

Interpreting the data was straightforward. i used a quick function to chunk the data into 4 byte arrays....thats it. everything else is the same as dealing with one led.

from machine import Pin
from apa102 import APA102
import usocket
port = 5500

clock = Pin(14,Pin.OUT)
data  = Pin(13,Pin.OUT)
numPixels=10
apa = APA102(clock,data,numPixels)


# https://gist.github.com/SZanlongo/f9290809d1f76289d40a52af949acfbc
def chunks(l, n):
    """Yield successive n-sized chunks from l."""
    for i in range(0, len(l), n):
        yield l[i:i + n]
        
        
def setPixelRaw(index,r,g,b,val=31):
  apa[index]=(r,b,g,val)
  
def setPixelStr(index,s):
  if len(s)<3: return
  val=31
  r= s[0]
  g= s[1]
  b= s[2]
  if len(s)>3: val = s[3]
  setPixelRaw(index,r,g,b,val)
    

s = usocket.socket(usocket.AF_INET,usocket.SOCK_DGRAM)
s.bind(('127.0.0.1',port))

while True:
  t=s.recvfrom(1024)
  l = t[0]
  for i,x in enumerate(chunks(l,4)):
    setPixelStr(i,x)
  apa.write()
  

then i needed some cute animations. i need to clean up the code a bit but the colorsys library comes to the rescue again.

i think with this successful test i need to lock in the dimensions of the acrylic and spin up the final base pcb.

Discussions