Hi,
I am wondering if it is possible to perform some tricks in an Arduino sketch such as compile-time detection of which libraries are used in a sketch by another sketch (say your scheduler library can optionally use functions from your RTC library if present), or which physical board is in use (a possible alternative to guessing based on ATmega variant as is used by some libraries), or set options from within the sketch.
For example, I am currently writing a library for a bizarre LCD and one thing I would like to do is include various size fonts, with the user being able to include a specific font(s) at compile-time (to save code memory) by adding e.g. "#define USE_FONT_4x6" in the sketch. Other solutions to this problem also welcome, of course
(Maybe it makes more sense to make each font table a separate 'library' and have the user pass the LCD lib a pointer to it?) But the visibility issue has now got me curious.
Just for fun I wrote the following library and sketch:
// detectme.cpp
#include "detectme.h"
DETECTME::DETECTME(void)
{
}
void DETECTME::sayhello(void)
{
Serial.println("Hello");
// ^^^ PASS. This works!
}
void DETECTME::detect_spi(void)
{
// See to what extent we can detect compile time #defined values
#if defined(_SPI_H_INCLUDED) // defined in Arduino SPI library
Serial.println("SPI library present (visible to libraries)");
#else
Serial.println("SPI library not detected");
#endif
// ^^^ FAIL! Library-to-library visibility not so much... how about environment to library?
#if defined(MOSQUINO) // This is defined inside the 'core' (e.g. pins_arduino.h) for my custom board.
Serial.println("Mosquino defined inside library");
#else
Serial.println("Mosquino NOT defined inside library");
#endif
// ^^^ PASS: #define inside core visible here
#if defined(MY_SKETCH) // Defined in the sketch calling this library
Serial.println("Sketch defines visible here");
#else
Serial.println("Sketch, what sketch?");
#endif
// ^^^ FAIL! Even if #defined before detectme.h included
}
#define MY_SKETCH
#include <SPI.h>
#include <detectme.h>
DETECTME detectme;
void setup() {
Serial.begin(9600);
}
void loop()
{
#if defined(MOSQUINO)
Serial.println("Mosquino defined");
#else
Serial.println("Mosquino not defined");
#endif
// ^^^ PASS: Visible
#if defined(_SPI_H_INCLUDED)
Serial.println("We can see defines inside libraries from the main sketch!");
#else
Serial.println("Bugger, can't see serial");
#endif
// ^^^ PASS: Visible
char a = 123;
detectme.sayhello(); // 'Serial' available from within sketch
detectme.detect_spi(); // See results in library code comments
while(1) { }
}
Results are as described in the code comments, for anyone interested.
Is there any sane / non-dangerous way to detect libraries from inside other libraries or similar compile-time visibility tricks?