if(file_exists('localConfig.h')) include 'localConfig.h';
Can I do something similar with precompiler directives in Arduino? I have a mature sketch that handles many options. I'd like - in future - to have a script that creates a localConfig.h and tests compilation for all possible options in order to avoid accidentally breaking an edge case. It would also mean that I could git .ignore the localConfig.h and stop bumping the commits.
If there's a better way to do this, I'm eager to know it.
You could create a library in the libraries subfolder of your sketchbook with a fallback localConfig.h file in it. When you do:
#include "localConfig.h"
it first searches the local folder, then the libraries folders so this will ensure that a file named localConfig.h will always be found. You could put something like this in the library file: #define LOCALCONFIG_MISSING
if you need your sketch to adapt to the fallback file being included.
I have a configure.h with default settings. I want to setup the defaults, then include the local config to override the defaults. That way I can mess with local config and check things without touching the github files. every time I touch the github files I am nervous I might be breaking a previous test.
aggrav8d:
every time I touch the github files I am nervous I might be breaking a previous test.
Start a new branch then and just don't touch the main branch. Or fork the whole repo. Either way the whole point of git is that you don't have to be afraid of breaking the code because you have all the previous copies you can go back to.
It sounds like either of the things I mentioned previously could provide a solution. For the former you could define the macro by a line in your sketch you uncomment or passing a -D flag to the compiler (the -D would lend itself better to automated testing).
Regarding automated tests, if you're using GitHub Travis CI is a popular option. That will have the greatest value if there are multiple people collaborating on the project. I wrote a script that attempts to make it easy to test Arduino projects with Travis CI:
Otherwise you can write a script/batch file to run tests locally using either the Arduino IDE CLI:
or arduino-builder:
You could adapt my script for that purpose also, which would be most easy if using Bash.