PicoLC Arduino Library
Arduino library for PicoLC hardware
Toggle main menu visibility
Loading...
Searching...
No Matches
PicoLCTimer.cpp
1
#include <Arduino.h>
2
#include "PicoLCTimer.h"
3
#include "PicoLCClass.h"
4
5
PicoLCTimer::PicoLCTimer
(
PicoLC
& plc, TimerMode mode, uint32_t preset) :
6
_plc(plc),
7
_mode(mode),
8
_preset(preset),
9
_started(0) {}
10
11
void
PicoLCTimer::presetMillis
(uint32_t ms) {
12
_preset = ms;
13
}
14
15
uint32_t
PicoLCTimer::presetMillis
()
const
{
16
return
_preset;
17
}
18
19
bool
PicoLCTimer::read
() {
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
49
void
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
91
bool
PicoLCTimer::update
(
bool
input) {
92
write
(input);
93
return
read
();
94
}
95
96
bool
PicoLCTimer::running
()
const
{
97
return
_running;
98
}
99
100
uint32_t
PicoLCTimer::elapsed
()
const
{
101
if
( _started == 0 ) {
102
return
0;
103
}
104
return
_plc.currentMillis() - _started;
105
}
106
107
uint32_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
118
void
PicoLCTimer::reset
() {
119
_started = 0;
120
_running =
false
;
121
}
PicoLC
Main interface to PicoLC hardware and shared runtime services.
Definition
PicoLCClass.h:25
PicoLCTimer::PicoLCTimer
PicoLCTimer(PicoLC &plc, TimerMode mode, uint32_t preset)
Constructs a timer.
Definition
PicoLCTimer.cpp:5
PicoLCTimer::read
bool read()
Returns the timer output state.
Definition
PicoLCTimer.cpp:19
PicoLCTimer::presetMillis
uint32_t presetMillis() const
Returns the configured timer preset.
Definition
PicoLCTimer.cpp:15
PicoLCTimer::update
bool update(bool input)
Applies a timer input and returns the resulting output state.
Definition
PicoLCTimer.cpp:91
PicoLCTimer::reset
void reset()
Stops and clears the current timer operation.
Definition
PicoLCTimer.cpp:118
PicoLCTimer::running
bool running() const
Reports whether the timer is currently accumulating time.
Definition
PicoLCTimer.cpp:96
PicoLCTimer::remaining
uint32_t remaining() const
Returns the time remaining before the preset is reached.
Definition
PicoLCTimer.cpp:107
PicoLCTimer::write
void write(bool input)
Applies the current logical input to the timer.
Definition
PicoLCTimer.cpp:49
PicoLCTimer::elapsed
uint32_t elapsed() const
Returns the accumulated timer value.
Definition
PicoLCTimer.cpp:100
src
timers
PicoLCTimer.cpp
Generated by
1.17.0