I hope someone can lend some insight.
I have an Arduino happily attached to OpenFrameworks. I can send messages back and forth. On startup the Arduino sends OF an identifier, OF sends an acknowledgment back, and the Arduino then starts monitoring some pins.
However, in this monitoring state, the Arduino will BUFFER the osc messages and only send them, one by one, in response to receiving OF messages. I'm sure this problem is on the Arduino side.
Some code (for brevity I'm just including the loop, the SLIPSerial setup is all correct as it works in other cases):
void loop() {
if (!identified) {
// This sends an identification message:
OSCMessage ident("/identification");
ident.add(deviceId);
SLIPSerial.beginPacket();
ident.send(SLIPSerial);
ident.empty();
// This waits for a response:
OSCBundle incoming;
int size;
while (!SLIPSerial.endofPacket())
if ((size = SLIPSerial.available()) > 0) {
while (size--)
incoming.fill(SLIPSerial.read());
}
if(!incoming.hasError()) {
incoming.dispatch("/identificationACK", identificationHandler); // This works, it just toggles an LED if the ack is received, and switches identified to TRUE.
}
} else {
// Read some pins, and if there is a change, send an OSC message:
OSCMessage ping("/some/data");
ping.add(0).add(1);
SLIPSerial.beginPacket();
ping.send(SLIPSerial);
SLIPSerial.endPacket();
ping.empty();
// Also, be listening for incoming messages:
OSCBundle comingIn;
int size;
while(!SLIPSerial.endofPacket())
if ((size = SLIPSerial.available()) > 0) {
while(size--)
comingIn.fill(SLIPSerial.read());
}
if (!comingIn.hasError()) {
comingIn.dispatch("/blink", blinkFunction); // The only messages that are incoming tell the Arduino to blink an LED, which is done in another function
}
}
}
If I am only checking pins and sending messages for changes, it works. It's when I'm also trying to listen for OF messages that the outgoing messages get buffered, and will only fire, one by one, when some message is received from OF. Weird!
I have tried alternating between reading the pins and listening for messages (with a toggle variable back and forth) but that didn't improve anything.
Is there a special technique for sending and listening at the same time? Am I missing something really obvious?
Thanks so much for reading, and for any ideas!