PicoLC Arduino Library
Arduino library for PicoLC hardware
Loading...
Searching...
No Matches
PicoLCOutput.h
1#pragma once
2
3#include <Arduino.h>
4
5#include "PicoLCTypes.h"
6
7/**
8 * @brief Represents one configurable PicoLC hardware output.
9 *
10 * PicoLCOutput exposes logical output behavior rather than instantaneous
11 * electrical state. The selected OutputMode defines the electrical drive
12 * configuration, PatternMode defines timed behavior, and duty() defines the
13 * linear PWM duty applied while the output pattern is active.
14 *
15 * Instances are created and owned by PicoLC and are accessed through members
16 * such as PicoLC::Y0 through PicoLC::Y3.
17 */
18class PicoLCOutput {
19 friend class PicoLC;
20
21public:
22 /** Maximum user-facing PWM duty value. */
23 static constexpr uint16_t DUTY_MAX = 1000;
24
25 /** Default PWM frequency in hertz. */
26 static constexpr uint32_t FREQUENCY_DEFAULT = 1000;
27
28private:
29 /**
30 * @brief Constructs an output.
31 *
32 * PicoLC creates and initializes output instances during begin().
33 *
34 */
35 explicit PicoLCOutput();
36
37 PicoLCOutput(const PicoLCOutput&) = delete;
38 PicoLCOutput& operator=(const PicoLCOutput&) = delete;
39
40 uint32_t _pulseStart = 0;
41 uint32_t _pulseLength = 0;
42 uint32_t _frequency = FREQUENCY_DEFAULT;
43 uint32_t _blinkPeriod = 0;
44 uint32_t _blinkStart = 0;
45 uint32_t _blinkTarget = 0;
46 uint32_t _blinkOnTime = 0;
47 uint16_t _duty = DUTY_MAX;
48 uint8_t _lastOut = 0;
49 bool _state = false;
50 OutputMode _outputMode = OutputMode::LowSide;
51 PatternMode _patternMode = PatternMode::Steady;
52
53 /**
54 * @brief Advances pulse, blink, and PWM output behavior for one scan.
55 *
56 * This method is called internally by PicoLC::update() using the shared
57 * PicoLC scan timestamp.
58 *
59 * @param currentMillis Current PicoLC scan time in milliseconds.
60 *
61 * @return Current logical output state encoded as a two-bit value. The least
62 * significant bit represents the logical output state, and the next
63 * bit represents the electrical drive state.
64 */
65 uint8_t update(uint32_t currentMillis);
66
67public:
68 /**
69 * @brief Returns the current logical output state.
70 *
71 * The returned value never represents the instantaneous electrical output
72 * state. In Pulse mode, read() returns false after a pulse is triggered,
73 * even while the timed pulse remains active.
74 *
75 * @return Current logical output state.
76 */
77 bool read() const;
78
79 /**
80 * @brief Applies a logical command to the output.
81 *
82 * In Steady and Blink modes, the supplied value becomes the logical output
83 * state. In Blink mode, a false-to-true transition starts a new cycle in
84 * the on phase, and false stops the pattern immediately.
85 *
86 * In Pulse mode, true starts or restarts the pulse timer and then returns
87 * the logical state to false. Writing false stops an active pulse
88 * immediately.
89 *
90 * Physical output drive remains disabled until PicoLC::enableOutputs() is
91 * called.
92 *
93 * @param state Logical command to apply.
94 * @return The supplied logical command, allowing the call to be chained
95 * in an expression.
96 */
97 bool write(bool state);
98
99 /**
100 * @brief Applies a logical command to the output and sets the electrical drive mode.
101 *
102 * In Steady and Blink modes, the supplied state becomes the logical output
103 * state. In Blink mode, a false-to-true transition starts a new cycle in
104 * the on phase, and false stops the pattern immediately.
105 *
106 * In Pulse mode, true starts or restarts the pulse timer and then returns
107 * the logical state to false. Writing false stops an active pulse
108 * immediately.
109 *
110 * Physical output drive remains disabled until PicoLC::enableOutputs() is
111 * called.
112 *
113 * @param state Logical command to apply.
114 * @param mode Desired electrical output configuration.
115 * @return The supplied logical command, allowing the call to be chained
116 * in an expression.
117 */
118 bool write(bool state, OutputMode mode);
119
120 /**
121 * @brief Sets the electrical drive mode.
122 *
123 * @param mode Desired electrical output configuration.
124 */
125 void outputMode(OutputMode mode);
126
127 /**
128 * @brief Returns the configured electrical drive mode.
129 *
130 * @return Current OutputMode.
131 */
132 OutputMode outputMode() const;
133
134 /**
135 * @brief Sets the timed behavior applied to the output.
136 *
137 * Pulse and blink configuration values are retained when changing modes.
138 *
139 * @param mode Desired PatternMode.
140 */
141 void patternMode(PatternMode mode);
142
143 /**
144 * @brief Returns the configured timed output behavior.
145 *
146 * @return Current PatternMode.
147 */
148 PatternMode patternMode() const;
149
150 /**
151 * @brief Sets the Pulse-mode duration.
152 *
153 * @param ms Pulse duration in milliseconds.
154 */
155 void pulseLength(uint32_t ms);
156
157 /**
158 * @brief Returns the configured Pulse-mode duration.
159 *
160 * @return Pulse duration in milliseconds.
161 */
162 uint32_t pulseLength() const;
163
164 /**
165 * @brief Sets the complete Blink-mode cycle period.
166 *
167 * Changing the period does not restart the current cycle. The new timing
168 * is applied during the next PicoLC::update().
169 *
170 * @param ms Blink period in milliseconds.
171 */
172 void blinkPeriod(uint32_t ms);
173
174 /**
175 * @brief Returns the complete Blink-mode cycle period.
176 *
177 * @return Blink period in milliseconds.
178 */
179 uint32_t blinkPeriod() const;
180
181 /**
182 * @brief Sets the active time within each Blink-mode cycle.
183 *
184 * @param ms Blink on-time in milliseconds.
185 */
186 void blinkOnTime(uint32_t ms);
187
188 /**
189 * @brief Returns the active time within each Blink-mode cycle.
190 *
191 * @return Blink on-time in milliseconds.
192 */
193 uint32_t blinkOnTime() const;
194
195 /**
196 * @brief Sets the requested PWM frequency.
197 *
198 * Outputs sharing an RP2 PWM slice also share a frequency. The returned
199 * value is the frequency actually applied after hardware constraints and
200 * shared-slice behavior are taken into account.
201 *
202 * @param hertz Requested frequency in hertz.
203 * @return Actual configured frequency in hertz.
204 */
205 uint32_t frequency(uint32_t hertz);
206
207 /**
208 * @brief Returns the configured PWM frequency.
209 *
210 * @return PWM frequency in hertz.
211 */
212 uint32_t frequency() const;
213
214 /**
215 * @brief Sets the linear PWM duty.
216 *
217 * The public duty range is 0 through DUTY_MAX. A value of 0 produces no
218 * active drive during an enabled pattern, and DUTY_MAX produces full duty.
219 *
220 * @param duty Requested duty value.
221 * @return Applied duty value after range handling.
222 */
223 uint16_t duty(uint16_t duty);
224
225 /**
226 * @brief Returns the configured linear PWM duty.
227 *
228 * @return Duty value from 0 through DUTY_MAX.
229 */
230 uint16_t duty() const;
231};
static constexpr uint32_t FREQUENCY_DEFAULT
Default PWM frequency in hertz.
uint32_t frequency() const
Returns the configured PWM frequency.
uint32_t pulseLength() const
Returns the configured Pulse-mode duration.
bool read() const
Returns the current logical output state.
uint16_t duty(uint16_t duty)
Sets the linear PWM duty.
uint16_t duty() const
Returns the configured linear PWM duty.
uint32_t blinkPeriod() const
Returns the complete Blink-mode cycle period.
uint32_t blinkOnTime() const
Returns the active time within each Blink-mode cycle.
static constexpr uint16_t DUTY_MAX
Maximum user-facing PWM duty value.
bool write(bool state)
Applies a logical command to the output.
PatternMode patternMode() const
Returns the configured timed output behavior.
OutputMode outputMode() const
Returns the configured electrical drive mode.