/*
0.4b
demo for rw
based on 0.4a
*/
#include <Bounce2.h>
#include "libFsmA.h"
#include "misc.h"
#include "z_macros.h"
#include "stFunctions.h"
// all buttons
const uint8_t btnPins[] = { 2, 3, 4, 5 };
Bounce *buttons = new Bounce[NUMELEMENTS(btnPins)];
// ledPin
const uint8_t ledPin = LED_BUILTIN;
// print output with millis()
const bool enableMillis = true;
// debug fsm
const bool debugFsm = true;
enum StateNames : uint16_t
{
stOff,
stBlink,
stOn,
};
Timer blinkTimer; // timer for blink
Timer stateTimer; // timer for states (how long do we stay in a state)
// create instance of Led
Led led(ledPin, LOW);
// transitions from state Off
Transition stOff_Transitions[] = {
{ stBlink, stOff_stBlink },
};
// transitions from state Blink
Transition stBlink_Transitions[] = {
{ stOff, stBlink_stOff },
{ stOn, stBlink_stOn },
};
// transitions from state On
Transition stOn_Transitions[] = {
{ stOff, stOn_stOff_ButtonAndTimer },
};
// All states
State states[] = {
{ stOff, "Off", stOff_onEnter, nullptr, stOff_onLeave, stOff_Transitions, NUMELEMENTS(stOff_Transitions) },
{ stBlink, "Blink", stBlink_onEnter, stBlink_onState, stBlink_onLeave, stBlink_Transitions, NUMELEMENTS(stBlink_Transitions) },
{ stOn, "stOn", stOn_onEnter, nullptr, stOn_onLeave, stOn_Transitions, NUMELEMENTS(stOn_Transitions) },
};
// the fsm
Fsm fsm = { states, NUMELEMENTS(states), stOff };
void setup()
{
Serial.begin(115200);
Serial.println(F("FSM demo 0.4b"));
// configure buttons
for (uint8_t cnt = 0; cnt < NUMELEMENTS(btnPins); cnt++)
{
buttons[cnt].attach(btnPins[cnt], INPUT_PULLUP); // setup the bounce instance for the current button
buttons[cnt].interval(25); // interval in ms
}
// configure LED
led.begin();
// validate fsm
if (fsm.validate() == false)
{
Serial.println(F("Fix your code"));
for (;;) {}
}
else
{
Serial.println("Succesfully validated");
}
// enable/disable printing of millis() in the fsm
fsm.enableMillis = enableMillis;
// enable/disable debugging of fsm
fsm.debug = debugFsm;
// print fsm info
//fsm.print();
}
void loop()
{
fsm.run();
return;
}