The following piece of code which is 2 dimensional array throws an error #29 expected an expression.
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;
The structure is defined as
typedef struct //state machine definition
{
void (*funcPtr)();
BMTState_e nextState;
}BMTAction_t;
BMTState_e BMTGlobal_State ; //global state
The function is defined as
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);
}
}
The idea is to execute the state machine based on the events received. The 2D array list the current state and all possible events for the state, once an event is received a function pointer will be invoked and the state will transition to the next state.
I am using MicroC/OS2 with GreenHills compiler/tools. I would appreciate your response.
I followed Keith's recommendation
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
};
The compiler gives error at line
"{{BMTProcess_PowGoodHigh,BATTERY_POW}}, //3"
"error # 146 too many initializer values"
That's not the right syntax for designated initializer. You can only specify one index at a time.I was incorrect; multiple designators are permitted. In fact, your code compiles without error using
gcc -std=c99 -pedantic -Wall -Wextraafter I add a few declarations.Most likely your compiler doesn't understand designated initializers. (They were added to the language by the 1999 standard, and even today not all compilers support them.)
If it doesn't, you'll need to remove the bracketed expressions (and make sure you have the elements in the right order). Probably something like: