-
Notifications
You must be signed in to change notification settings - Fork 0
/
EspPlcTimers2.h
131 lines (110 loc) · 2.23 KB
/
EspPlcTimers2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//V1.0 (c) 2023 Juan M. Uribe
#pragma once
#include "ListLib.h"
#define TIMERHANDLER xTaskCreatePinnedToCore(timerHandler, "timerHandler", 1024, NULL, 1, &timerHandlerTask, 1)
enum timerType {TON, TOFF};
struct timer {
timerType type; //timer type
bool in; //timer input
uint32_t pt; //timer preset time
uint32_t et; //timer elapsed time
bool q; //timer output
};
class genericTimer {
private:
struct timer t;
uint32_t lastTime;
public:
genericTimer(timerType type);
genericTimer(timerType type, uint32_t preset);
void updateTimer();
void preset(uint32_t time);
bool q();
uint32_t et();
void tin(bool timerIn);
bool tin();
void reset();
};
genericTimer::genericTimer(timerType type) {
t.type = type;
t.pt = 10000;
}
genericTimer::genericTimer(timerType type, uint32_t preset) {
t.type = type;
t.pt = preset;
}
void genericTimer::updateTimer()
{
switch (t.type)
{
case TON:
{
if (!t.in)
{
t.q = false;
t.et = t.pt;
}
t.q = (t.in && (t.et <= 0));
if (t.in && t.et > 0)
t.et -= millis() - lastTime;
lastTime = millis();
}
break;
case TOFF:
{
t.q = (!t.in && (t.et > 0));
if (!t.in && t.et > 0)
t.et -= millis() - lastTime;
if (t.in)
{
t.q = true;
t.et = t.pt;
}
lastTime = millis();
}
break;
}
}
void genericTimer::reset(void){
t.et = t.pt;
}
void genericTimer::preset(uint32_t time) {
t.pt = time;
}
bool genericTimer::q() {
return t.q;
}
uint32_t genericTimer::et() {
return t.et;
}
void genericTimer::tin(bool timerIn) {
t.in = timerIn;
}
bool genericTimer::tin() {
return t.in;
}
List<genericTimer *> timerList;
class espPlcTimer: public genericTimer {
private:
public:
espPlcTimer(timerType type);
espPlcTimer(timerType type, uint32_t preset);
};
espPlcTimer::espPlcTimer(timerType type): genericTimer(type) {
timerList.Add(this);
}
espPlcTimer::espPlcTimer(timerType type, uint32_t preset):
genericTimer(type, preset) {
timerList.Add(this);
}
// ESPPlcTimers updaters
TaskHandle_t timerHandlerTask;
void timerHandler(void *pvParameters)
{
(void)pvParameters;
for (;;)
{
for (int i=0; i<timerList.Count(); i++)
timerList[i]->updateTimer();
}
}