Line data Source code
1 : /* ✔ */
2 :
3 : #pragma once
4 :
5 : #include <AH/Hardware/LEDs/LEDs.hpp>
6 :
7 : BEGIN_AH_NAMESPACE
8 :
9 : /**
10 : * @brief An enumeration type to set an LED display to either bar or dot mode.
11 : */
12 : enum class DotBarMode : bool {
13 : Bar = false, ///< Turn on a range of LEDs up to the active LED.
14 : Dot = true, ///< Turn on only the active LED
15 : };
16 :
17 : /**
18 : * @brief A class for LED bars.
19 : *
20 : * @tparam N
21 : * The number of LEDs in the bar.
22 : *
23 : * @ingroup AH_HardwareUtils
24 : */
25 : template <uint16_t N>
26 : class DotBarDisplayLEDs : public LEDs<N> {
27 : public:
28 : /// Constructor from list of pins.
29 2 : DotBarDisplayLEDs(const PinList<N> &ledPins) : LEDs<N>{ledPins} {}
30 :
31 : /**
32 : * @brief Display the given number of LEDs on the LED bar.
33 : *
34 : * @param value
35 : * The number of the LED to activate.
36 : */
37 7 : void display(uint16_t value) const {
38 7 : if (value == 0)
39 2 : this->clear();
40 5 : else if (mode == DotBarMode::Bar)
41 5 : this->displayRange(0, value);
42 : else
43 0 : this->displayDot(value - 1);
44 7 : }
45 :
46 : /**
47 : * @brief Display the given fraction of the LED bar.
48 : *
49 : * @param value
50 : * The fraction of the LED bar to display.
51 : */
52 3 : void display(float value) const { display(uint16_t(value * (N + 1))); }
53 :
54 : /// Get the dot/bar mode.
55 : DotBarMode getMode() const { return mode; }
56 :
57 : /**
58 : * @brief Set the mode to either dot or bar mode.
59 : *
60 : * @param mode
61 : * The mode.
62 : */
63 : void setMode(DotBarMode mode) { this->mode = mode; }
64 :
65 : /// Set the mode to dot mode.
66 : void dotMode() { setMode(DotBarMode::Dot); }
67 :
68 : /// Set the mode to bar mode.
69 : void barMode() { setMode(DotBarMode::Bar); }
70 :
71 : /// Toggle the dot/bar mode.
72 : void toggleMode() { getMode() == DotBarMode::Bar ? dotMode() : barMode(); }
73 :
74 : private:
75 : DotBarMode mode = DotBarMode::Bar;
76 : };
77 :
78 : END_AH_NAMESPACE
|