Skip to content

Can only send after message received? #97

@pixelpusher

Description

@pixelpusher

Hi, I'm trying to do something simple. I'd like to constantly send OSC messages over serial, and also sometimes receive them. I've found that I can send messages constantly (as fast as possible) as long as I don't also receive them. The Ardruino code below illustrates this problem. It will only send back a response via serial when a response is received via serial, when it should be constantly sending messages:

#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCBoards.h>

#ifdef BOARD_HAS_USB_SERIAL
#include <SLIPEncodedUSBSerial.h>
SLIPEncodedUSBSerial SLIPSerial( thisBoardsSerialUSB );
#else
#include <SLIPEncodedSerial.h>
SLIPEncodedSerial SLIPSerial(Serial1);
#endif

#define UPDATE_INTERVAL_MSEC 100

int32_t next_time;
uint32_t counter = 0;
boolean LEDState = false; // off

OSCBundle bundleOut;

void setup()
{
  pinMode(13, OUTPUT); // LED

  // begin SLIPSerial just like Serial
  SLIPSerial.begin(115200);
  while (!Serial)
    continue;

  next_time = millis() + UPDATE_INTERVAL_MSEC;
}


void LEDcontrol(OSCMessage &msg)
{
  if (msg.isInt(0))
  {
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, (msg.getInt(0) > 0) ? HIGH : LOW);
  }
  else if (msg.isString(0))
  {
    int length = msg.getDataLength(0);
    if (length < 5)
    {
      char str[length];
      msg.getString(0, str, length);
      if ((strcmp("on", str) == 0) || (strcmp("On", str) == 0))
      {
        pinMode(LED_BUILTIN, OUTPUT);
        digitalWrite(LED_BUILTIN, HIGH);
      }
      else if ((strcmp("Of", str) == 0) || (strcmp("off", str) == 0))
      {
        pinMode(LED_BUILTIN, OUTPUT);
        digitalWrite(LED_BUILTIN, LOW);
      }
    }
  }
  else {
    LEDState = !LEDState;
    digitalWrite(13, LEDState);
  }
}



void loop()
{
  OSCBundle bundleIN;
  int size;

  while (!SLIPSerial.endofPacket())
    if ( (size = SLIPSerial.available()) > 0)
    {
      while (size--)
        bundleIN.fill(SLIPSerial.read());
    }

  if (!bundleIN.hasError()) {
    bundleIN.route("/OnMouseDownX", LEDcontrol);
  }
  bundleIN.empty();

  int32_t now = millis();

  if ((int32_t)(next_time - now) <= 0)
  {
    next_time += UPDATE_INTERVAL_MSEC;

    counter = (counter + 1) % 100;

    // the message wants an OSC address as first argument
    bundleOut.add("/CubeX").add((float)counter / 50);
  }

  SLIPSerial.beginPacket();
  bundleOut.send(SLIPSerial);       // send the bytes to the SLIP stream
  SLIPSerial.endPacket();     // mark the end of the OSC Packet
  bundleOut.empty();                // free space occupied by message
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions