当前位置:网站首页>State machine program framework
State machine program framework
2022-06-28 08:49:00 【DingUXiu】
state.h
#ifndef __STATE_H
#define __STATE_H
// Define state machine function pointers
typedef void (*state_func_p)(void);
#endif
state.c
#include "state.h"
// Record the current status , Implement state loop
static state_func_p g_state_cur = NULL;
//func: The state to jump to
void main_setState(state_func_p func){
g_state_cur = func;
}
// Get current status
state_func_p main_getState(void){
return g_state_funcCur;
}
Encapsulated into get、set Function for the upper layer to call
main.c
#include "state.h"
// Idle state
void main_stateIdle(void){
// Function realization
if( Began to run ){
main_setState(main_stateRun);
}
if( An error occurred ){
main_setState(main_stateError);
}
}
// Running state
void main_stateRun(void){
// Function realization
if( Stop running ){
main_setState(main_stateIdle);
}
if( An error occurred ){
main_setState(main_stateError);
}
}
// Error status
void main_stateError(void){
// Function realization
if( Error elimination ){
main_setState(main_stateIdle);
}
}
int main(void){
// Set to idle state initially
main_setState(main_stateIdle);
while(1){
(*(main_getState()))();
}
}
边栏推荐
- AWS builds a virtual infrastructure including servers and networks (2)
- Super Jumping! Jumping! Jumping!
- Set<String>
- Why are function templates not partial specialization?
- 利尔达低代码数据大屏,铲平数据应用开发门槛
- 找合适的PMP机构只需2步搞定,一查二问
- Analysis of prepaid power purchase device
- High rise building fire prevention
- What are the advantages of a differential probe over a conventional probe
- 罗氏线圈工作原理
猜你喜欢
随机推荐
Build an integrated kubernetes in Fedora
TCP那点事
Fire fighting work and measures in Higher Vocational Colleges
How to suppress SiC MOSFET crosstalk?
Redis deployment under Linux & redis startup
containerd1.5.5的安装
TCP
[introduction to SQL in 10 days] day5+6 consolidated table
[go ~ 0 to 1] on the first day, June 24, variables, conditional judgment cycle statement
实现全局双指长按返回桌面
Potential safety hazards in elderly care facilities
Set<String>
神殿
【无标题】
Map. ToCharArray( ),Map. getOrDefault(). Map. charAt()
webrtc优势与模块拆分
小程序 :遍历list里面的某个数组的值,等同于 for=“list” list.comment里面的某一项
Application of current limiting protector in preventing electrical fire in shopping malls
Kali Notes(1)
Anniversary party



![[untitled]](/img/bb/213f213c695795daecb81a4cf2adcd.jpg)





