PicoLC Arduino Library
Arduino library for PicoLC hardware
Loading...
Searching...
No Matches
PicoLCLED.h
1#pragma once
2
3#include <Arduino.h>
4
5#include "PicoLCTypes.h"
6
7/**
8 * @brief Represents one configurable PicoLC status LED.
9 *
10 * PicoLCLED exposes logical LED behavior rather than instantaneous physical
11 * output state. PatternMode controls steady, pulse, and blink behavior, while
12 * brightness() controls perceived LED brightness using a user-facing
13 * percentage scale.
14 *
15 * Instances are created and owned by PicoLC and are accessed through members
16 * such as PicoLC::StatusLED and PicoLC::FaultLED.
17 */
18class PicoLCLED {
19 friend class PicoLC;
20
21public:
22 /** Maximum user-facing brightness percentage. */
23 static constexpr uint16_t BRIGHTNESS_MAX = 100;
24
25private:
26 /**
27 * @brief Constructs an LED instance.
28 *
29 * PicoLC creates and initializes LED instances during begin().
30 *
31 */
32 explicit PicoLCLED();
33
34 /** Maximum internal PWM duty representation. */
35 static constexpr uint16_t PWM_MAX = 1000;
36
37 /**
38 * @brief Advances pulse, blink, and PWM behavior for one scan.
39 *
40 * This method is called internally by PicoLC::update() using the shared
41 * PicoLC scan timestamp.
42 *
43 * @param currentMillis Current PicoLC scan time in milliseconds.
44 *
45 * @return Current logical LED state.
46 */
47 uint16_t update(uint32_t currentMillis);
48
49 uint32_t _pulseStart = 0;
50 uint32_t _pulseLength = 0;
51 uint32_t _blinkPeriod = 0;
52 uint32_t _blinkStart = 0;
53 uint32_t _blinkTarget = 0;
54 uint32_t _blinkOnTime = 0;
55 uint16_t _brightness = BRIGHTNESS_MAX;
56 PatternMode _patternMode = PatternMode::Steady;
57 uint8_t _channel;
58 bool _state = false;
59
60 PicoLCLED(const PicoLCLED&) = delete;
61 PicoLCLED& operator=(const PicoLCLED&) = delete;
62
63public:
64 /**
65 * @brief Sets the timed behavior applied to the LED.
66 *
67 * Pulse and blink configuration values are retained when changing modes.
68 *
69 * @param mode Desired PatternMode.
70 */
71 void patternMode(PatternMode mode);
72
73 /**
74 * @brief Returns the configured timed LED behavior.
75 *
76 * @return Current PatternMode.
77 */
78 PatternMode patternMode() const;
79
80 /**
81 * @brief Returns the current logical LED state.
82 *
83 * The returned value never represents the instantaneous physical LED
84 * state. In Pulse mode, read() returns false after a pulse is triggered,
85 * even while the timed pulse remains active.
86 *
87 * @return Current logical LED state.
88 */
89 bool read() const;
90
91 /**
92 * @brief Applies a logical command to the LED.
93 *
94 * In Steady and Blink modes, the supplied value becomes the logical LED
95 * state. In Blink mode, a false-to-true transition starts a new cycle in
96 * the on phase, and false stops the pattern immediately.
97 *
98 * In Pulse mode, true starts or restarts the pulse timer and then returns
99 * the logical state to false. Writing false stops an active pulse
100 * immediately.
101 *
102 * @param state Logical command to apply.
103 * @return The supplied logical command, allowing the call to be chained
104 * in an expression.
105 */
106 bool write(bool state);
107
108 /**
109 * @brief Toggles the LED state or active pulse.
110 *
111 * In Steady and Blink modes, this is equivalent to writing the opposite
112 * logical state. In Pulse mode, an active pulse is stopped; otherwise a
113 * new pulse is started.
114 */
115 void toggle();
116
117 /**
118 * @brief Sets the Pulse-mode duration.
119 *
120 * @param ms Pulse duration in milliseconds.
121 */
122 void pulseLength(uint32_t ms);
123
124 /**
125 * @brief Returns the configured Pulse-mode duration.
126 *
127 * @return Pulse duration in milliseconds.
128 */
129 uint32_t pulseLength() const;
130
131 /**
132 * @brief Sets the complete Blink-mode cycle period.
133 *
134 * Changing the period does not restart the current cycle. The new timing
135 * is applied during the next PicoLC::update().
136 *
137 * @param ms Blink period in milliseconds.
138 */
139 void blinkPeriod(uint32_t ms);
140
141 /**
142 * @brief Returns the complete Blink-mode cycle period.
143 *
144 * @return Blink period in milliseconds.
145 */
146 uint32_t blinkPeriod() const;
147
148 /**
149 * @brief Sets the active time within each Blink-mode cycle.
150 *
151 * A default value of zero produces no visible output in Blink mode until
152 * the on-time is configured.
153 *
154 * @param ms Blink on-time in milliseconds.
155 */
156 void blinkOnTime(uint32_t ms);
157
158 /**
159 * @brief Returns the active time within each Blink-mode cycle.
160 *
161 * @return Blink on-time in milliseconds.
162 */
163 uint32_t blinkOnTime() const;
164
165 /**
166 * @brief Sets the perceived LED brightness.
167 *
168 * The public range is 0 through BRIGHTNESS_MAX percent. The value is
169 * converted internally to a nonlinear PWM duty so that user-facing
170 * brightness changes appear more visually uniform.
171 *
172 * Brightness is applied only during active steady, pulse, or blink phases.
173 * A brightness of zero does not change the logical LED state or pattern.
174 *
175 * @param brightness Brightness percentage from 0 through BRIGHTNESS_MAX.
176 */
177 void brightness(uint16_t brightness);
178
179 /**
180 * @brief Returns the configured perceived LED brightness.
181 *
182 * The internal PWM representation is converted back to the user-facing
183 * percentage scale.
184 *
185 * @return Brightness percentage from 0 through BRIGHTNESS_MAX.
186 */
187 uint16_t brightness() const;
188};
uint32_t blinkOnTime() const
Returns the active time within each Blink-mode cycle.
Definition PicoLCLED.cpp:83
uint32_t blinkPeriod() const
Returns the complete Blink-mode cycle period.
Definition PicoLCLED.cpp:81
uint32_t pulseLength() const
Returns the configured Pulse-mode duration.
Definition PicoLCLED.cpp:79
bool read() const
Returns the current logical LED state.
Definition PicoLCLED.cpp:55
void brightness(uint16_t brightness)
Sets the perceived LED brightness.
Definition PicoLCLED.cpp:85
uint16_t brightness() const
Returns the configured perceived LED brightness.
Definition PicoLCLED.cpp:90
bool write(bool state)
Applies a logical command to the LED.
Definition PicoLCLED.cpp:62
static constexpr uint16_t BRIGHTNESS_MAX
Maximum user-facing brightness percentage.
Definition PicoLCLED.h:23
PatternMode patternMode() const
Returns the configured timed LED behavior.
Definition PicoLCLED.cpp:11
void toggle()
Toggles the LED state or active pulse.
Definition PicoLCLED.cpp:70