I'm using this OSC library and TouchOSC to controls some motors. Right now I'm receiving those two kind of messages:
/u/1 // move motor 1 of default steps
/u/2 // move motor 2 of default steps
/u/1/5 // move motor 1 of 5 steps
/u/3/25 // move motor 1 of 25 steps
and so on.
Right now I have this piece of code:
void loop() {
OSCMsgReceive();
}
void OSCMsgReceive() {
OSCMessage msgIN;
int size;
if ((size = Udp.parsePacket()) > 0) {
//Serial.println("incoming message.. ");
while (size--) {
byte n = Udp.read();
msgIN.fill(n);
Serial.print((char)n);
}
Serial.println();
if (!msgIN.hasError()) {
msgIN.route("/u", up);
}
}
}
void upValueExtraction(char *ca, int &num_motor, int &num_step) {
String s = String(ca);
String num_motor_s = "";
String num_step_s = "-1";
int n = 0;
for(int i=0; i<s.length(); i++) {
char c = s[i];
if(c=='\\') {
n++;
}
if(n==0) num_motor_s += c;
if(n==1) num_step_s += c;
}
Serial.print("num motor: "); Serial.println(num_motor_s.toInt());
Serial.print("num step: "); Serial.println(num_step_s.toInt());
num_motor = num_motor_s.toInt();
num_step = num_step_s.toInt();
}
void up(OSCMessage &msg, int addrOffset) {
static boolean flag = false;
if (!flag) {
Serial.print("route: up "); Serial.println(addrOffset);
char ca[200];
msg.getAddress(ca, addrOffset+1);
int num_motor, num_step;
upValueExtraction(ca, num_motor, num_step);
if(num_step==-1) {
num_step = defaul_steps;
}
Serial.print("Have to move motor n ");
Serial.print(num_motor);
Serial.print(" of step ");
Serial.println(num_step);
}
flag = !flag;
}
Even if it is working and even if I'm using an Arduino DUE (much more memory then the UNO) I think it can be optimised.
Does it contains some leeks? How can I better use the OSC matching part of the library? Any kind of "embedded c++" guidelines? Can I do everything inline without a ca
char array and s
String variables? Is better to use Arduino Lib String
class or plain old C str* functions? Any kind of comment?
Thanks in advance.