以下二维数组代码会引发错误 #29 预期表达式。
typedef enum
{
BATTERY_POW = 0,
USB_POW = 1,
END_STATE = 2
} BMTState_e;
typedef enum //event enums
{
NO_EVENT = 0,
BOOT_EVENT =1,
//I/O events
POW_GOOD_LOW =2,
POW_GOOD_HIGH = 3,
VBUS_POW_LOW = 4,
VBUS_POW_HIGH =5
}BMTEvent_e;
结构定义为
typedef struct //state machine definition
{
void (*funcPtr)();
BMTState_e nextState;
}BMTAction_t;
BMTState_e BMTGlobal_State ; //global state
函数定义为
void BMTTest()
{
//do nothing for time being
}
BMTAction_t action[END_STATE][END_EVENT]={
[BATTERY_POW][NO_EVENT] = {BMTTest,BATTERY_POW}
[BATTERY_POW][BOOT_EVENT] = {BMTTest,BATTERY_POW},
[BATTERY_POW][POW_GOOD_LOW] = {BMTTest,USB_POW},
[BATTERY_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW}
[BATTERY_POW][VBUS_POW_LOW] = {BMTTest,BATTERY_POW},
[BATTERY_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW},
[USB_POW][BOOT_EVENT] = {BMTTest,USB_POW_}
[USB_POW][BOOT_EVENT] = {BMTTest,USB_POW},
[USB_POW][POW_GOOD_LOW] = {BMTTest,USB_POW},
[USB_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW}
[USB_POW][VBUS_POW_LOW] = {BMTTest,BATTERY_POW},
[USB_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW}
};
void BMT_HandleEvent(BMTEvent_e event)
{
BMTAction_t stAction;
if(event != NO_EVENT)
{
stAction.funcPtr = action[BMTGlobal_State][event].funcPtr;
stAction.nextState = action[BMTGlobal_State][event].nextState;
printf("current State =%d, event = %d, nextState = %d",BMTGlobal_State, event,stAction.nextState );
if(NULL!= stAction.funcPtr)
stAction.funcPtr();
BMTGlobal_State = stAction.nextState;
}
}
int main()
{
BMTEvent_e = BOOT_EVENT;
if(retVal)
{
BMTGlobal_State = BATTERY_POW;
}
else // PG is low so check VBUS signal
{
retVal = GPIO_Read_Pin(USB_VBUS_PWR_PIN);
if(retVal)
{
BMTGlobal_State = USB_POW;
}
else
{
BMTGlobal_State = CRADLE_POW;
}
}
event = BOOT_EVENT;
while (1)
{
BMT_HandleEvent(event);
}
}
这个想法是根据收到的事件执行状态机。二维数组列出当前状态和该状态的所有可能事件,一旦接收到事件,将调用函数指针并且状态将转换到下一个状态。
我正在使用带有 GreenHills 编译器/工具的 MicroC/OS2。我会很感激你的回应。
我听从了基思的建议
BMTAction_t action[END_STATE][END_EVENT] = {
{{NULL,BATTERY_POW}}, //0
{{BMTProcess_BatteryPowBoot,BATTERY_POW}},// 1
{{BMTProcess_PowGoodLow,BATTERY_POW}},//2
{{BMTProcess_PowGoodHigh,BATTERY_POW}}, //3
{{BMTProcess_VBUSPowerLow,BATTERY_POW}}, //4
{{BMTProcess_VBUSPowerHigh,BATTERY_POW}},//5
};
编译器在行给出错误
"{{BMTProcess_PowGoodHigh,BATTERY_POW}}, //3"
"error # 146 too many initializer values"