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