Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    LoPy as a lightorgan via OSC

    Scheduled Pinned Locked Moved LoPy
    osc micropythoncsound
    3 Posts 2 Posters 663 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Z Offline
      zappfinger
      last edited by

      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

      1 Reply Last reply Reply Quote 0
      • sebS Offline
        seb
        last edited by

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

        Z 1 Reply Last reply Reply Quote 0
        • Z Offline
          zappfinger @seb
          last edited by

          @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)
          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom