Skip to content

Commit

Permalink
whoos forgot osc.py
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmarsan committed Mar 12, 2012
1 parent d4e8930 commit 89a0777
Showing 1 changed file with 56 additions and 27 deletions.
83 changes: 56 additions & 27 deletions lights/python/osc.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,48 +1,72 @@
#!/usr/bin/python
from OSC import OSCServer, OSCClient, OSCMessage
import sys
import time
#import octoapi
# funny python's way to add a method to an instance of a class
import types

def handle_timeout(self):
self.timed_out = True

class ColorsIn:
# this method of reporting timeouts only works by convention
# that before calling handle_request() field .timed_out is
# set to False
def handle_timeout(self):
self.timed_out = True


def set_colors(path, tags, args, source):
# which user will be determined by path:
# we just throw away all slashes and join together what's left
print "Here's what we got : path:%s tags:%s args:%s source:%s"%(str(path),str(tags),str(args),str(source))
pixels = []
for i in range(0,len(args)/3):
pixel = (args[i*3],args[i*3+1],args[i*3+2])
pixels.append( pixel )
#print "Pixels: %s"%str(pixels)
#print "Time: "+str((time.time()*1000) % 10000)
#octoapi.write(pixels)
def set_colors(self,path, tags, args, source):
# which user will be determined by path:
# we just throw away all slashes and join together what's left
#print "Here's what we got : path:%s tags:%s args:%s source:%s"%(str(path),str(tags),str(args),str(source))
pixels = []
for i in range(0,len(args)/3):
pixel = (args[i*3],args[i*3+1],args[i*3+2])
pixels.append( pixel )
#print "Pixels: %s"%str(pixels)
#print "Time: "+str((time.time()*1000) % 10000)
#octoapi.write(pixels)
self.server.lastpixels = pixels

def diff_colors(self, path, tags, args, source):
# which user will be determined by path:
# we just throw away all slashes and join together what's left
#print "Here's what we got : path:%s tags:%s args:%s source:%s"%(str(path),str(tags),str(args),str(source))
pixels = server.lastpixels
for i in range(0,len(args)/3):
pp = (args[i*3],args[i*3+1],args[i*3+2])
p = pixels[i]
pixels[i] = (p[0]+pp[0], p[1]+pp[1], p[2]+pp[2])
#print "Pixels: %s"%str(pixels)
#print "Time: "+str((time.time()*1000) % 10000)
#octoapi.write(pixels)
self.server.lastpixels = pixels

def each_frame(self):
self.server.timed_out = False
while not self.server.timed_out:
self.server.handle_request()

def start(self):
#server = OSCServer( ("128.174.251.39", 11661) )
self.server = OSCServer( ("localhost", 11661) )
self.server.timeout = 0

self.server.handle_timeout = types.MethodType(handle_timeout, self.server)
self.server.lastpixels = [(0,0,0)]*24

self.server.addMsgHandler( "/setcolors", self.set_colors)
self.server.addMsgHandler( "/diffcolors", self.diff_colors)
while True:
self.each_frame()

self.server.close()

def each_frame():
server.timed_out = False
while not server.timed_out:
server.handle_request()

if __name__ == "__main__":
#server = OSCServer( ("128.174.251.39", 11661) )
server = OSCServer( ("localhost", 11661) )
server.timeout = 0

server.handle_timeout = types.MethodType(handle_timeout, server)
server.lastpixels = [(0,0,0)]*24
ColorsIn().start()

server.addMsgHandler( "/setcolors", set_colors)
while True:
each_frame()

server.close()


class ColorsOut:
Expand All @@ -54,3 +78,8 @@ def write(self, pixels):
message = OSCMessage("/setcolors")
message.append(pixels)
self.client.send( message )

def diff(self, pixels):
message = OSCMessage("/diffcolors")
message.append(pixels)
self.client.send( message )

0 comments on commit 89a0777

Please sign in to comment.