LoPy as a lightorgan via OSC



  • I could not find an OSC library for LoPy's version of microPython, so I created a quick and dirty OSC UDP server for my needs. The linked video shows a song of me being filtered by Csound into low. mid and high bands. Then via OSC to the LoPy where the 3 signals are converted to R,G and B colors for the LED... video



  • @seb Sure. I'll share it here. Like I said it is very quick and dirty. It accepts an OSC message with only one variable (integer or float) of the form:
    '/adress1, if, value'
    integer (i) should also work.
    I do not parse the address tag(s). Also, in a normal OSC message multiple type tags can be used in one message, like: '/adress1, if, intval, floatval'
    That is not implemented.

    import socket
    import time
    import struct
    import pycom
    
    pycom.heartbeat(False)
    
    def readFloat(data):
    	if(len(data)<4):
    		print("Error: too few bytes for float", data, len(data))
    		rest = data
    		float = 0
    	else:
    		float = struct.unpack(">f", data[0:4])[0]
    		rest  = data[4:]
    	return float
    
    def readInt(data):
        if(len(data)<4):
            print("Error: too few bytes for int", data, len(data))
            rest = data
            integer = 0
        else:
            integer = struct.unpack(">i", data[0:4])[0]
            rest    = data[4:]
        return integer
    
    def getAdrVal(msg):
    	if b',f' in msg:
    		l = msg.find(b',f')
    		vals = msg[l+4:]
    		adress = msg[1:l-1]
    		val = readFloat(vals)
    		#print(adress,':',int(400*readFloat(val)))
    	elif b',i' in msg:
    		l = msg.find(b',i')
    		vals = msg[l+4:]
    		adress = msg[1:l-1]
    		val = readInt(vals)
    	else:
    		print('not implemented')
    		val = 0
    		adress = ''
    	return(adress, int(200*val))
    #	UDP socket server
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    serversocket.setblocking(True)
    serversocket.bind(('0.0.0.0', 9000))	# only bind, no accept like in TCP
    print('OSC server started')
    red, green, blue = 0,255,0
    while True:
    	data, src = serversocket.recvfrom(1024)
    	if data:
    		#print(data)
    		adrVal = getAdrVal(data)
            if b'Lo' in adrVal[0]:red = min(adrVal[1],255)
            if b'Mid' in adrVal[0]:green = min(adrVal[1],255)
            if b'Hi' in adrVal[0]:blue = min(adrVal[1],255)
            pycom.rgbled(red*256*256 + green*256 + blue)


  • Very nice, have you shared your code anywhere so others who might need this could get a head start?


 

Pycom on Twitter