Search code examples
c++embeddedfinite-automatastate-machine

Finite State Machine For Embeded Devices


I have made several menus using FSM's but with a VERY clunky interface. I took a year long break from programming to facilitate a relocation and just tonight re-wrote my old FSM code.

It can be seen HERE

The problem with my code is it requires heavy rework of the StateMachine class and the event processor whenever you change the implementation. As this is on an embedded device I can not use the BOOST::FSM so I want to write my own class that is robust enough to handle things like menus and programming antilogarithms (ICSP for a PIC for example is a simple FSM)

How would you guys recommend I make my state machine more useable?


Solution

  • If you're writing your own class - consider implementing a generic FSM, and then fill it with states and events.

    Basically it would be a simple interface with something like this as methods:

    create();
    addState(someState, stateFunction);
    addEdge(someStateOrig, someStateDest, event);
    processEvent(event);
    start(startFromState);
    

    That should cover it. Then, when you create the FSM - feed it with your states, and functions to execute when you reach the state, and the events that make the FSM move from one state to another.

    Then you just start the FSM at some state which will be the start state, and feed the events.

    If you need to change the events, functions, or states - the FSM implementation remains the same, you change the code that uses it, as it should be.

    That is more or less what Boost.FSM gives you, but you said you can't use it - so do it on your own:-)