|
|
|
|
|
The **ButtonHandler** is a button observer: it reacts when a button is pressed.
|
|
|
|
|
|
The **ButtonHandler** is a button observer: it reacts when a button is pressed.
|
|
|
|
|
|
In particular when the ButtonHandler is trigger it calls a callback function (that is passed to its constructor) which is defined as:
|
|
|
|
|
|
```cpp
|
|
|
using ButtonCallback = std::function<void(uint8_t, ButtonPress)>;
|
|
|
```
|
... | ... | @@ -17,20 +18,20 @@ enum class ButtonPress |
|
|
VERY_LONG // After a very long press
|
|
|
};
|
|
|
```
|
|
|
As an example if we press the button for a long time, the callback will be called one time with a ButtonPress paramenter equal to `DOWN`, one time with `LONG` and one time with `UP`.
|
|
|
|
|
|
As an example if we press the button for a long time, the callback will be called one time with a ButtonPress paramenter equal to `DOWN`, one time with `LONG` and one time with `UP`.
|
|
|
|
|
|
The ButtonHandler class also exposes a method to check if the button is currently being pressed or not:
|
|
|
```cpp
|
|
|
static bool isPressed() { return Button::value(); }
|
|
|
```
|
|
|
|
|
|
Finally consider that the [GPIO](GPIO-Usage-&-Used-Pins.md) associated to the button has to be passed as a template argument when instantiating the ButtonHandler object.
|
|
|
|
|
|
Finally consider that the [GPIO](GPIO-Usage-&-Used-Pins.md) associated to the button has to be passed as a template argument when instantiating the ButtonHandler object.
|
|
|
|
|
|
You can find the complete implementation of the ButtonHandler in `src/shared/utils/ButtonHandler.h`.
|
|
|
|
|
|
## Example
|
|
|
This example shows the usage of the ButtonHandler, using the STM32F407VG on-board user button, which is connected to pin `PA0`.
|
|
|
|
|
|
This example shows the usage of the ButtonHandler, using the STM32F407VG on-board user button, which is connected to pin `PA0`.
|
|
|
|
|
|
First of all import the dependencies. Then define the GPIO `PA0` and an ID for the button.
|
|
|
```cpp
|
|
|
#include <Common.h>
|
... | ... | @@ -49,8 +50,8 @@ void buttonCallback(uint8_t btn_id, ButtonPress btn_press) |
|
|
TRACE("Callback! btn_id=%d, btn_press=%d \n", btn_id, btn_press);
|
|
|
}
|
|
|
```
|
|
|
This function is called every time the ButtonHandler is triggered. The `buttonCallback` will be called multiple times, first when the button is pressed (`btn_press` will be equal to `DOWN`) and then when it is released, passing a different value to `btn_press` according to amount of time the button has been kept pressed. When we release it, the callback is also called with `UP` as a paramater.
|
|
|
|
|
|
This function is called every time the ButtonHandler is triggered. The `buttonCallback` will be called multiple times, first when the button is pressed (`btn_press` will be equal to `DOWN`) and then when it is released, passing a different value to `btn_press` according to amount of time the button has been kept pressed. When we release it, the callback is also called with `UP` as a paramater.
|
|
|
|
|
|
In the main we can create a ButtonHandler passing the above defined GPIO, button ID and callback and start it:
|
|
|
```cpp
|
|
|
int main()
|
... | ... | |