0

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!

3
  • Are you talking about a real ISR, or a function that you use with attachInterrupt()? Commented Oct 8, 2016 at 13:37
  • @EdgarBonet A function that I use with attachInterrupt().
    – Izzo
    Commented Oct 8, 2016 at 13:41
  • 1
    Then it should be declared (not necessarily defined) from where you use it, or in an included .h. Commented Oct 8, 2016 at 13:48

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.