Introduction to state machine practice

Source: Internet
Author: User
Tags switch case

No doubt, the universal language of single-chip microcomputer is the state machine. I also hope that you don't need to think about the state machine without looking at it in a reflective way. What do I want to talk about? The state machine is a mode of thinking, it is the foundation of computer theory (I don't believe it can be referred to the theory and application of automation published by Tsinghua University Press). Therefore, the relationship between the implementation of state machines and the language itself is not absolute. The state machine to be discussed in this article is similar to the thinking mode commonly used in Java in implementation mode, but far from VHDL.
Let's take a step by step. We have to eat a bite. In order not to scare people away, I will write articles to discuss more complex parts of the state machine theory. Here I will first find a starting point, from the two commonly used state machine writing methods for everyone to slowly expand.


First, there are several questions, such as: Where is the state machine used? How can I write a state machine? Is the state machine highly efficient? Is it complicated to solve simple problems? This type of problem is not discussed in this article, in short-who knows who to use. In fact, it is not easy to come to the conclusion that there are a lot of people who don't want to solve the problem. Therefore, I would like to say: the questions about the state machine are "who thinks who practices who insist on who knows ".


The first entry to the state machine: Switch case to the end
Key Point: Use the switch structure and a state variable to change the status by modifying the value of the state variable.
Example:

//! Defines the relationship between status names and Status values to improve readability.
# Define fsm_start 0x00
# Define fsm_state_a 0x01
# Define fsm_state_ B 0x02
...
# Define fsm_reset 0xff
Bool fsm_example_a (<parameter list> ){
Static uint8_t s_chfsmstate = fsm_start ;//! <Define State Variables
...
Switch (s_chfsmstate ){
Case fsm_start:
//! Add the state machine initialization code here.
...
S_chfsmstate = fsm_state_a ;//! <Enter the next status
Break;
Case fsm_state_a:
//! Add the detection code for state machine a entering the next state.
If (<XX condition> ){
//! Here are some preparations for entering the next state.
S_chfsmstate = fsm_state_ B ;//! <Enter the next status
}
Break;
Case fsm_state_ B:
//! Add the detection code for state machine a entering the next state.
If (<XX condition> ){
//! Here are some preparations for entering the next state.
S_chfsmstate = fsm_state_a ;//! <Enter the next status
} Else if (<XX condition> ){
} Else if (<XX condition> ){
...
} Else {
}
Break;
...
Case fsm_stop:
Case fsm_reset:
Default:
//! Code related to state machine reset is added here
...
Chfsmstate = fsm_start ;//! <State machine Reset

//! If false is returned, the state machine does not need to continue running.
Return false;
}

//! Returns true, indicating that the state machine is running.
Return true;
}

Conclusion: we can see from the example that such a state machine is a single rib ...... It does not mean that he does not have any branches, but that he usually cannot make multiple states active at the same time.

 

Getting started with the State Machine Method 2: If judgment changes infinitely
Key Point: use if else... The combination of the else if structure to describe the status flowchart.
What is a status flowchart? I don't want to explain more, because it's just a simple thing. If I say more, it's mysterious. You can simply think of a status chart as a flow chart. When you use it more often, you will gradually understand why the word "status" is too much. If you have learned a status chart later or earlier, then you will soon understand how much the status flow chart is "advanced" than the status chart.
1. In any case, you can first draw a flowchart for the things you want to deal. If none of the flowchart is shown
Don't join in.
2. Next, we will regard every box or judgment basket in the flowchart as a simple and crude state.
3. Each status is represented in the IF structure.

If (<status flag> ){
//! Status Code
...
}

4. do it by yourself, merge unnecessary states, and optimize the code.
Example:

//! First, compress the Boolean status mark in one byte to save memory costs.
Typedef Union {
Uint8_t value;
Uint8_t byte;
Struct {
Unsigned bit0: 1;
Unsigned bit1: 1;
Unsigned bit2: 1;
Unsigned bit3: 1;
Unsigned bit4: 1;
Unsigned bit5: 1;
Unsigned bit6: 1;
Unsigned bit7: 1;
} Bits;
} Byte_t;

# Define fsm_action_flag s_tbstate.bits
# Define fsm_stop_all_actions () do {s_tbstate.value = 0;} while (0)
# Define fsm_start (0 = s_tbstate.value)
# Define fsm_state_a fsm_action_flag.bit0
# Define fsm_state_ B fsm_action_flag.bit1
...
# Define fsm_state_h fsm_action_flag.bit7

Bool fsm_example_ B (<parameter list> ){
Static byte_t s_tbstate = {0 };//! <Define State Variables

If (fsm_start ){//! <Start status
//! Code for state machine Initialization
...
Fsm_state_a = true ;//! <Enter status B, and the START installation ends automatically
}

If (fsm_state_a ){//! <A typical simple state
//! The code or
...
//! Some conditions are placed here to enable other statuses.
If (<some conditions> ){
//! Here are some preparations before "entering" the next state.
Fsm_state_ B = true ;//! <Enable next status
Fsm_state_a = false ;//! <End current status
}
}

If (fsm_state_ B ){//! <A typical monitoring status
...
//! Some conditions are detected here.
If (<some conditions> ){
//! Here are some preparations for "enabling" a certain state.
Fsm_state_c = true ;//! <Enable a certain status without ending the current status
Fsm_state_d = true ;//! <Of course you can trigger multiple statuses at a time
...
} Else if (<some conditions> ){
//! Disable the current status after certain conditions are met
Fsm_state_ B = false;
}
}
...
If (fsm_state_f ){//! <A typical sub-state machine call
If (! Fsm_example_a (<real parameter list> )){//! <Wait until the sub-state machine returns false
//! The sub-state machine is running successfully and enters the next state.
...
Fsm_state_f = false ;//! <End current status
Fsm_state_x = true ;//! <Enter the next state. X indicates a letter.
}
}

If (fsm_state_h ){//! <A typical stop status
//! <Some state machine operations, such as releasing some resources
...
Fsm_stop_all_actions ();//! <Reset the state machine
Return false ;//! <False indicates that the state machine ends.
}

Return true ;//! <True indicates that the state machine remains running
}

Summary: we can see from the example that this state machine is very flexible. With the enabling and disabling of Boolean variables, You can freely control the enabling of certain States. Multiple States may be activated at the same time. This structure can translate almost any flow chart. There are many other benefits to use.


State Machine entry method 3: state is in the heart, and stateless is abnormal
Key point: all functions can be regarded as state machines, but normal functions are state machines with only one State. If a function has a return value, and the return value can represent at least two different States (for example, if the return value is a pointer, null and non-null are two States. For example, if the return value is a Boolean variable, true and false are two States. For example, if an integer is returned and some features of the integer can be classified, these different classifications are in several different States ), these return values can be used to indicate the running status of the current state machine. A state machine can call substate machines. All States should be none-block. Simply put, the system will not be suspended for a long time in a certain state, such as while (1) or the for structure with a large number of loops; otherwise, the existence of the state machine will be greatly reduced-it is not good to write code directly according to the flowchart. Why do we have to translate it into a state machine? In a state machine, the state function is to wait for an event to occur (or meet conditions). In some cases, some one-time execution processes can also be independent into one state-it certainly does not wait for any conditions to be met, you can think that it is unconditional state transfer.


In general, the state machine is a 10 thousand-bit computer language expression, and has little to do with a specific carrier language. How can code be stateless if there is a status in mind? The state machine is a cheap implementation solution for multiple tasks under bare metal conditions. Almost all concepts involved in the operating system are involved under the state machine multi-task conditions, such as task synchronization, critical section protection, inter-task communication, and task priority, dynamic resource allocation. As you can understand, every state machine is a process, and every state is a thread. Because a process has its own resources, multiple threads in the same process share the same piece of resources. You can even use a state machine with a preemptible operating system. At this time, every task in the operating system is a kernel, so the entire system development can be smaller
Multi-core system developed. Is it interesting? Don't look at it! The state machine.


Have a good time.

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.