Well, I’ll have a bash at a reply, but I might not be completely correct! (Given the layout of your project, I’m more of an Uno man myself!)
For the main project, header files can live in src
or include
. Both are searched automatically at compile time without any further action on your part, as are any directories added using build_flags -I ...
.
For a library named “XXX”, there will be a directory, lib/XXX
and in there, all the headers and sources for that particular library. When building a library, as part of a project build, only the particular library will be searched for headers. The src
and include
directories are not scanned. Other directories added with build_flags
will be scanned.
If you have header files that you want to have included in the project, or library compilations, which do not live in the project itself, then, add the directory/directories they live in to the build_flags
in platformio.ini
.
Lets say you have headers in SourceCode/headers
then your build_flags
will look like:
build_flags =
-I SourceCode/headers
In your project, or libraries, to include SourceCode/headers/myHeader.h
you simply #include "myHeader.h"
and it will be found.
Example:
- I have a header file,
my_duino.h
which lives in /home/norman/SourceCode/headers/
. This header reads Arduino.h
and defines a macro, TOGGLE_LED
, which maps onto a function, in the asm
library, named toggle()
.
- I have a library, for want of a better word, named
asm,
which is located in lib/asm
under my project. The library contains:
- A header file,
asm.h
whgich defines the prototypes for two functions, setup()
and toggle()
.
- A source file, in assembly, named
asm.S
which has the source for the setup()
and toggle()
functions.
The files are:
platformio.ini
[env:uno]
platform = atmelavr
board = uno
framework = arduino
build_flags =
-I /home/norman/SourceCode/headers
main.cpp
// This header is in SourceCode/headers/.
#include "my_duino.h"
// This header is in lib/asm
#include "asm.h"
// setup() is called from the assembly code.
// And is not shown here.
// Loop simply calls toggle(), via TOGGLE_LED(), to toggle
// the LED.
void loop() {
TOGGLE_LED();
delay(1000);
}
lib/asm/asm.h
#ifndef ASM_H
#define ASM_H
extern "C" {
void setup();
void toggle();
}
#endif
lib/asm/asm.S
#define __SFR_OFFSET 0
#include <avr/io.h>
.section .text
.global setup
.global toggle
setup:
ldi r16,(1 << DDB5)
out DDRB,r16
ret
toggle:
ldi r16,(1 << DDB5)
out PINB,r16
ret
SourceCode/headers/my_duino.h
#ifndef MYDUINO_H
#define MYDUINO_H
// The main.cpp needs this header always. At least when
// using the arduino framework that is!
#include <Arduino.h>
// A silly example to get something usable from this header.
#define TOGGLE_LED() toggle()
#endif
I have #included
the header file which is out of the project structure, SourceCode.headers/my_duino.h
and the header file from the library, lib/asm/asm.h
, simply by using their terminal names. The platformio.ini
file tells the compiler where to find the additional header that is out of the project structure, and the compiler knows to search in the library’s directory when compiling the library.
If you run a verbose compilation, you get these options passed for headers etc.
Compiling main.cpp
avr-g++ ... -Iinclude -Isrc -Ilib/asm -I/home/norman/SourceCode/headers ...
Here you can see that src
and include
in the project structure are scanned, and also the directory added using build_flags
and the asm
library source folder too.
Compiling lib/asm/asm.S
avr-gcc ... -Ilib/asm -I/home/norman/SourceCode/headers ...
Here you can see that src
and include
in the project structure are not scanned, only the directory added using build_flags
and the asm
library source folder too.
HTH
Cheers,
Norm.