ivanseidel / DueTimer

⏳ Timer Library fully implemented for Arduino DUE
MIT License
211 stars 89 forks source link

How can I create a class with its own timer #76

Open sevketk opened 3 years ago

sevketk commented 3 years ago

I need a one class , have a timer. in class method can start, stop timer.

#include <DueTimer.h>

void RGBmatrixPanel::begin(void) {
   Timer3.attachInterrupt(updateDisplay);
   Timer3.start(250);
} 
void RGBmatrixPanel::updateDisplay() {
    if(something doing){
          Timer3.stop();
    }
}

But i get error on arduinodue

E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.cpp: In member function 'void RGBmatrixPanel::begin()':
E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.cpp:86:41: error: no matching function for call to 'DueTimer::attachInterrupt(<unresolved overloaded function type>)'
     Timer3.attachInterrupt(updateDisplay);
                                         ^
E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.cpp:86:41: note: candidate is:
In file included from E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.h:26:0,
                 from E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.cpp:3:
E:\kutuphane\belgelerim\Arduino\libraries\DueTimer-master/DueTimer.h:84:12: note: DueTimer& DueTimer::attachInterrupt(void (*)())
  DueTimer& attachInterrupt(void (*isr)());

how to use in a class duetimer library?

ivanseidel commented 3 years ago

Hi Sevtek,

Due to the lack of the C++ version used in Arduino Due, you can only attach callbacks and pass as parameter functions that do not require the scope of object instances, example: You can pass along a pointer to a static class function, but cannot pass a method (a function bound to the instance).

That being said, your way out of this problem is to either convert your "class" to static methods (and just be a better organization), or in case you need multiple instances, create a global mapping table (an array of mappings) that convert the instance ID (some value that starts with 0 in the first instance of your class and gets stored inside it, and goes up every time you instantiate it). Store itself in the mapping table in that index position, and once the callback is fired, you try to redirect the call to the proper class. This is not that easy to do, but is feasible if needed.

Hope it helps!

Em sáb., 17 de out. de 2020 às 19:12, sevketk notifications@github.com escreveu:

I need a one class , have a timer. in class method can start, stop timer.

include

void RGBmatrixPanel::begin(void) { Timer3.attachInterrupt(updateDisplay); Timer3.start(250); } void RGBmatrixPanel::updateDisplay() { if(something doing){ Timer3.stop(); } }

But i get error on arduinodue

E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.cpp: In member function 'void RGBmatrixPanel::begin()': E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.cpp:86:41: error: no matching function for call to 'DueTimer::attachInterrupt()' Timer3.attachInterrupt(updateDisplay); ^ E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.cpp:86:41: note: candidate is: In file included from E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.h:26:0, from E:\kutuphane\belgelerim\Arduino\libraries\RGB-matrix-Panel-master\RGBmatrixPanel.cpp:3: E:\kutuphane\belgelerim\Arduino\libraries\DueTimer-master/DueTimer.h:84:12: note: DueTimer& DueTimer::attachInterrupt(void ()()) DueTimer& attachInterrupt(void (isr)());

how to use in a class duetimer library?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ivanseidel/DueTimer/issues/76, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAXVLLYZAREPHTLHHJUT52TSLIJF5ANCNFSM4SUUOFUQ .