/////////// 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; ////////////////////////////////////////////////////////////// // EVENTS DEF //------------------------------------------------------------ Event event_AAAA = {AAAA}; Event event_BBBB = {BBBB}; Event event_CCCC = {CCCC}; ////////////////////////////////////////////////////////////// // USUAL SUSPECTS //------------------------------------------------------------ void setup() { Serial.begin(115200); Serial.println("starts"); sm = new StateMachine(ms); } void loop() { delay(2000); Serial.print("... - "); Serial.println(sm->state); if (Serial.available() > 0) { char in = (char) Serial.read(); switch(in) { case 'a': sm->work(event_AAAA); // transition to AAAA return; case 'b': sm->work(event_BBBB); // transition to BBBB return; case 'c': sm->work(event_CCCC); // transition to CCCC return; case 'x': sm->work(); // transition to next state as specified in 'event' (see MachineStates::event() above) return; default: Serial.println('??'); break; } } sm->tick(); /// <-------------------------------------------------- über important (see MachineStates::tick() above) }