Skip to content

Instantly share code, notes, and snippets.

@jedgarpark
Created January 16, 2024 22:53
Show Gist options
  • Save jedgarpark/1b7785b26bd7ed35506d6336cdce088d to your computer and use it in GitHub Desktop.
Save jedgarpark/1b7785b26bd7ed35506d6336cdce088d to your computer and use it in GitHub Desktop.
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
#
# SPDX-License-Identifier: Unlicense
"""Demonstrate MicroOSC library in CircuitPython, assumes native `wifi` support"""
import time
import os
import wifi
import socketpool
import microosc
UDP_HOST = "192.168.1.58"
UDP_PORT = 8000
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
print("connecting to WiFi", ssid)
wifi.radio.connect(ssid, password)
print("my ip address:", wifi.radio.ipv4_address)
socket_pool = socketpool.SocketPool(wifi.radio)
def fader_handler(msg):
"""Used to handle 'fader' OscMsgs, printing it as a '*' text progress bar
:param OscMsg msg: message with one required float32 value
"""
print("\t\t", msg.addr, "*" * int(20 * msg.args[0])) # make a little bar chart
def toggle_handler(msg):
addr = msg.addr
tog_num = int(addr.replace('/1/toggle',''))
if msg.args[0] is 1.0:
print(tog_num, "is ON")
else:
print(tog_num, "is off")
dispatch_map = {
"/": lambda msg: print("msg:", msg.addr, msg.args), # prints all messages
"/1/fader": fader_handler,
"/1/toggle": toggle_handler,
"/filter1": fader_handler,
}
osc_server = microosc.OSCServer(socket_pool, UDP_HOST, UDP_PORT, dispatch_map)
print("MicroOSC server started on ", UDP_HOST, UDP_PORT)
last_time = time.monotonic()
while True:
osc_server.poll()
if time.monotonic() - last_time > 1.0:
last_time = time.monotonic()
# print(f"waiting {last_time:.2f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment