Class that supports button state and timing events.
More...
#include <nrfcxx/sensor/button.hpp>
|
using | event_callback = std::function< void(unsigned int evt, uint64_t duration_utt)> |
| Callback type used for notification of button events. More...
|
|
|
static const char * | eventstr (unsigned int evt) |
|
|
static constexpr unsigned int | EVT_RELEASE = 0 |
| Event state indicating that the button has been released. More...
|
|
static constexpr unsigned int | EVT_CLICK = 1 |
| Event state indicating that the button has been pressed. More...
|
|
static constexpr unsigned int | EVT_HOLD = 2 |
| Event state indicating that the button is being held. More...
|
|
static constexpr unsigned int | EVT_STUCK = 3 |
| Event state indicating that the button appears to be stuck. More...
|
|
Class that supports button state and timing events.
This leverages sensor::contact to detect level changes using a shared hardware resource. It assumes human-initiated state changes, i.e. may discard events that occur too rapidly.
- Note
- The application infrastructure support for the GPIOTE IRQ described in contact must be provided when using Button.
◆ event_callback
Callback type used for notification of button events.
The callback is provided in the constructor, and is invoked from within process() under application control.
- Parameters
-
◆ Button()
nrfcxx::sensor::Button::Button |
( |
uint8_t |
psel, |
|
|
const notifier_type & |
notify, |
|
|
event_callback |
callback, |
|
|
bool |
active_low = board::button_active_low |
|
) |
| |
Construct a button instance.
- Parameters
-
psel | the GPIO pin to which the button is attached. |
notify | an event setter used to notify the application that process() needs to be invoked to update the button state. |
callback | the method by which state changes are communicated to the application. The callback is invoked only while process() is active. |
active_low | whether the button on psel is active low (true) or high (false). |
◆ debounce_utt() [1/2]
unsigned int nrfcxx::sensor::Button::debounce_utt |
( |
| ) |
const |
|
inline |
Duration after detected release before button is confirmed to be released.
A timer is set for this duration after the GPIO for the button transitions to a released state after being in a pressed state. If the GPIO transitions back to a pressed state before this deadline elapses the alarm is cancelled.
The default value is about 80 ms.
◆ debounce_utt() [2/2]
int nrfcxx::sensor::Button::debounce_utt |
( |
unsigned int |
utt | ) |
|
Set debounce_utt().
- Parameters
-
utt | the duration used to detect a button release. Values less than 256 (or one contact tick) are rejected. |
- Returns
- zero on success, a negative error code if
utt
is not acceptable.
◆ hold_utt() [1/2]
unsigned int nrfcxx::sensor::Button::hold_utt |
( |
| ) |
const |
|
inline |
Duration of button hold necessary to produce EVT_HOLD.
The default value is about 500 ms.
◆ hold_utt() [2/2]
int nrfcxx::sensor::Button::hold_utt |
( |
unsigned int |
utt | ) |
|
Set hold_utt().
- Parameters
-
utt | the duration used to identify a hold. Values less than 256 (or one contact tick) or greater than the current setting of stuck_utt() are rejected. |
- Returns
- zero on success, a negative error code if
utt
is not acceptable.
◆ process()
void nrfcxx::sensor::Button::process |
( |
| ) |
|
Method to be invoked by application when button state changes.
On receipt of the notification as provided to the constructor the application should invoke this method from the main event loop. During the invocation the callback provided to the constructor will be invoked with events related to the button.
- Note
- Multiple events may be emitted during a single process() invocation.
◆ state() [1/2]
unsigned int nrfcxx::sensor::Button::state |
( |
| ) |
const |
◆ state() [2/2]
unsigned int nrfcxx::sensor::Button::state |
( |
uint64_t & |
duration_utt | ) |
const |
Get the most recent event state and duration since click.
- Parameters
-
[out] | stores | zero if returning EVT_RELEASE, otherwise stores the time since the button was pressed. |
- Returns
- as with state().
◆ stuck_utt() [1/2]
unsigned int nrfcxx::sensor::Button::stuck_utt |
( |
| ) |
const |
|
inline |
Duration of button hold necessary to transition from hold to stuck.
The default value is about 20 s.
◆ stuck_utt() [2/2]
int nrfcxx::sensor::Button::stuck_utt |
( |
unsigned int |
utt | ) |
|
Set stuck_utt().
- Parameters
-
utt | the duration used to identify a stuck button. Values less than the current setting of hold_utt() are rejected. |
- Returns
- zero on success, a negative error code if
utt
is not acceptable.
◆ EVT_CLICK
constexpr unsigned int nrfcxx::sensor::Button::EVT_CLICK = 1 |
|
staticconstexpr |
Event state indicating that the button has been pressed.
This is the first event received for a press event. There is no delay before this event. It may be followed by EVT_HOLD or EVT_RELEASE.
◆ EVT_HOLD
constexpr unsigned int nrfcxx::sensor::Button::EVT_HOLD = 2 |
|
staticconstexpr |
Event state indicating that the button is being held.
This event is generated roughly hold_utt() ticks after EVT_CLICK if there is not an earlier EVT_RELEASE event.
- Note
- This event may be inhibited if the button is released shortly after hold_utt() ticks. The correct pressed duration will be present in the EVT_RELEASE event.
◆ EVT_RELEASE
constexpr unsigned int nrfcxx::sensor::Button::EVT_RELEASE = 0 |
|
staticconstexpr |
Event state indicating that the button has been released.
- Note
- That this value is zero, and all press-related events are nonzero, is part of the formal API.
◆ EVT_STUCK
constexpr unsigned int nrfcxx::sensor::Button::EVT_STUCK = 3 |
|
staticconstexpr |
Event state indicating that the button appears to be stuck.
This event is generated roughly stuck_utt() ticks after EVT_CLICK if there is not an earlier EVT_RELEASE event.
The documentation for this class was generated from the following file: