123 lines
3.5 KiB
C
123 lines
3.5 KiB
C
/**
|
||
* @file FSM_private.h
|
||
* @author 天神 (dalaoshi@stu.xjtu.edu.cn)
|
||
* @brief 状态机框架的私有变量,只能在状态机框架内使用。
|
||
* @details 基类私有变量对子类和外界隐藏实现,通过指针进行访问。
|
||
* @version 2.1
|
||
* @date 2024-05-07
|
||
*
|
||
* @copyright 天神创意无限公司 2024
|
||
*
|
||
*/
|
||
|
||
|
||
#ifndef __FSM_PRIVATE_H_
|
||
#define __FSM_PRIVATE_H_
|
||
|
||
#include <string.h>
|
||
#include "FSM_protected.h"
|
||
|
||
typedef struct FSMHandler FSMHandler;
|
||
typedef struct FSM FSM;
|
||
typedef struct FSMSignals FSMSignals;
|
||
|
||
/* -------------------------------------------------------------------------- */
|
||
/* private数据类型 */
|
||
/* -------------------------------------------------------------------------- */
|
||
/**
|
||
* @brief
|
||
* @deprecated 预装载器弃用
|
||
*
|
||
*/
|
||
typedef struct FSMDataLoader
|
||
{
|
||
void *shadowData;
|
||
int isReady;
|
||
int isOverflow;
|
||
size_t size;
|
||
}FSMDataLoader;
|
||
|
||
|
||
/**
|
||
* @brief 基类状态机的私有变量和函数,子类不可直接访问
|
||
*
|
||
*/
|
||
typedef struct FSMPrivateVars
|
||
{
|
||
int numState; /**< 状态数量 */
|
||
int defaultState; /**< Idle状态不能停留,必须指定一个初始状态 */
|
||
|
||
int curState; /**< 当前状态 */
|
||
int nextState; /**< nextState为Idle代表状态机不发生变化 */
|
||
int index; /**< 状态转移函数表对应的标号 */
|
||
|
||
FSMHandler fcns; /**< 状态函数表 */
|
||
|
||
// FSM *childFSM; /**< 限定只能有一个子状态机 */
|
||
FSM **childFSMTable; /**< 每个状态都可配置一个子状态机 */
|
||
|
||
FSMDataLoader preloader; /**< @deprecated 弃用 */
|
||
|
||
}FSMPrivateVars;
|
||
|
||
|
||
static void empty_func(){
|
||
}
|
||
|
||
|
||
/**
|
||
* @brief 仅在FSM.c中调用,新建私有变量并分配空间
|
||
*
|
||
* @param numState
|
||
* @return FSMPrivateVars*
|
||
*/
|
||
FSMPrivateVars* newFMSPrivateVars(int numState){
|
||
typedef void (*StateFuncPtr)(void *);
|
||
typedef void (*ChildFSMStepFuncPtr)(FSM **);
|
||
typedef int (*TransitionFuncPtr)(void *, int *);
|
||
typedef int (*SelectNextStateFcnPtr)(void *, FSMSignals *);
|
||
|
||
FSMPrivateVars *privateVars = calloc(1, sizeof(FSMPrivateVars));
|
||
privateVars->numState = numState;
|
||
privateVars->curState = 0;
|
||
privateVars->nextState = 0;
|
||
privateVars->childFSMTable = calloc(numState, sizeof(FSM));
|
||
|
||
privateVars->preloader.isReady = 0;
|
||
|
||
privateVars->fcns.duringActionTable = calloc(numState, sizeof(StateFuncPtr));
|
||
privateVars->fcns.enterActionTable = calloc(numState, sizeof(StateFuncPtr));
|
||
privateVars->fcns.exitActionTable = calloc(numState, sizeof(StateFuncPtr));
|
||
|
||
for (int i = 0; i < numState; i++)
|
||
{
|
||
privateVars->fcns.duringActionTable[i] = empty_func;
|
||
privateVars->fcns.enterActionTable[i] = empty_func;
|
||
privateVars->fcns.exitActionTable[i] = empty_func;
|
||
}
|
||
|
||
|
||
// 包括默认状态
|
||
privateVars->fcns.selectNextState = calloc(numState+1, sizeof(SelectNextStateFcnPtr));
|
||
privateVars->fcns.transitionTable = calloc(numState * numState, sizeof(TransitionFuncPtr));
|
||
for (int i = 0; i < numState * numState; i++)
|
||
{
|
||
privateVars->fcns.transitionTable[i] = empty_func;
|
||
}
|
||
|
||
privateVars->fcns.transitionGeneralAction = calloc(1, sizeof(StateFuncPtr));
|
||
privateVars->fcns.transitionGeneralAction = empty_func;
|
||
|
||
privateVars->fcns.childFSMStepTable = calloc(numState * numState, sizeof(ChildFSMStepFuncPtr));
|
||
|
||
return privateVars;
|
||
}
|
||
|
||
|
||
static int hasChildFSM(FSM *pFSM, int state){
|
||
return (pFSM->privateVars->childFSMTable[state] != NULL);
|
||
}
|
||
|
||
|
||
#endif
|