void setup() {
Serial.begin(115200);
Serial.println("hi Mom!");
}
void loop()
{
logicFSM(); // crank it along
// for now only a .. z make sense (no filter or tolower or...)
char theChar;
if (Serial.available()) {
theChar = Serial.read();
if ('a' <= theChar && theChar <= 'z') {
// Serial.println(theChar);
logicFSM(theChar);
}
}
logicFSM(0); // move it along.
delay(777); // sue me. this is a work in progress
}
enum logic {IDLE, AWAIT, ERROR, FINI};
enum display {OFF, GOOD, BAD, SUCCESS, WOW};
const char *logicTags[] = {"Idle", "Await", "Error", "Fini"};
void logicFSM(char theChar)
{
// byte state = IDLE;
static byte state = IDLE;
static unsigned long timer; // general use timer
static char previous;
static char undisposedChar;
if (!theChar && undisposedChar) {
theChar = undisposedChar;
undisposedChar = 0;
}
if (theChar) {
Serial.print("= ");
Serial.println(theChar);
}
{
static byte lastPrinted = -1;
if (lastPrinted != state) {
Serial.print("logic: ");
Serial.println(logicTags[state]);
lastPrinted = state;
}
}
switch (state) {
case IDLE :
// Serial.print(" IDLE char "); Serial.println(theChar ? theChar : '.');
if (theChar)
if (theChar != 'a') {
undisposedChar = theChar; // kludge
previous = theChar; // kludge
state = AWAIT; // make this fall through
}
else state = FINI;
break;
case AWAIT :
if (theChar)
if (theChar == previous) {
Serial.println(" !");
if (theChar == 'a')
state = FINI;
else
previous--;
}
else {
Serial.println(" X");
state = ERROR;
}
break;
// harsh punishment for now
case ERROR :
state = IDLE;
break;
case FINI :
Serial.println("ding ding!");
state = IDLE;
break;
default :
Serial.println("Rotten Denmark!");
for (; ; );
}
}
// service or just call logicFSM(0)
void logicFSM()
{
logicFSM(0);
}
/*
void logicFSM(int x)
{
if (x > 0)
Serial.println(x);
}
void logicFSM()
{
static int counter;
if (++counter >= 10) {
logicFSM(100);
counter = 0;
}
}
*/