一,问题:
主草图文件:
char foo; // required to clean up some other problems
#include <Arduino.h> // tried it in desperation, no help
#include "a.h"
void setup(){
Serial.begin(9600);
Serial.println("\nTest begins");
for (int num = -1; num < 1; num++){
Serial.print(num);
if (isNegative(num)){
Serial.println(" is negative");
} else {
Serial.println(" is NOT negative");
}
}
}
void loop(){}
// a.h
#ifndef H_A
#define H_A
boolean isNegative(int x); // Err#1
int anotherOdity();
#endif // H_A
// a.cpp
#include "a.h"
int isNegative(int x){
Serial.println("I can't print this from inside my INCLUDE FILE"); //Err#2
if (x<0) return true;
return false;
}
int anotherOdity(){
char ch[5];
memcpy(ch,"1",1); //doesn't work, memcpy not declared // Err#3
}
以上,不会编译,这些是我得到的错误:
In file included from a.cpp:1:
a.h:4: error: 'boolean' does not name a type
a.cpp: In function 'int isNegative(int)':
a.cpp:4: error: 'Serial' was not declared in this scope
a.cpp: In function 'int anotherOdity()':
a.cpp:11: error: 'memcpy' was not declared in this scope
第一个问题是布尔类型,似乎遭受了Arduino环境所做的一些名称修改,但这通常是由 char foo;
在主文件中。在某些情况下,它是。但要在中使用那种类型 .cpp
文件生成此错误。
我可以看到错误2和3是相关的,但我如何在范围内获得这些?我意识到问题的一部分可能是 #include
本身(也许)因为 Serial
和 memcpy
尚未定义/声明?我尝试包括了 Arduino.h
图书馆,但这没有帮助。实际上,它确实有助于布尔问题,但仅限于将所有内容放入其中 .h
文件(我在下面进一步讨论),它没有帮助上面的例子。
如果我将三个文件放在一起并将所有内容放在主草图中(.ino
)文件,它应该工作。但这里的想法是我想要打破一些代码并使我的草图更具可读性。
我找到了最接近解决方案的地方: http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/ 在经过我自己的测试之后,我确定如果我把一切都放进去了 .h
文件,它的作品!
例如,如果我删除,则保持主草图文件不变 a.cpp
并创造公正 a.h
(如下)它的作品!
#ifndef H_A
#define H_A
boolean isNegative(int x){
Serial.println("I can't print this from inside my INCLUDE FILE");
if (x<0) return true;
return false;
}
int anotherOdity(){
char ch[5];
memcpy(ch,"1",1); //doesn't work, memcpy not declared
}
#endif // H_A
这解决了布尔问题(嗯......我仍然需要 Arduino.h
要么 char foo;
),它修复了范围问题。
但它只是感觉不对。
这不是关于创建我可以在各种草图中使用的标准函数库,而是将我的代码分解为更小(可读)的块,并将它们保存在项目文件夹中。我想以最正确的方式做到这一点,它似乎只是受到IDE的限制。我确信我对如何设置标题和关联有一个合适的理解 .cpp
一起归档(我希望我没有错误)。
我完全自学了C / C ++的所有内容,并且最近才进入编程微程序。
我已经通过谷歌的深度研究了这一点,我只是不断出现。
不求助于 hacks
对于像我这样的人来说,保持简单,我怎样才能最好地组合上面的例子,以便Arduino IDE / gcc编译它?
编辑:我想我会包含一些我在这里打开的标签,以表明我对此做了一些研究!
http://arduino.cc/en/Reference/Include
http://arduino.cc/en/Hacking/LibraryTutorial
http://forum.arduino.cc/index.php/topic,124904.msg938861.html#msg938861
http://forum.arduino.cc/index.php?topic=84412.0 (这是我发现的地方 char foo;
解)
http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/