Go Down

Topic: Using the method of the object as ISR without external code in "user space" (Read 1 time) previous topic - next topic

ViacheslavMezentsev

I want to suggest how to do it.

1. Add new type to wiring_private.h

Code: [Select]
typedef void (*voidFuncPtr)(void);
typedef void (*voidFuncPtrEx)(void *);



2. Add some code to WInterrupts.c (see attached file).

3. Add description to Arduino.h

Code: [Select]
void attachInterruptEx(uint8_t, void (*userFunc)(void *), int, void * );

How to use:

Code: [Select]
class MyClass
{
public:
static void MyISRHelper( void * data )
{
MyClass * self = reinterpret_cast< MyClass * >( data );

self->MyISR();
}
   
    void MyISR()
    {
        // ...
    }

public:
MyClass() {}
~MyClass() {}
};

MyClass class1;

void setup()
{
    attachInterruptEx( 2, MyClass::MyISRHelper, RISING, ( void * ) & class1 );
}


void loop() {}



attachInterrupt() still works.


PaulS

So, if you have a class, Dog, with a Bark() method, and 12 instances of the class, Spot, Fido, DownDammit, etc., when the external interrupt happens, you want all 12 dogs to start barking?

ViacheslavMezentsev

No, attachInterruptEx() function takes as a parameter a pointer to the instance that is then used. When the method is called it will called with a pointer to an instance of an object as a parameter.



Go Up