Issues with passing pointer function to TimedAction class (Solved)

I have the Main file and 2 classes.

RGBLEDClass - Controls RGB Led
TimedAction - Purely for photothreading

Goal:

Have a blinking Red RGB LED while hardware is in setup process and switch to Green when ready

Issue:

Cannot use delay() to blink LED as components need to run as part of my setup

Therefore, I use the TimedAction library which allows you to set an interval and the in the loop it will check if that interval has passed and executes

When passing a normal function:

#include "RGBLEDClass.h"
#include "TimedAction.h"

RGBLEDClass *RGBLEDClassPtr;

void setup() {
  Serial.begin(9600);
  RGBLEDClassPtr = new RGBLEDClass(11, 9, 10);
  
  RGBLEDClassPtr->setColourRGB(255, 0, 0);
  RGBLEDClassPtr->changeLEDState(true);
  

  //Create your own timed action but me tired
}

// DOES NOT WORK - error: no matching function for call to 'TimedAction::TimedAction(int, <unresolved overloaded function type>)'
// TimedAction LEDBlink = TimedAction(1000, RGBLEDClassPtr->changeLEDState);
 
// WORKS
TimedAction LEDBlink = TimedAction(1000, test);

void loop() {
  
}

void test() {
  Serial.println("Im working");
}

void cleanUp() {
  delete RGBLEDClassPtr;
  RGBLEDClassPtr = NULL;
}

it works perfectly! However when passing a function from the RGBLEDClass (changeLEDState() to be exact) into the TimedAction function I get the following error:

error: no matching function for call to ‘TimedAction::TimedAction(int, )’

The TimedAction constructor:

TimedAction::TimedAction(unsigned long intervl,void (*function)()){
    active = true;
	previous = 0;
	interval = intervl;
	execute = function;
}

I understand that this is an issue with “void (*function)()” parameter and because the parameter being passed will actually be “this->changeLEDState()” making it 2 parameters but I do not know what to change the parameter value to let the constructor take function pointers.

Please help, I’m struggling!

I have attached all the files being used for some of you C++ smarties to tinker with!

RGBLEDClass.cpp (2.68 KB)

RGBLEDClass.h (503 Bytes)

RGBLEDPWMTimed.ino (740 Bytes)

TimedAction.cpp (793 Bytes)

TimedAction.h (2.13 KB)

You can't use a pointer to a member function the same way as pointer to regular function. The signatures are different. Explained here: Standard C++

Thank you for the information.

I just implemented a hotfix (By far not the best way of doing it but it will have to do so I can progress)

For any of you in the future with the same problem:

I created a local function and called the function I was trying to pass in.

So now:

//Pass local function
TimedAction LEDBlink = TimedAction(1000, changeState);

//Local function with the pointer function I wanted to call
void changeState() {
  RGBLEDClassPtr->changeLEDState();
}

I think the C++ community might kill me for this :smiley:

Styryl:
I think the C++ community might kill me for this :smiley:

Why would they? You have literally no other option since implementing the functional part of the STL would be massive overkill for this stuff.

void changeState() {
  RGBLEDClassPtr->changeLEDState();
}

How do you determine which instance of the class the pointer should point to?

If there is only ever to be one instance, then there is nothing wrong with using a static method.

On the other hand, if there is only ever to be one instance, the class is improperly defined. The constructor in a singleton is never public.