What is the clean way to include an ISR in a library?

DuaneB:
I would suggest having a quick look at either the pinchangeint library or the servo library both of these are well used class based libraries that include ISRs.

The basic technique is to define a static member function, any class member variables you wish to access inside the ISR should also be defined static. You can then call the static member from inside you ISR handler.

I have an example that is about as clean and simple as it gets here -

RCArduino: Arduino Serial Servos

Duane B

rcarduino.blogspot.com

Duane B, I hate to resurrect such an old thread, but I just want to thank you for this post. It has been immensely useful to me. I was really struggling with figuring out how to let an ISR access member variables too, without making them public, and I really like this technique. It is finally starting to make sense to me. Also, I finally have at least an upper-level understanding of how the servo library works now too, which is very nice.

What I found most useful from your link above was this right here:


// Timer1 Output Compare A interrupt service routine
// call out class member function OCR1A_ISR so that we can
// access out member variables
ISR(TIMER1_COMPA_vect)
{
CRCArduinoSerialServos::OCR1A_ISR();
}

Simply seeing you make the member function "OCR1A_ISR()" public, and then calling that from the interrupt was the key for me. I plan on massively expanding my eRCaGuy_Timer2_Counter library (also linked here under the "Timing" heading), giving it a whole slew of hardware and software PWM and timed interrupt functionality, and now I will be able to use your technique above to make it happen.

Thanks again!

Gabriel Staples