2

以下二维数组代码会引发错误 #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"
4

4 回答 4

5

这不是指定初始化程序的正确语法。您一次只能指定一个索引。

我错了;允许使用多个指示符。事实上,gcc -std=c99 -pedantic -Wall -Wextra在我添加了一些声明之后,您的代码使用编译没有错误。

typedef enum { BATTERY_POW, END_STATE } BMTState_e;
typedef enum { BOOT_EVENT, POW_GOOD_LOW, POW_GOOD_HIGH, END_EVENT } BMTEvent_e;

void BMTTest(void);

typedef struct  //state machine definition
{
    void (*funcPtr)();
    BMTState_e nextState;
}BMTAction_t;

BMTAction_t  action[END_STATE][END_EVENT]={
    [BATTERY_POW][BOOT_EVENT]    = {BMTTest,BATTERY_POW},
    [BATTERY_POW][POW_GOOD_LOW]  = {BMTTest,BATTERY_POW},
    [BATTERY_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW}
}; 

您的编译器很可能不理解指定的初始化程序。(它们被 1999 年的标准添加到语言中,即使在今天,也不是所有的编译器都支持它们。)

如果没有,您需要删除括号中的表达式(并确保您的元素顺序正确)。大概是这样的:

BMTAction_t  action0[END_STATE][END_EVENT] = {
    {{ BMTTest, BATTERY_POW }},
    {{ BMTTest, BATTERY_POW }},
    {{ BMTTest, BATTERY_POW }}
};
于 2015-02-02T05:19:13.723 回答
0

-c99如果您使用该选项,您正在使用的初始化将与 greenhills 编译器一起使用。

我必须对您的代码进行一些小改动才能使其编译:

  • 添加END_EVENT枚举
  • action在声明的项目之间添加一些逗号。
  • 修正错字USB_POW_USB_POW

这是修改后的代码:

#include <stdio.h>
typedef enum {
    BATTERY_POW = 0,
    USB_POW = 1,
    END_STATE = 2
} BMTState_e;

typedef enum {
    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,
    END_EVENT = 6

} BMTEvent_e;

typedef struct {
    void (*funcPtr)();
    BMTState_e nextState;
} BMTAction_t;

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}
};
int main()
{
    return 1;
}

这是没有的编译器输出-c99

$ ccppc test.c
"test.c", line 30: error #29: expected an expression
  [BATTERY_POW][NO_EVENT]    = {BMTTest,BATTERY_POW},
  ^

"test.c", line 31: error #29: expected an expression
  [BATTERY_POW][BOOT_EVENT]    = {BMTTest,BATTERY_POW},
  ^

"test.c", line 32: error #29: expected an expression
  [BATTERY_POW][POW_GOOD_LOW]  = {BMTTest,USB_POW},
  ^

"test.c", line 33: error #29: expected an expression
  [BATTERY_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW},
  ^

"test.c", line 34: error #29: expected an expression
  [BATTERY_POW][VBUS_POW_LOW]  = {BMTTest,BATTERY_POW},
  ^

"test.c", line 35: error #29: expected an expression
  [BATTERY_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW},
  ^

"test.c", line 36: error #29: expected an expression
   [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},
   ^

"test.c", line 37: error #29: expected an expression
  [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},
  ^

"test.c", line 38: error #29: expected an expression
  [USB_POW][POW_GOOD_LOW]  = {BMTTest,USB_POW},
  ^

"test.c", line 39: error #29: expected an expression
  [USB_POW][POW_GOOD_HIGH] = {BMTTest,BATTERY_POW},
  ^

"test.c", line 40: error #29: expected an expression
  [USB_POW][VBUS_POW_LOW]  = {BMTTest,BATTERY_POW},
  ^

"test.c", line 41: error #29: expected an expression
  [USB_POW][VBUS_POW_HIGH] = {BMTTest,USB_POW}
  ^

并与-c99

$ ccppc -c99 test.c
于 2015-02-04T21:48:54.177 回答
0

基思的回答帮助我弄清楚了正确的初始化。我必须在 {{ }} 中列出给定状态的所有可能事件和相关函数指针。

这就像初始化 row[0][0] 和 row [0][1] 的元素我必须列出顶层双花括号内的所有元素,例如

 { **{{**values/function/events associated with row [0][0],{values/function/events associated with row [0][1] }}, 
//now do the same for row 1. 
**{{**values/function/events associated with row [1][0],{values/function/events associated with row [1][1] **}}** }; //end of the array

BMTAction_t  action[END_STATE][END_EVENT] = {
                 //STATE = BATTERY_POW
                  {{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 
                  //STATE = USB_POW
                  {{NULL,USB_POW}, //0
                  {BMTProcess_BatteryPowBoot,USB_POW},//   1
                  {BMTProcess_PowGoodLow,USB_POW},//2
                  {BMTProcess_PowGoodHigh,USB_POW}, //3
                  {BMTProcess_VBUSPowerLow,USB_POW}, //4
                  {BMTProcess_VBUSPowerHigh,USB_POW}}//5 
                };
于 2015-02-03T14:30:08.443 回答
0

您确实意识到您的代码包括以下内容:

   [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW_}
   [USB_POW][BOOT_EVENT]    = {BMTTest,USB_POW},

甚至我都会对此感到困惑。你的意思是第一个NO_EVENT吗?

于 2016-09-12T11:58:26.580 回答