LCOV - code coverage report
Current view: top level - src/AH/Hardware/LEDs - LEDs.hpp (source / functions) Hit Total Coverage
Test: ffed98f648fe78e7aa7bdd228474317d40dadbec Lines: 23 24 95.8 %
Date: 2022-05-28 15:22:59 Functions: 17 19 89.5 %
Legend: Lines: hit not hit

          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 <uint16_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(uint16_t startOn, uint16_t startOff) const {
      49          14 :         for (uint16_t pin = 0; pin < startOn; pin++)
      50           6 :             clear(pin);
      51          31 :         for (uint16_t pin = startOn; pin < startOff; pin++)
      52          23 :             set(pin);
      53          34 :         for (uint16_t pin = startOff; pin < N; pin++)
      54          26 :             clear(pin);
      55           8 :     }
      56             : 
      57             :     /// Turn on the given LED.
      58          23 :     void set(uint16_t index) const {
      59             :         // TODO: bounds check?
      60          23 :         ExtIO::digitalWrite(ledPins[index], HIGH);
      61          23 :     }
      62             : 
      63             :     /// Turn off the given LED.
      64          32 :     void clear(uint16_t index) const {
      65             :         // TODO: bounds check?
      66          32 :         ExtIO::digitalWrite(ledPins[index], LOW);
      67          32 :     }
      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(uint16_t led) const { displayRange(led, led + 1); }
      76             : 
      77             :     /**
      78             :      * @brief   Turn off all LEDs.
      79             :      */
      80           2 :     void clear() const {
      81          12 :         for (pin_t pin : ledPins)
      82          10 :             ExtIO::digitalWrite(pin, LOW);
      83           2 :     }
      84             : 
      85             :   private:
      86             :     const PinList<N> ledPins;
      87             : };
      88             : 
      89             : END_AH_NAMESPACE
      90             : 
      91             : AH_DIAGNOSTIC_POP()

Generated by: LCOV version 1.15