In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to realize the simple state machine with function pointer method". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "function pointer method how to achieve a simple state machine" bar!
Brief introduction of State Machine
Finite state machine (FSM) is a mathematical model of finite states, transitions and actions between these states. It is an efficient programming method within logic units. It can carry out corresponding processing logic according to different states or message types, which makes the program logic clear and easy to understand.
Function pointer implements FSM
Using function pointers to implement FSM can be divided into three steps.
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Establish the corresponding status table and action query table
Locate the corresponding action handling function according to the state table, event and action table
Switch the status after the execution is completed.
Code implementation steps
1. Define enumerated types of state data
Typedef enum {state_1=1, state_2, state_3, state_4} State
two。 Define enumerated types of events
Typedef enum {event_1=1, event_2, event_3, event_4, event_5} EventID
3. Define the data type of the state table
Typedef struct {int event; / / event int CurState; / / current state void (* eventActFun) (); / / function pointer int NextState; / / next state} StateTable
4. Define the processing function and establish the state table
Void f121 () {printf ("this is f121\ n");} void f221 () {printf ("this is f221\ n");} void f321 () {printf ("this is f321\ n");} void f122 () {printf ("this is f122\ n") } StateTable fTable [] = {/ / {incoming event, current state, function to be executed Next state} {event_1, state_1, f121, event_2}, {event_2, state_2, f221, event_3}, {event_3, state_3, f321, event_4}, {event_4, state_4, f122, event_1}, / / add your code here}
5. The type of state machine and the interface function of state machine
/ * State machine type * / typedef struct {number of entries in the current StateTable* stateTable;// state table int size;// table} fsmType; / * State machine registration, give it a state table * / void fsmRegist (fsmType* pFsm, StateTable* pTable) {pFsm- > stateTable = pTable;} / * State transition * / void fsmStateTransfer (fsmType* pFsm, int state) {pFsm- > curState = state } / * event handling * / void fsmEventHandle (fsmType* pFsm, int event) {StateTable* pActTable = pFsm- > stateTable; void (* eventActFun) () = NULL; / / function pointer initialized to null int NextState; int CurState = pFsm- > curState; int maxNum = pFsm- > size; int flag = 0; / / indicate whether the condition is met / * get the current action function * / for (int I = 0; istateTable = pTable) } / * State transition * / void fsmStateTransfer (fsmType* pFsm, int state) {pFsm- > curState = state;} / * event handling * / void fsmEventHandle (fsmType* pFsm, int event) {StateTable* pActTable = pFsm- > stateTable; void (* eventActFun) () = NULL; / / function pointer initialized to null int NextState; int CurState = pFsm- > curState; int maxNum = pFsm- > size; int flag = 0 / / identify whether the condition is met / * get the current action function * / for (int I = 0; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 290
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.