https://github.com/redoccheng/state_machine
一个用C语言实现功能丰富但简单的有限状态机(FSM)。
https://github.com/redoccheng/state_machine
Last synced: 4 months ago
JSON representation
一个用C语言实现功能丰富但简单的有限状态机(FSM)。
- Host: GitHub
- URL: https://github.com/redoccheng/state_machine
- Owner: redocCheng
- License: mit
- Created: 2021-01-11T08:13:56.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-03-26T07:01:19.000Z (about 4 years ago)
- Last Synced: 2023-08-22T09:08:46.958Z (over 1 year ago)
- Language: C
- Size: 64.5 KB
- Stars: 9
- Watchers: 2
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-rt-thread - state_machine - A feature-rich, yet simple finite state machine (FSM) implementation in C. (Packages / Misc)
README
# state_machine
## 1、介绍
state_machine是基于RT-Thread格式移植的状态机软件包。
state_machine的作者是misje, github地址: https://github.com/misje/stateMachine
对该软件包进行了如下修改:
1.修复部分函数反馈,由void改为int,如果异常反馈负数;
2.修改状态儿子数的判断,如果这个状态没有儿子,还需要判断它的父亲(原作者不判断父亲,该状态会停掉);## 使用方法
1.先申请一个状态结构,定义事件```
enum event_type {
EVENT_KEYBOARD,
};
struct state_machine m;
```
2.初始化状态对象```
static struct state state_idle = {
.state_parent = NULL,
.state_entry = NULL,
.transitions = (struct transition[]){
{ EVENT_KEYBOARD, (void *)(intptr_t)'t', &keyboard_char_compare, NULL, &state_next },// turn next state
{ EVENT_KEYBOARD, (void *)(intptr_t)'d', &keyboard_char_compare, &action_fun, &state_idle },// do action
},
.transition_nums = 1,
.data = "idle",
.action_entry = &print_msg_enter,
.action_exti = &print_msg_exit,
};static struct state state_error = {
.data = "Error",
.action_entry = &print_msg_err
};
```
3.初始化状态机```
statem_init( &m, &state_idle, &state_error );
...
```
4.执行事件```
statem_handle_event( &m, &(struct event){ EVENT_KEYBOARD, (void *)(intptr_t)ch } );
```## 特性
state_machine 使用C语言实现,基于面向对象方式设计思路,每个状态对象单独用一份数据结构管理:
## Examples
使用示例在 [examples](./examples) 下。