nrfcxx  0.1.0
C++-17 Framework for Nordic nRF5 Devices
led.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 /* Copyright 2015-2019 Peter A. Bigot */
3 
8 #ifndef NRFCXX_LED_HPP
9 #define NRFCXX_LED_HPP
10 #pragma once
11 
12 #include <nrfcxx/clock.hpp>
13 #include <nrfcxx/gpio.hpp>
14 #include <nrfcxx/impl.hpp>
15 
16 namespace nrfcxx {
17 
19 namespace led {
20 
22 class led_type
23 {
24 private:
25  static led_type** const ledps_;
26  static uint8_t const nleds_;
27 
28 protected:
29  led_type () = default;
30 
31 public:
32  virtual ~led_type ()
33  {
34  disable();
35  }
36 
37  /* You can't copy, assign, move, or otherwise change the set of LEDs
38  * that the system provides to you. You can define additional LEDs
39  * that aren't in the board-defined sequence. */
40  led_type (const led_type&) = delete;
41  led_type& operator= (const led_type&) = delete;
42  led_type (const led_type&&) = delete;
43  led_type& operator= (led_type&&) = delete;
44 
53  virtual bool exists (void) const
54  {
55  return false;
56  }
57 
61  void enable (void)
62  {
63  off();
64  enable_();
65  }
66 
70  void disable (void)
71  {
72  disable_();
73  }
74 
80  void set (int v)
81  {
82  if (0 < v) {
83  on();
84  } else if (0 == v) {
85  off();
86  } else {
87  toggle();
88  }
89  }
90 
92  virtual void off (void)
93  { }
94 
96  virtual void on (void)
97  { }
98 
104  virtual void toggle (void)
105  {
106  if (is_on()) {
107  off();
108  } else {
109  on();
110  }
111  }
112 
116  virtual bool is_on (void) const
117  {
118  return false;
119  }
120 
122  static size_t count (void)
123  {
124  return nleds_;
125  }
126 
133  static led_type& lookup (unsigned int i)
134  {
135  if (i < nleds_) {
136  return *(ledps_[i]);
137  }
138  return no_led;
139  }
140 
141  static led_type no_led;
142 
143 protected:
144  virtual void enable_ ()
145  { }
146 
147  virtual void disable_ ()
148  { }
149 };
150 
151 inline led_type& lookup (unsigned int i)
152 {
153  return led_type::lookup(i);
154 }
155 
165 template <bool active_low = true>
166 class generic_led : public led_type
167 {
168 public:
173  explicit generic_led (gpio::generic_pin& pin) :
174  pin_{pin}
175  { }
176 
177 private:
178  gpio::generic_pin& pin_;
179 
180  bool exists (void) const override
181  {
182  return true;
183  }
184 
185  void enable_ (void) override
186  {
188  }
189 
190  void disable_ (void) override
191  {
193  }
194 
195  void off (void) override
196  {
197  if constexpr (active_low) {
198  pin_.set();
199  } else {
200  pin_.clear();
201  }
202  }
203 
204  void on (void) override
205  {
206  if constexpr (active_low) {
207  pin_.clear();
208  } else {
209  pin_.set();
210  }
211  }
212 
216  bool is_on (void) const override
217  {
218  return pin_.is_set() ^ active_low;
219  }
220 };
221 
234 class Pattern
235 {
236  static constexpr uint8_t FLAG_LOOP = 0x01;
237  static constexpr uint8_t FLAG_ACTIVE = 0x02;
238 
239 public:
240  /* Can't set an interval less than ~ 250 us. (Really it's that you
241  * can't set an interval of zero, but one less than 2 will livelock,
242  * and really anything less than 500 us will probably be
243  * indistinguishable.) */
244  static constexpr unsigned int MIN_INTERVAL_utt = 8U;
245 
248  led{led},
249  alarm_{alarm_callback, this}
250  { }
251 
254 
256  unsigned int pattern () const
257  {
258  return pattern_;
259  }
260 
264  {
265  notify_complete_ = notify;
266  }
267 
276  int reps () const
277  {
278  return (FLAG_LOOP & flags_) ? -1 : initial_reps_;
279  }
280 
282  unsigned int interval_utt () const
283  {
284  return interval_;
285  }
286 
301  int configure (unsigned int pattern,
302  unsigned int interval_utt,
303  int reps = -1);
304 
306  bool active () const
307  {
308  return FLAG_ACTIVE & flags_;
309  }
310 
313  bool loops () const
314  {
315  return FLAG_LOOP & flags_;
316  }
317 
322  unsigned int deadline () const
323  {
324  return alarm_.deadline();
325  }
326 
331  int set_deadline (unsigned int deadline);
332 
345  int start (int delay_utt = -1);
346 
353  void cancel ();
354 
355 private:
356  static bool alarm_callback (clock::alarm& alarm);
357 
358  void complete_ (bool notify);
359 
360  bool process_ ();
361 
362  clock::alarm alarm_;
363  notifier_type notify_complete_;
364 
365  unsigned int pattern_ = 0;
366  unsigned int interval_ = 0;
367 
368  uint8_t flags_ = 0;
369 
374  uint8_t initial_reps_ = 0;
375 
380  uint8_t reps_ = 0;
381 
389  uint8_t idx_ = 0;
390 };
391 
392 } // namespace led
393 } // namespace nrfcxx
394 
395 #endif /* NRFCXX_LED_HPP */
nrfcxx::led::Pattern::reps
int reps() const
Return the configured number of repetitions.
Definition: led.hpp:276
nrfcxx::led::Pattern::deadline
unsigned int deadline() const
Return the deadline of the underlying alarm.
Definition: led.hpp:322
nrfcxx::gpio::generic_pin::clear
virtual void clear()
Set the pin to drive the output low.
Definition: gpio.hpp:460
nrfcxx::led::Pattern
Class that supports background LED toggles in a repeating pattern.
Definition: led.hpp:234
nrfcxx::led::led_type::count
static size_t count(void)
Get the number of LEDs supported by the target board.
Definition: led.hpp:122
nrfcxx::led::led_type::is_on
virtual bool is_on(void) const
Read the LED.
Definition: led.hpp:116
nrfcxx::led::Pattern::pattern
unsigned int pattern() const
Return the currently configured pattern.
Definition: led.hpp:256
nrfcxx::led::led_type::disable
void disable(void)
Disable the nrf5::GPIO associated with the LED.
Definition: led.hpp:70
nrfcxx::gpio::generic_pin
Class supporting a generic GPIO pin interface.
Definition: gpio.hpp:439
nrfcxx::led::led_type::on
virtual void on(void)
Turn the LED on.
Definition: led.hpp:96
clock.hpp
Core clock-related functionality.
nrfcxx::led::Pattern::configure
int configure(unsigned int pattern, unsigned int interval_utt, int reps=-1)
Configure the pattern.
gpio.hpp
Core GPIO functionality.
nrfcxx::led::led_type::exists
virtual bool exists(void) const
Indicate whether the referenced LED exists.
Definition: led.hpp:53
nrfcxx::gpio::PIN_CNF_WRONLY
constexpr uint32_t PIN_CNF_WRONLY
GPIO pin configuration for output only.
Definition: gpio.hpp:31
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::gpio::generic_pin::is_set
virtual bool is_set() const
Return true iff the pin is valid and is configured to drive the output high.
Definition: gpio.hpp:500
nrfcxx::led::led_type::enable
void enable(void)
Set the GPIO associated with the LED to enable the LED.
Definition: led.hpp:61
nrfcxx::led::generic_led::generic_led
generic_led(gpio::generic_pin &pin)
Create an LED instance that is bound to a generic GPIO pin.
Definition: led.hpp:173
nrfcxx::led::Pattern::Pattern
Pattern(led_type &led)
Construct referencing a specific LED.
Definition: led.hpp:247
nrfcxx::led::Pattern::cancel
void cancel()
Cancel an active pattern.
nrfcxx::led::led_type::off
virtual void off(void)
Turn the LED off.
Definition: led.hpp:92
nrfcxx::led::led_type::set
void set(int v)
Set the LED to a specific state.
Definition: led.hpp:80
nrfcxx::led::led_type::toggle
virtual void toggle(void)
Toggle the LED state.
Definition: led.hpp:104
nrfcxx::led::Pattern::set_deadline
int set_deadline(unsigned int deadline)
Set the deadline of the underlying alarm.
nrfcxx::led::Pattern::led
led_type & led
Provide access to the LED controlled by the pattern.
Definition: led.hpp:253
nrfcxx::led::led_type::lookup
static led_type & lookup(unsigned int i)
Return a reference to an LED identified by ordinal position.
Definition: led.hpp:133
impl.hpp
Primary header for nrfcxx implementation dependencies.
nrfcxx::notifier_type
std::function< void()> notifier_type
Type used to hold a notifier.
Definition: core.hpp:514
nrfcxx::gpio::generic_pin::set
virtual void set()
Set the pin to drive the output high.
Definition: gpio.hpp:456
nrfcxx::led::Pattern::loops
bool loops() const
Indicates whether the pattern is configured to loop until cancel() is invoked.
Definition: led.hpp:313
nrfcxx::clock::alarm
Class supporting an alarm with custom callback and repeatability.
Definition: clock.hpp:498
nrfcxx::led::Pattern::start
int start(int delay_utt=-1)
Enable led and start the pattern.
nrfcxx::gpio::PIN_CNF_PWRUP
constexpr uint32_t PIN_CNF_PWRUP
GPIO pin configuration at power-up.
Definition: gpio.hpp:54
nrfcxx::clock::alarm::deadline
unsigned int deadline() const
The value of (the low 32 bits of) uptime::now() at which the alarm should fire.
Definition: clock.hpp:767
nrfcxx::led::Pattern::set_notify_complete
void set_notify_complete(notifier_type notify)
Provide an event setter used to tell the application when the pattern has completed.
Definition: led.hpp:263
nrfcxx::led::led_type
Base class supporting LEDs of different types.
Definition: led.hpp:22
nrfcxx::led::Pattern::interval_utt
unsigned int interval_utt() const
Return the interval between bits of the pattern.
Definition: led.hpp:282
nrfcxx
Primary namespace for nrfcxx functionality.
Definition: clock.hpp:17
nrfcxx::led::Pattern::active
bool active() const
Indicates whether the pattern is currently running.
Definition: led.hpp:306
nrfcxx::led::generic_led
A class used to manage LEDs.
Definition: led.hpp:166
nrfcxx::gpio::generic_pin::configure
virtual void configure(unsigned int pin_cnf)
Set the PIN_CNF entry for the pin if the reference is valid.
Definition: gpio.hpp:480