Hi all,
I have a project in which I designed a state machine class by composition of a whole bunch of features that are in the project
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class MasterFSM
{
public:
MasterFSM()
~MasterFSM() {}
private:
ArrayCircularBuffer _eventBuffer;
Sound _beeper;
UI _ui;
NtpRtc _clock;
}
|
So, in my project, I created a header file called events in which I simply created an enum with all the events that can occur during the operation
1 2 3 4 5 6 7 8
|
enum event_t
{
EV_NOTHING = 0,
// wifi msgs
EV_START_SENSORS_CMD,
EV_STOP_SENSORS_CMD
// etc..
}
|
Now, I used an enum because I liked that I didn't have to explicitly set number codes to each events, but the way I proceed to call those events from internal objects in the FSM is a bit too convoluted...
In the State machine constructor, I call functions of each internal object that can trigger events, and pass them the events that they should call, and a callback function that they can call with the occuring event codes. ex:
1 2 3
|
MasterFSM() {
_Sensor.setEvents(EV_PATIENT_GOING_LEFT, EV_PATIENT_GOING_RIGHT, callbackfct);
}
|
Then, the state machine has a polling fct repeatedly called, which is a big switch case and executes whatever events were called.
I feel like it looked nice at first, but as the internal objects of the FSM grew bigger, and events may be added, it just doesnt scale. plus, some internal objects have themselves internal objects that may need to trigger events, so I just find myself creating an endless amount of setters and getters for those event codes.
I could have gone with global defined macros like it is done in most C firmware,
and I guess it would have been solved, but it doesn't really sound very Cplusplussy...
Is there a clean way to do this, or I am just overthinking it?
Thanks a lot!