arduino 中有多个 tabs/files?
Multiple tabs/files in arduino?
我看了很多,但没有找到涵盖所有这些内容的好教程。
所以,我需要把项目分成多个 tabs/ino 个文件,只是为了更清楚。
所以打开新标签后,有几个问题想请教:
如果主项目文件,比方说 main
,还有另外 2 个选项卡,比方说 A
和 B
,那么 B 中的每个函数都是main
和 A
?
可见
中断会发生什么?如果我在文件A
中定义了一些中断,它可以调用main
文件中的中断函数吗?
defines
会怎样? includes
?如果文件 A
包含一些库让我们说 Wire
,main
文件是否也能看到它,反之亦然?
使用文件的策略是什么?您是将所有库都添加到主文件中,还是应该将它们也添加到其他文件中? (例如处理陀螺仪的文件必须包含一些库)
您可能已经注意到,每个 .ino 文件都必须包含在同名文件夹中。然后,此文件夹中包含的所有其他 .ino 文件都被视为 "extra tab".
据我所知,当你编译时,.ino 文件被合并为一个文件。例如,这是由于在同一文件夹中的不同 .ino 文件中具有 setup
(或 loop
或其他任何内容)而导致的令人吃惊的 "redefinition of void setup()" 错误的原因。
来自“the docs”:
Multi-file sketches
A sketch can contain multiple files (tabs). To manage them, click on the right-facing arrow just above the scroll bar near the top of the environment. Tabs have one of four extensions: no extension, .c, .cpp, or .h (if you provide any other extension, the period will be converted to an underscore).
When your sketch is compiled, all tabs with no extension are concatenated together to form the "main sketch file". Tabs with .c or .cpp extensions are compiled separately. To use tabs with a .h extension, you need to #include it (using "double quotes" not ).
虽然它提到了 "files with no extension",但我相信它也适用于扩展名为 .ino
的文件。
我一直在使用 Arduino IDE 处理多个源文件时遇到问题。我倾向于使用 Arduino-Makefile 这样的东西,它可以让您更好地控制 Arduino 项目的构建过程。
- 您需要创建一个 header 文件来声明这些函数,然后将该 header 文件包含在您的
.ino
文件中。
- 假设您正在使用 AVR 库,您可以让中断处理程序调用一个函数(如中断处理程序所示)。然后只需在 ISR 之外调用
interrupt_handler()
函数。
- 您应该包括每个文件所需的依赖项。这样,如果
A
或 B
删除 #include <Wire.h>
,您的主文件仍将包含依赖项。由于 header include guards,该文件不会被包含两次。
中断处理程序
#include <avr/interrupt.h>
#include "A.h"
/* Declare our ISR */
ISR(interrupt_vector)
{
/* Call our handler (located in A) */
interrupt_handler();
}
我看了很多,但没有找到涵盖所有这些内容的好教程。 所以,我需要把项目分成多个 tabs/ino 个文件,只是为了更清楚。
所以打开新标签后,有几个问题想请教:
如果主项目文件,比方说
main
,还有另外 2 个选项卡,比方说A
和B
,那么 B 中的每个函数都是main
和A
? 可见
中断会发生什么?如果我在文件
A
中定义了一些中断,它可以调用main
文件中的中断函数吗?defines
会怎样?includes
?如果文件A
包含一些库让我们说Wire
,main
文件是否也能看到它,反之亦然?
使用文件的策略是什么?您是将所有库都添加到主文件中,还是应该将它们也添加到其他文件中? (例如处理陀螺仪的文件必须包含一些库)
您可能已经注意到,每个 .ino 文件都必须包含在同名文件夹中。然后,此文件夹中包含的所有其他 .ino 文件都被视为 "extra tab".
据我所知,当你编译时,.ino 文件被合并为一个文件。例如,这是由于在同一文件夹中的不同 .ino 文件中具有 setup
(或 loop
或其他任何内容)而导致的令人吃惊的 "redefinition of void setup()" 错误的原因。
来自“the docs”:
Multi-file sketches
A sketch can contain multiple files (tabs). To manage them, click on the right-facing arrow just above the scroll bar near the top of the environment. Tabs have one of four extensions: no extension, .c, .cpp, or .h (if you provide any other extension, the period will be converted to an underscore).
When your sketch is compiled, all tabs with no extension are concatenated together to form the "main sketch file". Tabs with .c or .cpp extensions are compiled separately. To use tabs with a .h extension, you need to #include it (using "double quotes" not ).
虽然它提到了 "files with no extension",但我相信它也适用于扩展名为 .ino
的文件。
我一直在使用 Arduino IDE 处理多个源文件时遇到问题。我倾向于使用 Arduino-Makefile 这样的东西,它可以让您更好地控制 Arduino 项目的构建过程。
- 您需要创建一个 header 文件来声明这些函数,然后将该 header 文件包含在您的
.ino
文件中。 - 假设您正在使用 AVR 库,您可以让中断处理程序调用一个函数(如中断处理程序所示)。然后只需在 ISR 之外调用
interrupt_handler()
函数。 - 您应该包括每个文件所需的依赖项。这样,如果
A
或B
删除#include <Wire.h>
,您的主文件仍将包含依赖项。由于 header include guards,该文件不会被包含两次。
中断处理程序
#include <avr/interrupt.h>
#include "A.h"
/* Declare our ISR */
ISR(interrupt_vector)
{
/* Call our handler (located in A) */
interrupt_handler();
}