unsigned long now;
void displayFSM();
void displayFSM(int);
enum display {OFF, GOOD, BAD, SUCCESS, WOW};
const char *displayTags[] = {"Off", "Good", "Bad", "Success", "Wow!"};
void displayFSM()
{
displayFSM(1000);
}
void displayFSM(int command)
{
static byte state = OFF;
static unsigned long timer;
{
static byte lastPrinted = -1;
if (lastPrinted != state) {
Serial.print("display: ");
Serial.println(displayTags[state]);
lastPrinted = state;
}
}
if (command == 1000) {
if (state != OFF && now - timer > 777) {
Serial.println(" and off");
state = OFF;
}
}
if (command != 1000) {
switch (command) {
case OFF :
break;
case GOOD :
case BAD :
case SUCCESS :
case WOW :
Serial.println(" LED made some color");
timer = now;
state = command; // OK, well...
break;
default :
Serial.println("Display FSM Error!\n");
for (; ; );
}
}
}
void setup() {
Serial.begin(115200);
Serial.println("hi Mom!");
}
void loop()
{
now = millis();
{
static unsigned long timer;
if (now - timer > 1000) {
displayFSM(WOW);
timer = now;
}
}
displayFSM();
logicFSM(); // crank it along
// displayFSM(42);
// 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);
}
}
delay(100); // sue me. this is a work in progress
}
enum logic {IDLE, AWAIT, ERROR, FINI};
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);
}
/*
// trying to add the displayFSM
// these tiny stubs work fine
void displayFSM(int x)
{
if (x > 0)
Serial.println(x);
}
void displayFSM()
{
static int counter;
if (++counter >= 10) {
displayFSM(100);
counter = 0;
}
}
*/