user26991: 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?

user10794: 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.

26991: <p>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:</p>
<pre class="lang-cpp prettyprint-override"><code>portEXIT_CRITICAL_ISR(&amp;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], &amp;timmerCB, true);
  class_instance_number++;
}
</code></pre>
<p>However, I have this problem: how can I change the variable inside the interrupt regarding to each class instance?</p>
 10794: <h1>Interrupt Service Routine (ISR) outside a class</h1>
<p>Let's consider a simple use of interrupts:</p>
<pre><code>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
</code></pre>
<p>That compiles fine.</p>
<hr />
<h1>ISR inside a class as a class function (method)</h1>
<p>Now let's imagine we want to put the interrupt handling into a class for our convenience.</p>
<pre><code>class myClass
  {
  volatile bool switchChanged;
  
  public:

  void begin ()
    {
    pinMode (2, INPUT_PULLUP);
    attachInterrupt (0, switchPressed, CHANGE);   // &lt;--- 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
</code></pre>
<p>That does <strong>not</strong> compile:</p>
<pre><code>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 (*)()’
</code></pre>
<p>What is going on here?</p>
<p>ISRs have to be static functions, taking no arguments. However (non-static) class functions have an implied <strong>this-&gt;</strong> pointer which points to the particular <em>instance</em> of the class.</p>
<p>For example, if we have two instances:</p>
<pre><code>myClass foo;  
myClass bar; 
</code></pre>
<p>If we call foo.begin() then <strong>this-&gt;</strong> points to &quot;foo&quot;, and if we call bar.begin then <strong>this-&gt;</strong> points to &quot;bar&quot;.</p>
<p>However an ISR, when fired by the processor, cannot know whether <strong>this-&gt;</strong> is &quot;foo&quot; or &quot;bar&quot; or something else. Thus the compiler cannot compile that line.</p>
<hr />
<h1>ISR inside a class as a static class function</h1>
<p>We can try to work around this by making the class function <strong>static</strong>. Doing that means that the function is not tied to any particular instance, and thus the attachInterrupt line will compile.</p>
<pre><code>class myClass
  {
  volatile bool switchChanged;   // &lt;--- line 3
  
  public:

  void begin ()
    {
    pinMode (2, INPUT_PULLUP);
    attachInterrupt (0, switchPressed, CHANGE);
    }  // end of myClass::begin
    
  static void switchPressed ()
    {
    switchChanged = true;   // &lt;--- 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
</code></pre>
<p>However now we have a different problem:</p>
<pre><code>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
</code></pre>
<p>A non-static class variable cannot be called from a static class function. Why? Because the compiler doesn't know <em>which</em> variable you want. Is it foo.switchChanged or bar.switchChanged?</p>
<hr />
<h1>ISR inside a class as a static class function with static variables</h1>
<p>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.</p>
<pre><code>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
</code></pre>
<p>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.</p>
<hr />
<h1>Glue routines</h1>
<p>To work around this problem we can write short &quot;glue&quot; routines. These are functions that interface between an ISR and an <strong>instance</strong> of a class.</p>
<pre><code>class myClass
  {
  volatile bool switchChanged;

  static myClass * instances [2];
 
  static void switchPressedExt0 ()
    {
    if (myClass::instances [0] != NULL)
      myClass::instances [0]-&gt;switchPressed ();
    }  // end of myClass::switchPressedExt0
  
  static void switchPressedExt1 ()
    {
    if (myClass::instances [1] != NULL)
      myClass::instances [1]-&gt;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
</code></pre>
<p>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 &quot;glue&quot; routines switchPressedExt0 and switchPressedExt1 call the appropriate instance of the switchPressed function by using the remembered class pointer.</p>
<p>Now the non-static function switchPressed can access non-static class variables.</p>
<hr />
<p>This answer was reproduced from <a href="http://www.gammon.com.au/forum/bbshowpost.php?id=12983" rel="nofollow noreferrer">my forum</a>, but I can't post link-only answers here.</p>
