leaflabs/libmaple — pull request #53 by lostinspacebar (2012-08-25 13:53:24+00:00)
Title: attachInterrupt with class members (with access to instance data)

Hi,

I've made some changes that allow a second version of attachInterrupt that lets you use class member functions (albeit static) but with instance data. I have an example written up too, but I can't find where to actually commit it. Neither maple-ide nor libmaple have the examples folders that comes with the maple IDE release.

The current attachInterrupt call still works just as it did before.

I've included some code below to show what these changes let you do.

```c++
// Notice the extra "this" parameter to this attachInterrupt call.
attachInterrupt(BOARD_BUTTON_PIN, this, this->InterruptHandler, RISING);

/**
* Static interrupt handler. The instance parameter can be used to
* accesss instance specific data and methods
*
* @param instance Instance this interrupt is for.
*
*/
void MyClass::InterruptHandler(void *instance)
{
MyClass *myInstance = (MyClass *)instance;

// Obviously, this can be done in other ways, but this code
// is just to show the use of attachInterrupt with class members.
if(myInstance->_toggle == 0)
{
digitalWrite(BOARD_LED_PIN, LOW);
myInstance->_toggle = 1;
}
else
{
digitalWrite(BOARD_LED_PIN, HIGH);
myInstance->_toggle = 0;
}
}```