Go Down

Topic: String to function: any best practices? (Read 1 time) previous topic - next topic

AWOL

Quote
No faster than the above approach, either way you have compare strings and call functions.

A linear search will (on average, for random data) always be longer than a binary search - sort those keys!
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

majenko


Quote
No faster than the above approach, either way you have compare strings and call functions.

A linear search will (on average, for random data) always be longer than a binary search - sort those keys!


Absolutely!  Order matters.  For the stuff I posted there it has to be in "mask" order to work properly.  I would suggest you build your array in descending order of popularity of the commands sent - the most common ones at the top.
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

supermaggel

#7
Jun 13, 2012, 04:40 pm Last Edit: Jun 13, 2012, 04:54 pm by supermaggel Reason: 1
Okay, i now have this:


// command structure (function name, parameter, actual function)
struct command {
        char* functionName;
        void (*function)(String, String);
};

// array of command structures
struct command commands[] = {
  {"GET_Speed", GET_Speed},
  {"SET_Mode", SET_Mode}
};


The rest now looks like this:

  // extracting function and parameters (yeah ugly i know).
  String data = String(dataString);
  String command;
  String param1;
  String param2;
 
  command = data.substring(0, data.indexOf(' '));
  param1 = data.substring(data.indexOf(' '));
  param2 = data.substring(data.lastIndexOf(' '));


struct command *scan;

 for(scan=commands; scan->function; scan++)
  {
    if(command == scan->functionName)
    {
      scan->function(param1, param2);
      break;
    }
  }

void GET_Speed(String param1, String param2) {
  Serial.println("function GET_Speed executing with params: ");
  Serial.println(param1);
  Serial.println("\t and ");
  Serial.println(param2);
  Serial.println("\n\n");
}

void SET_Mode(String param1, String param2) {
  Serial.println("function SET_Mode executing with params: ");
  Serial.println(param1);
  Serial.println("\t and ");
  Serial.println(param2);
  Serial.println("\n\n");
}

As you can see i still use String instead of char*, but that's because i don't know C++ and how to do subString(), indexOf() and lastIndexOf() on char (assuming that's even possible)

It compiles, let's see if it works. Thanks for all your help! :)

majenko

It won't work :P

First, you need to terminate your array.  Have an entry at the very end of all 0, then you look for that 0 to know when you have reached the end of your array.

Secondly, you're not comparing the string contents, but the string addresses.

You need to use
Code: [Select]

if(!strcmp(command, scan->functionName))
{
   ...
}


instead of if(command == scan->functionName)
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

supermaggel

Ah thanks, i've added the {0,0} struct.

As everything is now a String, i'm using the arduino String compareTo function:

if(command.compareTo(scan->functionName))

shit yeah, it even works:

http://s17.postimage.org/vfiukqj7j/Screen_Shot_2012_06_13_at_17_17_36_PM.png

Go Up