C language optimal state machine specification and C language optimal specification

Source: Internet
Author: User

C language optimal state machine specification and C language optimal specification

Reprinted statement if reprinted content of this blog, please contact the 869119842@qq.com, get the author's written authorization

Preface

Recently, my thoughts may not be able to stop. I have conceived a GUI framework (very small for the Cotex-M Platform). I hope to implement it later. There is a touch screen detection in it, naturally, I want to use the state machine to generate and distribute messages, so I want to implement a model implemented by the state machine, which will be convenient for other projects in the future.
The advantages of the state machine are needless to say. Baidu goes on its own, but the traditional programming mode uses the switch-case structure for both the C language and the hardware FPGA, it is parallel, but if the state machine is implemented in C language, you may need to judge each case. The status is few, such as a few problems that may not be efficient, however, if there are more than a dozen or even hundreds of States, it is necessary to make hundreds of checks to determine whether a match exists. There is no doubt thatEfficiencyVery low, switch the status each timeSwitch timeNot sure. Is there a good implementation mode without the switch-case structure? Next I will establish an optimal specification for the implementation of the C state machine.

Implementation

I have talked about a pile of bi nonsense. Now I am going to the topic. Some time ago I studied functional programming and found that the cycle structure of C language can be fully implemented by tail recursion (not understanding Baidu, finally, only if and switch are left in C and cannot be implemented by functional programming. The state machine mode to be discussed today is to implement switch-case through functional programming, switch-case can actually be seen as a search jump. We know that goto in C can achieve the jump, so what else can achieve the jump? The answer is function call, but how does one implement the specified function call? Is it meaningless to use the if statement? Is there anything more efficient than the if Statement (if hundreds of times? Someone has already thought of a look-up table. If we combine a look-up table with a function callFunction pointer ArrayApplication!

Code on! First, define a function pointer type. Why do we need the void * parameter:

typedef unsigned char State;typedef State(*Procedure)(void *);

In this way, you can easily define a function pointer array:

Procedure Steps[] = { step_init, step_count, step_done, step_default };

Step_init, step_count, etc. are function names, and then define the status:

enum states{ s_init, s_count, s_done, s_default };

Enumeration definitions correspond to {, 2, 3}. With these re-state machine connections, we can think that the index of an array is the State definition. There are two lines of core code (simple! The key is to think ):

Void BestStateMachine (void * invar) {static State NS = s_init; // define the next State NS = Steps [NS] (invar );
}

The static variable NS will be maintained every time BestStateMachine is called. We only need to return the next state for each Steps and save it to the NS to save and switch the state. Let's talk about why add one.Void *The state machine generally maintains many of its own variables, and the mealy state machine also needs to be judged based on the input, because the return of function calls does not retain local variables, therefore, you need to pass the variable for modification and storage.Void *The parameter is because if many variables need to be saved and transferred, direct transfer will waste a lot of stack space in calling the function, and the efficiency is low. This mode is used, you can encapsulate the variable with a struct, and then pass the struct pointer to the form parameter of void *. Then, the variable inside the struct can be forcibly converted inside the function. Now you are already mutating that the author is really cool, so the instance code is a simple counter (previously learned that the state machine started from the counter)
To print the information after counting:
# Include <stdio. h> typedef unsigned char State; typedef State (* Procedure) (void *); enum states {s_init, s_count, s_done, s_default }; // State definition typedef struct _ SM_VAR // encapsulate {int cnt;} SM_VAR; 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 s_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 s_count;} else {printf ("CS: count; cnt = % d; NS: done \ n ", p-> cnt); return s_done ;}} State step_done (void * arg) // counting completed {SM_VAR * p = (SM_VAR *) arg; printf ("CS: done; cnt = % d; NS: init \ n", p-> cnt ); return s_init;} State step_default (void * arg) // error process {SM_VAR * p = (SM_VAR *) arg; printf ("Wrong State \ n"); return s_init ;} procedure Steps [] = {step_init, step_count, step_done, step_default}; void BestStateMachine (void * invar) {static State NS = s_init; // define the next state NS = Steps [NS] (invar);} int main (void) {SM_VAR var; int I; for (I = 0; I <8; I ++) {// drive BestStateMachine (& var);} return 0;} to the eight-cycle clock of the state machine ;}

Finally, debug on VS2013 as follows:

CS: init; cnt = 0; NS: countCS: count; cnt = 1; NS: countCS: count; cnt = 2; NS: countCS: count; cnt = 3; NS: countCS: count; cnt = 3; NS: doneCS: done; cnt = 3; NS: initCS: init; cnt = 0; NS: countCS: count; cnt = 1; NS: count press any key to continue...
Summary

In this mode, we can not only implement the Moore-type state machine above, but also implement the Mealy-type state machine based on the actual situation. The structure is clear and easy to understand. Here I dare say the best, because this is the limit of C function programming, if you have a better idea contact me, mail: 869119842@qq.com, tang tong shoes. If you are interested in Embedded GUI, we can explore and write our own GUI together.

 
 

Related Article

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.