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