Skip to main content
added 179 characters in body
Source Link
nkint
  • 461
  • 3
  • 9
  • 22
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 upupValueExtraction(OSCMessagechar &msg*ca, int addrOffset) {
  static boolean flag = false; // because of TouchOSC 
                               // sends 2 messages 
                               // for each Push Button
 &num_motor, ifint (!flag&num_step) {
    Serial.print("route: up "); Serial.println(addrOffset);

    char ca[200];
    msg.getAddress(ca, addrOffset+1);
    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());

    ifnum_motor = num_motor_s.toInt();
    num_step = num_step_s.toInt()==-1;
}


void up(OSCMessage &msg, int addrOffset) {
  static boolean flag = false;
  motors[num_motor_sif (!flag) {
    Serial.toIntprint("route: up ")]; Serial.moveprintln(defaultsaddrOffset); 

    }char elseca[200];
    msg.getAddress(ca, addrOffset+1);
    
    int num_motor, num_step;
    upValueExtraction(ca, num_motor, num_step);
    
    if(num_step==-1) {
      num_step = motors[num_motor_sdefaul_steps;
    }
    
    Serial.toIntprint("Have to move motor n ")];
    Serial.moveprint(num_step_snum_motor);
    Serial.toIntprint()" of step ");
    }
Serial.println(num_step);
  }
  flag = !flag;
}
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 up(OSCMessage &msg, int addrOffset) {
  static boolean flag = false; // because of TouchOSC 
                               // sends 2 messages 
                               // for each Push Button
  if (!flag) {
    Serial.print("route: up "); Serial.println(addrOffset);

    char ca[200];
    msg.getAddress(ca, addrOffset+1);
    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());

    if(num_step_s.toInt()==-1) {
        motors[num_motor_s.toInt()].move(defaults);
    } else {
        motors[num_motor_s.toInt()].move(num_step_s.toInt());
    }

  }
  flag = !flag;
}
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;
}
Source Link
nkint
  • 461
  • 3
  • 9
  • 22

OSC parsing code. Can it be optimised?

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 up(OSCMessage &msg, int addrOffset) {
  static boolean flag = false; // because of TouchOSC 
                               // sends 2 messages 
                               // for each Push Button
  if (!flag) {
    Serial.print("route: up "); Serial.println(addrOffset);

    char ca[200];
    msg.getAddress(ca, addrOffset+1);
    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());

    if(num_step_s.toInt()==-1) {
        motors[num_motor_s.toInt()].move(defaults);
    } else {
        motors[num_motor_s.toInt()].move(num_step_s.toInt());
    }

  }
  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.