Hello, I'm still quite new to all of this and wondered if anyone wanted to help refine my code to make it better, e.g. I use both *char and String which seems wrong to me but that is where I am with coding. Also which variables should I make static or global, or any other optimisations.
char rx_byte = 0;
String rx_str = "";
String rx_str_prompt = "OK";
void setup() {
// SERIAL: init
//Serial.begin(9600);
Serial.begin(115200);
while (!Serial)
{
; // SERIAL: wait USB port
}
Serial.println("init");
// SERIAL: send ready prompt
Serial.println(rx_str_prompt);
}
void loop() {
cmd_main();
}
void cmd_main()
{
if (Serial.available() > 0)
{
rx_byte = Serial.read();
if (rx_byte != '\n')
{
rx_str += rx_byte;
}
else
{
// PARSE
rx_str.trim();
if(rx_str.length() > 0)
{
cmd_parse();
}
// RESET
rx_str = "";
Serial.println(rx_str_prompt);
}
}
}
void cmd_parse()
{
const int max_index = 4;
char *strings[max_index];
byte index = 0;
const char s[2] = " ";
char *token;
// COPY
char str[rx_str.length()+1];
rx_str.toCharArray(str, rx_str.length()+1);
// SPLIT
token = strtok(str, s);
while( token != NULL && (index <= max_index-1) ) {
strings[index] = token;
index++;
token = strtok(NULL, s);
}
/*
// DUMP
for(int n = 0; n < index; n++)
{
Serial.println(strings[n]);
}
*/
// PARSE
if(strcmp(strings[0], "set") == 0)
{
}
else
if(strcmp(strings[0], "home") == 0)
{
Serial.println("house");
if(index >= 2){
//int value2 = rx_str.toInt(); // string to int
int value = atoi(strings[1]); /// char* to int
Serial.println(value);
}
}
else
{
Serial.print("Unknown command: ");
Serial.println(strings[0]);
}
}
Thankyou