Matthew,
In the parse function's for() loop - Why should setting 'pString = NULL;' advance to the next field? Shouldn't it be more like 'pString += strlen(pFields[i]);'? Is strtok() supposed to get the address from pString, remember where it's last search ended, and use that when given a NULL pointer?
Is key_mouse.move() supposed to be able to receive string addresses, or do you need to use atoi() as well?
key_mouse.move(atoi(pFields[1]), atoi(pFields[2]));
The NULL Causes it to clear the string for that token which is stored so that it can move onto the next one easily by detecting if it is blank, if the token is blank it is ignored and not added.
The strtok only splits the string into a list of fields which are used to grab the value, its pretty simple.
Check this out: http://mbed.org/compiler/?import=http://mbed.org/users/Elitism/programs/KeyboardMouseSerialTest/mbfof3
Here is the code:
#include "mbed.h"
#include "USBMouseKeyboard.h"
USBMouseKeyboard key_mouse;
Serial pc(USBTX, USBRX);
#define NFIELDS (6)
char* pFields[NFIELDS];
void ParseCommands(char* Buffer, char** pFields, uint32_t numFields, char* delimiter) {
char* pString = Buffer;
char* pField;
for (uint32_t i=0; i<numFields; i++) {
pField = strtok(pString, delimiter);
if (pField != NULL) {
pFields[i] = pField;
} else {
pFields[i] = "";
}
pString = NULL; //parse next
}
if (strcmp("Vector", pFields[0]) == 0) {
int x;
int y;
x = atoi (pFields[1]);
y = atoi (pFields[2]);
key_mouse.move(x,y);
}
if (strcmp("Click", pFields[0]) == 0) {
if (strcmp("LPress", pFields[1]) == 0) {
key_mouse.press(MOUSE_LEFT);
}
if (strcmp("LRelease", pFields[1]) == 0) {
key_mouse.release(MOUSE_LEFT);
}
if (strcmp("LClick", pFields[1]) == 0) {
key_mouse.click(MOUSE_LEFT);
}
if (strcmp("RPress", pFields[1]) == 0) {
key_mouse.press(MOUSE_RIGHT);
}
if (strcmp("RRelease", pFields[1]) == 0) {
key_mouse.release(MOUSE_RIGHT);
}
if (strcmp("RClick", pFields[1]) == 0) {
key_mouse.click(MOUSE_RIGHT);
}
pc.printf("Click Issued\r\n");
}
if (strcmp("Keyboard", pFields[0]) == 0) {
key_mouse.printf(pFields[1]);
pc.printf("Keyboard Issued\r\n");
}
}
int main(int argc, char* argv[]) {
pc.baud(9600);
char buf[64];
while (1) {
//Vector,x,y
//Click,LClick //Example: RClick or LPress or LRelease or RPress or RRelease
//Keyboard,sTrInG //Example: Keyboard,Hello_World //Ignores Spaces and trailing words after a space
pc.scanf("%s", buf);
ParseCommands(buf, pFields, NFIELDS, ",");
}
}
It's mostly complete, the function serial commands are in the Main Event commented out
Can anyone give a newbie some help converting a decimal string (ie "127") to a string in hex formatted string plus additional characters (ie "!a7F")? Is there a conversion method?
And can anyone give me help on passing string to functions?