-
-
Notifications
You must be signed in to change notification settings - Fork 65
Description
I think I found a bug:
Issue
If I create an Easybutton object as a global object, the libary works fine.
However, If I create it as a local object, it misbehaves badly:
The object sometimes performs callbacks to random adresses on the microcontrollers memory, when using EsayButton::read()
, causign the programm to malfunction or crash.
This only happens sometimes and only if no callback function was used/specified.
Reason
I think I also found the issue causing this:
In EasyButtonBase.h, the callback function is specified like this:
class EasyButtonBase
{
...
#ifndef EASYBUTTON_DO_NOT_USE_SEQUENCES
callback_t _pressed_sequence_callbacks[MAX_SEQUENCES];
#endif
callback_t _pressed_callback; // Callback function for pressed events.
callback_t _pressed_for_callback; // Callback function for pressedFor events.
This results in the callbacks being initialized with 0 as the default value if being created as a global object. If created as a local object, they are not initialized and start with a random value.
In EasyButton.cpp it then checks if a callback is set by looking for non-zero values;
if (_pressed_callback)
{
_pressed_callback();
}
So If ther random starting value is not zero, this results in callbacks to random adresses.
Possible fix
Initialize the values properly:
#ifndef EASYBUTTON_DO_NOT_USE_SEQUENCES
callback_t _pressed_sequence_callbacks[MAX_SEQUENCES] ={0};
#endif
callback_t _pressed_callback = 0; // Callback function for pressed events.
callback_t _pressed_for_callback = 0; // Callback function for pressedFor events.