If I make a state machine and want to use an interface like this:
AddState ( state1, state2, Key_UP );
AddEvent ( Key_UP );
AddEventFunction ( Key_UP, &UP_Function);
AddStateFunction ( state1, &State1_In_Function, &State1_Out_Function);
AddStateFunction ( state2, &State2_In_Function, &State2_Out_Function);
State1_In_Function ( void ) { printf ( "In #1 \n" ); }
State1_Out_Function ( void ) { printf ( "Out #1 \n" ); }
State2_In_Function ( void ) { printf ( "In #2 \n" ); }
State2_Out_Function ( void ) { printf ( "Out #2 \n" ); }
UP_Function ( void ) { printf ( "Goin UP \n" ); }
That way when I am in state1 and the FSM receives Key_UP the program prints:
Out #1
Goin UP
In #2
The question is how to store the state and transitional information inside the class without the programmer being required to change an array size. I was thinking I could use a 2D array and make it a state table like usual and to make it more portable I would just handle the addition of events and states by using the vector type to resize as needed. The problem with vectors is that not many embedded devices can use the memory allocation calls. My second option is to call a constructor with the state machine and pass it the size the table would require to be but then if I add any new states or events I need to change these values as well…
So how should I store my states, events and function pointers?!
You could simply store them on the stack, though it’s a bit more difficult 🙂
Still, it’s an amusing solution, so here you go. The underlying principle is to play with decorators and mutability. Example of code:
How does it work ?
Instead of
state1being a “simple” object, it will be slightly more convoluted. Something like:And then we define a
Transition:Informally, we are building a singly-linked list, with the head sitting in
State. We update the head each time we add a transition.Upon an event occurrence, one needs to walk this list until either the event is encountered, and thus suitably dispatched, or the null pointer is reached, indicating that the event shall not have been received in this state.