chen
2024-09-20 292ed46c6066d47289f1330b1c2bcc6d74761f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*******************************************************************************
 * File Name         : PointTable.h
 * Description       :
 * Created on        : 2019Äê5ÔÂ12ÈÕ
 * Author            : www.hido-studio.com
 *******************************************************************************/
#ifndef _HIDO_FSM_H_
#define _HIDO_FSM_H_
 
/*******************************************************************************
 *                              Include Files                                  *
 *******************************************************************************/
#include "HIDO_TypeDef.h"
#include "stdarg.h"
 
/*******************************************************************************
 *                                  Macro                                      *
 *******************************************************************************/
#define HIDO_FSM_DECLARE(name) \
    extern HIDO_FSMStruct g_stFSM##name                                         /* ÉùÃ÷Ò»¸ö״̬»ú */
 
#define HIDO_FSM_STATE_EXTERN(name) \
    extern const HIDO_StateStruct g_stState##name                               /* ÉùÃ÷Ò»¸ö״̬ */
    
#define HIDO_FSM_DECLARE_IMPLEMENT(name) \
    HIDO_FSMStruct g_stFSM##name = { HIDO_NULL };                               /* ÊµÏÖÒ»¸ö״̬»ú */
 
#define HIDO_FSM_STATE_DECLARE(name) \
    extern const HIDO_StateStruct g_stState##name; \
    HIDO_INT32 name##Proc(HIDO_FSMStruct *_pstFSM, HIDO_UINT32 _u32Event, void *_pArg);
                                                                                /* ÉùÃ÷״̬»úµÄÒ»¸ö״̬,[name]״̬µÄÃû×Ö */
 
#define HIDO_FSM_DETACHED_STATE(name) \
    extern HIDO_StateStruct g_stState##name; \
    HIDO_INT32 name##Proc(HIDO_FSMStruct *_pstFSM, HIDO_UINT32 _u32Event, void *_pArg);
                                                                                /* ÉùÃ÷״̬»úµÄÒ»¸ö״̬,[name]״̬µÄÃû×Ö */
 
#define HIDO_FSM_STATE_FULL_DECLARE(parent, name) HIDO_FSM_STATE_DECLARE(name)
 
#define HIDO_FSM_STATE_IMPLEMENT(name, parent, init) \
    const HIDO_StateStruct g_stState##name = { #name, parent, init, name##Proc }; \
    HIDO_INT32 name##Proc(HIDO_FSMStruct *_pstFSM, HIDO_UINT32 _u32Event, void *_pArg)
                                                                                /* ÊµÏÖÒ»¸ö״̬ */
 
#define HIDO_FSM_DETACHED_STATE_IMPLEMENT(name, parent, init) \
    HIDO_StateStruct g_stState##name = { #name, parent, init, name##Proc }; \
    HIDO_INT32 name##Proc(HIDO_FSMStruct *_pstFSM, HIDO_UINT32 _u32Event, void *_pArg)
                                                                                /* ÊµÏÖÒ»¸ö״̬ */
#define HIDO_FSM_DETACHED_STATE_SETTLE(name, parent, init) \
    g_stState##name.m_pstParent = (parent);\
    g_stState##name.m_pstInit = (init);
                                                                                /* ¶¯Ì¬ÐÞ¸Ä״̬µÄ¹ØÁª¹ØÏµ£¬ÐÞ¸ÄËüµÄ·ò״̬ºÍ×Ó״̬ */
#define HIDO_FSM(name) (&g_stFSM##name)
#define HIDO_FSM_STATE(name) (&g_stState##name)
 
#define HIDO_EVENT_ENTRY                                          0             /* ½øÈë״̬µÄʼþ */
#define HIDO_EVENT_EXIT                                           1             /* Í˳ö״̬µÄʼþ */
#define HIDO_EVENT_ENTRY_NAME                                     "EVENT_ENTRY" /* ½øÈë״̬µÄʼþÃû×Ö */
#define HIDO_EVENT_EXIT_NAME                                      "EVENT_EXIT"  /* Í˳ö״̬µÄʼþÃû×Ö */
 
#define HIDO_EVENT_OK                                             0             /* Ê¼þÖ´ÐÐOK */
#define HIDO_EVENT_NO_PROC                                        1             /* µ±Ç°×´Ì¬²»ÄÜ´¦ÀíÄ¿±êʼþ£¬ÐèÒªÓɸ¸×´Ì¬Íê³É */
#define HIDO_STATE_MAX_DEPTH                                      10            /* ×´Ì¬»ú״̬ǶÌ×µÄ×î´óÉî¶È */
 
/*******************************************************************************
 *                             Type Definition                                 *
 *******************************************************************************/
typedef struct HIDO_StateStruct HIDO_StateStruct;
typedef struct HIDO_StateListStruct HIDO_StateListStruct;
typedef struct HIDO_FSMStruct HIDO_FSMStruct;
typedef struct HIDO_FSMQueueStruct HIDO_FSMQueueStruct;
typedef struct HIDO_FSMQueueDataStruct HIDO_FSMQueueDataStruct;
 
/* ×´Ì¬²ÎÊý */
typedef struct
{
    HIDO_VOID *m_pArg;                                                          /* Óû§²ÎÊýÖ¸Õë */
    const HIDO_StateStruct *m_pstAfterState;                                    /* Ê¼þÖ´ÐÐÍê½øÈëµÄÏÂÒ»¸ö״̬ */
} HIDO_StateArgStruct;
 
/* ×´Ì¬µÄʵÏÖ */
typedef HIDO_INT32 (* HIDO_DebugFunc)(HIDO_FSMStruct *_pstStateMachine, HIDO_INT32 _i32Level, HIDO_CHAR *_pcFmt, va_list _ap);
typedef HIDO_INT32 (* HIDO_StateFunc)(HIDO_FSMStruct *, HIDO_UINT32, void *);
struct HIDO_StateStruct
{
    const char *m_pcName;
    const HIDO_StateStruct *m_pstParent;
    const HIDO_StateStruct *m_pstInit;
    HIDO_StateFunc m_fnProc;
};
 
/* Î´Ê¹Óà*/
struct HIDO_FSMQueueDataStruct
{
    HIDO_UINT32 m_u32Event;
    void *m_pArg;
};
 
/* Î´Ê¹Óà*/
struct HIDO_FSMQueueStruct
{
    HIDO_UINT32 m_u32Cnt;
    HIDO_UINT32 m_u32Rear;
    HIDO_UINT32 m_u32Front;
    HIDO_UINT32 m_u32Size;
    HIDO_FSMQueueDataStruct *m_pstBuff;
};
 
/* ×´Ì¬»úµÄʵÏֽṹÌå */
struct HIDO_FSMStruct
{
#define HIDO_FSM_DBG_FLAG_OFF      0                                            /* ¿ªÈÕÖ¾ */
#define HIDO_FSM_DBG_FLAG_ON       1                                            /* ¹ØÈÕÖ¾ */
 
    HIDO_UINT16 m_u16ID;                                                        /* ×´Ì¬»úID Î´Ê¹Óà*/
    HIDO_UINT16 m_u16DbgFlag;                                                   /* ×´Ì¬»úµ÷ÊÔ´òÓ¡¿ª¹Ø */
    const HIDO_StateStruct *m_pstCurrentState;                                  /* ×´Ì¬»úµ±Ç°µÄ״̬ */
    void *m_pPrivateData;                                                       /* ×´Ì¬»ú˽ÓÐÊý¾Ý */
    const char *m_pcName;                                                       /* ×´Ì¬»úÃû×Ö */
    const char * const*m_ppcEventName;                                          /* ×´Ì¬»úʼþÃû×ÖÃû×Ö */
    HIDO_FSMStruct *m_pstNext;                                                  /* Î´Ê¹Óà*/
    HIDO_FSMQueueStruct m_stEventQueue;                                         /* Î´Ê¹Óà*/
    HIDO_DebugFunc m_fnDebugFunc;
};
 
/* Î´Ê¹Óà*/
struct HIDO_StateListStruct
{
    const HIDO_StateStruct *m_pstState;
    HIDO_StateListStruct *m_pstNext;
};
 
/*******************************************************************************
 *                             Global Function                                 *
 *******************************************************************************/
/*******************************************************************************
 * Function Name     : HIDO_FSMEventExecute
 * Description       : ×´Ì¬»úÖ´ÐÐÒ»¸öʼþ
 * Input             : _ptStatMachine         ×´Ì¬»ú½á¹¹ÌåÖ¸Õë
 *                     _uEvent                ×´Ì¬»úµÄʼþ
 *                     _pArg                  ×´Ì¬»úÖ´ÐÐʼþʱ´«µÝµÄ²ÎÊý
 * Output            : None
 * Return            : None
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê07ÔÂ15ÈÕ
 *******************************************************************************/
void HIDO_FSMEventExecute(HIDO_FSMStruct *_ptStateMachine, HIDO_UINT32 _uEvent, void *_pArg);
 
/*******************************************************************************
 * Function Name     : HIDO_FSMRegister
 * Description       : ×´Ì¬»ú×¢²á
 * Input             : _ptStatMachine         ×´Ì¬»ú½á¹¹ÌåÖ¸Õë
 *                     _ptInitState           ×´Ì¬»úµÄ³õʼ״̬
 *                     _pcName                ×´Ì¬»úµÄÃû×Ö(ÓÃÓÚµ÷ÊÔ´òÓ¡)
 *                     _ppcEventName          ×´Ì¬»úʱ¼äµÄÃû×Ö(ÓÃÓÚµ÷ÊÔ´òÓ¡)
 *                     _pPrivateData          ×´Ì¬»ú˽ÓÐÊý¾Ý,¿ÉÒÔ¸ø×´Ì¬»ú¹ØÁªÒ»¸ö˽ÓÐÊý¾Ý
 *                     _u16DbgFlag            µ÷ÊÔ¿ª¹Ø HIDO_FSM_DBG_FLAG_OFF=¹Ø HIDO_FSM_DBG_FLAG_ON=¿ª
 * Output            : None
 * Return            : None
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê07ÔÂ15ÈÕ
 *******************************************************************************/
void HIDO_FSMRegister(HIDO_FSMStruct *_ptStatMachine, const HIDO_StateStruct *_ptInitState, const char *_pcName, const char * const*_ppcEventName,
        void *_pPrivateData, HIDO_UINT16 _u16DbgFlag);
 
/*******************************************************************************
 * Function Name     : DB_FSMRegisterDebugFunc
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : DuJian
 * Modified Date:    : 2019Äê11ÔÂ18ÈÕ
 *******************************************************************************/
HIDO_INT32 HIDO_FSMRegisterDebugFunc(HIDO_FSMStruct *_pstStatMachine, HIDO_DebugFunc _fnDebugFunc);
 
/*******************************************************************************
 * Function Name     : HIDO_FSMStateChange
 * Description       : ×´Ì¬»ú×´Ì¬Ç¨ÒÆ½Ó¿Ú
 * Input             : _ptStateMachine        ×´Ì¬»ú½á¹¹ÌåÖ¸Õë
 *                     _ptNewState            ÐµÄ״̬(×´Ì¬Ç¨ÒÆµÄÄ¿±ê״̬)
 *                     _pData                 ×´Ì¬Ç¨ÒÆÊ±Ð¯´øµÄÊý¾Ý
 * Output            : None
 * Return            : None
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê07ÔÂ15ÈÕ
 *******************************************************************************/
void HIDO_FSMStateChange(HIDO_FSMStruct *_ptStateMachine, const HIDO_StateStruct *_ptNewState, void *_pData);
 
/*******************************************************************************
 * Function Name     : HIDO_FSMStartTimer
 * Description       : ×´Ì¬»úÆô¶¯¶¨Ê±Æ÷½Ó¿Ú
 * Input             : _eTimerID              ¶¨Ê±Æ÷µÄID
 *                     _u8Type                ¶¨Ê±Æ÷µÄÀàÐÍ(ͬHIDO_Timer¡£hÖеÄÀàÐÍ)
 *                     _uTick                 ¶¨Ê±Æ÷µÄ¶¨Ê±tick(µ¥Î»ms)
 *                     _ptStatMachine         ×´Ì¬»ú½á¹¹ÌåÖ¸Õë
 *                     _uEvent                ¶¨Ê±Æ÷³¬Ê±ºó,Ïò״̬»ú·¢Ë͵Äʼþ
 * Output            : None
 * Return            : None
 * Author            : www.hido-studio.com
 * Modified Date:    : 2019Äê07ÔÂ15ÈÕ
 *******************************************************************************/
void HIDO_FSMStartTimer(HIDO_UINT32 _eTimerID, HIDO_UINT8 _u8Type, HIDO_UINT32 _uTick, HIDO_FSMStruct *_ptStatMachine, HIDO_UINT32 _uEvent);
 
#endif