2018-11-13 08:10:15 +01:00

99 lines
3.4 KiB
C++
Executable File

/////////// include FSM lib
#include <FSM.h>
#include <MachineStates.h>
//////////////////////////////////////////////////////////////
// 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<AAAA>() { Serial.println("enter AAAA"); } // when entering the state for the fist time
machine_template void MachineStates::exit<AAAA>() { Serial.println("exit AAAA"); } // when exiting the state for the fist time
machine_template void MachineStates::tick<AAAA>() { Serial.println("tick AAAA"); } // called from the loop() below
machine_template int MachineStates::event<AAAA>() { 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<BBBB>() { Serial.println("enter BBBB"); }
machine_template void MachineStates::exit<BBBB>() { Serial.println("exit BBBB"); }
machine_template void MachineStates::tick<BBBB>() { Serial.println("tick BBBB"); }
machine_template int MachineStates::event<BBBB>() { Serial.println("self transition BBBB"); return CCCC; }
// function definitions of state CCCC (third)
machine_template void MachineStates::enter<CCCC>() { Serial.println("enter CCCC"); }
machine_template void MachineStates::exit<CCCC>() { Serial.println("exit CCCC"); }
machine_template void MachineStates::tick<CCCC>() { Serial.println("tick CCCC"); }
// declaration the master (parent) statemachine (its a pointer)
StateMachine<LaMachine>* 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<LaMachine>(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<AAAA>() above)
return;
default:
Serial.println('??');
break;
}
}
sm->tick(); /// <-------------------------------------------------- über important (see MachineStates::tick<AAAA>() above)
}