In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use C language to achieve state machine". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "how to use C language to realize state machine".
Traditional implementation scheme
If...else: make a lot of if else, one function is very, very long.
Swich...case: also make a lot of a function to write a very long.
Let's first take a look at a recent project, what the state machine implemented by the wireless communication protocol looks like:
There are three types of events: upper-level command events; lower-level arrival flags and data transfer events; and timeout timer timeout events. There are 10 states, the relevance is very large, complex bar, this is a variety of if/else will be written how long.
Judge the state in the event, judge the event in the state, the code of the two writing methods is relatively lengthy, and it doesn't look good. Once you add or subtract it, you have to use your head to sort it out again. It's very tiring.
How do you write it? The principle of state machine: according to the current state (cur_state), after the event (event) occurs, it is transferred to the next state number (nxt_state) to determine the action to be performed (action). Steal a picture.
Here we first define a structure as follows:
Typedef struct {State curState;// current status EventID eventId;// event ID State nextState;// next state Action action;// specific performance} StateTransform
Let's assume that there are three states, which can be added at will, and the states are enumerated as follows:
Typedef enum {state_1=1, state_2, state_3} State
Let's assume that there are five events, or you can add them at will. The event ID is enumerated as follows:
Typedef enum {event_1=1, event_2, event_3, event_4, event_5} EventID
Encapsulate it in StateMachine:
Typedef struct {State state; int transNum; StateTransform* transform;} StateMachine
Specific process: current state-event triggered-jump to the next state-specific performance, refactoring code
StateTransform* findTranss (StateMachine* pSM, const EventID evt) {int i; for (I = 0; I)
< pSM->TransNum; iTunes +) {if ((pSM- > transform [I] .curstate = = pSM- > state) & & (pSM- > transform [I] .eventID = = evt)) {return & pSM- > transform [I];}} return NULL;}
The state machine is implemented as follows:
Void runStateMachine (StateMachine* pSM, EventID evt) {StateTransform* pTrans; pTrans = findTranss (pSM, evt); if (pTrans = = NULL) {xil_printf ("CurState=% s Do not process enent:% s\ r\ n", pSM- > state,evt); return;} pSM- > state = pTrans- > nextState; Action act = pTrans- > action; if (act = NULL) {xil_printf ("change state to% s. No action\ n", pSM- > state); return } act (& evt);}
Finally, I simulate some random events. We just need to figure out the event ID, the state switch, and the specific performance. Fill in the stateTran [] table in the code. Once there are added or subtracted events, states, and so on, there is no need to use switch/case, Tafenol. The code is as follows:
Int run () {StateMachine stateMachine; stateMachine.state = state_1; stateMachine.transNum = 7 StateTransform stateTran [] = {{state_1,event_3,state_2,f121}, {state_1,event_4,state_2,NULL}, {state_2,event_1,state_3,f231}, {state_2,event_4,state_2,f221}, {state_3,event_2,state_1,f311}, {state_3,event_3,state_2,f321}, {state_3,event_5,state_3 F331}} StateMachine.transform = stateTran
EventID inputEvent [15] = {event_1, event_2, event_3, event_4, event_5, event_1, event_2, event_3, event_4, event_5, event_1, event_2, event_3, event_4, event_5}
Int i; for (I = 0; I)
< 15; i++) { runStateMachine(&stateMachine, inputEvent[i]); } return 0;} 最后运行结果如下Thank you for your reading. the above is the content of "how to realize the state machine in C language". After the study of this article, I believe you have a deeper understanding of how to implement the state machine in C language. the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 0
*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.