I have some code on Arduino (so, written in CC++) that receives a String through the UART terminal, reads the String, then decides what String to print back and how many things to print depending on the data.
I've used an if-else stack to decide on what to print back. Something like this:
if (cmdtype == "VALCN" && canSend == true) { //canSend is a flag from above.
cmdparam = "00000000";
rtnProtocols(cmdtype, cmdparam);
}
else if (cmdtype == "FIRMV" && canSend == true) {
cmdparam = "00000000"; //See desktop code for reason why cmdparam is what it is.
rtnProtocols(cmdtype, cmdparam);
}
else if (cmdtype == "RQSPC" && canSend == true) { //RQSPC is a command
//Need to get serial info, date, end of device info
cmdtype = "GVSPC"; //Send device info signal
cmdparam = "00101201"; //cmdparam is serial number
rtnProtocols(cmdtype, cmdparam);
cmdparam = "30082022"; //cmdparam is date
rtnProtocols(cmdtype, cmdparam);
cmdtype = "ENSPC"; //Send device info signal
cmdparam = "00000000"; //cmdparam is date
rtnProtocols(cmdtype, cmdparam);
}
There are a lot of variations to the receivable String, so I'm foreseeing this if-else stack spiralling out of control. I am wondering whether there is a way of doing this without using an if-else stack. Or even if there is a way of doing this without conditionals, since all I'm doing with the branches is having it run the same function with different inputs, different times.
I've considered a switch-case, but wasn't able to do so since switch-case can't take Strings. Please let me know if this question is not suitable here, or if more information is needed.