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

查看:77
本文介绍了使用 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;

在信标.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 和状态"的尾随实例关闭,反之亦然.上面的beacon.h文件需要在users目录下.\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天全站免登陆