Control Surface  1.1.0
MIDI Control Surface library for Arduino
LEDs.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
6 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7 
8 #include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp>
9 
11 
12 /**
13  * @brief A class for collections of LEDs that can display ranges.
14  *
15  * @tparam N
16  * The number of LEDs in the collection.
17  *
18  * @ingroup AH_HardwareUtils
19  */
20 template <uint8_t N>
21 class LEDs {
22  public:
23  /**
24  * @brief Create a LEDs object.
25  *
26  * @param ledPins
27  * An array of pins with the LEDs connected.
28  */
29  LEDs(const PinList<N> &ledPins) : ledPins(ledPins) {}
30 
31  /**
32  * @brief Initialize (set LED pins as outputs).
33  */
34  void begin() const {
35  for (const pin_t &pin : ledPins)
36  ExtIO::pinMode(pin, OUTPUT);
37  }
38 
39  /**
40  * @brief Turn on a range of the LEDs.
41  *
42  * @param startOn
43  * The first LED of the range to turn on (the LEDs before this one
44  * are turned off).
45  * @param startOff
46  * The first LED after the range to turn off.
47  */
48  void displayRange(uint8_t startOn, uint8_t startOff) const {
49  for (uint8_t pin = 0; pin < startOn; pin++)
50  clear(pin);
51  for (uint8_t pin = startOn; pin < startOff; pin++)
52  set(pin);
53  for (uint8_t pin = startOff; pin < N; pin++)
54  clear(pin);
55  }
56 
57  /// Turn on the given LED.
58  void set(uint8_t index) const {
59  // TODO: bounds check?
60  ExtIO::digitalWrite(ledPins[index], HIGH);
61  }
62 
63  /// Turn off the given LED.
64  void clear(uint8_t index) const {
65  // TODO: bounds check?
66  ExtIO::digitalWrite(ledPins[index], LOW);
67  }
68 
69  /**
70  * @brief Turn on a single LED, and turn off all others.
71  *
72  * @param led
73  * The LED to turn on.
74  */
75  void displayDot(uint8_t led) const { displayRange(led, led + 1); }
76 
77  /**
78  * @brief Turn off all LEDs.
79  */
80  void clear() const {
81  for (pin_t pin : ledPins)
83  }
84 
85  private:
87 };
88 
90 
AH::LEDs::set
void set(uint8_t index) const
Turn on the given LED.
Definition: LEDs.hpp:58
Warnings.hpp
AH::LEDs::displayDot
void displayDot(uint8_t led) const
Turn on a single LED, and turn off all others.
Definition: LEDs.hpp:75
AH::LEDs::clear
void clear(uint8_t index) const
Turn off the given LED.
Definition: LEDs.hpp:64
AH::ExtIO::pinMode
void pinMode(pin_t pin, uint8_t mode)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:36
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::LEDs::begin
void begin() const
Initialize (set LED pins as outputs).
Definition: LEDs.hpp:34
HIGH
const uint8_t HIGH
Definition: ExtendedInputOutput.hpp:46
AH::LEDs::ledPins
const PinList< N > ledPins
Definition: LEDs.hpp:86
AH::Array< pin_t, N >
LOW
const uint8_t LOW
Definition: ExtendedInputOutput.hpp:47
AH::LEDs::clear
void clear() const
Turn off all LEDs.
Definition: LEDs.hpp:80
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::LEDs
A class for collections of LEDs that can display ranges.
Definition: LEDs.hpp:21
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
OUTPUT
const uint8_t OUTPUT
Definition: ExtendedInputOutput.hpp:50
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10
AH::LEDs::displayRange
void displayRange(uint8_t startOn, uint8_t startOff) const
Turn on a range of the LEDs.
Definition: LEDs.hpp:48
AH::ExtIO::digitalWrite
void digitalWrite(pin_t pin, uint8_t val)
An ExtIO version of the Arduino function.
Definition: ExtendedInputOutput.cpp:47
AH::LEDs::LEDs
LEDs(const PinList< N > &ledPins)
Create a LEDs object.
Definition: LEDs.hpp:29