You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.8 KiB
72 lines
1.8 KiB
#pragma once
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
|
|
/**
|
|
* @brief
|
|
*
|
|
* @Usage:
|
|
*
|
|
*
|
|
*/
|
|
|
|
typedef struct state_machine_state_s state_machine_state_t;
|
|
typedef struct state_machine_s state_machine_t;
|
|
typedef state_machine_state_t* (*state_machine_process_event_t)(state_machine_t* machine, state_machine_state_t* nowstate, int event);
|
|
|
|
struct state_machine_state_s {
|
|
char const* name; // name
|
|
int stateId; // stateID
|
|
int duration; //当前这个状态持续了多久
|
|
};
|
|
struct state_machine_s {
|
|
state_machine_state_t* states; //
|
|
int nstate; //
|
|
state_machine_state_t* nowstate; //
|
|
state_machine_process_event_t process_event; //
|
|
state_machine_state_t* nextstate; //
|
|
state_machine_state_t* laststate; //
|
|
};
|
|
/**
|
|
* @brief
|
|
* 0->1000 私有事件
|
|
*/
|
|
#define TIME_EVENT 0x01 //
|
|
#define ENTER_STATE 0x02 //
|
|
#define EXIT_STATE 0x03 //
|
|
#define STATE_MACHINE_PUBLIC_EVENT 1000 //
|
|
|
|
/**
|
|
* @brief 初始化状态机
|
|
*
|
|
* @param machine
|
|
* @param statetable
|
|
* @param len
|
|
*/
|
|
void state_machine_init(state_machine_t* machine, state_machine_state_t* statetable, size_t len, state_machine_process_event_t process_event);
|
|
/**
|
|
* @brief 触发事件,主要用于触发私有事件
|
|
*
|
|
* @param machine
|
|
* @param event
|
|
*/
|
|
void state_machine_trigger_event(state_machine_t* machine, int event);
|
|
|
|
state_machine_state_t* state_machine_get_now_state(state_machine_t* machine);
|
|
|
|
/**
|
|
* @brief 在主循环中每隔10ms调用一次
|
|
*
|
|
* @param machine
|
|
*/
|
|
void state_machine_schedule_each10ms(state_machine_t* machine);
|
|
|
|
/**
|
|
* @brief 获取已经进入当前状态过去了多久时间
|
|
*
|
|
* @param state
|
|
* @return uint32_t
|
|
*/
|
|
uint32_t state_machine_get_state_duration_ms(state_machine_state_t* state);
|