C Language State machine templates

Source: Internet
Author: User

Reprint statement: If reproduced this blog content, please contact [email protected], obtain the written authorization of the author.

Objective

In my blog, I explored a non-swtich-case structure of state machine, but the personal feeling is more troublesome to write, if you add a state, you need to manually add a function pointer array of the corresponding function function, and the state function must be written in front of the function pointer array leading to poor code structure, If written in the back, but also in front of the statement, it is more troublesome, in short, difficult to maintain, think of Adam Dunkels in lwIP in the inverse of the day macro definition usage, so that the code has the function of automatic updating, and then try to apply its wording, now this code easier to maintain, can do template use.

Realize

Or an example of the previous blog, just a few more macro definitions, this time to define a list of states:

#define STATE_LIST(_) _(init)_(count)_(done)_(dft)

Maybe you're wondering, so don't worry, just look at the next definition:

#define DEFINE_STATE(state) State##_##state,enum States {    STATE_LIST(DEFINE_STATE)    State_Nums};#undef DEFINE_STATE

For beginners may have fainted, in fact, you just remember that the macro is replaced, you can understand, the following I first expand the first layer in the States (note comma is not less):

enum States {    DEFINE_STATE(init),    DEFINE_STATE(count),    DEFINE_STATE(done),    DEFINE_STATE(dft),    State_Nums};

Next expand the macro (# #起连接Token的作用) According to Define_state, there are:

enum States {    State_init,    State_count,    State_stop,    State_dft,    State_Nums};

So we get a complete state enumeration, use this method to declare the state function, and put the declared state function into the function pointer array, your state function will not have a position limit, the complete code is as follows:

#include <stdio.h>typedef unsignedChar State;typedef State (*procedure) (void *);State list, state or increment is only written here by format#Define State_list (_) _ (init) _ (count) _ (Done) _ (DfT)#Define STEP_NULL ((void *) 0)State execution function declaration, your function must follow a format such as Step_initand must return to the next state such as State_init#Define STATEMENT_STEP (state) State STEP# #_ # #state (void * arg); State_list (Statement_step)#Undef Statement_stepState enumeration#Define Define_state (state) state# #_ # #state,Enum states {State_list (define_state) state_nums};#Undef define_stateState Execution Function Lookup table#Define State_procedure (state) step# #_ # #State, PROCEDURE steps[] = {state_list (state_procedure) Step_null};#Undef state_proceduretypedefstruct _sm_varEncapsulates the state machine parameter {int cnt;} Sm_var;void Beststatemachine (void * Invar) {static state NS = State_init;Define next state NS = Steps[ns] (invar);}int main (void) {Sm_varvarint i;for (i =0; I <=8; i++) {Beststatemachine (&var); }Return0;} State Step_init (void * arg)Initialize {Sm_var *p = (Sm_var *) arg; p->cnt =0; printf"Cs:init; cnt=%d; Ns:count\n ", p->cnt);return state_count;} State Step_count (void * arg)Count {Sm_var *p = (Sm_var *) arg;if (p->cnt < 3) {p->cnt + = 1 ; printf ( "cs:count;cnt=%d; Ns:count\n ", p->cnt); return State_count;} else{printf (return State_done;} State Step_done (void * arg) //count complete {sm_var *p = (Sm_var *) arg; printf ( "cs:done; cnt=%d; Ns:init\n ", p->cnt); return State_init;} State step_dft (void * arg) //error procedure {Sm_var *p = (Sm_var *) arg; printf ( "wrong state\n"); return state_init;}           

In the VS2013 debugging, the output and the previous blog, you must now find that the code changes are very simple, you just need to change the state in the State_list, the macro will help you to do everything behind, and your state function can be placed in any position, The only thing to note is that the function name must follow the definition format, and the state reference name must follow the definition format, but since the template has a corresponding format, follow it.

Summarize

Not much to say, this template, state machine writing becomes exceptionally simple, you can transfer attention completely to the state machine run, without having to consider other things. e-mail: [email protected] Tang Dong shoes ^_^

C Language State machine templates

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.