Line data Source code
1 : /* ✔ */ 2 : 3 : #pragma once 4 : 5 : #include <AH/Settings/Warnings.hpp> 6 : AH_DIAGNOSTIC_WERROR() // Enable errors on warnings 7 : 8 : #include <AH/Hardware/ExtendedInputOutput/ExtendedInputOutput.hpp> 9 : 10 : BEGIN_AH_NAMESPACE 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 3 : LEDs(const PinList<N> &ledPins) : ledPins(ledPins) {} 30 : 31 : /** 32 : * @brief Initialize (set LED pins as outputs). 33 : */ 34 3 : void begin() const { 35 24 : for (const pin_t &pin : ledPins) 36 21 : ExtIO::pinMode(pin, OUTPUT); 37 3 : } 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 8 : void displayRange(uint8_t startOn, uint8_t startOff) const { 49 14 : for (uint8_t pin = 0; pin < startOn; pin++) 50 6 : clear(pin); 51 33 : for (uint8_t pin = startOn; pin < startOff; pin++) 52 25 : set(pin); 53 23 : for (uint8_t pin = startOff; pin < N; pin++) 54 15 : clear(pin); 55 8 : } 56 : 57 : /// Turn on the given LED. 58 25 : void set(uint8_t index) const { 59 : // TODO: bounds check? 60 25 : ExtIO::digitalWrite(ledPins[index], HIGH); 61 25 : } 62 : 63 : /// Turn off the given LED. 64 21 : void clear(uint8_t index) const { 65 : // TODO: bounds check? 66 21 : ExtIO::digitalWrite(ledPins[index], LOW); 67 21 : } 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 0 : void displayDot(uint8_t led) const { displayRange(led, led + 1); } 76 : 77 : /** 78 : * @brief Turn off all LEDs. 79 : */ 80 1 : void clear() const { 81 9 : for (pin_t pin : ledPins) 82 8 : ExtIO::digitalWrite(pin, LOW); 83 1 : } 84 : 85 : private: 86 : const PinList<N> ledPins; 87 : }; 88 : 89 : END_AH_NAMESPACE 90 : 91 : AH_DIAGNOSTIC_POP()