I'm currently attempting to implement some interrupts on an Arduino project. Looking through some of the examples, it appears that all the ISRs are defined in the top level Arduino program. I'm organizing my project by C++ classes and namespaces, so I'm curious, is it possible to reference an ISR that is not declared and defined in the top level Arduino program? Could I instead reference an ISR that is declared in a separate C++ namespace? This question has been spawned primarily for organizational purposes.
Otherwise, I could always just use a #include to import a file that defines the ISR such that they're imported to the top level.
Thanks!
1 Answer 1
Why don't you try it and see?
is it possible to reference an ISR that is not declared and defined in the top level Arduino program? Could I instead reference an ISR that is declared in a separate C++ namespace?
Yes. Example here:
const byte LED = 13;
const byte BUTTON = 2;
namespace foo
{
// Interrupt Service Routine (ISR)
void switchPressed ()
{
if (digitalRead (BUTTON) == HIGH)
digitalWrite (LED, HIGH);
else
digitalWrite (LED, LOW);
} // end of switchPressed
} // end of namespace foo
void setup ()
{
pinMode (LED, OUTPUT); // so we can update the LED
digitalWrite (BUTTON, HIGH); // internal pull-up resistor
attachInterrupt (0, foo::switchPressed, CHANGE); // attach interrupt handler
} // end of setup
void loop ()
{
// loop doing nothing
}
I put the ISR in the namespace "foo" and it compiled OK.
attachInterrupt()
?