alilia / touchdesigner

0 stars 0 forks source link

Compare latency diff for rgbled between serial (over usb c) and osc (over wifi) irl #11

Open alilia opened 4 weeks ago

alilia commented 3 weeks ago

The sample code I was executing was nowhere near the serial communication. One of the reasons is that it took ~1s te receive all the 768+1 ints for the 8x8 rgb frame.

Was running the following pieces of codes:

def send_large_osc_message(osc_sender, address, data, chunk_size=255):
    osc_sender.sendOSC(address, [255])
    total_chunks = (len(data) + chunk_size - 1) // chunk_size

    for i in range(total_chunks):
        start_index = i * chunk_size
        end_index = min((i + 1) * chunk_size, len(data))
        chunk = data[start_index:end_index]

        # Send each chunk as a separate OSC message
        osc_sender.sendOSC(address, chunk)

osc_sender = op('oscout1')
data = [(i % 255) for i in range(500)]
send_large_osc_message(osc_sender, '/test', data)

and

#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>

#include "RGBLed.h"

const char* ssid = "XXXXXXXXXXXXX";
const char* password = "XXXXXXXXXXXXX";
unsigned int localPort = 7000;
WiFiUDP Udp;

RGBLed matrix(16, 16, 0xFF, 6);

void setup() {
    Serial.begin(115200);

  while (WiFi.status() != WL_CONNECTED) {
    WiFi.begin(ssid, password);
    delay(10000);  // Retry every 10 seconds if not connected
  }

  Udp.begin(localPort);

  Serial.print(WiFi.localIP());
  Serial.print(":");
  Serial.println(localPort);

    matrix.begin();
}

void loop() {
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.print("there is something: ");
    Serial.println(packetSize);

    OSCMessage msg;
    while (packetSize--) {
      msg.fill(Udp.read());
    }

    // if (!msg.hasError()) {
      msg.dispatch("/test", oscReceiveCallback);
    // } else {
    //   Serial.println("Error receiving OSC message");
    // }
  }
}

void oscReceiveCallback(OSCMessage &msg) {
  int numArgs = msg.size();
  Serial.println(numArgs);

  for (int i = 0; i < numArgs; i++) {
    if (msg.isInt(i)) {
      int value = msg.getInt(i);  // Get integer value
      Serial.print(value);
      Serial.print(" ");
      matrix.receiveSerialData(value);
    }
  }

  Serial.println("end");
}