2

Currently to use TimedAction for a function a program would look like so:

#include <TimedAction.h>
void functionX();
TimedAction funtionTimed = TimedAction(1000, functionX);
void setup()
{
}
void loop()
{
functionTimed.check();
}
void functionX()
{
 //run function
}

My question is say I have an object class with some functions I would like to use with this TimedAction library. How would this be written?

asked May 26, 2018 at 14:30

1 Answer 1

1

You can't. The TimedAction class doesn't support the function prototype required for your undefined class (it has to match your class, so can't be written).

You have to call a normal function which then calls the class function, or you have to have your class function as static so it doesn't include the class in the function prototype.

class foo {
 public:
 static void runMe() { 
 // blah blah
 }
};
TimedAction funtionTimed = TimedAction(1000, foo::runMe);

However, TimedAction is pretty simplistic and you don't really need it. You can just write a function to only run every so often:

class foo {
 private:
 uint32_t _runMeTimestamp;
 public:
 void runMe() {
 if (millis() - _runMeTimestamp < 1000) return;
 _runMeTimestamp = millis();
 // blah blah
 }
};
foo myFoo;
void loop() {
 myFoo.runMe();
}

Those two lines of code, plus a variable to store the timestamp in, are all you need to make a function run at periodic intervals.

answered May 26, 2018 at 14:38
2
  • 1
    Thanks, yes I had been using it to call a normal function which then calls the class function, was just curious if I could reduce redundancy Commented May 26, 2018 at 14:50
  • Protip: You can re-write the library to use std::function instead of raw function pointers. You can then use lambda expressions or std::bind to call into a member function of a particular object. This would be the C++ way to do it. Commented May 26, 2018 at 16:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.