A state machine is essentially a while loop that runs continuously with a case structure that executes different code for a variety of "states". See the picture below:
The shift register keeps track of the state of the state machine. In this implementation, the state is a string. It begins at the state "Init". The shift register is initialized to the value "Init" for the first iteration of the while loop by wiring it from outside of the loop to the left portion of the shift register. Note that the "Init" case is shown. Anything in that case will execute only when the state machine is in the Init state. This will happen immediately on startup. The state is changed to "State 1" via the output tunnel of the case structure which is wired to the shift register to the right.
The next iteration will receive "State 1" from the shift register and execute the code in that case. This code modifies the state to "State 2".
Above is State 2. The state is modified back to State 1.This program will start in the "Init" state, then begin alternating between the "State 1" and "State 2" states infinitely. You can imagine using this design pattern to produce more complex programs by adding additional states and logic to determine the next state. You could add any code you like to each individual state.
0 Nhận xét