-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathml2output.cpp
298 lines (256 loc) · 4.83 KB
/
ml2output.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "ml2classes.h"
extern bool externalEventsEnabled;
extern EventManager externalEM;
extern OutputList outputList;
ML2Output::ML2Output(const String &id)
: m_pin(0)
, m_timeout(0)
, m_dim_t0(0)
, m_dim_t1(0)
, m_dim_v0(0)
, m_dim_v1(0)
, m_pwm(false)
, m_noreport(false)
, m_on(false)
, m_invert(false)
, m_saveState(OutputStateSave::None)
, m_value(0)
{
id.toCharArray(ID, ID_SIZE);
setupPin();
}
void ML2Output::setPin(byte pin)
{
m_pin = pin;
setupPin();
}
void ML2Output::setPWM(bool on)
{
m_pwm = on;
setupPin();
}
void ML2Output::setNoreport(bool no)
{
m_noreport = no;
}
void ML2Output::setValue(byte value, uint32_t timeout)
{
if (timeout)
{
m_dim_v0 = m_value;
m_dim_v1 = value;
m_dim_t0 = millis();
m_dim_t1 = m_dim_t0 + timeout;
check();
}
else
{
m_value = value;
m_dim_t1 = 0;
updatePin(this->timeout());
}
}
void ML2Output::setOn(uint32_t timeout)
{
m_on = true;
updatePin(timeout);
}
void ML2Output::setOff(uint32_t timeout)
{
m_on = false;
updatePin(timeout);
}
bool ML2Output::toggle(uint32_t timeout)
{
on() ? setOff(timeout) : setOn(timeout);
}
void ML2Output::incValue(int val, uint32_t timeout)
{
val += (int)m_value;
if (val < 0)
val = 0;
else if (val > PWM_HIGH)
val = PWM_HIGH;
setValue(val, timeout);
}
bool ML2Output::action(OutputAction::Action action, int param, uint32_t timeout)
{
switch (action) {
case OutputAction::NoAction:
updatePin();
break;
case OutputAction::On:
setOn(timeout);
break;
case OutputAction::Off:
setOff(timeout);
break;
case OutputAction::Toggle:
toggle(timeout);
break;
case OutputAction::Value:
setValue(param, timeout);
break;
case OutputAction::IncValue:
incValue(param, timeout);
break;
}
return value();
}
void ML2Output::check(uint32_t millisec)
{
if (!millisec)
millisec = millis();
byte nv;
if (m_dim_t1)
{
if (millisec >= m_dim_t1)
{
nv = m_dim_v1;
m_dim_t1 = 0;
emitState();
}
else
{
int dV = m_dim_v1 - m_dim_v0;
int32_t dT = (m_dim_t1 - m_dim_t0);
int32_t t = millisec - m_dim_t0;
double v = t * dV / dT + m_dim_v0;
if (v < 0)
nv = 0;
else if (v > PWM_HIGH)
nv = PWM_HIGH;
else
nv = (byte)v;
}
if (nv != m_value)
{
m_value = nv;
updatePin(timeout(), false); }
}
if (m_timeout)
{
if (millisec > m_timeout)
toggle();
}
}
uint32_t ML2Output::timeout()
{
if (!m_timeout)
return 0;
return max(0, m_timeout - millis());
}
OutputStateSave::Save ML2Output::saveState()
{
return m_saveState;
}
void ML2Output::setSaveState(OutputStateSave::Save saveState)
{
m_saveState = saveState;
}
bool ML2Output::invert()
{
return m_invert;
}
void ML2Output::setInvert(bool inv)
{
m_invert = inv;
}
void ML2Output::setupPin()
{
if (m_pin)
{
pinMode(m_pin, OUTPUT);
}
updatePin();
}
void ML2Output::updatePin(uint32_t timeout, bool doEmit)
{
if (timeout)
m_timeout = millis() + timeout;
else
m_timeout = 0;
if (m_pin)
{
if (m_pwm)
{
byte onVal = m_invert ? PWM_HIGH - m_value : m_value;
byte offVal = m_invert ? PWM_HIGH : 0;
analogWrite(m_pin, m_on ? onVal : offVal);
}
else
{
int onState = m_invert ? LOW : HIGH;
int offState = m_invert ? HIGH : LOW;
digitalWrite(m_pin, m_on ? onState : offState);
}
}
if (doEmit)
emitState();
}
void ML2Output::emitState()
{
if (externalEventsEnabled && !this->m_noreport)
externalEM.queueEvent(0, new EventParam(this));
}
OutputList::OutputList() : SimpleList<ML2Output * >()
{
}
bool OutputList::addOutput(ML2Output *output)
{
if (!output || hasOutput(output) || find(output->ID))
return false;
this->push_back(output);
return true;
}
bool OutputList::removeOutput(ML2Output *output)
{
if (output)
{
for (OutputList::iterator itr = begin(); itr != end(); ++itr)
{
if ((*itr) == output)
{
erase(itr);
delete (*itr);
return true;
}
}
}
return false;
}
bool OutputList::hasOutput(ML2Output *output)
{
if (output)
{
for (OutputList::iterator itr = begin(); itr != end(); ++itr)
{
if ((*itr) == output)
return true;
}
}
return false;
}
ML2Output *OutputList::find(const char* id)
{
if (strlen(id))
{
for (OutputList::iterator itr = begin(); itr != end(); ++itr)
{
if (strcmp((*itr)->ID, id) == 0)
return (*itr);
}
}
return 0;
}
void OutputList::check()
{
for (OutputList::iterator itr = begin(); itr != end(); ++itr)
(*itr)->check();
}
void OutputList::clearOutputs()
{
for (OutputList::iterator itr = this->begin(); itr != this->end(); ++itr)
delete (*itr);
this->clear();
}