midiOSCesp/esp/lserver.py

215 lines
4.8 KiB
Python
Raw Normal View History

2021-01-06 02:32:39 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2021-01-06 02:48:09 +00:00
'''
Led data pin : D5 (GPIO14)
'''
2021-01-06 02:32:39 +00:00
import sys, time
import socket
from uosc.server import run_server, split_oscstr, parse_message, parse_bundle
from uosc.client import send
2021-01-16 18:57:46 +00:00
from uosc.client import Client
2021-01-06 02:32:39 +00:00
from machine import Pin
import mynetconf as netconf
import machine, neopixel
print('Loading OSC server module...')
MAX_DGRAM_SIZE = 1472
OSCport = 9001
ledpin = Pin(2, Pin.OUT)
ESPledstate = False
2021-01-16 18:57:46 +00:00
stripin = Pin(14, Pin.OUT)
2021-01-06 02:32:39 +00:00
2021-01-16 18:57:46 +00:00
n = 144
2021-01-06 02:32:39 +00:00
np = neopixel.NeoPixel(stripin, n)
2021-01-16 18:57:46 +00:00
motherip = "192.168.2.104"
motherosc = Client(motherip, 9002)
2021-01-06 02:32:39 +00:00
2021-01-16 18:57:46 +00:00
leftnote = 36
2021-01-06 02:32:39 +00:00
def ESPledon():
global ESPledstate
ledpin.value(0) # LED ON
ESPledstate = True
def ESPledoff():
global ESPledstate
ledpin.value(1) # LED OFF
ESPledstate = False
ESPledoff()
2021-01-16 18:57:46 +00:00
def noteledon(notenumber, velocity, r = 128, g=0 , b=64 ):
if notenumber > leftnote-1:
np[int((notenumber-leftnote)*2)] = (velocity*2,g,b)
np[int((notenumber-leftnote)*2)+1] = (velocity*2,g,b)
np.write()
motherosc.send('/noteledon', notenumber)
else:
motherosc.send('/notetoolow', notenumber)
def noteledoff(notenumber):
if notenumber > leftnote-1:
np[int((notenumber-leftnote)*2)] = (0, 0, 0)
np[int((notenumber-leftnote)*2)+1] = (0, 0, 0)
np.write()
motherosc.send('/noteledoff', notenumber)
else:
motherosc.send('/notetoolow', notenumber)
2021-01-06 02:32:39 +00:00
def npcycle():
for i in range(2 * n):
for j in range(n):
np[j] = (0, 0, 0)
np[i % n] = (126, 20, 126)
np.write()
2021-01-16 18:57:46 +00:00
time.sleep_ms(15)
2021-01-06 02:32:39 +00:00
'''
def npcycle2(r, g, b, wait):
print(r,g,b,wait)
for i in range(2 * n):
for j in range(n):
np[j] = (0, 0, 0)
np[int(i % n)] = (r, g, b)
#print(np)
np.write()
print()
for count in range(n):
print(i%n)
time.sleep(wait)
'''
def npcolor(r, g, b):
for i in range(n):
np[i] = (r, g, b)
np.write()
def npbounce():
for i in range(4 * n):
for j in range(n):
np[j] = (0, 0, 128)
if (i // n) % 2 == 0:
np[i % n] = (0, 0, 0)
else:
np[n - 1 - (i % n)] = (0, 0, 0)
np.write()
time.sleep_ms(60)
def OSCHandler(t, msg):
2021-01-16 18:57:46 +00:00
#print()
2021-01-06 02:32:39 +00:00
print("OSCHandler")
2021-01-16 18:57:46 +00:00
'''
2021-01-06 02:32:39 +00:00
print("OSC address:", msg[0]) # /on
print("Type tags:", msg[1]) # 'i'
print("Arguments:", msg[2]) # (1,)
print()
2021-01-16 18:57:46 +00:00
'''
2021-01-06 02:32:39 +00:00
# /noteon midichannel note velocity
if msg[0] == "/noteon":
2021-01-16 18:57:46 +00:00
print("ESP : NOTEON channel", msg[2][0], "note",msg[2][1], "velocity", msg[2][2])
noteledon(int(msg[2][1]), int(msg[2][2]))
#np[int(msg[2][1]%n)] = (255, 0, 153)
#np.write()
2021-01-06 02:32:39 +00:00
# /noteoff midichannel note
if msg[0] =='/noteoff':
2021-01-16 18:57:46 +00:00
print("ESP : NOTEOFF channel", msg[2][0], "note",msg[2][1])
noteledoff(int(msg[2][1]))
#np.write()
2021-01-06 02:32:39 +00:00
def handle_rawosc(data, src, strict=False):
try:
head, _ = split_oscstr(data, 0)
2021-01-16 18:57:46 +00:00
#if __debug__: print("head", head)
2021-01-06 02:32:39 +00:00
if head.startswith('/'):
messages = [(-1, parse_message(data, strict))]
elif head == '#bundle':
messages = parse_bundle(data, strict)
except Exception as exc:
if __debug__:
pass
return
try:
for timetag, (oscaddr, tags, args) in messages:
2021-01-16 18:57:46 +00:00
'''
2021-01-06 02:32:39 +00:00
if __debug__:
print("OSC address: %s" % oscaddr)
print("OSC type tags: %r" % tags)
print("OSC arguments: %r" % (args,))
2021-01-16 18:57:46 +00:00
'''
2021-01-06 02:32:39 +00:00
print("Dispatching", timetag, (oscaddr, tags, args, src))
OSCHandler(timetag, (oscaddr, tags, args, src))
except Exception as exc:
print("Exception in OSC handler: %s", exc)
def run_server(saddr, port):
2021-01-16 18:57:46 +00:00
motherosc.send('/runningserver', 1)
2021-01-06 02:32:39 +00:00
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if __debug__: print("Created OSC UDP server socket.")
sock.bind((saddr, port))
2021-01-16 18:57:46 +00:00
print("Listening for OSC messages on", saddr, port)
2021-01-06 02:32:39 +00:00
try:
while True:
data, caddr = sock.recvfrom(MAX_DGRAM_SIZE)
if __debug__: print("Server received", len(data), "bytes from",caddr)
handle_rawosc(data, caddr)
print()
finally:
sock.close()
print("Bye!")
def main(ip):
#import time
ESPledon()
print("Ledserver main IP",ip)
time.sleep(1)
npcolor(0,0,0)
#npcycle(255, 0, 0, 2)
#npbounce()
npcycle()
npcolor(0,0,0)
start = time.time()
try:
run_server("0.0.0.0", int(netconf.OSCin))
except KeyboardInterrupt:
pass
ESPledoff()
if __name__ == '__main__':
main(0)