In uno
void serialEvent() {
static int cmd=0;
static String lstr="";
static String subCmd="";
while(Serial.available() > 0) {
char inChar=(char)Serial.read();
if (cmdIsSet) {
if ((inChar == '\n') || (inChar == '\r')) {
// cmd is set and subCmd is populated fully.
} else
subCmd+=inChar;
} else
lstr+=inChar;
if (inChar == ':') {
Serial.print("Received from esp - ");
if (lstr.equalsIgnoreCase("MtrSpeed:"))
cmd=CMD_MtrSpeed;
if(cmd != 0)
cmdIsSet=true;
} else if ((inChar == '\n') || (inChar == '\r')) {
if(cmdIsSet) {
switch(cmd) {
case CMD_MtrSpeed:
{
int sped=subCmd.toInt();
analogWrite(mtrPin, sped);
String resp=" MtrSpeed:"+subCmd;
Serial.println(resp);
loopSpeedSent=0;
lstr="";
subCmd="";
cmdIsSet=0;
cmd=0;
break;
}
}
}
In esp
void findConfInSerial() {
static String subCmd="";
static int cmd=0;
bool cmdIsSet=false;
static String lstr="";
while(Serial.available() > 0) {
char inChar=(char)Serial.read();
if (cmdIsSet) {
if ((inChar == '\n') || (inChar == '\r')) {
Log+=lstr+" : "+subCmd;
} else
subCmd+=inChar;
if (inChar == ':') {
Serial.print(" Received colon ");
Serial.println(lstr);
if (lstr.equalsIgnoreCase( "MtrSpeed:")) {
String spd=Serial.readStringUntil('\n');
respondSpeedReq(spd);
}
}
The snippet is stripped out of my original code, since this is the section I am trying to unit test. Other wise the combined code runs to few hundreds of lines.