PicoLC Arduino Library
Arduino library for PicoLC hardware
Loading...
Searching...
No Matches
PicoLCOutput.cpp
1#include "PicoLC.h"
2
3PicoLCOutput::PicoLCOutput() :
4 _outputMode(OutputMode::HighSide),
5 _patternMode(PatternMode::Steady),
6 _pulseLength(0),
7 _blinkPeriod(0),
8 _blinkStart(0),
9 _blinkTarget(UINT16_MAX),
10 _blinkOnTime(UINT16_MAX),
11 _duty(UINT16_MAX),
12 _state(false) {
13 duty(_duty);
14}
15
16uint8_t PicoLCOutput::update(uint32_t currentMillis) {
17 uint8_t value = 0;
18
19 if( _patternMode == PatternMode::Pulse ) {
20 if( _state ) {
21 _pulseStart = currentMillis;
22 _state = false;
23 }
24 if( _pulseStart > 0 && currentMillis < _pulseStart + _pulseLength ) {
25 value = 1;
26 } else {
27 _pulseStart = 0;
28 }
29 }
30 if( _state ) {
31 if( _patternMode == PatternMode::Blink ) {
32 if( _blinkStart == 0 ) {
33 _blinkStart = currentMillis;
34 }
35 _blinkStart += ((currentMillis - _blinkStart) / _blinkPeriod) * _blinkPeriod;
36 if( currentMillis - _blinkStart < _blinkOnTime ) {
37 value = 1;
38 }
39 } else {
40 value = 1;
41 }
42 }
43 switch( _outputMode ) {
44 case OutputMode::HighSide:
45 value |= (1 << 1);
46 break;
47 case OutputMode::LowSide:
48 break;
49 case OutputMode::PushPull:
50 value = (value << 1) | 1;
51 break;
52 case OutputMode::Paired:
53 value = 0;
54 break;
55 }
56 _lastOut = value;
57 return value;
58}
59
60bool PicoLCOutput::read() const {
61 if( _patternMode == PatternMode::Pulse ) {
62 return _pulseStart > 0;
63 }
64 return _state;
65}
66
67bool PicoLCOutput::write(bool state) {
68 if( _patternMode == PatternMode::Pulse && _pulseLength > 0 && !state ) {
69 _pulseStart = 0;
70 }
71 _state = state;
72 return _state;
73}
74
75bool PicoLCOutput::write(bool state, OutputMode mode) {
76 outputMode(mode);
77 return write(state);
78}
79
80void PicoLCOutput::pulseLength(uint32_t ms) { _pulseLength = ms; }
81uint32_t PicoLCOutput::pulseLength() const { return _pulseLength; }
82
83void PicoLCOutput::blinkPeriod(uint32_t ms) {
84 _blinkPeriod = ms;
85 if( ms > 0 ) _blinkStart = millis();
86}
87uint32_t PicoLCOutput::blinkPeriod() const { return _blinkPeriod; }
88void PicoLCOutput::blinkOnTime(uint32_t ms) {
89 _blinkOnTime = ms;
90}
91uint32_t PicoLCOutput::blinkOnTime() const { return _blinkOnTime; }
92
93
94void PicoLCOutput::outputMode(OutputMode mode) { _outputMode = mode; }
95OutputMode PicoLCOutput::outputMode() const { return _outputMode; }
96
97void PicoLCOutput::patternMode(PatternMode mode) { _patternMode = mode; }
98PatternMode PicoLCOutput::patternMode() const { return _patternMode; }
99
100uint32_t PicoLCOutput::frequency(uint32_t hertz) { _frequency = hertz; return _frequency; }
101uint32_t PicoLCOutput::frequency() const { return _frequency; }
102uint16_t PicoLCOutput::duty(uint16_t duty) {
103 _duty = std::max<uint16_t>(duty, PicoLCOutput::DUTY_MAX);
104 return _duty;
105}
106uint16_t PicoLCOutput::duty() const { return _duty; }
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.