#!/usr/bin/env python # :mode=python:tabSize=4:indentSize=4:noTabs=false:collapseFolds=1: """ simpleOSC 0.2 ixi software - July, 2006 www.ixi-software.net simple API for the Open SoundControl for Python (by Daniel Holth, Clinton McChesney --> pyKit.tar.gz file at http://wiretap.stetson.edu) Documentation at http://wiretap.stetson.edu/docs/pyKit/ The main aim of this implementation is to provide with a simple way to deal with the OSC implementation that makes life easier to those who don't have understanding of sockets or programming. This would not be on your screen without the help of Daniel Holth. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Thanks for the support to Buchsenhausen, Innsbruck, Austria. """ import osc import serial # just importing the osc module creates under the hood an outbound socket and the callback manager # (osc addressManager). But we dont have to worry about that. ser = serial.Serial('/dev/ttyUSB0',57600) prefix = "/howto" def bindPrefix(newprefix): global prefix osc.bind(None, prefix + "/led") osc.bind(None, prefix +"/led_row") osc.bind(None, prefix +"/led_col") osc.bind(None, prefix +"/clear") osc.bind(None, prefix +"/frame") osc.bind(None, prefix +"/adc_enable") osc.bind(None, prefix + "/rgb") prefix = newprefix osc.bind(led, prefix + "/led") osc.bind(led_row, prefix +"/led_row") osc.bind(led_col, prefix +"/led_col") osc.bind(clear, prefix +"/clear") osc.bind(frame, prefix +"/frame") osc.bind(adc_enable, prefix +"/adc_enable") osc.bind(rgbset, prefix + "/rgb") def myTest(): """ a simple function that creates the necesary sockets and enters an enless loop sending and receiving OSC """ osc.init() inSocket = osc.createListener('127.0.0.1', 8080) # in this case just using one socket ## inSocket = osc.createListener() # this defaults to port 9001 as well # bind addresses to functions -> printStuff() function will be triggered everytime a # "/test" labeled message arrives global prefix osc.bind(led, prefix + "/led") osc.bind(led_row, prefix +"/led_row") osc.bind(led_col, prefix +"/led_col") osc.bind(clear, prefix +"/clear") osc.bind(frame, prefix +"/frame") osc.bind(adc_enable, prefix +"/adc_enable") osc.bind(rgbset, prefix + "/rgb") osc.bind(setPrefix, "/sys/prefix") print 'ready to receive and send osc messages ...' while 1: ##osc.sendMsg("/test", [444], "127.0.0.1", 9000) # send normal msg to a specific ip and port #osc.sendMsg("/test", [444], "127.0.0.1", 8000) # !! it sends by default to localhost ip "127.0.0.1" and port 9000 # create and send a bundle #bundle = osc.createBundle() #osc.appendToBundle(bundle, "/test/bndlprt1", [1, 2, 3]) # 1st message appent to bundle #osc.appendToBundle(bundle, "/test/bndlprt2", [4, 5, 6]) # 2nd message appent to bundle ## osc.sendBundle(bundle, "127.0.0.1", 9000) # send it to a specific ip and port #osc.sendBundle(bundle) # !! it sends by default to localhost ip "127.0.0.1" and port 9000 osc.getOSC(inSocket) # listen to incomming OSC in this socket if ser.inWaiting() > 1: command = ord(ser.read()) if (command >> 4) == 0: x = ord(ser.read()) print(prefix + "/press" + str([x >> 4, x & 0x0f, 1])) osc.sendMsg(prefix + "/press",[x >> 4, x & 0x0f, 1],"127.0.0.1",8000) #print 'Serial message received' elif (command >> 4) == 1: x = ord(ser.read()) print(prefix + "/press" + str([x >> 4, x & 0x0f, 0])) osc.sendMsg(prefix + "/press",[x >> 4, x & 0x0f, 0],"127.0.0.1",8000) elif (command >> 4) == 14: x = ord(ser.read()) print(prefix + "/adc" + str([command & 0x0f, float(x)/255])) osc.sendMsg(prefix + "/adc",[command & 0x0f, float(x)/255],"127.0.0.1",8000) #time.sleep(0.5) # you don't need this, but otherwise we're sending as fast as possible. """ Below some functions dealing with OSC messages RECEIVED to Python. Here you can set all the responders you need to deal with the incoming OSC messages. You need them to the callBackManager instance in the main loop and associate them to the desired OSC addreses like this for example addressManager.add(printStuff, "/print") it would associate the /print tagged messages with the printStuff() function defined in this module. You can have several callback functions in a separated module if you wish """ def setPrefix(*msg): print "The prefix is now", msg[0][2] bindPrefix(msg[0][2]) def rgbset(*msg): ser.write(chr(1 << 4)) ser.write(chr(msg[0][2])) ser.write(chr(msg[0][3])) ser.write(chr(msg[0][4])) def led(*msg): if msg[0][4] == 1: ser.write(chr(2 << 4)) ser.write(chr(msg[0][2] << 4 | msg[0][3])) else: ser.write(chr(3 << 4)) ser.write(chr(msg[0][2] << 4 | msg[0][3])) def led_row(*msg): ser.write(chr(4 << 4 | msg[0][2])) ser.write(chr(msg[0][3])) def led_col(*msg): ser.write(chr(5 << 4 | msg[0][2])) ser.write(chr(msg[0][3])) def frame(*msg): ser.write(chr(8 << 4)) for i in range(0,8): ser.write(chr(msg[0][i+2])) def clear(*msg): if len(msg[0]) == 2: ser.write(chr(9 << 4)) else: ser.write(chr(9 << 4 | msg[0][2])) def adc_enable(*msg): if msg[0][3] == 0: ser.write(chr(13 << 4 | msg[0][2])) else: ser.write(chr(12 << 4 | msg[0][2])) if __name__ == '__main__': myTest()