# Interrupt in class, ESP32

**URL:** <https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326>\
**Category:** Programming\
**Created:** [October 7, 2022, 6:21am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326 "2022-10-07T06:21:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankfb13](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@frankfb13](https://forum.arduino.cc/u/frankfb13)\
**Post date:** [October 7, 2022, 6:21am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/1 "2022-10-07T06:21:23Z")

</div>

Hi everyone,

I habe a problem with an interrupt in a class. I tried several variants to write the code but i always get errors.  
I use a ESP32 and I want to get a interrupt when pin 36 changes its state. Below is my actual code and i get the error: invalid conversion from 'void (_)(void_)' to 'void (\*)()' on line "attachInterrupt(...)".  
IDE is version 1.8.13

Has anyone an idea how i can do it?

Greetings

Frank

```cpp
class Test
{
	public:
	Test();
	void Init();
	private:
	static void IRAM_ATTR Interrupt_static(void *pInterrupt);
	int x;
};

Test::Test()
{
}

void Test::Init()
{
	pinMode(36,INPUT_PULLUP);
	int t=digitalPinToInterrupt(36);
	attachInterrupt(t,&Test::Interrupt_static,CHANGE);
}

void IRAM_ATTR Test::Interrupt_static(void *pInterrupt)
{
	Test* self = static_cast<Test*>(pInterrupt);
	self->x++;
}

```

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 7, 2022, 9:20am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/2 "2022-10-07T09:20:53Z")

</div>

shouldn't the function passed in attachInterrupt() have a void argument?

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [October 7, 2022, 9:47am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/3 "2022-10-07T09:47:13Z")

</div>

One thing. GPIO\_NUM\_36 does not have pullup resistors. All GPIO pins one a ESP32's portB do not have pullups, [GPIO & RTC GPIO - ESP32 - — ESP-IDF Programming Guide latest documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html). To use pullups on pin 36 will require the use of the ESP32's RTC API.

---

<div class="post-metadata">

**Author:** ![frankfb13](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@frankfb13](https://forum.arduino.cc/u/frankfb13)\
**Post date:** [October 7, 2022, 9:49am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/4 "2022-10-07T09:49:02Z")

</div>

Good point, the only problem then is how can I access the variable (x) in the class...  
error: invalid use of member 'Test::x' in static member function

When I declare x as static the Linker says undefined reference to `Test::x

---

<div class="post-metadata">

**Author:** ![frankfb13](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@frankfb13](https://forum.arduino.cc/u/frankfb13)\
**Post date:** [October 7, 2022, 9:50am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/5 "2022-10-07T09:50:06Z")

</div>

The missing pullup is no problem, the signal comes out of an HC14. But Thanks!

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 7, 2022, 9:54am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/6 "2022-10-07T09:54:36Z")

</div>

> [@frankfb13](#):
>
> how can I access the variable (x) in the class...

interrupts are not executed with arguments. they are simply a function pointer located in an interrupt vector. that function can be hardcoded with variables

not sure what the overhead is accessing a class member within the interrupt

---

<div class="post-metadata">

**Author:** ![Idahowalker](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/idahowalker/32/238858_2.png) [@Idahowalker](https://forum.arduino.cc/u/Idahowalker)\
**Post date:** [October 7, 2022, 9:56am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/7 "2022-10-07T09:56:13Z")

</div>

Good luck.

---

<div class="post-metadata">

**Author:** ![frankfb13](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@frankfb13](https://forum.arduino.cc/u/frankfb13)\
**Post date:** [October 7, 2022, 9:56am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/8 "2022-10-07T09:56:59Z")

</div>

That is exactly my problem. I was also confused with the void\* in the interrupt routine but I found it on the web... but it seems not to be working...

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 7, 2022, 10:01am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/9 "2022-10-07T10:01:31Z")

</div>

> [@frankfb13](#):
>
> I was also confused with the void\*

isn't it "`void(*)(void)`", not "`void*`"? -- a function-ptr (the "\*") having no argument (the "(void)" and not returning anything

maybe you should get this working without an interrupt

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [October 7, 2022, 10:24am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/10 "2022-10-07T10:24:30Z")

</div>

The method `attachInterrupt()` will not allow you to specify an argument for the ISR. If you want to pass a pointer to an object to the ISR, you must use a different approach which includes [gpio\_isr\_handler\_add](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/gpio.html?highlight=gpio_isr_handler_add#_CPPv420gpio_isr_handler_add10gpio_num_t10gpio_isr_tPv), here is [an example](https://github.com/espressif/esp-idf/blob/master/examples/peripherals/gpio/generic_gpio/main/gpio_example_main.c).

---

<div class="post-metadata">

**Author:** ![oqibidipo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/oqibidipo/32/221794_2.png) [@oqibidipo](https://forum.arduino.cc/u/oqibidipo)\
**Post date:** [October 7, 2022, 11:27am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/11 "2022-10-07T11:27:38Z")

</div>

> [@frankfb13](#):
>
> When I declare x as static the Linker says undefined reference to `Test::x

You need a separate definition for it:

```arduino
int Test::x = 0;

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [October 7, 2022, 11:56am UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/12 "2022-10-07T11:56:24Z")

</div>

**Try this:**

```cpp
#include "Arduino.h"
#include "FunctionalInterrupt.h"

class TestClass {
public:
	TestClass(uint8_t pin) {
		attachInterrupt(digitalPinToInterrupt(pin), std::bind(&TestClass::classIsr, this), RISING);
	}

private:
	uint32_t count = 0;
	void classIsr() {
		count++;
	}
};

TestClass testObject(5);

void setup() {
}

void loop() {
}

```

---

<div class="post-metadata">

**Author:** ![frankfb13](https://avatars.discourse-cdn.com/v4/letter/f/7bcc69/32.png) [@frankfb13](https://forum.arduino.cc/u/frankfb13)\
**Post date:** [October 7, 2022, 12:04pm UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/13 "2022-10-07T12:04:22Z")

</div>

It seems to work. Many Thanks! I will continue testing...

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 7, 2022, 12:43pm UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/14 "2022-10-07T12:43:55Z")

</div>

> [@gfvalvo](#):
>
> `bind(&TestClass::classIsr, this)`

what does the "bind" do?

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [October 7, 2022, 12:53pm UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/15 "2022-10-07T12:53:34Z")

</div>

> [@gcjr](#):
>
> what does the "bind" do?

It's one of those "Modern C++" black magic things. My hand-waiving explanation is that the signature of class member functions include a hidden 'this' parameter so that the function can know which instance to operate on.  
`std::bind(&TestClass::classIsr, this)`  
Creates an object of the std::bind class that specifies ("binds") the function pointer and instance pointer.

The "FunctionalInterrupt.h" header includes an overload of attachInterrupt() that accepts a std::function\<\> --- yet more "Modern C++" black magic. The std::function constructor has an overload that accepts a std::bind.

[https://en.cppreference.com/w/cpp/utility/functional/bind](https://en.cppreference.com/w/cpp/utility/functional/bind)  
[https://en.cppreference.com/w/cpp/utility/functional/function](https://en.cppreference.com/w/cpp/utility/functional/function)

---

<div class="post-metadata">

**Author:** ![Danois90](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@Danois90](https://forum.arduino.cc/u/Danois90)\
**Post date:** [October 7, 2022, 1:22pm UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/16 "2022-10-07T13:22:29Z")

</div>

Would a lambda not be the same as using bind?

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [October 7, 2022, 1:25pm UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/17 "2022-10-07T13:25:14Z")

</div>

> [@Danois90](#):
>
> Would a lambda not be the same as using bind?

Not sure. Try it and see if it compiles / works.

---

<div class="post-metadata">

**Author:** ![spokes](https://avatars.discourse-cdn.com/v4/letter/s/e79b87/32.png) [@spokes](https://forum.arduino.cc/u/spokes)\
**Post date:** [October 7, 2022, 2:22pm UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/18 "2022-10-07T14:22:30Z")

</div>

@gfvalvo : Good example of why people hate C++ :-/

A lambda would work for encapsulation but the OP wants a contextual and conditional variable so the lambda would have to have access to a outer/global variable. However, even though it's "private", it's the same thing with the class (a function pointer could work too). Since it's all about the intention of usage, realistically, a single global variable makes this simpler than me typing this out :-/

NOTE: don't forcefully bind/tie/use a ISR with a class as there's a lot of overhead that just doesn't make sense (a vtable to boot). An ISR isn't/doesn't have polymorphism as every ISR is functionally independent and identical.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 7, 2022, 2:41pm UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/19 "2022-10-07T14:41:44Z")

</div>

> [@gfvalvo](#):
>
> It's one of those "Modern C++" black magic things. My hand-waiving explanation is that the signature of class member functions include a hidden 'this' parameter so that the function can know which instance to operate on.  
> `std::bind(&TestClass::classIsr, this)`

getting ... just curious

C:\stuff\SW\Arduino\_Others\Tst\Tst.ino: In constructor 'TestClass::TestClass(uint8\_t)':  
Tst:28:52: error: 'bind' is not a member of 'std'  
attachInterrupt(digitalPinToInterrupt(pin), std::bind(&TestClass::classIsr, this), RISING);

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [October 7, 2022, 2:41pm UTC](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326/20 "2022-10-07T14:41:55Z")

</div>

> [@spokes](#):
>
> @gfvalvo : Good example of why people hate C++

I'm not aware of any C++ police that would force you to use this technique ... or even use C++ for that matter.

> [@spokes](#):
>
> NOTE: don't forcefully bind/tie/use a ISR with a class as there's a lot of overhead that just doesn't make sense (a vtable to boot). An ISR isn't/doesn't have polymorphism as every ISR is functionally independent and identical.

There's no vtable or polymorphism required for the code I posted. The overload of attachInterrupt simply stores a std::function object that is invoked when the interrupt occurs and is processed by the initial ISR that's vectored based on the specific interrupt. I doubt there's much more processing overhead than the regular attachInterrupt technique that stores a function pointer.

[Next page](https://forum.arduino.cc/t/interrupt-in-class-esp32/1039326.md?page=2)
