状态机框架

AUTHOR
Version 0.1alpha

Table of Contents

Table of contents

Deprecated List
Global FSMDataLoader  
预装载器弃用 
Global FSMPrivateVars::preloader  
弃用  
Global FSMPureVtbl::initDataLoader  )(FSM *pFSM)
和预装载器相关  
Global FSMPureVtbl::loadExternalData  )(void *extData)
暂时没用，后续可能考虑外部数据是否允许随时修改 

Data Structure Index
Data Structures
Here are the data structures with brief descriptions:
_templateFSMExternalData (自定义外部数据 ) 	pagenum
_templateFSMInnerData (自定义内部数据 ) 	pagenum
FSMDataLoader  	pagenum
FSMHandler (状态处理函数集合，全部需要子类重新填充内容 ) 	pagenum
FSMPrivateVars (基类状态机的私有变量和函数，子类不可直接访问 ) 	pagenum
FSMPureVtbl (纯虚函数，子类必须重新实现，父类构造函数不会初始化 ) 	pagenum
FSMSignals (状态机用的信号，protected子类可以访问 ) 	pagenum

File Index
File List
Here is a list of all documented files with brief descriptions:
FSM_OOP/baseFSM/FSM_private.h (状态机框架的私有变量，只能在状态机框架内使用。 ) 	pagenum
FSM_OOP/baseFSM/FSM_protected.h (状态机框架的protected函数和变量，在继承的子类中可使用，在外界不可使用 ) 	pagenum
FSM_OOP/baseFSM/FSM_public.h  	pagenum
FSM_OOP/childtest/childFSM.h  	pagenum
FSM_OOP/childtest/childFSM_private.h  	pagenum
FSM_OOP/childtest/parentFSM.h  	pagenum
FSM_OOP/keytest/keyFSM.h  	pagenum
FSM_OOP/keytest/keyFSM_private.h  	pagenum
FSM_OOP/template/templateFSM.h  	pagenum
FSM_OOP/template/templateFSM_private.h  	pagenum

Data Structure Documentation
_templateFSMExternalData Struct Reference

自定义外部数据 
#include <templateFSM.h>

Detailed Description
自定义外部数据 

_templateFSMInnerData Struct Reference

自定义内部数据 
#include <templateFSM.h>

Detailed Description
自定义内部数据 

FSMDataLoader Struct Reference

#include <FSM_private.h>

Detailed Description

Deprecated:
预装载器弃用

FSMHandler Struct Reference

状态处理函数集合，全部需要子类重新填充内容 
#include <FSM_protected.h>
Data Fields
int(** selectNextState )(void *data, FSMSignals *signals)

Detailed Description
状态处理函数集合，全部需要子类重新填充内容 
这个结构体暴露给protect文件，方便在子类中添加函数。不然添加函数得再写个函数添加， 有点麻烦 

Field Documentation
int(** FSMHandler::selectNextState) (void *data, FSMSignals *signals)
每个状态配置的状态选择函数 

FSMPrivateVars Struct Reference

基类状态机的私有变量和函数，子类不可直接访问 
#include <FSM_private.h>
Data Fields
int defaultState
int nextState
int index
FSMDataLoader preloader

Detailed Description
基类状态机的私有变量和函数，子类不可直接访问 

Field Documentation
int FSMPrivateVars::defaultState
Idle状态不能停留，必须指定一个初始状态 
int FSMPrivateVars::nextState
nextState为Idle代表状态机不发生变化 
int FSMPrivateVars::index
状态转移函数表对应的标号 
FSMDataLoader FSMPrivateVars::preloader

Deprecated:
弃用 

FSMPureVtbl Struct Reference

纯虚函数，子类必须重新实现，父类构造函数不会初始化 
#include <FSM_protected.h>
Data Fields
void(* setupHandler )(FSMHandler *fcns)
void(* initData )(FSM *pFSM)
void(* loadExternalData )(void *extData)
void(* initDataLoader )(FSM *pFSM)
void(* initSignals )(FSM *pFSM)
void(* resetSignals )(FSMSignals *signalRegs, void *data)

Detailed Description
纯虚函数，子类必须重新实现，父类构造函数不会初始化 

Field Documentation
void(* FSMPureVtbl::setupHandler) (FSMHandler *fcns)
设置所有的FSMHandler，必须重新实现 
void(* FSMPureVtbl::initData) (FSM *pFSM)
初始化状态机用数据，包括内部数据和外部数据 
void(* FSMPureVtbl::loadExternalData) (void *extData)

Deprecated:
暂时没用，后续可能考虑外部数据是否允许随时修改 
void(* FSMPureVtbl::initDataLoader) (FSM *pFSM)

Deprecated:
和预装载器相关 
void(* FSMPureVtbl::initSignals) (FSM *pFSM)
初始化状态机信号 
void(* FSMPureVtbl::resetSignals) (FSMSignals *signalRegs, void *data)
根据条件重置状态机信号 

FSMSignals Struct Reference

状态机用的信号，protected子类可以访问 
#include <FSM_protected.h>
Data Fields
Uint16 external
Uint16 lastTriggeredSignal

Detailed Description
状态机用的信号，protected子类可以访问 

Field Documentation
Uint16 FSMSignals::external
外部信号 
Uint16 FSMSignals::lastTriggeredSignal
上一次触发的外部信号 

File Documentation
FSM_OOP/baseFSM/FSM_private.h File Reference

状态机框架的私有变量，只能在状态机框架内使用。 
#include <string.h>
#include "FSM_protected.h"
Data Structures
struct FSMDataLoaderstruct FSMPrivateVars
基类状态机的私有变量和函数，子类不可直接访问 
Typedefs
typedef struct FSMHandler FSMHandler
状态处理函数集合，全部需要子类重新填充内容 

typedef struct FSMSignals FSMSignals
状态机用的信号，protected子类可以访问 

typedef struct FSMDataLoader FSMDataLoader
typedef struct FSMPrivateVars FSMPrivateVars
基类状态机的私有变量和函数，子类不可直接访问 

Detailed Description
状态机框架的私有变量，只能在状态机框架内使用。 

Author
天神 (dalaoshi@stu.xjtu.edu.cn) 
Version
2.1 
Date
2024-05-07
Copyright
天神创意无限公司 2024 

Typedef Documentation
typedef struct FSMHandler FSMHandler

状态处理函数集合，全部需要子类重新填充内容 
这个结构体暴露给protect文件，方便在子类中添加函数。不然添加函数得再写个函数添加， 有点麻烦 
typedef struct FSMDataLoader FSMDataLoader

Deprecated:
预装载器弃用

FSM_private.h
Go to the documentation of this file.
1 
13 #ifndef __FSM_PRIVATE_H_
14 #define __FSM_PRIVATE_H_
15 
16 #include <string.h>
17 #include "FSM_protected.h"
18 
19 typedef struct FSMHandler FSMHandler;
20 typedef struct FSM FSM;
21 typedef struct FSMSignals FSMSignals;
22 
23 /* -------------------------------------------------------------------------- */
24 /*                             private数据类型                                */
25 /* -------------------------------------------------------------------------- */
31 typedef struct FSMDataLoader
32 {   
33     void *shadowData;
34     int isReady;
35     int isOverflow;
36     size_t size;
37 }FSMDataLoader;
38 
39 
44 typedef struct FSMPrivateVars
45 {
46     int numState;  
47     int defaultState; 
49     int curState;    
50     int nextState;   
51     int index;  
53     FSMHandler fcns;    
54 
55     int numChild;
56     FSM **childFSM;
57 
58     FSMDataLoader preloader;    
60 }FSMPrivateVars;
61 
62 FSMPrivateVars* newFMSPrivateVars(int numState, int defaultState){
63     typedef void (*StateFuncPtr)(void *); 
64     typedef void (*ChildFSMStepFuncPtr)(FSM **); 
65     typedef int (*TransitionFuncPtr)(void *, int *);
66     typedef int (*SelectNextStateFcnPtr)(void *, FSMSignals *);
67 
68     FSMPrivateVars *privateVars = calloc(1, sizeof(FSMPrivateVars));
69     privateVars->numState = numState;
70     privateVars->defaultState = defaultState;
71     privateVars->curState = 0;
72     privateVars->nextState = 0;
73     privateVars->numChild = 0;
74 
75     privateVars->preloader.isReady = 0;
76 
77     privateVars->fcns.duringActionTable = calloc(numState,  sizeof(StateFuncPtr));
78     privateVars->fcns.enterActionTable = calloc(numState, sizeof(StateFuncPtr));
79     privateVars->fcns.exitActionTable = calloc(numState, sizeof(StateFuncPtr));
80     
81     privateVars->fcns.selectNextState = calloc(numState, sizeof(SelectNextStateFcnPtr));
82     privateVars->fcns.transitionTable = calloc(numState * numState, sizeof(TransitionFuncPtr));
83     privateVars->fcns.transitionGeneralAction = calloc(1, sizeof(StateFuncPtr));
84 
85     privateVars->fcns.childFSMStepTable = calloc(numState * numState, sizeof(ChildFSMStepFuncPtr));
86     
87     return privateVars;
88 
89 }
90 
91 #endif

FSM_OOP/baseFSM/FSM_protected.h File Reference

状态机框架的protected函数和变量，在继承的子类中可使用，在外界不可使用 
#include "FSM_public.h"
Data Structures
struct FSMHandler状态处理函数集合，全部需要子类重新填充内容 
struct FSMSignals状态机用的信号，protected子类可以访问 
struct FSMPureVtbl纯虚函数，子类必须重新实现，父类构造函数不会初始化 
Macros
#define Idle_Signal  0
Typedefs
typedef struct FSMHandler FSMHandler
状态处理函数集合，全部需要子类重新填充内容 

typedef struct FSMSignals FSMSignals
状态机用的信号，protected子类可以访问 

typedef struct FSMPureVtbl FSMPureVtbl
纯虚函数，子类必须重新实现，父类构造函数不会初始化 

Functions
void stepBaseFSM (FSM *pFSM)
运行状态机，状态机框架的核心函数 

Detailed Description
状态机框架的protected函数和变量，在继承的子类中可使用，在外界不可使用 

Author
天神 (dalaoshi@stu.xjtu.edu.cn) 
Version
2.1 
Date
2024-05-07
Copyright
天神创意无限公司 2024 

Macro Definition Documentation
#define Idle_Signal  0
啥意思？ 

Typedef Documentation
typedef struct FSMHandler FSMHandler

状态处理函数集合，全部需要子类重新填充内容 
这个结构体暴露给protect文件，方便在子类中添加函数。不然添加函数得再写个函数添加， 有点麻烦 

Function Documentation
void stepBaseFSM (FSM *  pFSM)

运行状态机，状态机框架的核心函数 
共分为三部首先判断是否有状态机切换， 
Parameters
pFSM 	状态机基类指针 	

FSM_protected.h
Go to the documentation of this file.
1 
12 #ifndef __FSM_PROTECTED_H_
13 #define __FSM_PROTECTED_H_
14 
15 #include "FSM_public.h"
16 
17 // #define DSP28377
18 #ifdef      DSP28377
19     #define     NDEBUG
20 #else
21     #define     FSM_LOG_ON
22 #endif
23 
24 
25 #ifdef          FSM_LOG_ON
26     #define     FSM_LOG(...)   printf(__VA_ARGS__)
27 #else
28     #define     FSM_LOG(...)   ((void)0)
29 #endif
30 
31 #define     during  during
32 #define     enter   enter
33 #define     exit    exit
34 
35 #define     selectNextStateFcn(curstate)                       curstate##_##selectNextState
36 #define     transitionHandler(curstate, nextstate)             curstate##2##nextstate##Handler
37 #define     actionFcn(action, curstate)                        curstate##_##action##Action
38 #define     index(curstate, nextstate)                         (curstate*Count_State + nextstate)
39 
40 #define     addSelectNextStateFcn(curstate)                         fcns->selectNextState[curstate] = (Avoid_WARNING_SelectNextState)selectNextStateFcn(curstate)
41 #define     addStateActionFcn(type, state)                          fcns->type##ActionTable[state] = (Avoid_WARNING_State_Fcns)actionFcn(type, state)
42 #define     addTransitionHandler(curstate, nextstate)               fcns->transitionTable[index(curstate, nextstate)] = (Avoid_WARNING_Transition_Handler)transitionHandler(curstate, nextstate)
43 
44 
45 
46 typedef struct FSMHandler FSMHandler;
47 typedef struct FSM FSM;
48 typedef struct FSMSignals FSMSignals;
49 typedef struct FSMPrivateVars FSMPrivateVars;
50 
51 typedef void (*Avoid_WARNING_State_Fcns)(void *data, FSM **childFSM);
52 typedef void (*Avoid_WARNING_General_Handlers)(void *data);
53 typedef void (*Avoid_WARNING_Transition_Handler)(void *data);
54 typedef void (*Avoid_WARNING_Overrider_Fcns)(FSM *pFSM);
55 typedef void (*Avoid_WARNING_ResetSignals_Fcns)(FSMSignals* signals, void *);
56 typedef void (*Avoid_WARNING_loadData_Fcns)(void*);
57 typedef int (*Avoid_WARNING_SelectNextState)(void *data, FSMSignals* signals);
58 typedef void (*Avoid_WARNING_void_fcns)(FSMHandler *fcns);
59 
60 
67 typedef struct FSMHandler{
68     int (**selectNextState)(void *data, FSMSignals* signals); 
70     void (**transitionTable)(void *data);
71     void (*transitionGeneralAction)(void *data);
72 
73     void (**enterActionTable)(void *data, FSM **cFSM);
74     void (**duringActionTable)(void *data, FSM **cFSM);
75     void (**exitActionTable)(void *data, FSM **cFSM);
76 
77     void (**childFSMStepTable)(FSM **cFSM);
78 }FSMHandler;
79 
84 typedef struct FSMSignals
85 {
86     Uint16 external;    
87     Uint16 lastTriggeredSignal; 
88 }FSMSignals;
89 #define     Idle_Signal     0 
91 /* -------------------------------------------------------------------------- */
92 /*             纯虚函数，子类必须重新实现，父类构造函数不会初始化               */
93 /* -------------------------------------------------------------------------- */
94 
99 typedef struct FSMPureVtbl {
100     // 状态机控制
101     void (*setupHandler)(FSMHandler *fcns);    
103     void (*initData)(FSM *pFSM);    
104     void (*loadExternalData)(void *extData);    
106     void (*initDataLoader)(FSM *pFSM);    
108     void (*initSignals)(FSM *pFSM);    
109     void (*resetSignals)(FSMSignals *signalRegs, void *data);   
110 }FSMPureVtbl;
111 
112 
113 
114 /* -------------------------------------------------------------------------- */
115 /*                          基类定义，纯虚类                                  */
116 /* -------------------------------------------------------------------------- */
117 typedef struct FSM
118 {
119     // public functions
120     void (*step)(FSM *pFSM);    // 子类重新实现可以添加一些打印信息
121     void (*reset)(FSM *pFSM);   // 子类重新实现可以添加一些打印信息
122 
123     // protected
124     FSMPureVtbl pureVtbl;
125     
126     void *data;
127     FSMSignals signals;
128     // private
129     FSMPrivateVars* privateVars; 
130 } FSM;
131 
132 
133 /* -------------------------------------------------------------------------- */
134 /*                    protected函数，子类能用外界用不了                        */
135 /* -------------------------------------------------------------------------- */
136 void stepBaseFSM(FSM *pFSM);
137 void constructFSM(FSM* pFSM);
138 FSM*  newBaseFSM(int numState, int defaultState);
139 void resetBaseFSM(FSM *pFSM);
140 
141 
142 /* -------------------------------------------------------------------------- */
143 /*                               内联函数                                    */
144 /* -------------------------------------------------------------------------- */
145 static inline void clearSignal(FSMSignals *signals, Uint16 signalFlag){
146     signals->external &= ~(1 << signalFlag);
147 }
148 static inline Uint16 getSignal(FSMSignals *signals, Uint16 signalFlag){
149     return (signals->external >> signalFlag) & 1;
150 }
151 static inline void clearAllSignals(FSMSignals *signals){
152     signals->external = 0;
153 }
154 
155 
156 #endif

FSM_public.h
1 /*
2  * @Author: godcreator02 qq974980621@gmail.com
3  * @Date: 2024-04-20 18:52:14
4  * @LastEditors: godcreator02 qq974980621@gmail.com
5  * @LastEditTime: 2024-04-23 21:51:55
6  * @FilePath: \vscode\FSM_OOP\baseFSM\FSM_public.h
7  * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
8  */
9 #ifndef __FSM_PUBLIC_H_
10 #define __FSM_PUBLIC_H_
11 
12 typedef unsigned int       Uint16;
13 typedef unsigned long        Uint32;
14 
15 #define     BASE_FSM(ptr)          ((FSM *)(ptr))
16 #define     BASE_PTR               (FSM *)
17 
18 typedef struct FSM FSM;
19 
20 /* -------------------------------- 避免警告的函数定义 ------------------------------- */
21 #define     Step(pFSM)                       _stepFSM((FSM *)pFSM)
22 #define     PreloaderGetReady(pFSM)          _preloaderGetReady((FSM *)pFSM);
23 #define     GetPreloaderOverFlag(pFSM)       _getPreloaderOverFlag((FSM *)pFSM)
24 #define     ClearPreloaderOverFlag(pFSM)     _clearPreloaderOverFlag((FSM *)pFSM)
25 #define     GetData(pFSM)                    _getData((FSM*)pFSM)
26 #define     SetSignal(pFSM, signal)          _setSignal((FSM*)pFSM, signal)
27 
28 
29 
30 void _stepFSM(FSM *pFSM);
31 /* --------------------------------- 状态机信息获取 -------------------------------- */
32 int getFSMCurState(FSM *pFSM);
33 int getFSMNextState(FSM *pFSM);
34 void setChildNum(FSM *pFSM, int num);
35 void registerChildFSM(FSM *parent, FSM *child, int index);
36 
37 /* ------------------------------ Data和Signals ------------------------------ */
38 const void *_getData(FSM* pFSM);
39 void _setSignal(FSM *pFSM, Uint16 signalFlag);
40 
41 /* ---------------------------------- 预装载器 ---------------------------------- */
42 void *_preloadIn(FSM *pFSM);
43 void _preloaderGetReady(FSM *pFSM);
44 int _getPreloaderOverFlag(FSM *pFSM);
45 void _clearPreloaderOverFlag(FSM *pFSM);
46 
47 
48 
49 #endif

childFSM.h
1 #ifndef __CHILD_FSM_H_
2 #define __CHILD_FSM_H_
3 
4 #define     ChildFSM_Input(pFSM)      ((ChildFSMData *)_preloadIn((FSM *)pFSM))
5 
6 typedef struct _childFSMData
7 {
8     int arrindex;
9 }ChildFSMData;
10 
11 typedef struct _ChildFSM ChildFSM;
12 
13 ChildFSM *createChildFSM();
14 
15 
16 
17 #endif

childFSM_private.h
1 #ifndef __CHILD_PRIVATE_FSM_H_
2 #define __CHILD_PRIVATE_FSM_H_
3 
4 #include "FSM_protected.h"
5 #include "childFSM.h"
6 #include <stdio.h>
7 
8 
9 /* 
10     继承基类
11  */
12 typedef struct _ChildFSM
13 {   
14     // 继承父类
15     FSM base;
16 }ChildFSM;
17 
18 /* 
19     状态和对应的 action, exit, during 函数
20  */
21 
22 typedef enum _State
23 {
24     Idle,
25     D,
26     E,
27 
28     Count_State,
29 } State;
30 #define     DEFAULT_STATE       E
31 
32 void actionFcn(enter, D)()
33 {
34     FSM_LOG(" enterD ");
35 }
36 void actionFcn(during, D)()
37 {
38     FSM_LOG(" duringD ");
39 }
40 void actionFcn(exit, D)()
41 {
42     FSM_LOG(" exitD ");
43 }
44 
45 void actionFcn(enter, E)()
46 {
47     FSM_LOG(" enterE ");
48 }
49 void actionFcn(during, E)()
50 {
51     FSM_LOG(" duringE ");
52 }
53 void actionFcn(exit, E)()
54 {
55     FSM_LOG(" exitE ");
56 }
57 
58 /* 
59     事件和对应的转移函数
60  */
61 
62 typedef enum _Event{
63     Idle_Event,
64     Idle2D,
65     Idle2E,
66     D2E,
67     E2D,
68 
69     Count_Event,
70 }Event;
71 
72 
73 State transitionHandler(Idle2D)()
74 {
75     FSM_LOG(" Idle2D ");
76     return D;
77 }
78 
79 State transitionHandler(Idle2E)()
80 {
81     FSM_LOG(" Idle2E ");
82     return E;
83 }
84 
85 State transitionHandler(D2E)()
86 {
87     FSM_LOG(" D2E ");
88     return E;
89 }
90 
91 State transitionHandler(E2D)()
92 {
93     FSM_LOG(" E2D ");
94     return D;
95 }
96 
97 
98 
99 
100 /* 
101     用户自定义事件选择逻辑—————————————————begin 
102 */
103 const static char *stateStr[] = {
104     "Idle",
105     "D",
106     "E",
107     };
108 const static char *eventStr[] = {
109     "Idle",
110     "Idle2D",
111     "Idle2E",
112     "D2E",
113     "E2D",
114 };
115 static void printFSM(ChildFSM* pFSM){
116     FSM_LOG(" \t\tChildFSM: ");
117     FSM_LOG("当前状态:%s, 当前事件:%s, 动作：", stateStr[getFSMCurState((FSM *)pFSM)], eventStr[getFSMCurEvent((FSM *)pFSM)]);
118 }
119 
120 
121 #endif

parentFSM.h
1 #ifndef __PARENT_FSM_H_
2 #define __PARENT_FSM_H_
3 
4 #define     ParentFSM_Input(pFSM)      ((ParentFSMData *)_preloadIn((FSM *)pFSM))
5 
6 typedef struct _parentFSMData
7 {
8     int arrindex;
9 }ParentFSMData;
10 typedef struct _ParentFSM ParentFSM;
11 
12 ParentFSM *createParentFSM();
13 
14 
15 #endif

keyFSM.h
1 #ifndef __KEY_FSM_H_
2 #define __KEY_FSM_H_
3 
4 
5 #define     KeyFSM_Input(pFSM)      (KeyIn *)_preloadIn((FSM *)pFSM)
6 
7 typedef enum _keyIn{
8     pUp,
9     pDown,
10 }KeyIn;
11 
12 
13 typedef enum _keyOut{
14     Idle,
15     Down,
16     Hold,
17     Down_2times,
18     Down_3times,
19 }KeyOut;
20 
21 const static char *keyStr[] = 
22 {
23     "Idle",
24     "Down",
25     "Hold",
26     "Down_2times",
27     "Down_3times",
28 };
29 
30 typedef struct _keyFSMExternalData
31 {
32     KeyIn in;
33 }KeyFSMExternalData;
34 
35 typedef struct _keyFSMInternalData
36 {
37     int countDelay;
38     int countMultiDown;
39     KeyOut out;
40 
41 }KeyFSMInternalData;
42 
43 typedef struct _keyFSMData
44 {
45     KeyFSMExternalData external;
46     KeyFSMInternalData internal;
47 }KeyFSMData;
48 
49 typedef struct _KeyFSM KeyFSM;
50 
51 KeyFSM *createKeyFSM();
52 
53 
54 
55 #endif

keyFSM_private.h
1 #ifndef __KEY_PRIVATE_FSM_H_
2 #define __KEY_PRIVATE_FSM_H_
3 
4 #include "FSM_protected.h"
5 #include "keyFSM.h"
6 #include <stdio.h>
7 #include <assert.h>
8 
9 extern KeyIn keystat;
10 
11 
12 /* 
13     继承基类
14  */
15 typedef struct _KeyFSM
16 {   
17     // 继承父类
18     FSM base;
19 }KeyFSM;
20 
21 /* 
22     状态和对应的 action, exit, during 函数
23  */
24 
25 typedef enum _State
26 {
27     Idle_State,
28     Waiting,
29     DownDebouncing,
30     Downing,
31     Holding,
32     HoldUpDebouncing,
33     UpDebouncing,
34     MultiDownWaiting,
35     MultiDowning,
36     MultiDownDebouncing,
37 
38     Count_State,
39 } State;
40 #define     DEFAULT_STATE       Waiting
41 
42 /* -------------------------------------------------------------------------- */
43 /*                         下一个状态选择函数                                 */
44 /* -------------------------------------------------------------------------- */
45 static State selectNextStateFcn(Waiting)(KeyFSMData *data){
46     
47     if(data->external.in == Down){
48         FSM_LOG(" 物理按键按下 ");
49         return DownDebouncing;
50     }
51     return Idle_State;
52 }
53 
54 static State selectNextStateFcn(DownDebouncing)(KeyFSMData *data){
55     if(data->external.in == pUp){
56         FSM_LOG(" 物理按键松开 ");
57         return Waiting;
58     }
59     
60     else if(data->internal.countDelay == 5){
61         return Downing;
62     }
63     return Idle_State;
64 }
65 
66 static State selectNextStateFcn(Downing)(KeyFSMData *data){
67     if(data->external.in == pUp){
68         FSM_LOG(" 物理按键松开 ");
69         return UpDebouncing;
70     }
71     else if(data->internal.countDelay == 10){
72         return Holding;
73     }
74     return Idle_State;
75 }
76 
77 static State selectNextStateFcn(Holding)(KeyFSMData *data){
78     if(data->external.in == pUp){
79         FSM_LOG(" 物理按键松开 ");
80         return HoldUpDebouncing;
81     }
82     return Idle_State;
83 }
84 
85 static State selectNextStateFcn(HoldUpDebouncing)(KeyFSMData *data){
86     if(data->external.in == Down){
87         FSM_LOG(" 物理按键按下 ");
88         return Holding;
89     }
90     else if(data->internal.countDelay == 5){
91         return Waiting;
92     }
93     return Idle_State;
94 }
95 
96 static State selectNextStateFcn(UpDebouncing)(KeyFSMData *data){
97     if(data->external.in == Down){
98         FSM_LOG(" 物理按键按下 ");
99         if(data->internal.countMultiDown){
100             return MultiDowning;
101         }
102         else{
103             return Downing;
104         }
105     }
106     else if(data->internal.countDelay == 5){
107         return MultiDownWaiting;
108     }
109 
110     return Idle_State;
111 }
112 
113 static State selectNextStateFcn(MultiDownWaiting)(KeyFSMData *data){
114     if(data->external.in == Down){
115         FSM_LOG(" 物理按键按下 ");
116         return MultiDownDebouncing;
117     }
118     else if(data->internal.countDelay == 5){
119         return Waiting;
120     }
121     return Idle_State;
122 }
123 
124 static State selectNextStateFcn(MultiDowning)(KeyFSMData *data){
125     if(data->external.in == pUp){
126         FSM_LOG(" 物理按键松开 ");
127         return UpDebouncing;
128     }
129     return Idle_State;
130 }
131 
132 static State selectNextStateFcn(MultiDownDebouncing)(KeyFSMData *data){
133     if(data->external.in == pUp){
134         FSM_LOG(" 物理按键松开 ");
135         return MultiDownWaiting;
136     }
137     else if(data->internal.countDelay == 5){
138         return MultiDowning;
139     }
140     return Idle_State;
141 }
142 
143 /* -------------------------------------------------------------------------- */
144 /*                    对应的 action, exit, during 函数                        */
145 /* -------------------------------------------------------------------------- */
146 static void actionFcn(enter, Waiting)(KeyFSMData* data){
147     data->internal.out = Idle;
148     data->internal.countDelay = 0;
149     data->internal.countMultiDown = 0;
150 }
151 
152 
153 static void actionFcn(during, DownDebouncing)(KeyFSMData* data)
154 {
155     data->internal.countDelay++;
156     FSM_LOG(" during开启消抖 ");
157 }
158 
159 static void actionFcn(during, Downing)(KeyFSMData* data)
160 {
161  
162     data->internal.countDelay++;
163     data->internal.out = Idle;
164 }
165 
166 
167 
168 static void actionFcn(during, UpDebouncing)(KeyFSMData* data){
169  
170     data->internal.countDelay++;
171     FSM_LOG(" during松开消抖 ");
172 }
173 
174 
175 static void actionFcn(during, Holding)(KeyFSMData* data){
176     data->internal.out = Idle;
177     data->internal.countDelay++;
178     if(data->internal.countDelay == 2){
179         data->internal.out = Hold;
180         data->internal.countDelay = 0;
181     }
182 }
183 
184 static void actionFcn(exit, Holding)(KeyFSMData* data){
185     data->internal.out = Idle;
186 }
187 
188 
189 
190 static void actionFcn(during, MultiDownWaiting)(KeyFSMData* data){
191     data->internal.countDelay++;
192     FSM_LOG(" during多击等待 ");
193 }
194 
195 
196 
197 static void actionFcn(during, HoldUpDebouncing)(KeyFSMData* data)
198 {
199     data->internal.countDelay++;
200 }
201 
202 
203 static void actionFcn(during, MultiDownDebouncing)(KeyFSMData* data)
204 {
205     data->internal.countDelay++;
206 }
207 
208 
209 /* -------------------------------------------------------------------------- */
210 /*                                转移函数                                    */
211 /* -------------------------------------------------------------------------- */
212 void transitionGeneralAction(KeyFSMData *data){
213     data->internal.countDelay = 0;
214 }
215 
216 static State transitionHandler(DownDebouncing, Downing)(KeyFSMData* data){
217     data->internal.out = Down;
218 }
219 
220 
221 static State transitionHandler(UpDebouncing, MultiDownWaiting)(KeyFSMData* data){
222     data->internal.out = Idle;
223     data->internal.countMultiDown++;
224 }
225 
226 static State transitionHandler(MultiDownDebouncing, MultiDowning)(KeyFSMData* data){
227     switch (data->internal.countMultiDown + 1)
228     {
229     case 2:
230         data->internal.out = Down_2times;
231         break;
232     case 3:
233         data->internal.out = Down_3times;
234         break;
235     
236     default:
237         printf("reach MultiDown Limit!\n");
238         break;
239     }
240     FSM_LOG(" 多击%d ", data->internal.countMultiDown+1);
241 }
242 
243 
244 static State transitionHandler(Downing, Holding)(KeyFSMData* data){
245     data->internal.out = Hold;
246 }
247 
248 static State transitionHandler(MultiDownWaiting, Waiting)(KeyFSMData* data){
249     data->internal.countMultiDown = 0;
250 }
251 
252 
253 
254 /* 
255     用户自定义事件选择逻辑—————————————————begin 
256 */
257 const static char *stateStr[] = {
258     "Idle",
259     "Waiting",
260     "DownDebouncing",
261     "Downing",
262     "Holding",
263     "HoldUpDebouncing",
264     "UpDebouncing",
265     "MultiDownWaiting",
266     "MultiDowning",
267     "MultiDownDebouncing",
268     };
269 
270 static void printFSM(KeyFSM* pFSM){
271     KeyFSMData* data = pFSM->base.data;
272     FSM_LOG("KeyFSM: ");
273     FSM_LOG("%d, 当前状态:%s, \t上一个下次事件:%s \t转移：",  data->internal.countDelay, stateStr[getFSMCurState((FSM *)pFSM)], stateStr[getFSMNextState((FSM *)pFSM)]);
274 }
275 
276 
277 #endif

templateFSM.h
1 #ifndef __TEMPLATE_FSM_H_
2 #define __TEMPLATE_FSM_H_
3 
4 /* -------------------------------------------------------------------------- */
5 /*                               提前做类型声明                               */
6 /* -------------------------------------------------------------------------- */
7 
8 
9 /* -------------------------------------------------------------------------- */
10 /*                               自定义数据和信号                              */
11 /* -------------------------------------------------------------------------- */
16 enum TemplateFSMSignals{
17     Signal_A = 1, 
18     Signal_B,
19 };
20 
25 typedef struct _templateFSMExternalData
26 {
27     int arrindex;
28     int x;
29 }TemplateFSMExternalData;
30 
31 
36 typedef struct _templateFSMInnerData
37 {
38     int y1;
39     int inner1;
40 }TemplateFSMInnerData;
41 
42 /* -------------------------------------------------------------------------- */
43 /*                           下面的改个名字就行了                             */
44 /* -------------------------------------------------------------------------- */
45 #define     TemplateFSM_Input(pFSM)          ((TemplateFSMExternalData *)_preloadIn((FSM *)pFSM))
46 
47 typedef struct _templateFSMData
48 {
49     TemplateFSMExternalData external;
50     TemplateFSMInnerData internal;
51 }TemplateFSMData;
52 
53 typedef struct _TemplateFSM TemplateFSM;
54 TemplateFSM *createTemplateFSM();
55 
56 
57 #endif

templateFSM_private.h
1 #ifdef __TEMPLATE_PRIVATE_FSM_H_
2 #error This file can only be included once!!!
3 
4 #else
5 #define __TEMPLATE_PRIVATE_FSM_H_
6 
7 #include "FSM_protected.h"
8 #include "templateFSM.h"
9 #include <stdio.h>
10 
11 /* -------------------------------------------------------------------------- */
12 /*                            用到的外部变量                                  */
13 /* -------------------------------------------------------------------------- */
14 extern int arrindex;
15 extern int x;
16 
17 
18 /* -------------------------------------------------------------------------- */
19 /*                                继承基类                                    */
20 /* -------------------------------------------------------------------------- */
21 typedef struct _TemplateFSM
22 {   
23     FSM base;
24 }TemplateFSM;
25 
26 /* -------------------------------------------------------------------------- */
27 /*                               状态定义                                     */
28 /* -------------------------------------------------------------------------- */
29 typedef enum _State
30 {
31     Idle,
32     D,
33     E,
34 
35     Count_State,
36 } State;
37 #define     DEFAULT_STATE       E
38 
39 /* -------------------------------------------------------------------------- */
40 /*                         下一个状态选择函数                                 */
41 /* -------------------------------------------------------------------------- */
42 
43 
44 static State selectNextStateFcn(D)(TemplateFSMData *data, FSMSignals *signals){
45     if(getSignal(signals, Signal_A)){
46         signals->lastTriggeredSignal = Signal_A;
47         return E;
48     }
49     else if(arrindex == 0){
50         return E;
51     }
52     return Idle;
53 }
54 
55 static State selectNextStateFcn(E)(TemplateFSMData *data, FSMSignals *signals){
56     if(getSignal(signals, Signal_A)){
57         signals->lastTriggeredSignal = Signal_A;
58         return D;
59     }
60     else if(x == 0){
61         return D;
62     }
63     return Idle;
64 }
65 
66 
67 
68 /* -------------------------------------------------------------------------- */
69 /*                    对应的 action, exit, during 函数                        */
70 /* -------------------------------------------------------------------------- */
71 static void actionFcn(enter, D)()
72 {
73     FSM_LOG(" enterD ");
74 }
75 static void actionFcn(during, D)()
76 {
77     FSM_LOG(" duringD ");
78 }
79 static void actionFcn(exit, D)()
80 {
81     FSM_LOG(" exitD ");
82 }
83 
84 static void actionFcn(enter, E)()
85 {
86     FSM_LOG(" enterE ");
87 }
88 static void actionFcn(during, E)()
89 {
90     FSM_LOG(" duringE ");
91 }
92 static void actionFcn(exit, E)()
93 {
94     FSM_LOG(" exitE ");
95 }
96 
97 
98 
99 /* -------------------------------------------------------------------------- */
100 /*                                转移函数                                    */
101 /* -------------------------------------------------------------------------- */
102 static void transitionGeneralAction(TemplateFSMData *data){
103     FSM_LOG("general");
104 }
105 
106 static void transitionHandler(Idle, D)()
107 {
108     FSM_LOG(" Idle2D ");
109 }
110 
111 static void transitionHandler(Idle, E)()
112 {
113     FSM_LOG(" Idle2E ");
114 }
115 
116 static void transitionHandler(D, E)(TemplateFSMData *data, int *delayedIndex)
117 {
118     *delayedIndex = index(D, E);
119     FSM_LOG(" D2E ");
120 }
121 
122 static void transitionHandler(E, D)()
123 {
124     FSM_LOG(" E2D ");
125 }
126 
127 
128 /* -------------------------------------------------------------------------- */
129 /*                                打印调试                                    */
130 /* -------------------------------------------------------------------------- */
131 const static char *stateStr[] = {
132     "Idle",
133     "D",
134     "E",
135 };
136 
137 static void printFSM(TemplateFSM* pFSM){
138     FSM_LOG(" \tTemplateFSM: ");
139     FSM_LOG("预装载器溢出:%d, 当前状态:%s, 动作：", _getPreloaderOverFlag((FSM *)pFSM), stateStr[getFSMCurState((FSM *)pFSM)]);
140 }
141 
142 
143 #endif

Index
INDEX