本帖最后由 紫阳真人 于 2019-2-21 10:47 编辑
最近看别人的代码,运用了状态机,各种指针,比如下面,有些难懂,各位谁有类似的资料用来学习的。
EVENT_buf eventBuf;
FSM_T m_Fsm;/*在头文件中extern声明*/
ACT_TABLE_T stateInitActTable[] = {
{EVENT_INIT_OK, stateInitOKFun},
{EVENT_INIT_FAIL, stateInitFailFun}
};
ACT_TABLE_T stateSelftestActTable[] = {
{EVENT_R_MOVE_COMPLETE, stateSelftestRMoveCompleteFun},
{EVENT_L_MOVE_COMPLETE, stateSelftestLsMoveCompleteFun},
{EVENT_R_MOVE_COMPLETE, stateSelftestRsMoveCompleteFun},
{EVENT_R_MOVE_COMPLETE, stateSelftestRMoveCompleteFun},
{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun},
{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun},
{EVENT_R_SWITCH_INACTIVE, stateSelfTestRSwitchInActiveFun}
};
ACT_TABLE_T stateIdleActTable[] = {
{EVENT_SET_COL_RECT, stateIdleRtMoveFun},
{EVENT_SET_COL_SLOT, stateIdleSMoveFun},
{EVENT_SET_COL_ROT, stateIdleRMoveFun},
{EVENT_R_MOVE_COMPLETE, stateIdleRtMoveCompleteFun},
{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun}
//{EVENT_L_SWITCH_ACTIVE, stateSelfTestLSwitchActiveFun},
//{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun},
//{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun}
};
ACT_TABLE_T stateSlotActTable[] = {
{EVENT_SET_COL_RECT, stateIdleRMoveFun},
{EVENT_SET_COL_SLOT, stateSlotSMoveFun},
{EVENT_SET_COL_ROT, stateSlotRtMoveFun},
{EVENT_L_MOVE_COMPLETE, stateSlotLMoveCompleteFun},
{EVENT_R_MOVE_COMPLETE, stateSlotRMoveCompleteFun},
{EVENT_R_MOVE_COMPLETE, stateIdleRMoveCompleteFun},
{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun}
//{EVENT_LS_SWITCH_ACTIVE, stateSelfTestLSwitchActiveFun},
//{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun}
};
ACT_TABLE_T stateRotActTable[] = {
{EVENT_SET_COL_RECT, stateIdleRMoveFun},
{EVENT_SET_COL_ROT, stateRotRMoveFun},
{EVENT_SET_COL_SLOT, stateRotSMoveFun},
{EVENT_R_MOVE_COMPLETE, stateRotRMoveCompleteFun},
{EVENT_R_MOVE_COMPLETE, stateIdleRMoveCompleteFun},
{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun},
{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun}
};
ACT_TABLE_T statePrestageActTable[] = {
{EVENT_P_STOP, statePrestageStopFun},
{EVENT_R_MOVE_COMPLETE, statePrestageRMoveCompleteFun},
{EVENT_L_MOVE_COMPLETE, statePrestageLMoveCompleteFun},
{EVENT_R_MOVE_COMPLETE, statePrestageRMoveCompleteFun},
{EVENT_R_MOVE_COMPLETE, statePrestageRMoveCompleteFun},
{EVENT_R_SWITCH_ACTIVE, stateSelfTestRSwitchActiveFun}
};
ACT_TABLE_T stateErrorActTable[] = {
{EVENT_ERR_TO_SELFTEST, stateErrorReSelftestFun}
};
STATE_TABLE_T fsmTable[] = {
{STATE_INIT, sizeof(stateInitActTable) / sizeof(ACT_TABLE_T), enterInit, exitInit, stateInitActTable},
{STATE_SELFTEST, sizeof(stateSelftestActTable) / sizeof(ACT_TABLE_T), enterSelftest, exitSelftest, stateSelftestActTable},
{STATE_IDLE, sizeof(stateIdleActTable) / sizeof(ACT_TABLE_T), enterIdle, exitIdle, stateIdleActTable},
{STATE_S, sizeof(stateSlotActTable) / sizeof(ACT_TABLE_T), enterSlot, exitSlot, stateSActTable},
{STATE_R, sizeof(stateRotActTable) / sizeof(ACT_TABLE_T), enterRot, exitRot, stateRActTable},
{STATE_P, sizeof(statePrestageActTable) / sizeof(ACT_TABLE_T), enterPrestage, exitPrestage, statePrestageActTable},
{STATE_ERROR, sizeof(stateErrorActTable) / sizeof(ACT_TABLE_T), enterError, exitError, stateErrorActTable}
}; /*在状态转换中,执行入口和出口函数,在processEvent();处执行后面列表中函数*/
void initStateMachine(void)
{
memset(&m_Fsm, 0, sizeof(m_Fsm));/*在m_Fsm后面开辟sizeof(m_Fsm)个内存空间,并填写0,然后返回m_Fsm*/
fsmRegister(&m_Fsm, fsmTable);/*把上面状态表赋值给m_Fsm*/
m_Fsm.curState = STATE_INIT;/*当前状态为STATE_INIT*/
}
void addEvent(EVENT event)
{
if(eventBuf.count >= MAX_EVENT_BUF)
{
/* eventBuf overflow */
//while(1);
}
else
{
intMasterDisable();
eventBuf.event[eventBuf.writeIndex] = event;
eventBuf.count++;
eventBuf.writeIndex++;
if(MAX_EVENT_BUF <= eventBuf.writeIndex)
{
eventBuf.writeIndex = 0;
}
intMasterEnable();
}
}
void processEvent(void)
{
if(eventBuf.count != 0)
{
EVENT event = EVENT_MAX;/*20*/
/* there is event in the eventBuf */
intMasterDisable();
event = eventBuf.event[eventBuf.readIndex];
eventBuf.count--;
eventBuf.readIndex++;
if(MAX_EVENT_BUF <= eventBuf.readIndex)
{
eventBuf.readIndex = 0;
}
intMasterEnable();
fsmEventHandle(&m_Fsm, event);/*m_Fsm在初始化状态机过程中,包含了所有的状态信息*/
}
}
|