Simple test CircuitPython

Create a simple CircuitPython OSC UDP Server to receive OSC Messages

examples/microosc_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6"""Demonstrate MicroOSC library in CircuitPython, assumes native `wifi` support"""
 7
 8import time
 9import os
10import wifi
11import socketpool
12
13import microosc
14
15UDP_HOST = ""  # set to empty string to auto-set unicast UDP
16# UDP_HOST = "224.0.0.1"  # multicast UDP
17UDP_PORT = 5000
18
19ssid = os.getenv("CIRCUITPY_WIFI_SSID")
20password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
21
22print("connecting to WiFi", ssid)
23wifi.radio.connect(ssid, password)
24print("my ip address:", wifi.radio.ipv4_address)
25
26socket_pool = socketpool.SocketPool(wifi.radio)
27
28
29def fader_handler(msg):
30    """Used to handle 'fader' OscMsgs, printing it as a '*' text progress bar
31    :param OscMsg msg: message with one required float32 value
32    """
33    print(msg.addr, "*" * int(20 * msg.args[0]))  # make a little bar chart
34
35
36dispatch_map = {
37    "/": lambda msg: print("\t\tmsg:", msg.addr, msg.args),  # prints all messages
38    "/1/fader": fader_handler,
39    "/filter1": fader_handler,
40}
41
42if not UDP_HOST:
43    # fall back to non-multicast UDP on my IP addr
44    UDP_HOST = str(wifi.radio.ipv4_address)
45
46osc_server = microosc.OSCServer(socket_pool, UDP_HOST, UDP_PORT, dispatch_map)
47
48print("MicroOSC server started on ", UDP_HOST, UDP_PORT)
49
50last_time = time.monotonic()
51
52while True:
53    osc_server.poll()
54
55    if time.monotonic() - last_time > 1.0:
56        last_time = time.monotonic()
57        print(f"waiting {last_time:.2f}")

Simple test CPython

Create a simple desktop Python (CPython) OSC UDP Server to receive OSC Messages

examples/microosc_simpletest_cpython.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6"""Demonstrate MicroOSC library in desktop Python (CPython)"""
 7
 8import sys
 9import time
10import socket
11
12import microosc
13
14if len(sys.argv) < 2 or len(sys.argv) > 3:
15    print("microosc_simpletest_cpython.py <host> <port>")
16    sys.exit(0)
17
18UDP_HOST = sys.argv[1]
19UDP_PORT = int(sys.argv[2])
20
21last_velocity = 0
22
23
24def note_handler(msg):
25    """Used to handle 'Note1' and 'Velocity1' OscMsgs from Ableton Live.
26    Live's "OscSend" plugin sends two OSC Packets for every MIDI note.
27    This function reconctructs that into a single print().
28    :param OscMsg msg: message with one required int32 value
29    """
30    global last_velocity  # pylint: disable=global-statement
31    if msg.addr == "/Note1":
32        if last_velocity != 0:
33            print("NOTE ON ", msg.args[0], last_velocity)
34        else:
35            print("NOTE OFF", msg.args[0], last_velocity)
36    elif msg.addr == "/Velocity1":
37        last_velocity = msg.args[0]
38
39
40def fader_handler(msg):
41    """Used to handle 'fader' OscMsgs, printing it as a '*' text progress bar
42    :param OscMsg msg: message with one required float32 value
43    """
44    print(msg.addr, "*" * int(20 * msg.args[0]))  # make a little bar chart
45
46
47dispatch_map = {
48    # matches all messages
49    "/": lambda msg: print(
50        "\tmsg:", msg.addr, msg.args, "types:" + ",".join(msg.types)
51    ),
52    # maches how Live's OSC MIDI Send plugin works
53    "/Note1": note_handler,
54    "/Velocity1": note_handler,
55    # /1/fader3 matches how TouchOSC sends faders ,"/1" is screen, "fader3" is 3rd fader
56    "/1/fader": fader_handler,
57    "/filter1": fader_handler,
58}
59
60osc_server = microosc.OSCServer(socket, UDP_HOST, UDP_PORT, dispatch_map)
61
62print("MicroOSC server started on ", UDP_HOST, UDP_PORT)
63
64last_time = time.monotonic()
65
66while True:
67    osc_server.poll()
68
69    if time.monotonic() - last_time > 5.0:
70        last_time = time.monotonic()
71        print(f"waiting {last_time:.2f}")

Simple send

Create a simple CircuitPython OSC UDP Client to send OSC Messages

examples/microosc_simplesend.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6"""Demonstrate MicroOSC library in CircuitPython, assumes native `wifi` support"""
 7
 8import time
 9import os
10import wifi
11import socketpool
12
13import microosc
14
15UDP_HOST = ""  # set to empty string to auto-set unicast UDP
16# UDP_HOST = "224.0.0.1"  # multicast UDP
17UDP_PORT = 5000
18
19ssid = os.getenv("CIRCUITPY_WIFI_SSID")
20password = os.getenv("CIRCUITPY_WIFI_PASSWORD")
21
22print("connecting to WiFi", ssid)
23wifi.radio.connect(ssid, password)
24print("my ip address:", wifi.radio.ipv4_address)
25
26socket_pool = socketpool.SocketPool(wifi.radio)
27
28if not UDP_HOST:
29    # fall back to non-multicast UDP on my IP addr
30    UDP_HOST = str(wifi.radio.ipv4_address)
31
32osc_client = microosc.OSCClient(socket_pool, "224.0.0.1", 5000)
33
34# two floats
35msg = microosc.OscMsg( "/1/xy1", [0.99, 0.3, ], ("f", "f", ) )  # fmt: skip
36
37i = 100
38while True:
39    msg.args[0] = i * 0.01  # move the value a bit
40
41    pkt_size = osc_client.send(msg)
42    print("packet_size sent:", pkt_size, "msg:", msg)
43
44    time.sleep(1)
45    i -= 1

Simple send CPython

Create a simple desktop Python (CPython) OSC UDP Client to send OSC Messages

examples/microosc_simplesend_cpython.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2023 Tod Kurt
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6"""Demonstrate MicroOSC library in CPython"""
 7
 8import time
 9import socket
10
11import microosc
12
13# osc_client = microosc.OSCClient(socket, "224.0.0.1", 5000)  # Multicast UDP
14osc_client = microosc.OSCClient(socket, "127.0.0.1", 5000)  # regular UDP
15
16# send two floats
17# msg = microosc.OscMsg("/1/xy1", [0.99, 0.3], ("f", "f")) # fmt-skip
18# send two ints
19# msg = microosc.OscMsg("/1/xy1", [3, 3], ("i", "i")) # fmt-skip
20# send four ints
21msg = microosc.OscMsg("/1/xyzw", [3, 4, 5, 6], ("i", "i", "i", "i"))  # fmt-skip
22# send alternating floats and ints
23# msg = microosc.OscMsg("/1/xyzw", [3, 4, 5, 6], ("f", "i", "f", "i")) # fmt-skip
24# send int and string
25# msg = microosc.OscMsg("/1/message", [123, "hello there"], ("i", "s")) # fmt-skip
26
27i = 100
28while True:
29    # msg.args[0] = i * 0.01  # move the value a bit
30    msg.args[0] = i * 1
31
32    pkt_size = osc_client.send(msg)
33    print("packet_size sent:", pkt_size, "msg:", msg)
34
35    time.sleep(1)
36    i -= 1