PicoLC Arduino Library
Arduino library for PicoLC hardware
Loading...
Searching...
No Matches
PicoLCTimer.cpp
1#include <Arduino.h>
2#include "PicoLCTimer.h"
3#include "PicoLCClass.h"
4
5PicoLCTimer::PicoLCTimer(PicoLC& plc, TimerMode mode, uint32_t preset) :
6 _plc(plc),
7 _mode(mode),
8 _preset(preset),
9 _started(0) {}
10
11void PicoLCTimer::presetMillis(uint32_t ms) {
12 _preset = ms;
13}
14
15uint32_t PicoLCTimer::presetMillis() const {
16 return _preset;
17}
18
20 uint32_t currentMillis = _plc.currentMillis();
21 switch( _mode) {
22 case TimerMode::TON:
23 if( _running ) {
24 return (currentMillis - _started) >= _preset;
25 } else {
26 return false;
27 }
28 break;
29 case TimerMode::TOF:
30 if( _running ) {
31 return (currentMillis - _started) < _preset;
32 } else {
33 return true;
34 }
35 break;
36 case TimerMode::TP:
37 if( _running ) {
38 if( currentMillis - _started >= _preset ) {
39 _running = false;
40 _started = 0;
41 }
42 }
43 return _running;
44 break;
45 }
46 return false;
47}
48
49void PicoLCTimer::write(bool input) {
50 uint32_t currentMillis = _plc.currentMillis();
51 switch( _mode) {
52 case TimerMode::TON:
53 if( input ) {
54 if( !_running ) {
55 _started = currentMillis;
56 _running = true;
57 }
58 } else {
59 _started = 0;
60 _running = false;
61 }
62 break;
63 case TimerMode::TOF:
64 if( input ) {
65 _started = 0;
66 _running = false;
67 } else {
68 if( _started == 0 ) {
69 _started = currentMillis;
70 _running = true;
71 }
72 }
73 break;
74 case TimerMode::TP:
75 if( _running ) {
76 if( currentMillis - _started >= _preset ) {
77 _running = false;
78 _started = 0;
79 }
80 } else {
81 if( input && !_lastInput) {
82 _running = true;
83 _started = currentMillis;
84 }
85 }
86 break;
87 }
88 _lastInput = input;
89}
90
91bool PicoLCTimer::update(bool input) {
92 write(input);
93 return read();
94}
95
97 return _running;
98}
99
100uint32_t PicoLCTimer::elapsed() const {
101 if( _started == 0 ) {
102 return 0;
103 }
104 return _plc.currentMillis() - _started;
105}
106
107uint32_t PicoLCTimer::remaining() const {
108 if( _started == 0 ) {
109 return _preset;
110 }
111 uint32_t elapsed = _plc.currentMillis() - _started;
112 if( elapsed >= _preset ) {
113 return 0;
114 }
115 return _preset - elapsed;
116}
117
119 _started = 0;
120 _running = false;
121}
Main interface to PicoLC hardware and shared runtime services.
Definition PicoLCClass.h:25
PicoLCTimer(PicoLC &plc, TimerMode mode, uint32_t preset)
Constructs a timer.
bool read()
Returns the timer output state.
uint32_t presetMillis() const
Returns the configured timer preset.
bool update(bool input)
Applies a timer input and returns the resulting output state.
void reset()
Stops and clears the current timer operation.
bool running() const
Reports whether the timer is currently accumulating time.
uint32_t remaining() const
Returns the time remaining before the preset is reached.
void write(bool input)
Applies the current logical input to the timer.
uint32_t elapsed() const
Returns the accumulated timer value.