I'm trying with my class to programm an traffic light pole and we are still at the beginning but now we are working on a button class.
The button class has a static method which is calling an interupt routine on Pin 2 or 3, now I want to output an error message if the wrong pin(integer) was set in the .ino file.
I've seen this example but i couldn't figure out how to implement it in my code.
How can I show an error?
Is there an README file or an Website showing/explaining what I need?
Or is it stupid to do this in an method?
The example:
#define interruptPin 2
//confirm the correct interrupt pin has been selected
#if interruptPin != 2 && interruptPin != 3
#error interruptPin must be 2 or 3!
#endif
The static method:
static void Button::ISRswitch(int pin) {
if (pin == 2) {
attachInterrupt(digitalPinToInterrupt(2), isr, FALLING);
} else if (pin == 3) {
attachInterrupt(digitalPinToInterrupt(3), isr, FALLING);
} else {
//confirm the wrong interrupt pin has been selected
}
}
void Button::isr() {
// Something would come in here
}
Sorry I can't help with generating a compile-time error in run-time code. I don't think you can. To find an error at compile time you have to be working with compile-time constants. I don't see how you can determine if the argument to a function is going to be a 2, a 3, or something else.
I see, I could make an work around with some #ifdef and #if/#else but this would be a bit unnecessary and to complicated for the stuff that I needed for.
I haven't used/worked with macros and I'm too stupid right now to use it correctly.
Maybe I will use these macros later if I understand them, but now i will go templates.
C++ is a huge language. I suspect few people know all the ins and outs, I certainly do not.
I would prefer templates, inline functions, constants, etc. over macros any time. You may want to read: So, what's wrong with using macros? near the bottom of the page.