Closed pedvide closed 3 years ago
Which platforms / boards did you checked, with and without functionals?
So far only on a ESP8266, tomorrow I can test it on a Teensy, which also has functional support. I don't have any other boards.
Did you test with the __arm__
macro ? I will have a look with the STM32 boards. Because I never worked with <functional>
and templates
, is it possible to make a callback to a simple function like timer(int value)
instead of functions without any parameter? The class example is very complex. Which IDE you use? If it works I will made some additional wishes and will also make a support for the Mbed library.
Did you test with the arm macro ? I will have a look with the STM32 boards
I tested the __arm__
macro, as far as I could find all arm's have functional, but they also declare it with the __has_include macro, so I prefer to use that.
Because I never worked with
and templates, is it possible to make a callback to a simple function like timer(int value) instead of functions without any parameter?
The only difference between std::function
and a normal function pointer is to be able to use lambdas so capture context, otherwise it's the same. You can already define typedef void (*fptr)(int);
for example, but I don't know how you could use it, maybe by passing a pointer to the argument in the Ticker constructor and calling the function with it?
Indeed the nice thing about lambdas is that they allow you to do that, eg:
void func(int a) {
Serial.println(a);
}
int a = 5;
int* p_a = &a;
Ticker ticker([](){func(*p_a);});
Then you can change a
and the function will see the new value via the pointer. (I haven't tested this).
The class example is very complex. Which IDE you use?
I used the Arduino IDE, and tested it on platformIO too. I can remove the B
class from the example if it makes it easier.
I have updated the example above with an other usage of a std::function, a functor, ie: a class that defines a operator()
so it can be called like a function.
Can you download from master branch and test it? I added the example and I'm using arm macro to avoid to much processor specific macros. I'm not using your PR because I made some other improvements.
Hey, I'll try to test it soon. I had a look at the code and I strongly recommend you don't import the whole std
namespace on line 56 (https://github.com/sstaub/Ticker/blob/master/Ticker.h#L56). Just do using fptr = std::function<void()>;
, you gain nothing by importing the whole namespace and have a lot to lose if you ever try to define a function or variable that already exists in that namespace. Even worse, any program that uses your library now has imported that namespace, so the potential for conflicts is huge.
Regarding allowing only arm boards to use std::function, it doesn't work for ESP8266, as it's not an arm. It's also kind of risky to assume that any arm board will have functional
(probably a very small risk though).
changing to std::function is ok. Has an ESP32 also a std library?
I made an update for use with ESP32 and ESP8266, sorry I thought ESPs are also ARMs.
Great, thank you! I'll close this PR now.
Hey guys, I'm trying to instantiate a Ticker inside a class constructor, but if I capture the "this" reference in the lamda function, I get the error
no instance of constructor "Ticker::Ticker" matches the argument list: argument types are: (lambda []()->void, int, int, resolution_t)
And if I don't capture the constructor accepts it fine, but then I get this one:
the enclosing-function 'this' cannot be referenced in a lambda body unless it is in the capture listC/C++(1738)
CPP noob here btw, so this is more a coding question than a library question, but since you used my exaact use case in your examples I feel like I should just ask you @pedvide
Nevermind, seems like this enhancement doesn't work for Arduino UNO. Great library @sstaub! Thank you
Add support for std::function so users can use class members and lambdas as callback. Example (tested on a ESP8266):