nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes
nrfcxx::sensor::Button Class Reference

Class that supports button state and timing events. More...

#include <nrfcxx/sensor/button.hpp>

Public Types

using event_callback = std::function< void(unsigned int evt, uint64_t duration_utt)>
 Callback type used for notification of button events. More...
 

Public Member Functions

unsigned int state () const
 Get the most recent event state. More...
 
unsigned int state (uint64_t &duration_utt) const
 Get the most recent event state and duration since click. More...
 
 Button (uint8_t psel, const notifier_type &notify, event_callback callback, bool active_low=board::button_active_low)
 Construct a button instance. More...
 
unsigned int debounce_utt () const
 Duration after detected release before button is confirmed to be released. More...
 
int debounce_utt (unsigned int utt)
 Set debounce_utt(). More...
 
unsigned int hold_utt () const
 Duration of button hold necessary to produce EVT_HOLD. More...
 
int hold_utt (unsigned int utt)
 Set hold_utt(). More...
 
unsigned int stuck_utt () const
 Duration of button hold necessary to transition from hold to stuck. More...
 
int stuck_utt (unsigned int utt)
 Set stuck_utt(). More...
 
void process ()
 Method to be invoked by application when button state changes. More...
 

Static Public Member Functions

static const char * eventstr (unsigned int evt)
 

Static Public Attributes

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...
 

Detailed Description

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.

Member Typedef Documentation

◆ event_callback

using nrfcxx::sensor::Button::event_callback = std::function<void(unsigned int evt, uint64_t duration_utt)>

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
evtOne of EVT_RELEASE, EVT_CLICK, EVT_HOLD, or EVT_STUCK.
duration_uttthe length of time since the button was pressed, in uptime ticks.

Constructor & Destructor Documentation

◆ 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
pselthe GPIO pin to which the button is attached.
notifyan event setter used to notify the application that process() needs to be invoked to update the button state.
callbackthe method by which state changes are communicated to the application. The callback is invoked only while process() is active.
active_lowwhether the button on psel is active low (true) or high (false).

Member Function Documentation

◆ 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
uttthe 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
uttthe 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

Get the most recent event state.

Returns
One of EVT_RELEASE, EVT_CLICK, EVT_HOLD, or EVT_STUCK.

◆ 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]storeszero 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
uttthe 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.

Field Documentation

◆ 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: