I have a function that I wrote to hide all this. Basically, while there's data available, I read it all in. Then, wait 1ms, check to see if there's still more data, and if there is, continue reading, and I do this until I go for 1ms without any data coming in. This seems to be working pretty well for my setup. Then, I call this function from loop() on every cycle through.
My only issue now is that sending OSC data back to TouchOSC isn't working, and i'd like to be able to report the state of various things in my digix controlled environment back to TouchOSC.
(Also - the arduino OSC code appears to have a bug in it where if you read in a bundle that's really a single message, it will strip the leading / off of the path - I have it fixed locally, but I'm still shaking it out before I submit a pull request back to the maintainers for it).
void loopOSC() {
int size;
bool data = false;
OSCBundle bundleIn;
while( (size=wifi.available()) > 0) {
data =true;
while(size--) {
int x = wifi.read();
bundleIn.fill(x);
}
delay(1);
}
if(!bundleIn.hasError() && bundleIn.size() > 0) {
bundleIn.route("/1", routeAnalog);
}
if(bundleOut.size() > 0) {
bundleOut.send(wifi);
bundleOut.empty();
}
}