3

I intended to use a timer interrupt for limited instances of a class in my ESP32 Arduino project. My first code conception was as follows:

portEXIT_CRITICAL_ISR(&lock0);              
  }

hw_timer_t * timer[3] = NULL;

class myClass
{
private:
  irqCallback timmerCB;
  volatile uint8_t pri_var1, pri_var2;
  static uint8_t class_instance_number=0;
public:
  volatile uint8_t pub_var1, pub_var2;
  myClass(/* args */);
  ~myClass();
};

myClass::myClass(/* args */)
{
  timmerCB=timerCBArray[class_instance_number];
  timer[class_instance_number] = timerBegin(0, 80, true);  
  timerAttachInterrupt(timer[class_instance_number], &timmerCB, true);
  class_instance_number++;
}

However, I have this problem: how can I change the variable inside the interrupt regarding to each class instance?

2
  • 1
    I attempted to migrate to SO, but it was blocked by the system
    – Juraj
    Mar 26 at 6:23
  • 1
    That is certainly sad. It looks like we will actually have to answer his question.
    – Nick Gammon
    Mar 26 at 7:25

1 Answer 1

11

Interrupt Service Routine (ISR) outside a class

Let's consider a simple use of interrupts:

volatile bool switchChanged;

void switchPressed ()
  {
  switchChanged = true;
  }  // end of switchPressed

void setup ()
  {
  pinMode (2, INPUT_PULLUP);
  attachInterrupt (0, switchPressed, CHANGE);
  }  // end of setup

void loop ()
  {
  // whatever    
  }  // end of loop

That compiles fine.


ISR inside a class as a class function (method)

Now let's imagine we want to put the interrupt handling into a class for our convenience.

class myClass
  {
  volatile bool switchChanged;
  
  public:

  void begin ()
    {
    pinMode (2, INPUT_PULLUP);
    attachInterrupt (0, switchPressed, CHANGE);   // <--- line 10
    }  // end of myClass::begin
    
  void switchPressed ()
    {
    switchChanged = true;
    }  // end of myClass::switchPressed
    
  };  // end of class myClass
  
myClass foo;  // make an instance of myClass
  
void setup ()
  {
  foo.begin ();
  }  // end of setup

void loop ()
  {
  // whatever    
  }  // end of loop

That does not compile:

ISR_in_class_test.ino: In member function ‘void myClass::begin()’:
ISR_in_class_test:10: error: argument of type ‘void (myClass::)()’ does not match ‘void (*)()’

What is going on here?

ISRs have to be static functions, taking no arguments. However (non-static) class functions have an implied this-> pointer which points to the particular instance of the class.

For example, if we have two instances:

myClass foo;  
myClass bar; 

If we call foo.begin() then this-> points to "foo", and if we call bar.begin then this-> points to "bar".

However an ISR, when fired by the processor, cannot know whether this-> is "foo" or "bar" or something else. Thus the compiler cannot compile that line.


ISR inside a class as a static class function

We can try to work around this by making the class function static. Doing that means that the function is not tied to any particular instance, and thus the attachInterrupt line will compile.

class myClass
  {
  volatile bool switchChanged;   // <--- line 3
  
  public:

  void begin ()
    {
    pinMode (2, INPUT_PULLUP);
    attachInterrupt (0, switchPressed, CHANGE);
    }  // end of myClass::begin
    
  static void switchPressed ()
    {
    switchChanged = true;   // <--- line 15
    }  // end of myClass::switchPressed
    
  };  // end of class myClass
  
myClass foo;  // make an instance of myClass
  
void setup ()
  {
  foo.begin ();
  }  // end of setup

void loop ()
  {
  // whatever    
  }  // end of loop

However now we have a different problem:

ISR_in_class_test.ino: In static member function ‘static void myClass::switchPressed()’:
ISR_in_class_test:3: error: invalid use of member ‘myClass::switchChanged’ in static member function
ISR_in_class_test:15: error: from this location

A non-static class variable cannot be called from a static class function. Why? Because the compiler doesn't know which variable you want. Is it foo.switchChanged or bar.switchChanged?


ISR inside a class as a static class function with static variables

To make the static function work, it can only access static variables. So we can make switchChanged static. Plus we need to define an instance of this static variable.

class myClass
  {
  static volatile bool switchChanged;  // declare
  
  public:

  void begin ()
    {
    pinMode (2, INPUT_PULLUP);
    attachInterrupt (0, switchPressed, CHANGE);
    }  // end of myClass::begin
    
  static void switchPressed ()
    {
    switchChanged = true;
    }  // end of myClass::switchPressed
    
  };  // end of class myClass
  
volatile bool myClass::switchChanged;  // define
  
myClass foo;  // make an instance of myClass
  
void setup ()
  {
  foo.begin ();
  }  // end of setup

void loop ()
  {
  // whatever    
  }  // end of loop

Now the class compiles. However we have thrown away most of the advantages of having a class in the first place, as we are forced to use a static function, and that static function can only access static variables.


Glue routines

To work around this problem we can write short "glue" routines. These are functions that interface between an ISR and an instance of a class.

class myClass
  {
  volatile bool switchChanged;

  static myClass * instances [2];
 
  static void switchPressedExt0 ()
    {
    if (myClass::instances [0] != NULL)
      myClass::instances [0]->switchPressed ();
    }  // end of myClass::switchPressedExt0
  
  static void switchPressedExt1 ()
    {
    if (myClass::instances [1] != NULL)
      myClass::instances [1]->switchPressed ();
    }  // end of myClass::switchPressedExt1


  public:

  void begin (const byte whichPin)
    {
    pinMode (whichPin, INPUT_PULLUP);
    switch (whichPin)
      {
      case 2: 
        attachInterrupt (0, switchPressedExt0, CHANGE);
        instances [0] = this;
        break;
        
      case 3: 
        attachInterrupt (1, switchPressedExt1, CHANGE);
        instances [1] = this;
        break;
        
      } // end of switch
    }  // end of myClass::begin
    
  void switchPressed ()
    {
    switchChanged = true; 
    }
    
  };  // end of class myClass
  
myClass * myClass::instances [2] = { NULL, NULL };

// instances of our class  
myClass foo; 
myClass bar;

void setup ()
  {
  foo.begin (2);   // pin D2
  bar.begin (3);   // pin D3
  }  // end of setup

void loop ()
  {
  // whatever    
  }  // end of loop

This is a bit fiddly, however what it is doing is remembering in an array which instance of the class is associated with which interrupt. The "glue" routines switchPressedExt0 and switchPressedExt1 call the appropriate instance of the switchPressed function by using the remembered class pointer.

Now the non-static function switchPressed can access non-static class variables.


This answer was reproduced from my forum, but I can't post link-only answers here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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