Hi. I am trying to create a timer for some function call and pass a parameter to it. Using global param inside a function is inapplicable in my case because this param can change before the actual deferred call of the function, but I need to use the initial value.
If we simply have some function like: void testTimer(int valToSet)
and create timer like this: timer.setTimeout(1000L, testTimer, testParameter);
where testParameter is some int var: int testParameter = 45;
declared before setTimeout call,
then we have an error:
invalid conversion from 'void (*)(int)' to 'timer_callback_p {aka void (*)(void*)}' [-fpermissive]
I looked through the BlynkTimer sources and went to this solution for int vals:
function should use void * arg: void testTimer(void *valToSet)
And timer should be created like this: timer.setTimeout(1000L, testTimer, (void*)testParameter);
Then in function we need to use (intptr_t)valToSet
type cast.
Or in sketch:
#include <BlynkSimpleEsp8266_SSL.h>
BlynkTimer timer;
void setup()
{
Serial.begin(115200);
int testParameter = 45;
timer.setTimeout(1000L, testTimer, (void*)testParameter);
testParameter = 99;
testTimer((void*)testParameter);
}
void loop()
{
timer.run();
}
void testTimer(void *valToSet) {
//intptr_t n = (intptr_t)(valToSet);
Serial.print("Test function with input: ");
Serial.println((intptr_t)valToSet);
}
The question is: Is there a simplier way to use blynk timer parameter as arg for called function
and how to use float args (I didn’t succeed in passing float args like this)?
Thanks