nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
button.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2018-2019 Peter A. Bigot */
3 
7 #ifndef NRFCXX_SENSOR_BUTTON_HPP
8 #define NRFCXX_SENSOR_BUTTON_HPP
9 #pragma once
10 
12 
13 namespace nrfcxx {
14 namespace sensor {
15 
24 class Button
25 {
26 public:
27 
32  static constexpr unsigned int EVT_RELEASE = 0;
33 
39  static constexpr unsigned int EVT_CLICK = 1;
40 
49  static constexpr unsigned int EVT_HOLD = 2;
50 
55  static constexpr unsigned int EVT_STUCK = 3;
56 
57  static const char* eventstr (unsigned int evt);
58 
69  using event_callback = std::function<void(unsigned int evt, uint64_t duration_utt)>;
70 
75  unsigned int state () const;
76 
83  unsigned int state (uint64_t& duration_utt) const;
84 
98  Button (uint8_t psel,
99  const notifier_type& notify,
100  event_callback callback,
101  bool active_low = board::button_active_low);
102 
112  unsigned int debounce_utt () const
113  {
114  return debounce_utt_;
115  }
116 
125  int debounce_utt (unsigned int utt);
126 
130  unsigned int hold_utt () const
131  {
132  return hold_utt_;
133  }
134 
144  int hold_utt (unsigned int utt);
145 
150  unsigned int stuck_utt () const
151  {
152  return stuck_utt_;
153  }
154 
162  int stuck_utt (unsigned int utt);
163 
173  void process ();
174 
175 private:
176  static constexpr bool state_xor = board::button_active_low;
177 
178  static void level_callback_ (Button* bp);
179 
180  static bool alarm_callback (clock::alarm& alarm);
181 
182  uint64_t process_ (contact::ordinal_type eo,
184 
185  notifier_type notify_;
186  contact contact_;
188  event_callback callback_;
189 
190  clock::alarm alarm_;
191  contact::state_type snapshot_;
192  uint64_t process_utt{};
193  uint64_t press_utt{};
194 
195  unsigned int debounce_utt_ = clock::uptime::from_ms(80U);
196  unsigned int hold_utt_ = clock::uptime::from_ms(500U);
197  unsigned int stuck_utt_ = clock::uptime::from_ms(20000U);
198 
199  uint8_t state_ = 0;
200 };
201 
202 } // ns sensor
203 } // ns nrfcxx
204 
205 #endif /* NRFCXX_SENSOR_BUTTON_HPP */
nrfcxx::sensor::Button::event_callback
std::function< void(unsigned int evt, uint64_t duration_utt)> event_callback
Callback type used for notification of button events.
Definition: button.hpp:69
nrfcxx::sensor::contact
State and functionality related to monitoring dry contacts.
Definition: contact.hpp:81
nrfcxx::sensor::contact::ordinal_type
uint16_t ordinal_type
The type used to store state changes event ordinals.
Definition: contact.hpp:147
nrfcxx::sensor::Button
Class that supports button state and timing events.
Definition: button.hpp:24
nrfcxx::sensor::contact::state_type
An aggregate capturing consistent contact state.
Definition: contact.hpp:155
nrfcxx::periph::GPIOTE::sense_listener
Object used to manage sense callbacks.
Definition: periph.hpp:854
nrfcxx::sensor::contact::timestamp_type
uint64_t timestamp_type
The type used for full-scale contact state change event times.
Definition: contact.hpp:108
nrfcxx::sensor::Button::debounce_utt
unsigned int debounce_utt() const
Duration after detected release before button is confirmed to be released.
Definition: button.hpp:112
nrfcxx::gpio::active_low
active_signal< false > active_low
Alias type used for CSn, RESETn, and other active low output signals.
Definition: gpio.hpp:669
nrfcxx::sensor::Button::Button
Button(uint8_t psel, const notifier_type &notify, event_callback callback, bool active_low=board::button_active_low)
Construct a button instance.
nrfcxx::sensor::Button::state
unsigned int state() const
Get the most recent event state.
contact.hpp
Abstraction of a dry contact monitor using nrfcxx::gpio::gpiote.
nrfcxx::sensor::Button::hold_utt
unsigned int hold_utt() const
Duration of button hold necessary to produce EVT_HOLD.
Definition: button.hpp:130
nrfcxx::sensor::Button::EVT_STUCK
static constexpr unsigned int EVT_STUCK
Event state indicating that the button appears to be stuck.
Definition: button.hpp:55
nrfcxx::notifier_type
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
nrfcxx::clock::alarm
Class supporting an alarm with custom callback and repeatability.
Definition: clock.hpp:498
nrfcxx::sensor::Button::stuck_utt
unsigned int stuck_utt() const
Duration of button hold necessary to transition from hold to stuck.
Definition: button.hpp:150
nrfcxx::clock::uptime::from_ms
constexpr static int64_t from_ms(int64_t ms)
Convert integral milliseconds to uptime ticks (rounding down).
Definition: clock.hpp:422
nrfcxx::sensor::Button::EVT_HOLD
static constexpr unsigned int EVT_HOLD
Event state indicating that the button is being held.
Definition: button.hpp:49
nrfcxx::sensor::Button::process
void process()
Method to be invoked by application when button state changes.
nrfcxx::sensor::Button::EVT_CLICK
static constexpr unsigned int EVT_CLICK
Event state indicating that the button has been pressed.
Definition: button.hpp:39
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
nrfcxx::sensor::Button::EVT_RELEASE
static constexpr unsigned int EVT_RELEASE
Event state indicating that the button has been released.
Definition: button.hpp:32