☰
Current Page
Main Menu
Home
Home
Editing StateMachineStyle
Edit
Preview
H1
H2
H3
default
Set your preferred keybinding
default
vim
emacs
markdown
Set this page's format to
Markdown
Plain Text
Pod
RDoc
reStructuredText
Rendering unavailable for
AsciiDoc
BibTeX
Creole
MediaWiki
Org-mode
Textile
Help 1
Help 1
Help 1
Help 2
Help 3
Help 4
Help 5
Help 6
Help 7
Help 8
Autosaved text is available. Click the button to restore it.
Restore Text
--- title: StateMachineStyle --- * Contrast methods of implementing state machines in C # State-Event lookup table * Lookup event and state in 2D array * Optionally lookup next state in table # Enum state variable * Switch statement * OnState (transition) and DoState (tick) * function-scope static state variables? ## Example typedef enum STATE STATE_T; enum STATE { STATE_ONE, STATE_TWO, ... STATE_COUNT } currentState; void OnEvent(EVENT_T evt) { switch(currentState) { case STATE_ONE: switch (evt) { case EVENT_ONE: ... break; ... } ... break; case STATE_TWO: ... break; } } # Static arrays 1 * State is an array of (event, transition) pairs * FSM is a struct containing set of states * Linear event lookup ## Benefits * Variable transition array per state * Static initialization * Closely matches theoretical definition of FSM * OO adaptability * Refactor easily by passing fsm context? ## Drawbacks * Linear lookup ## Example /* Transition function */ typedef void (* const HANDLER_FNP)(EVENT_T e); typedef struct { EVENT_T event; /* Event ID */ HANDLER_FNP handler; /* Pointer to corresponding handler */ } TRANSITION_T; static const TRANSITION\_T STATE\_ONE[] = { {EVENT\_ONE, handler\_one}, {EVENT\_TWO, handler\_two}, ... {EVENT_MAX, NULL} }; struct FSM { const TRANSITION_T** currentState; const TRANSITION_T* stateOne; const TRANSITION_T* stateTwo; ... }; // Instantiation struct FSM fsm = { NULL, // currentState STATE_ONE, STATE_TWO, ... }; void OnEvent(EVENT_T event) { const TRANSITION_T* current = fsm->current; // stop searching when a NULL handler is found for (i = ; currentState[i].handler != NULL ; i++) { if (currentState[i].event == event) { currentState[i].handler(event); break; } } } # Static arrays 2 * State is an array of (event, new state, handler) triples * FSM is a pointer to the current state. * Explicit initialization of resultant states. TODO [3]: StateMachineStyle?action=sourceblock&num=1 [4]: StateMachineStyle?action=sourceblock&num=2 <!-- vim: filetype=markdown -->
Uploading file...
Sidebar
# SideBar * [Home][1] * [Projects][2] * * * <!-- --> * [Code][3] * [Tech][4] * [Network][5] * [MediaCentre][6] * [UAV][7] * * * <!-- --> * [Travel][8] * [Music][9] * [Horse Riding][10] * [Study][11] * [Games][12] * [Other Activities][13] * * * <!-- --> * [Car][14] * [House][15] * [Watch][16] * [Clothing][17] * [Miscellany][18] * * * [1]: /Home [2]: /Projects [3]: /Code/Code [4]: /Tech/Tech [5]: /Network/Network [6]: /MediaCentre/MediaCentre [7]: /UAV/UAV [8]: /Travel/Travel [9]: /Music/Music [10]: /HorseRiding/HorseRiding [11]: /Study/Study [12]: /Games/Games [13]: /Do/Do [14]: /Car/Car [15]: /House/House [16]: /Watch/Watch [17]: /Clothing/Clothing [18]: /Miscellany/Miscellany <!-- vim: filetype=markdown -->
Edit message:
Cancel