Pointer problem in a self-made c++ library

Hi everyone,

my question is rather c++-oriented since I have trouble with a self-made library...

Abstract of my project : I wrote a sketch for controlling a 8x8 led matrix, then transformed it in a library.

In this libray, a "screen refreshment" method uses the MsTimer2 library to induce an interruption every 40 ms, calling a "show" function to update the matrix display

#include "Led.h"
#include <inttypes.h>
#include "Arduino.h"
#include <MsTimer2.h>

//constructeur
Led::Led() 
{

//initialisation
void Led::begin()

    MsTimer2::set(40, show); //setting of the timer to invoke the "show" method ( see below ) when an interrupt occurs ( every 40 ms )
    MsTimer2::start();
  
}

void Led::show()  {  //the "refresh screen" method.....

MsTimer2::stop(); /unables the timer

    for(int row = 0; row < 8; row++)
    {
      digitalWrite(rowPins[row], HIGH);  
      for(int column = 0; column < 8; column++)  
      {
        bool pixel = bitRead(courant[row],7-column);  //"courant" is the array to be displayed on the matrix
        if(pixel == 1)                           
        {
          digitalWrite(columnPins[column], LOW); //
        }                                        

        delayMicroseconds(300); 
        digitalWrite(columnPins[column], HIGH); 
      }

      digitalWrite(rowPins[row], LOW); 

    } 

MsTimer2::start();
  
}

and here is my trouble : the timer delay, and the function to call when an interrupt occurs, are set in my library; when I try to compile, I receive that error :

libraries/Led/Led.cpp:72: error: argument of type ‘void (Led::)()’ does not match ‘void (*)()’

The "set" method of the MsTimer2 library waits for a function pointer as second argument...I guess here is the trouble, but I'm unable to find the right formulation...In the example supplied with the MsTimer2 library, the argument is the name of the function only...But there's still a trick....Maybe the problem is my library calls another library :astonished:??

Anyaway, if anyone more comfortable with pointers can find the answer, I'd be very grateful !

Oxirane

The problem here is you can't use a class function as the callback to a non-class function that's not been specifically set up for it.

I have usually got round it by having a non-class function that uses a global object for accessing the class:

myClass ClassObject;

void callback(void)
{
  ClassObject.callback();
}

All your sketches can then use ClassObject to access the already made class (be sure to export it in your .h file) and you can use the callback routine in your MSTimer2 setup.

A bit of a fudge, I know, and I am sure someone here knows of a better way. I'd be glad to know of it :slight_smile:

majenko:
The problem here is you can't use a class function as the callback to a non-class function that's not been specifically set up for it.

oh ! I could have guessed I could not mix class and non-class function..shame on me... =(

Yes, the problem looks very tricky...To solve it, I think about including the whole MsTmer2 in my own library...sure a bit radical ;)....

Anyway, many thanks for your answer !

Oxirane

Your crappy indentation makes your code impossible to read.

Led::Led() 
{

//initialisation
void Led::begin()

    MsTimer2::set(40, show); //setting of the timer to invoke the "show" method ( see below ) when an interrupt occurs ( every 40 ms )
    MsTimer2::start();
  
}

What's that void Led::begin() mess without the semicolon supposed to do?