// https://wokwi.com/projects/409963746798376961
// a modificaton of:
// https://wokwi.com/projects/409963275966630913
// to use a post-fix/Reverse Polish Notation protocol
// Code from https://www.gammon.com.au/forum/?id=11425&reply=1#reply1
// Other simulations https://forum.arduino.cc/t/wokwi-simulations-for-arduino-built-in-examples/1304754
// See also Nick Gammon's Serial examples
// Serial Example 1: https://wokwi.com/projects/409963075177910273
// Serial Example 2: https://wokwi.com/projects/409963275966630913
/*
Non-Blocking Serial Parsing, RPN-style with switch-case
Another way of processing incoming data without blocking, is to
use single letter commands in a Post-fix/Reverse Polish Notation
form.
Effectively this means looking at each byte in the
input stream, and handling it with the current state.
As an example, say you had this coming into the serial port:
4500R 80S 3G
Where R is RPM, S is speed, and G is the gear setting, using
the current accumulated number.
The code below acts on the bytes as they come in. When it gets
a letter "R", "S" or "G" it copies the number ot the register and
acts. Otherwise it processes incoming digits by multiplying the
previous result by 10, and adding in the new one. Whitespace
is skipped.
This has the advantage of handling long messages without even
needing any buffer, thus saving RAM. You can also process message
as soon as the state changes, rather than waiting for end-of-line.
*/
// Example state machine reading serial input
// Author: DaveX
// Date: 2024-19-24
// Modified from https://www.gammon.com.au/forum/?id=11425&reply=1#reply1
// current partial number
unsigned int currentValue;
unsigned int Rvalue, Svalue, Gvalue;
void processRPM (const unsigned int value)
{
// do something with RPM
Serial.print ("RPM = ");
Serial.println (value);
Rvalue = value;
} // end of processRPM
void processSpeed (const unsigned int value)
{
// do something with speed
Serial.print ("Speed = ");
Serial.println (value);
Svalue = value;
} // end of processSpeed
void processGear (const unsigned int value)
{
// do something with gear
Serial.print ("Gear = ");
Serial.println (value);
Gvalue = value;
} // end of processGear
void processLine ()
{
// do something with line
char buff[50];
snprintf(buff, 50, "Settings are %uRPM %uSpeed %uGear ", Rvalue, Svalue, Gvalue);
Serial.println(buff);
} // end of processGear
void clearValue(void) {
currentValue = 0;
}
void processIncomingByte (const byte c)
{
switch (c)
{
case '0' ... '9':
currentValue *= 10;
currentValue += c - '0';
break;
case 'R':
processRPM (currentValue);
clearValue();
break;
case 'S':
processSpeed (currentValue);
clearValue();
break;
case 'G':
processGear (currentValue);
clearValue();
break;
case ' ':
case '\t':
break;
case '\n':
processLine();
break;
default:
Serial.print("Character '");
Serial.print(char(c));
Serial.println("' is unknown.");
break;
} // end of switch on incoming byte
} // end of processIncomingByte
// ########### Arduino basics: setup and loop
void setup ()
{
Serial.begin (115200);
Serial.print("enter numbers or commands 0-9RSG:\n");
} // end of setup
void loop ()
{
while (Serial.available ())
processIncomingByte (Serial.read ());
// do other stuff in loop as required
} // end of loop