How to attachInterrupt to a function inside a class

Im making a class for my encoder that uses interrupts and in begin() function i need it to attach to a function of the class. but it throws me an error

error: invalid use of non-static member function 'void ENCODER::recordTime()'

Minimal Code that throws error

Class ENCODER{
private:
   void recordTime(){
   }

public:
   void begin(){
      attachInterrupt(digitalPinToInterrupt(5), this->recordTime, RISING)
   }
};

I have tried and they all met the same error.

attachInterrupt(digitalPinToInterrupt(5), recordTime, RISING)
attachInterrupt(digitalPinToInterrupt(5), this.recordTime, RISING)
attachInterrupt(digitalPinToInterrupt(5), this->recordTime, RISING)

Did you see this clue in the error message?

What makes You think You should use class?
Interrupt is a hardware dependant function. You can't "class", like clone, one piece of hardware inte several instances.

although true that you cant clone one piece hardware to several instance, it is possible to pass a pointer to the class , just like how you can pass the SPI or Wire pointer to multiple class instance.

but im not sure though that attachInterrupt is a class or just an ordinary function

I apologize but im going to need a bit more explination...

You can't attach the interrupt to the class method.

You should create an intermediate non-class static function, from which you will be able to call a class method for specific class instance:

class Example_class
{
  public:
   void foo() {};
};

// pointer to the our class instance
Example_class* E;

// interrupt function, calling the class method
static void irq_func() {
  E->foo();
}

Example_class A;

void setup() {
  E = &A; 
  attachInterrupt(2, FALLING, irq_func);
}

void loop() {}

Think about it for a moment.
A class is designed to have multiple instances.
But an interrupt is bound to a single hardware resource.
If you had an ISR that was a class member, then every instance of the class would contend for the same hardware resource.

That's not necessarily true. Consider multiple sensors, all tied to the same external interrupt signal with a big "or" gate. However, ISRs are pure C functions, and do not know how to set up "this" for calling a class function.

b707's suggestion of an intermediate non-class function is the way to go.

You can look at the way Serial is handled in the core (on boards with multiple serial ports, like a Zero or Nano Every), for an example.

I meant it in the sense of "it's bound to a single processor hardware resource" - like a falling or rising edge on a particular pin, or a receiver register full condition

I dunno. Quibbling over semantics. There are CPUs without the concept of "vectored" interrupts, with on

How would a static member function get the right context? Can they access class variables using this. to this-> perhaps?

Not if each class instance deals with interrupts from a distinct pin. I do it in one of my projects it works flawlessly.

If we didn't, we wouldn't be programmers, would we?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.