Arduino的自定义枚举类型声明 [英] Custom enum type declaration with Arduino

查看:879
本文介绍了Arduino的自定义枚举类型声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Arduino中使用自定义枚举类型时遇到一些麻烦.

I'm having some trouble using a custom enum type in Arduino.

我已阅读其他地方,由于自定义类型声明,必须使用头文件到Arduino IDE预处理.因此,我已经做到了,但是我仍然无法使用我的自定义类型.这是我的主要arduino文件(beacon.ino)中代码的相关部分

I've read elsewhere that using a header file is necessary for custom type declarations, due to Arduino IDE preprocessing. So, I've done that, but I'm still unable to use my custom type. Here's the relevant portions of my code in my main arduino file (beacon.ino)

#include <beacon.h>

State state;

在beacon.h中:

And in beacon.h:

typedef enum {
  menu,
  output_on,
  val_edit
} State;

但是,当我尝试编译时,出现以下错误:

But, when I try to compile, I get the following error:

beacon:20: error: 'State' does not name a type

我认为我编写或包含头文件的方式有问题.但是呢?

I assume something is wrong about the way I have written or included my header file. But what?

推荐答案

beacon.h应该如下:

beacon.h should be as follows:

/* filename: .\Arduino\libraries\beacon\beacon.h */

typedef enum State{  // <-- the use of typedef is optional
  menu,
  output_on,
  val_edit
};

/* filename: .\Arduino\beacon\beacon.ino */
#include <beacon.h>
State state; // <-- the actual instance
void setup()
{
  state = menu; 
}

void loop()
{
  state = val_edit;
}

在主INO文件中实例化它时,请不要输入typdef以及尾随的"state"实例,反之亦然.上面的beacon.h文件需要位于用户目录.\ Arduino \ libraries \ beacon \目录中,并且需要重新启动IDE才能缓存其位置.

Leave the typdef's out and either the trailing instance of "state" off as you are instancing it in the main INO file, or vice verse. Where the above beacon.h file needs to be in users directory .\Arduino\libraries\beacon\ directory and the IDE needs to be restarted to cache its location.

但是您可以在INO中一次定义它并立即实例化它

But you could just define it and instance it all at once in the INO

/* filename: .\Arduino\beacon\beacon.ino */

enum State{
  menu,
  output_on,
  val_edit
} state; // <-- the actual instance, so can't be a typedef

void setup()
{
  state = menu;
}

void loop()
{
  state = val_edit;
}

两者都可以编译.

您还可以使用以下内容:

You can also use the following:

/* filename: .\Arduino\beacon\beacon2.ino */

typedef enum State{ // <-- the use of typedef is optional.
  menu,
  output_on,
  val_edit
};

State state; // <-- the actual instance

void setup()
{
  state = menu;
}

void loop()
{
  state = val_edit;
}

这里实例与枚举是分开的,允许枚举完全是typedef.在上面的地方是一个实例,而不是typedef.

here the instance is separate from the enum, allowing the enum to be solely a typedef. Where above it is a instance and not typedef.

这篇关于Arduino的自定义枚举类型声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆