I need to execute the function "keyboard_control()" each 100ms.
I have tried several ways of calling function from inside a class, using a Ticker object, but it have not worked. It only works if I declare "keyboard_control()" as static "static void keyboard_control()" inside the class, but then it will not be able
to access "key_pressed" variable from this class. What can I do to run "keyboard_control()" using a Thicker object?
Thanks a lot for any helping!
.H file
include <Ticker.h>
class Keyb
{
public:
bool key_pressed; //true if any key was pressed
Ticker keyboard_ticker;
Keyb(/*several input values...*/);
int inkey();
void keyboard_control(); //This function must be called each 100ms
//and uses "key_pressed" variable declared as public in this class
void inittesta() {
keyboard_ticker.attach_ms(100, keyboard_control); //it shows error that it should be declared as static
//it if keyboard_control is declared as static, it will not be able
//to access "key_pressed" variable from this class. What can I do?
}
};
I´m using default Arduino Ticker lib, but it is not clear for me what version is.
It is very interesting that the Ticker object call the attached function without the need to put a ticker.update() at loop() from Arduino, it seems that it is calling through an interrupt timer.
The code bellow worked, but I do not know whether is it the better form to do it.
Please, comment if possible
.H file
include <Ticker.h>
class Keyb
{
public:
bool key_pressed; //true if any key was pressed
Ticker keyboard_ticker;
Keyb(/*several input values...*/);
int inkey();
void keyboard_control(); //This function must be called each 100ms
//and uses "key_pressed" variable declared as public in this class
static void ticker_trigger(Keyb* callback_func) { //I did use an intermediate function(needs to be public!)
callback_func->keyboard_control();
}
void inittesta() {
keyboard_ticker.attach_ms(100, &ticker_trigger, this); //it finally worked like a charm ;-)
}
};
I see at least two libraries in the Arduino Library manager with a "Ticker.h" header file. That's why I asked for a GitHub link to the one you're using. Why make me guess?
It's kind of involved and, like most things in "Modern C++", any documentation you find online will be full of pedantic Mumbo-Jumbo. But, breaking it down ....
It's creating and initializing an object (i.e. variable) of type "callback_function_t". The variable name happens to be "f".
The callback_function_t type is defined in Ticker scope (i.e. Ticker::). So, looking in Ticker.h, you find:
So, callback_function_t is just an alias for an object of type std::function. This is a wrapper class that can be used to encapsulate different types of functions and give them a common "look".
In this case, f is being initialized to encapsulate a function of type std::bind. Here's were the Mumbo-Jumbo really starts. But, suffice it to say that it gives you a way to call the keyboard_control function on the object pointed to by the "this" pointer.
Finally, this line:
keyboard_ticker.attach_ms(100, f);
Calls the overload of the attach_ms function that accepts a std::function argument (aliased as type callback_function_t) . Again, from Ticker.h: