EnableInterrupt library Class files error

Hi,

I am trying to split OOSimple example code in .h and .cpp files but do not know the correct way for externing simpleObject in .ino file.

> // Errors
> // Attempting: separate class in a file
> 
> // Error message ....
> /*
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino: In function 'void setup()':
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:79:3: error: 'simpleObject2' was not declared in this scope
>    simpleObject2 = createSimple();
>    ^~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:79:19: error: 'createSimple' was not declared in this scope
>    simpleObject2 = createSimple();
>                    ^~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:79:19: note: suggested alternative: 'Simple'
>    simpleObject2 = createSimple();
>                    ^~~~~~~~~~~~
>                    Simple
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:80:32: error: 'interruptFunction2' was not declared in this scope
>    enableInterrupt(ARDUINOPIN2, interruptFunction2, RISING);
>                                 ^~~~~~~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:80:32: note: suggested alternative: 'interruptFunctionType'
>    enableInterrupt(ARDUINOPIN2, interruptFunction2, RISING);
>                                 ^~~~~~~~~~~~~~~~~~
>                                 interruptFunctionType
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:83:3: error: 'simpleObject3' was not declared in this scope
>    simpleObject3 = createSimple();
>    ^~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:84:32: error: 'interruptFunction3' was not declared in this scope
>    enableInterrupt(ARDUINOPIN3, interruptFunction3, RISING);
>                                 ^~~~~~~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:84:32: note: suggested alternative: 'interruptFunctionType'
>    enableInterrupt(ARDUINOPIN3, interruptFunction3, RISING);
>                                 ^~~~~~~~~~~~~~~~~~
>                                 interruptFunctionType
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino: In function 'void loop()':
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:92:35: error: 'simpleObject2' was not declared in this scope
>    Serial.print(getSimpleVariable2(simpleObject2), DEC);  // print the interrupt count.
>                                    ^~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:92:16: error: 'getSimpleVariable2' was not declared in this scope
>    Serial.print(getSimpleVariable2(simpleObject2), DEC);  // print the interrupt count.
>                 ^~~~~~~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:96:35: error: 'simpleObject3' was not declared in this scope
>    Serial.print(getSimpleVariable3(simpleObject3), DEC);  // print the interrupt count.
>                                    ^~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:96:16: error: 'getSimpleVariable3' was not declared in this scope
>    Serial.print(getSimpleVariable3(simpleObject3), DEC);  // print the interrupt count.
>                 ^~~~~~~~~~~~~~~~~~
> 
> exit status 1
> 
> Compilation error: 'simpleObject2' was not declared in this scope
> */
> 
> > Blockquote
> // Errors
> // Attempting: separate class in a file
> 
> // Error message ....
> /*
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino: In function 'void setup()':
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:79:3: error: 'simpleObject2' was not declared in this scope
>    simpleObject2 = createSimple();
>    ^~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:79:19: error: 'createSimple' was not declared in this scope
>    simpleObject2 = createSimple();
>                    ^~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:79:19: note: suggested alternative: 'Simple'
>    simpleObject2 = createSimple();
>                    ^~~~~~~~~~~~
>                    Simple
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:80:32: error: 'interruptFunction2' was not declared in this scope
>    enableInterrupt(ARDUINOPIN2, interruptFunction2, RISING);
>                                 ^~~~~~~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:80:32: note: suggested alternative: 'interruptFunctionType'
>    enableInterrupt(ARDUINOPIN2, interruptFunction2, RISING);
>                                 ^~~~~~~~~~~~~~~~~~
>                                 interruptFunctionType
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:83:3: error: 'simpleObject3' was not declared in this scope
>    simpleObject3 = createSimple();
>    ^~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:84:32: error: 'interruptFunction3' was not declared in this scope
>    enableInterrupt(ARDUINOPIN3, interruptFunction3, RISING);
>                                 ^~~~~~~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:84:32: note: suggested alternative: 'interruptFunctionType'
>    enableInterrupt(ARDUINOPIN3, interruptFunction3, RISING);
>                                 ^~~~~~~~~~~~~~~~~~
>                                 interruptFunctionType
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino: In function 'void loop()':
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:92:35: error: 'simpleObject2' was not declared in this scope
>    Serial.print(getSimpleVariable2(simpleObject2), DEC);  // print the interrupt count.
>                                    ^~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:92:16: error: 'getSimpleVariable2' was not declared in this scope
>    Serial.print(getSimpleVariable2(simpleObject2), DEC);  // print the interrupt count.
>                 ^~~~~~~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:96:35: error: 'simpleObject3' was not declared in this scope
>    Serial.print(getSimpleVariable3(simpleObject3), DEC);  // print the interrupt count.
>                                    ^~~~~~~~~~~~~
> C:\Users\test\Documents\Arduino\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file\Interrupt_lib_EnableInterrupt_OOSimple_multi_pins_class_file.ino:96:16: error: 'getSimpleVariable3' was not declared in this scope
>    Serial.print(getSimpleVariable3(simpleObject3), DEC);  // print the interrupt count.
>                 ^~~~~~~~~~~~~~~~~~
> 
> exit status 1
> 
> Compilation error: 'simpleObject2' was not declared in this scope
> */

.ino

> // EnableInterrupt OOSimple object-oriented example sketch.
> // See https://github.com/GreyGnome/EnableInterrupt and the README.md for more information.
> 
> #include <EnableInterrupt.h>
> #include "Simple.h"
> 
> 
> // Modify this at your leisure. See https://github.com/GreyGnome/EnableInterrupt/wiki/Usage#Summary
> #define ARDUINOPIN2 2
> #define ARDUINOPIN3 3
> 
> // None of these externs helps
> // extern "Simple" void *createSimple();
> // extern Simple simpleObject2;
> // extern Simple simpleObject3;
> // extern void *simpleObject2;
> // extern void *simpleObject3;
> 
> // Attach the interrupt in setup()
> void setup() {
>   Serial.begin(115200);
>   Serial.println("---------------------------------------");
> 
>   // If you want to enable more pins, you will need to recreate these 3 lines for
>   // your new pin, object, and function.
>   pinMode(ARDUINOPIN2, INPUT_PULLUP);  // See http://arduino.cc/en/Tutorial/DigitalPins
>   simpleObject2 = createSimple();
>   enableInterrupt(ARDUINOPIN2, interruptFunction2, RISING);
> 
>   pinMode(ARDUINOPIN3, INPUT_PULLUP);  // See http://arduino.cc/en/Tutorial/DigitalPins
>   simpleObject3 = createSimple();
>   enableInterrupt(ARDUINOPIN3, interruptFunction3, RISING);
> }
> 
> // In the loop, we just print our object's simple variable. It is updated by the interrupt routine.
> void loop() {
>   Serial.println("---------------------------------------");
>   delay(1000);  // Every second,
>   Serial.print("Pin2 was interrupted: ");
>   Serial.print(getSimpleVariable2(simpleObject2), DEC);  // print the interrupt count.
>   Serial.println(" times so far.");
> 
>   Serial.print("Pin3 was interrupted: ");
>   Serial.print(getSimpleVariable3(simpleObject3), DEC);  // print the interrupt count.
>   Serial.println(" times so far.");
> }

.h

> #ifndef SIMPLE_H
> #define SIMPLE_H
> #include <Arduino.h>
> 
> 
> 
> class Simple {
> 
> 
> private:
>   volatile uint8_t _sv2;  // a simple variable. Notice that it is volatile.
>   volatile uint8_t _sv3;
>   void init();
> 
> public:
>   Simple() {
>     init();
>   }
> 
>   void updateSimpleVariable2();
>   void updateSimpleVariable3();
>   uint8_t getSimpleVariable2();
>   uint8_t getSimpleVariable3();
> };
> 
> #endif






.cpp
> #include "Simple.h"
> 
> void Simple::init() {
>   _sv2 = 0;
>   _sv3 = 0;
> }
> void Simple::updateSimpleVariable2() {
>   _sv2++;
> }
> 
> void Simple::updateSimpleVariable3() {
>   _sv3++;
> }
> 
> uint8_t Simple::getSimpleVariable2() {
>   return _sv2;
> }
> 
> uint8_t Simple::getSimpleVariable3() {
>   return _sv3;
> }
> 
> #ifdef __cplusplus
> extern "C" {
> #endif
>   void *createSimple() {
>     return new Simple();
>   }
> #ifdef __cplusplus
> }
> #endif
> 
> #ifdef __cplusplus
> extern "C" {
> #endif
>   uint8_t getSimpleVariable2(void *anObject) {
>     return static_cast<Simple *>(anObject)->getSimpleVariable2();
>   }
>     uint8_t getSimpleVariable3(void *anObject) {
>     return static_cast<Simple *>(anObject)->getSimpleVariable3();
>   }
> #ifdef __cplusplus
> }
> #endif
> 
> #ifdef __cplusplus
> extern "C" {
> #endif
>   void updateSimpleVariable2(void *anObject) {
>     static_cast<Simple *>(anObject)->updateSimpleVariable2();
>   }
>     void updateSimpleVariable3(void *anObject) {
>     static_cast<Simple *>(anObject)->updateSimpleVariable3();
>   }
> #ifdef __cplusplus
> }
> #endif
> 
> void *simpleObject2;  // this is numbered 0. You can create more, for example void *simpleObject1
> void *simpleObject3;
> #ifdef __cplusplus
> extern "C" {
> #endif
>   // If you create more objects, you need to create more interruptFunctionN()'s
>   // that update their simple variables, for example interruptFunction1() .
>   void interruptFunction2() {
>     updateSimpleVariable2(simpleObject2);
>   }
>   void interruptFunction3() {
>     updateSimpleVariable3(simpleObject3);
>   }
> #ifdef __cplusplus
> }
> #endif

Has anyone successfully done this ? kindly share the solution

If I understand your intent correctly, the following post should explain how to use interrupts with classes: Interrupt in class, ESP32 - #44 by PieterP

You should never return the result of new as a raw pointer, definitely not as a pointer to void. You should not use new at all, except in some very low level memory allocation code.
Using new on embedded systems can also cause issues with memory fragmentation.

Simply create variables of type Simple and assign their address to the pointers to your instance table (see post above) or your simpleObjectN variables.

Again, why the void pointers? You're casting to Simple unconditionally, so since you know the type, the argument type should be Simple *.

Why? It's a C++ file, __cplusplus will always be defined.

Why not just use the constructor for its intended purpose?

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