Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Something like ~28fps for 3 channels on a MacBook Pro

  • Loading branch information...
commit 0a649adf85237a777ed06c105b97c7e51a706c2e 1 parent 0a6853c
@cibomahto cibomahto authored
Showing with 60 additions and 25 deletions.
  1. +20 −11 host/LedStrips.py
  2. +40 −14 host/domelistener.py
View
31 host/LedStrips.py
@@ -6,11 +6,12 @@
import random
class LedStrips:
- def __init__(self, offset):
+ def __init__(self, image_width, offset):
"""
Initialize an med strip
@param offset X position of the image to get LED image data from
"""
+ self.image_width = image_width
self.offset = offset
def connect(self, port):
@@ -57,18 +58,25 @@ def RgbRowToStrips(self, data):
return output
- def draw(self, data, width):
+ def draw(self, data):
"""
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
+ """
+ self.load_data(data)
+ self.flip()
+
+ def load_data(self, data):
+ """
+ Load the next frame into the strips, but don't actually clock it out.
+ @param data Image data, as a 1D, 8bit RGB array.
"""
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 + self.offset)*3
+ for row in range(0,len(data)/3/self.image_width):
+ start_index = (self.image_width*row + self.offset)*3
s += self.RgbRowToStrips(data[start_index:start_index+24])
for x in range(0, len(s)/64): # TODO: What this means?
@@ -76,9 +84,10 @@ def draw(self, data, width):
self.ser.write(t)
+ def flip(self):
# TODO: Why does 20 work? it make a'no sense.
# 1 does not work with the listener.
- for i in range(0,20):
+ for i in range(0,64):
self.ser.write('\x00')
if __name__ == "__main__":
@@ -90,10 +99,10 @@ def draw(self, data, width):
(options, args) = parser.parse_args()
- strip = LedStrips(0)
- strip.connect(options.serial_port)
+ image_width = 8 # width of the picture
- image_width = 8 # width of the picture index
+ strip = LedStrips(image_width, 0)
+ strip.connect(options.serial_port)
i = 0
j = 0
@@ -122,4 +131,4 @@ def draw(self, data, width):
j = (j+1)%255
- strip.draw(data, image_width)
+ strip.draw(data)
View
54 host/domelistener.py
@@ -17,25 +17,39 @@
strip_names = [
['/dev/tty.usbmodem12341', 0],
['/dev/tty.usbmodem121', 8],
+ ['/dev/tty.usbmodem122', 16],
]
-
class threadedLedStrips(multiprocessing.Process):
- q = multiprocessing.Queue(2)
- def __init__(self, port_name, offset):
+ def __init__(self, port_name, offset,
+ data_array, new_data_event, draw_event):
self.port_name = port_name
self.offset = offset
- multiprocessing.Process.__init__(self, target=self.run)
- self.strip = LedStrips.LedStrips(offset)
+ self.strip = LedStrips.LedStrips(image_width, offset)
self.strip.connect(port_name)
+ self.data = data_array
+ self.new_data_event = new_data_event
+ self.draw_event = draw_event
+
+ multiprocessing.Process.__init__(self, target=self.run)
+
def run(self):
while True:
- command = self.q.get()
- self.strip.draw(command,image_width)
+ # Wait for new data, then load it into the strip
+# print " %i wait for data"%self.offset
+ self.new_data_event.wait()
+# print " %i got data"%self.offset
+ self.strip.load_data(self.data)
+
+ # Wait for a write event, then signal the frame to flip over
+# print " %i wait for draw--"%self.offset
+ self.draw_event.wait()
+# print " %i got draw--"%self.offset
+ self.strip.flip()
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
@@ -43,8 +57,13 @@ def run(self):
strips = []
+new_data_event = multiprocessing.Event()
+draw_event = multiprocessing.Event()
+image_data = multiprocessing.Array('c', image_width*image_height*3)
+
for strip_name in strip_names:
- strip = threadedLedStrips(strip_name[0], strip_name[1])
+ strip = threadedLedStrips(strip_name[0], strip_name[1],
+ image_data, new_data_event, draw_event)
strip.start()
strips.append(strip)
@@ -66,14 +85,21 @@ def run(self):
print "bad data length, expected %i, got %i"%(expected_length, len(data))
continue
- for strip in strips:
- if strip.q.full():
- print "dropped a frame!"
- else:
- strip.q.put(data[1:])
+# print "Send draw"
+ # Clock out the previous frame
+ new_data_event.clear()
+ draw_event.set()
+
+ # Load in the next frame
+ for pos in range(0, image_width*image_height*3):
+ image_data[pos] = data[pos+1]
+
+# print "Send data--"
+ draw_event.clear()
+ new_data_event.set()
frame_count = (frame_count + 1) % 30
if (frame_count == 0):
print "Frame rate: %3.1f"%(30/(time.time() - start_time))
start_time = time.time()
-
+
Please sign in to comment.
Something went wrong with that request. Please try again.