简体   繁体   English

找不到在.ino中声明的函数

[英]Function declared in .ino not found

I'm trying to create a simple application that stores a struct in the EEPROM memory and on startup it reads the stored struct from the EEPROM and then executes the rest based on the stored data. 我试图创建一个将结构存储在EEPROM存储器中的简单应用程序,并在启动时从EEPROM读取存储的结构,然后根据存储的数据执行其余的结构。

This is my main ino file: 这是我的主要ino文件:

extern "C" {
    #include "Structs.h"
}

Settings settings;

void setup()
{
    settings = settings_read();
}

void loop()
{

  /* add main program code here */

}

This is the Structs.h file: 这是Structs.h文件:

#ifndef _STRUCTS_h
#define _STRUCTS_h

#if defined(ARDUINO) && ARDUINO >= 100
    #include "arduino.h"
#else
    #include "WProgram.h"
#endif

typedef struct {
    byte lastCountry;
    byte lastMode;
} Settings;

#endif

This is the Settings.ino file: 这是Settings.ino文件:

#include <EEPROM.h>

Settings settings_read() {
    byte country, mode;
    Settings settings;

    country = EEPROM.read(0);
    mode = EEPROM.read(1);

    settings = {
        country,
        mode
    };

    return settings;
}

This is the compiler error: 这是编译器错误:

Compiling 'Stoplicht' for 'Arduino Uno'
Stoplicht.ino:5:1: error: 'Settings' does not name a type
Stoplicht.ino:In function 'void setup()'
Stoplicht.ino:9:27: error: 'settings_read' was not declared in this scope
Error compiling

I tried a lot of different things to get this to work. 我尝试了很多不同的方法来使它起作用。 I tried putting the code from Settings.ino into a .C file. 我尝试将来自Settings.ino的代码放入.C文件。 That gave more error's and it said the EEPROM.h function were not declared. 这带来了更多的错误,并说未声明EEPROM.h函数。 I also tried putting the struct and settings_read into Settings.ino that gave even more errors. 我还尝试将structsettings_read放到Settings.ino ,这会带来更多错误。 I'm completely new to C, I just can't find what I'm doing wrong here. 我是C语言的新手,在这里找不到我做错的事情。

As I said in a comment, the langage de prédilection for Arduino is C++, not C. 正如我在评论中所说 ,Arduino的语言是C ++,而不是C。

So is seems a good idea to comply to the C++ way of structuring your applications. 因此,遵循C ++构造应用程序的方式似乎是一个好主意。 Forget the C way which often consists of workarounds for the lack of adequate mechanisms for clean programming in C. (not that C++ is perfect...). 忘记C语言,它通常包含一些变通办法,因为它们缺乏在C中进行干净编程的适当机制(不是C ++是完美的……)。

So I restructured your sample code, which now shows how to define and use a library for the Settings type. 因此,我重新构造了示例代码,该示例代码现在说明如何为Settings类型定义和使用库。

First start from the main ino file 首先从主ino文件开始

// StopLicht.ino

#include "Settings.h"

Settings settings;

void setup()
{
   settings.read(); // doing it the OO way
}

void loop()
{
   // code here. does some stuff with 
   // settings.mode and settings.country
}

The Settings type is defined as a struct with 2 fields, and (here comes C++) a member function read() which acts on Settings : Settings类型定义为具有2个字段的struct ,并且(在C ++中是read()作用于Settings的成员函数read()

// Settings.h

#ifndef SETTINGS_H_INCLUDED
#define SETTINGS_H_INCLUDED

struct Settings 
{
  byte country; 
  byte mode;

  void read();   // to get values from EEPROM
};

#endif

And it is implemented in an ino file 它在一个ino文件中实现

// Settings.ino

#include <EEPROM.h>

#include "Settings.h"

void Settings::read() 
{
     country = EEPROM.read(0);
     mode    = EEPROM.read(1);
};

Remark: Settings could of course be written as a class instead of a struct . 备注: Settings当然可以写成而不是struct Not a big deal in this case. 在这种情况下没什么大不了的。

It's "Arduino.h", not "arduino.h" so you should change the lines to: 它是“ Arduino.h”,而不是“ arduino.h”,因此您应该将行更改为:

#ifndef _STRUCTS_h
#define _STRUCTS_h

#if defined(ARDUINO) && ARDUINO >= 100
    #include "Arduino.h"
#else
    #include "WProgram.h"
#endif

typedef struct {
    byte lastCountry;
    byte lastMode;
} Settings;

#endif

The next problem is your awkward include syntax?! 下一个问题是您笨拙的include语法吗? I don't know this extern "C" stuff, but it should work by simply using: 我不知道这个外部的“ C”东西,但是它应该可以通过简单地使用以下命令来工作:

#include "Structs.h"

without any boilder plate. 没有任何沸腾板。

Just as a tip, when working with multiple files Arduino IDE, it's missing autocomplete and its *.ino garbage will get your project messy and ensure you to get compile problems from time to time. 提示:在处理多个文件Arduino IDE时,它缺少自动完成功能,并且* .ino垃圾会使您的项目混乱,并确保您时不时遇到编译问题。 For multifile projects i would recommend using biicode( http://docs.biicode.com/arduino/gettingstarted.html ) with default c/++. 对于多文件项目,我建议在默认c / ++下使用biicode( http://docs.biicode.com/arduino/gettingstarted.html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM