Permalink
Browse files

Merge branch 'master' of github.com:hackrockcity/domeFirmware

  • Loading branch information...
mhenstell committed May 3, 2012
2 parents 53d20cf + 74bd74f commit 98883fe3a978c4d0b13e7f359810f3f1a261dc02
Showing with 92 additions and 19 deletions.
  1. +17 −19 host/{host.py → LedStrips.py}
  2. +75 −0 host/domelistener.py
@@ -6,6 +6,13 @@
import random
class LedStrips:
+ def __init__(self, offset):
+ """
+ Initialize an med strip
+ @param offset X position of the image to get LED image data from
+ """
+ self.offset = offset
+
def connect(self, port):
self.ser = serial.Serial(port, 1000002, timeout=0)
@@ -47,29 +54,27 @@ def RgbRowToStrips(self, data):
return output
- def draw(self, data, width, offset):
+ def draw(self, data, width):
"""
Draw a portion of an image frame to LED strips.
@param data Image data, as a 1D, 8bit RGB array.
@param width Width of the image, in pixels
- @param offset X position of the image to draw to the LEDs
"""
s = ''
# for each 'row' in the data, assemble a byte stream for it.
for row in range(0,len(data)/3/width):
- start_index = (width*row + offset)*3
+ start_index = (width*row + self.offset)*3
s += self.RgbRowToStrips(data[start_index:start_index+24])
- print len(s)
for x in range(0, len(s)/64):
t = s[64 * x : (64 * x) + 64]
self.ser.write(t)
# TODO: Why 80?
- for i in range(0,80):
+ for i in range(0,64):
self.ser.write('\x00')
if __name__ == "__main__":
@@ -79,7 +84,7 @@ def draw(self, data, width, offset):
(options, args) = parser.parse_args()
- strip = LedStrips()
+ strip = LedStrips(0)
strip.connect(options.serial_port)
strip_length = 160 # length, in pixels
@@ -93,19 +98,12 @@ def draw(self, data, width, offset):
for col in range (0,image_width):
if j == 0:
data += chr(0xFF) # B
- data += chr(0) # B
data += chr(0xFF) # B
- if j == 1:
- data += chr(0) # B
data += chr(0xFF) # B
- data += chr(0xFF) # B
- if j == 2:
- data += chr(0) # B
- data += chr(0) # B
- data += chr(0xFF) # B
- j = (j + 1) % 3
-
- i = (i + 5)%256
+ else:
+ data += chr(i) # B
+ data += chr(i) # B
+ data += chr(i) # B
+ j = (j+1)%2
- strip.draw(data, image_width, 0)
- time.sleep(1)
+ strip.draw(data, image_width)
View
@@ -0,0 +1,75 @@
+import socket
+import time
+import Queue
+import threading
+import LedStrips
+
+# UDP settings
+host = "0.0.0.0"
+port = 58082
+buffer_size = 200000
+
+# Image settings
+image_height = 160
+image_width = 25
+
+# Serial port settings
+strip_names = [
+ ['/dev/tty.usbmodem12341', 0],
+ ['/dev/tty.usbmodem63', 8],
+ ['/dev/tty.usbmodem64', 16],
+]
+
+
+
+class threadedLedStrips(threading.Thread):
+ q = Queue.Queue()
+
+ def __init__(self, port_name, offset):
+ threading.Thread.__init__(self)
+ self.strip = LedStrips.LedStrips(offset)
+ self.strip.connect(port_name)
+
+ def run(self):
+ while True:
+ command = self.q.get()
+ self.strip.draw(data,image_width)
+ self.q.task_done()
+
+
+sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+sock.bind((host,port))
+
+strips = []
+
+for strip_name in strip_names:
+ strip = threadedLedStrips(strip_name[0], strip_name[1])
+ strip.start()
+ strips.append(strip)
+
+start_time = time.time()
+frame_count = 0
+while 1:
+ data, addr = sock.recvfrom(buffer_size)
+
+ if not data:
+ print "no data."
+ continue
+
+ if data[0] != '\x01':
+ print "bad header, expected=%i, got=%i"%(1, ord(data[0]))
+ continue
+
+ expected_length = image_width*image_height*3+1
+ if len(data) != expected_length:
+ print "bad data length, expected %i, got %i"%(expected_length, len(data))
+ continue
+
+ for strip in strips:
+ strip.q.put(data[1:])
+
+ frame_count = (frame_count + 1) % 30
+ if (frame_count == 0):
+ print "Frame rate: %3.1f"%(30/(time.time() - start_time))
+ start_time = time.time()
+

0 comments on commit 98883fe

Please sign in to comment.