Advertisement
JetSerge

Untitled

Apr 20th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | None | 0 0
  1. import struct
  2. import ctypes
  3. from time import sleep
  4.  
  5. from serial import Serial
  6.  
  7.  
  8. LEDCOUNT = 30
  9. BAUDRATE = 115200
  10.  
  11. data = ctypes.create_string_buffer(6)
  12. # magic word + led number + checksum which are recognized as valid by my adalight
  13. struct.pack_into(">3s", data, 0, "Ada")
  14. struct.pack_into(">H", data, 3, LEDCOUNT)
  15. HEADER = bytearray(data)
  16. HEADER[5] = HEADER[3] ^ HEADER[4] ^ 0x55
  17.  
  18. # special header to set all leds to the same color (saves serial bandwidth)
  19. # requires special handling in the sketch
  20. ALLHEADER = ['A', 'd', 'a', 0xff, 0xff, 0x55]
  21.  
  22. arduino = Serial('COM6', BAUDRATE)
  23.  
  24. LEDBYTES = LEDCOUNT * 3  # r,g,b bytes
  25. LEDBUF = [0] * LEDBYTES  # buffer
  26.  
  27.  
  28. def setLedColor(num, r, g, b):
  29.     idx = num * 3
  30.     LEDBUF[idx] = chr(r)
  31.     LEDBUF[idx + 1] = chr(g)
  32.     LEDBUF[idx + 2] = chr(b)
  33.  
  34.  
  35. def setAllLedsFast(r, g, b):
  36.     arduino.write(ALLHEADER)
  37.     arduino.write(chr(r))
  38.     arduino.write(chr(g))
  39.     arduino.write(chr(b))
  40.     arduino.flushOutput()
  41.  
  42.  
  43. def setAllLeds(r, g, b):
  44.     for idx in range(0, LEDCOUNT):
  45.         setLedColor(idx, r, g, b)
  46.     display()
  47.  
  48.  
  49. def display():
  50.     arduino.write(HEADER)
  51.     arduino.write(LEDBUF)
  52.     arduino.flushOutput()
  53.  
  54.  
  55. def setStripColor(c):
  56.     if (c <= 255):
  57.         r = 255 - c
  58.         g = c
  59.         b = 0
  60.     elif (c <= 511):
  61.         r = 0
  62.         g = 255 - (c - 256)
  63.         b = c - 256
  64.     else:
  65.         r = c - 512
  66.         g = 0
  67.         b = 255 - (c - 512)
  68.     setAllLedsFast(r, g, b)
  69.     sleep(0.01)
  70.  
  71.  
  72. def checkColors():
  73.     for c in range(0, 768):
  74.         setStripColor(c)
  75.     for c in range(0, 768):
  76.         setStripColor(768 - c - 1)
  77.  
  78.  
  79. while 1:
  80.     sleep(2)  # wait for Arduiono initialization after reset
  81.     setAllLeds(0x00, 0x00, 0x00)
  82.     sleep(1)
  83.     setAllLeds(0xff, 0x00, 0x00)
  84.     sleep(1)
  85.     setAllLedsFast(0x00, 0xff, 0x00)
  86.     sleep(1)
  87.     setAllLedsFast(0x00, 0x00, 0xff)
  88.     sleep(1)
  89.     checkColors()
  90.  
  91.     for i in range(0, 4):
  92.         setAllLedsFast(0x00, 0x00, 0x00)
  93.         LEDBUF = [0] * LEDBYTES
  94.         sleep(1)
  95.         for n in range(0, LEDCOUNT):
  96.             if i % 2 == 0:
  97.                 LEDBUF = [0] * LEDBYTES
  98.                 if i % 4 == 0:
  99.                     setLedColor(n, 0xff, 0xff, 0xff)
  100.                 else:
  101.                     setLedColor(LEDCOUNT - n - 1, 0xff, 0xff, 0xff)
  102.             else:
  103.                 setLedColor(n, 0x00, 0x00, 0xff)
  104.  
  105.             display()
  106.             sleep(0.02)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement