If there a way to signal the Arduino preprocessor to Halt/Abort at compile time?
I'm not very familiar with advanced preprocessor directives in general, but I have seen that there are 'libraries' for compile time assertions in some compilers.
Background:
In order to prevent assignments of duplicate pins in code I created an enum to identify each pin
enum DataPins
{
/* 00 */ UNUSED_00,
/* 01 */ UNUSED_01,
/* 02 */ PIN_IN_SNR0_ECHO,
/* 03 */ PIN_IN_SNR1_ECHO,
...
/* 53 */ UNUSED_53,
/* check */ z_DONOTUSE54
};
And then I'd like to check the final name to make sure it has the correct value at compile time:
#define PinsOK z_DONOTUSE54==54
Is there a way to evaluate this at compile time? If so then based on that result, can I signal a compile-time error in Arduino?
The preprocessor can generate errors, but remember it runs before compile time. It simply process the source file making substitutions and translations as indicated by preprocessor directives. Strictly speaking, it doesn't care that your file is even C++ source code.
So no, the preprocess cannot have knowledge of what the enum {} defines would be.
But if you still need the error aspect of the question:
#define X
#ifdef X
#error ERROR my message
#endif
If you are looking for an alternate to save some typing and consistently construct the enum names, you can use the preprocessor to construct the enum lines. If you explicitly assign the values, you may not care about the gaps in the pins.
#define P(no_,name_) Pin##name_ = no_
#define U(no_) PinUnused##no_ = no_
enum {
P(0,Abc),
U(1),
P(2,Something),
P(15,OutWhatever)
};
int p;
p = PinAbc;
p = PinUnused1;
p = PinSomething;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With