/////////// include FSM lib #include #include ////////////////////////////////////////////////////////////// // STATE MACHINE DEF //------------------------------------------------------------ // list of states enum States { AAAA, BBBB, CCCC }; // definition of the actual machine "LaMachine" inheriting from master (parent) statemachine (defined in lib) class LaMachine : public MachineStates { public: //** super important -- list states in the machine // ('LIST3' for 3 states, 'LIST4' for 4 states, ..., 'LIST8' for 8 states) typedef LIST3(AAAA, BBBB, CCCC) StateList; }; // function definitions of state AAAA (first) machine_template void MachineStates::enter() { Serial.println("enter AAAA"); } // when entering the state for the fist time machine_template void MachineStates::exit() { Serial.println("exit AAAA"); } // when exiting the state for the fist time machine_template void MachineStates::tick() { Serial.println("tick AAAA"); } // called from the loop() below machine_template int MachineStates::event() { Serial.println("self transition AAAA"); return BBBB; } // where to transition to from here (see void transition() below) // function definitions of state BBBB (second) machine_template void MachineStates::enter() { Serial.println("enter BBBB"); } machine_template void MachineStates::exit() { Serial.println("exit BBBB"); } machine_template void MachineStates::tick() { Serial.println("tick BBBB"); } machine_template int MachineStates::event() { Serial.println("self transition BBBB"); return CCCC; } // function definitions of state CCCC (third) machine_template void MachineStates::enter() { Serial.println("enter CCCC"); } machine_template void MachineStates::exit() { Serial.println("exit CCCC"); } machine_template void MachineStates::tick() { Serial.println("tick CCCC"); } // declaration the master (parent) statemachine (its a pointer) StateMachine* sm; // declaration the actual statemachine LaMachine ms; ////////////////////////////////////////////////////////////// // TIMER DEF //------------------------------------------------------------ #define one_sec 1000000 // IntervalTimer is in microseconds #define timeout 0.25 * one_sec * 60 IntervalTimer timer; ////////////////////////////////////////////////////////////// // USUAL SUSPECTS //------------------------------------------------------------ void setup() { Serial.begin(115200); Serial.println("starts"); //instantiation of the master statemachine with actual machine as param sm = new StateMachine(ms); // start the timer with callback (could be both void transition() or void transition_random() -- see below) timer.begin(transition_random, timeout); randomSeed(analogRead(A4)); } void loop() { delay(2000); Serial.print("... - "); Serial.println(sm->state); sm->tick(); /// <-------------------------------------------------- über important (see MachineStates::tick() above) } ////////////////////////////////////////////////////////////// // TRANSITIONS DEF //------------------------------------------------------------ // normal transition to next state as specified in 'event' (see MachineStates::event() above) void transition() { sm->work(); } // random transition depending on the number of states void transition_random() { int r = random(0, 3); // we do indeed have three states (i.e, { AAAA, BBBB, CCCC };) // force new state ? (don't need this is re-entry is ok) while(r == sm->state) r = random(0, 3); Event ev = {r}; sm->work(ev); // transition }