nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
Data Structures | Public Types | Public Member Functions | Static Public Member Functions | Data Fields | Static Public Attributes | Protected Member Functions | Protected Attributes
nrfcxx::sensor::contact Class Reference

State and functionality related to monitoring dry contacts. More...

#include <nrfcxx/sensor/contact.hpp>

Data Structures

struct  state_type
 An aggregate capturing consistent contact state. More...
 

Public Types

using timestamp_type = uint64_t
 The type used for full-scale contact state change event times. More...
 
using timediff_type = std::make_signed< uint64_t >::type
 The type used for full-scale time differences between event times.
 
using reduced_timestamp_type = uint16_t
 The type used for stored contact state changes. More...
 
using duration_type = std::chrono::duration< timediff_type, std::ratio< 1, TIMESTAMP_Hz > >
 Full precision duration type for timestamp clock. More...
 
using mutex_type = periph::GPIOTE::mutex_type
 An alias to an RAII type suitable for GPIOTE mutex protection.
 
using ordinal_type = uint16_t
 The type used to store state changes event ordinals.
 
using change_callback_bi = std::function< void(ordinal_type ordinal)>
 Signature for a contact state change notification. More...
 

Public Member Functions

 contact (uint8_t psel, change_callback_bi callback, uint32_t pin_cnf=gpio::PIN_CNF_RDONLY)
 Construct and configure a contact with callback on change. More...
 
void record_change_bi (bool double_tap=false)
 Update the ordinal to reflect a state change. More...
 
periph::GPIOTE::sense_listener sense_listener ()
 Construct a GPIOTE sense listener for contact state changes. More...
 
periph::GPIOTE::event_listener event_listener ()
 Construct a GPIOTE event listener for contact state changes. More...
 
bool live_state () const
 Read the contact state. More...
 
state_type snapshot () const
 Return a consistent snapshot of the contact state. More...
 
timestamp_type epoch_ts () const
 The absolute time at which the contact initial state was captured. More...
 

Static Public Member Functions

template<typename INTEGRAL >
static constexpr INTEGRAL ts_from_ms (INTEGRAL dur_ms)
 Calculate a timestamp duration derived from milliseconds.
 
template<typename INTEGRAL >
static constexpr INTEGRAL utt_from_ts (INTEGRAL dur_ts)
 Calculate an uptime duration derived from timestamp ticks.
 
template<typename DurT = duration_type>
constexpr static DurT to_duration (timediff_type dts)
 Generic conversion from timestamp ticks to a std::chrono::duration type. More...
 
static timestamp_type now ()
 Get the full-scale current time in the representation of recorded contact events.
 
static reduced_timestamp_type timestamp ()
 Get the reduced-range current time in the representation of recorded contact events. More...
 
static bool state_from_ordinal (ordinal_type eo)
 Synthesize pin state from an event ordinal.
 

Data Fields

uint8_t const psel
 The GPIO pin on which the contact is monitored.
 

Static Public Attributes

static constexpr unsigned int HISTORY_LENGTH = 8
 The number of timestamped state changes recorded.
 
static constexpr unsigned int TIMESTAMP_Hz = contact_internal::ts_from_utt(clock::uptime::Frequency_Hz)
 The clock rate used to record state transition timestamps.
 

Protected Member Functions

bool recorded_state_bi_ () const
 Return the last confirmed contact state. More...
 
void gpiote_sense_bi_ (const periph::GPIOTE::sense_status_type *sp)
 
void gpiote_event_bi_ (periph::GPIOTE &instance)
 

Protected Attributes

change_callback_bi callback_
 
state_type volatile state_bi_
 

Detailed Description

State and functionality related to monitoring dry contacts.

Instances of this class track edge transitions of a GPIO connected to a dry contact such as a hall-effect switch or leak detector. The total number of state changes, as well as low-resolution timestamps of recent state changes, are maintained for access by an application.

State change events are represented as ordinals described at state_type::ordinal. Note that the system assumes the default state of the contact is open; if the state is closed at the point the instance is constructed a close event will be synthesized. To use this infrastructure you will need the following in your application: extern "C" { void GPIOTE_IRQHandler (void) { nrfcxx::periph::GPIOTE::irq_handler(); } } // extern "C" You will also need the following in your application main function, placed after initializing the contacts: NVIC_EnableIRQ(GPIOTE_IRQn); These are required for both edge and level contact types. Methods and members with a suffix bi are expected to be invoked under mutex_type mutex.

Member Typedef Documentation

◆ change_callback_bi

using nrfcxx::sensor::contact::change_callback_bi = std::function<void(ordinal_type ordinal)>

Signature for a contact state change notification.

Parameters
ordinalthe value from state_type::ordinal at the time the function is invoked.
Note
If the contact state changes fast enough that state_type::ordinal increments multiple times, the notification will occur after the last increment.
Functions with this signature are expected to be invoked when the GPIOTE IRQ is blocked.

◆ duration_type

using nrfcxx::sensor::contact::duration_type = std::chrono::duration<timediff_type, std::ratio<1, TIMESTAMP_Hz> >

Full precision duration type for timestamp clock.

Warning
As with other std::chrono durations the representation type is signed, so be careful if a negative duration might end up in a context where it's converted to an unsigned counter value.

◆ reduced_timestamp_type

The type used for stored contact state changes.

The value of the timestamp will be as though obtained through timestamp(). At the default TIMESTAMP_Hz of 128 the timestamp wraps at the same interval as clock::uptime::now24().

◆ timestamp_type

The type used for full-scale contact state change event times.

See now().

Constructor & Destructor Documentation

◆ contact()

nrfcxx::sensor::contact::contact ( uint8_t  psel,
change_callback_bi  callback,
uint32_t  pin_cnf = gpio::PIN_CNF_RDONLY 
)

Construct and configure a contact with callback on change.

Parameters
pselthe GPIO pin number on which the contact will be monitored.
callbackfunction invoked when the contact state changes.
pin_cnfthe pin configuration to use for the GPIO identified by psel_.

Member Function Documentation

◆ epoch_ts()

timestamp_type nrfcxx::sensor::contact::epoch_ts ( ) const

The absolute time at which the contact initial state was captured.

Values are from the range of now().

◆ event_listener()

periph::GPIOTE::event_listener nrfcxx::sensor::contact::event_listener ( )
inline

Construct a GPIOTE event listener for contact state changes.

This method allows contacts to use hardware notifications of state changes:

auto notify = [&events](ordinal_type ordinal) {events.set(EVT_BUTTON1);};
contact ordinal1{NRFCXX_BOARD_PSEL_BUTTON1, notify, pin_cnf};
auto b0ch = GPIOTE::allocate();
b0ch->config_event(ordinal1.psel);
GPIOTE::event_listener button1{ordinal1.event_listener(*b0ch)};
button1.enable();
b0ch->enable_event();

◆ live_state()

bool nrfcxx::sensor::contact::live_state ( ) const
inline

Read the contact state.

A logic high level is considered closed and is represented by true.

A logic low level is considered open and is represented by false.

◆ record_change_bi()

void nrfcxx::sensor::contact::record_change_bi ( bool  double_tap = false)

Update the ordinal to reflect a state change.

Increment the ordinal to reflect a state change, recording the current time as the time when the change occurred.

If double_tap we've been informed that there was a state change, but the observed state is the same as the last one, so record a second, concurrent, state change.

Parameters
double_tapif true record two changes.

◆ recorded_state_bi_()

bool nrfcxx::sensor::contact::recorded_state_bi_ ( ) const
inlineprotected

Return the last confirmed contact state.

Values match those of live_state().

◆ sense_listener()

periph::GPIOTE::sense_listener nrfcxx::sensor::contact::sense_listener ( )
inline

Construct a GPIOTE sense listener for contact state changes.

This method allows contacts to use a shared hardware notification of state changes:

auto notify = [&events](ordinal_type ordinal) {events.set(EVT_BUTTON1);};
contact ordinal1{NRFCXX_BOARD_PSEL_BUTTON1, notify, pin_cnf};
GPIOTE::sense_listener button1{ordinal1.sense_listener()};
GPIOTE::enable_sense();
button1.enable();
Warning
If the sense returns to its original state prior to the GPIOTE FLIH executing no change will be detected. This is in contrast to the use of event_listener(), which will detect even transient changes but at the cost of requiring a dedicated GPIOTE resource.

◆ snapshot()

state_type nrfcxx::sensor::contact::snapshot ( ) const

Return a consistent snapshot of the contact state.

Note
See state_type::live_state for a situation where its value and the low bit of state_type::ordinal may be inconsistent.

◆ timestamp()

static reduced_timestamp_type nrfcxx::sensor::contact::timestamp ( )
inlinestatic

Get the reduced-range current time in the representation of recorded contact events.

This is equivalent to but faster than fetching the low 16 bits of now().

◆ to_duration()

template<typename DurT = duration_type>
constexpr static DurT nrfcxx::sensor::contact::to_duration ( timediff_type  dts)
inlinestaticconstexpr

Generic conversion from timestamp ticks to a std::chrono::duration type.

Template Parameters
DurTthe duration type to use.
Parameters
tsthe difference between two timestamps.

The documentation for this class was generated from the following file: