/
ECandle.cpp
401 lines (360 loc) · 13.2 KB
/
ECandle.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "ECandle.h"
//==================================== class ECandle ======================
oid_t ECandle::init(port_t ledPort, uint16_t fadeinTimeout, uint16_t fadeoutTimeout,
uint8_t maxLevel, uint8_t minLevel)
{
//сохраняем правильные состояни
// this->isOn=false;
this->port=ledPort;
//инициируем порт в выключенном режиме
pinMode(this->port,OUTPUT);
this->setLevel(0);
this->currentMaxLevel = maxLevel;
this->currentMinLevel = minLevel;
this->fadeinTimeout = fadeinTimeout * 1000;
this->fadeoutTimeout = fadeoutTimeout * 1000;
fadeinTimer.init( this->fadeinTimeout );
fadeoutTimer.init( this->fadeoutTimeout );
return getID();
};
void ECandle::setLevel( const int value )
{
currentLevel = constrain( value, 0, 255 );
// int i = this->currentLevel;
analogWrite( this->port, this->currentLevel );
//#ifdef DEBUG_ECANDLE
// Serial.print("ECandle::setCurrentLevel level=");
// Serial.println(i);
//#endif
};
void ECandle::setMinLevel(const int value)
{
currentMinLevel=constrain( value, CANDLEMINIMALLIGHTLEVEL+1,
CANDLEMAXIMALLIGHTLEVEL-1);
};
void ECandle::setMaxLevel(const int value)
{
currentMaxLevel=constrain(value,CANDLEMINIMALLIGHTLEVEL+1,
CANDLEMAXIMALLIGHTLEVEL-1);
};
void ECandle::getName(char* result)
{
sprintf( result,"ECandle: ID=%d port=%d reverseOn=%d", getID(),
this->port, this->reverseOn );
};
int ECandle::handleEvent( Event& tmpEvent )
{
if ( eventForMe( tmpEvent ) ) {
if ( this->isEnabled ) {
#ifdef DEBUG_ECANDLE
Serial.println("ECandle::handleEvent(): Its my event!");
#endif
switch ( this->currentState ) {
case csOff: //свет полностью включен
if ( tmpEvent.eventType == evTurnOn ) {
setState(csFadeIn);
};
return getID();
break;
case csFadeIn:
if ( tmpEvent.eventType == evTurnOff ) {
setState( csFadeOut );
};
if ( tmpEvent.eventType == evTurnOn ) {
setState( csOn );
};
return getID();
break;
case csOn:
if ( tmpEvent.eventType == evTurnOff ) {
setState( csFadeOut );
};
return getID();
break;
case csFadeOut:
if ( tmpEvent.eventType == evTurnOn ) {
setState( csFadeIn );
};
if ( tmpEvent.eventType == evTurnOff ) {
setState( csOff );
};
return getID();
break;
// case csFlickering:
// ???????????????????
default:
break;
}
}
return EOutputDevice::handleEvent( tmpEvent );
}
return 0;
};
void ECandle::setState(const CandleState newState)
//переключение в новое состояние
{
#ifdef DEBUG_ECANDLE
Serial.print("ECandle::setState() ID=");
int tmp = getID();
Serial.print( tmp );
Serial.print(" new State=");
Serial.println(newState);
#endif
switch ( newState ) {
case csOn:
this->currentState = csOn;
on();
break;
case csOff:
this->currentState = csOff;
off();
break;
case csFadeIn: //?????
this->currentState=csFadeIn;
fadeIn();
break;
case csFadeOut: //?????
this->currentState = csFadeOut;
fadeOut();
break;
case csFlickering: //??????
this->currentState = csFlickering;
startFlickering();
break;
} //switch newState
};
void ECandle::idle()
//процедура очередного такта
//пробег по состояниям, для затухания, разгорания и свечки
//нужно расчитать новое значение яркости
// !!!!!!!!!!сделать обработку isEnabled
{
int newLevel;
switch ( this->currentState ) {
case csFadeIn:
if ( fadeinTimer.expired() ) {
setState( csOn ); //таймер истек, переходим в полное включение
} else {
//расчитаем уровень света и зададим его!
// newLevel=calcLightLevel(timeOfLastEvent,FadeInDelay,1,255);
newLevel=calcLightLevel( fadeinTimer.getStartTime(),
this->fadeinTimeout, 1, 255 );
#ifdef DEBUG_ECANDLE
Serial.print("ECandle::idle() newLevel=");
Serial.println(newLevel);
#endif
this->setLevel(newLevel);
};
break;
case csFadeOut:
if ( fadeoutTimer.expired() ) {
setState( csOff ); //таймер истек, переходим в полное включение
} else {
//расчитаем уровень света и зададим его!
// newLevel=calcLightLevel(timeOfLastEvent,FadeInDelay,1,255);
newLevel=calcLightLevel( fadeoutTimer.getStartTime(),
this->fadeoutTimeout, 255, 1 );
#ifdef DEBUG_ECANDLE
Serial.print("ECandle::idle() newLevel=");
Serial.println( newLevel );
#endif
this->setLevel( newLevel );
};
break;
default:
break;
}
}
int ECandle::calcLightLevel(long startTime,long timeOut, int minLevel, int maxLevel)
/*
функция расчитывает и возвращает значение уровня света в данный момент времени
исходя из запомненного времени последнего события timeOfLastEvent
*/
{
#ifdef DEBUG_ECANDLE1
Serial.print("calcLightLevel: startTime=");
Serial.print(startTime);
Serial.print(", timeOut=");
Serial.print(timeOut);
Serial.print(", minLevel");
Serial.print(minLevel);
Serial.print(", maxLevel=");
Serial.print(maxLevel);
#endif
int tmp, res;
int scale = 4;
if ( minLevel < maxLevel ) { scale = -1 * scale;};
tmp=map( millis(), startTime, startTime + timeOut, minLevel, maxLevel );
if (minLevel<maxLevel) {
tmp = constrain( tmp, minLevel, maxLevel );
} else {
tmp = constrain( tmp, maxLevel, minLevel );
}
//пересчитаем по плавающей логарифмической шкале
//res = fscale(minLevel,maxLevel,minLevel,maxLevel,tmp,scale);
// res=tmp;
res = fscale( startTime, startTime + timeOut, minLevel, maxLevel,
millis(), scale );
// Serial.println(res);
#ifdef DEBUG_ECANDLE1
Serial.print(", res=");
Serial.println( res );
#endif
return res;
};
void ECandle::on()
{
setLevel( CANDLEMAXIMALLIGHTLEVEL );
#ifdef DEBUG_ECANDLE
Serial.print("ECandle::on() ID=");
int tmp = getID();
Serial.println( tmp );
#endif
};
void ECandle::fadeIn()
{
unsigned long timeOfLastEvent;
unsigned long now = millis();
this->fadeinTimer.start();
if ( this->currentLevel > 0 ) { //!!!???надо бы отладить получше, т.к. не согласуются уровни
//уже горел свет, значит переход не из состояния Off, сдвинем дату последнего события
// в прошлое, чтобы скомпенсировать долю времени, обсуловленную тем, что свет уже горит.
//посчитаем условное время предыдущего события
//правило: если текущее время совпадает с временем последнего события, значит затухание только началось
// и яркость должна быть максимальна, а последнее событие (время начала увеличения уровня)
// нужно сдвинуть назад на FadeInDelay, есои впемя совпадает
//timeOfLastEvent=map(now,timeOfLastEvent,timeOfLastEvent+FadeOutDelay,now-FadeInDelay,now);
timeOfLastEvent=map(now,
this->fadeoutTimer.getStartTime(),this->fadeoutTimer.getStartTime()+this->fadeoutTimeout,
now-this->fadeinTimeout,now);
//на всякий случай ограничим время последнего события разумными рамками
//timeOfLastEvent=constrain(timeOfLastEvent,now-FadeInDelay,now);
timeOfLastEvent=constrain(timeOfLastEvent,now-this->fadeinTimeout,now);
this->fadeinTimer.setStartTime(timeOfLastEvent);
};
#ifdef DEBUG_ECANDLE
Serial.print("ECandle::fadeIn() ID=");
int tmp = getID();
Serial.println(tmp);
#endif
};
void ECandle::fadeOut()
{
unsigned long timeOfLastEvent;
unsigned long now=millis();
this->fadeoutTimer.start();
if (this->currentLevel<this->currentMaxLevel) {
//уже горел свет, значит переход не из состояния Off, сдвинем дату последнего события
// в прошлое, чтобы скомпенсировать долю времени, обсуловленную тем, что свет уже горит.
//посчитаем условное время предыдущего события
//правило: если текущее время совпадает с временем последнего события, значит затухание только началось
// и яркость должна быть максимальна, а последнее событие (время начала увеличения уровня)
// нужно сдвинуть назад на FadeInDelay, есои впемя совпадает
//timeOfLastEvent=map(now,timeOfLastEvent,timeOfLastEvent+FadeOutDelay,now-FadeInDelay,now);
timeOfLastEvent=map(now,
this->fadeinTimer.getStartTime(),this->fadeinTimer.getStartTime()+this->fadeinTimeout,
now-this->fadeoutTimeout,now);
//на всякий случай ограничим время последнего события разумными рамками
//timeOfLastEvent=constrain(timeOfLastEvent,now-FadeInDelay,now);
timeOfLastEvent=constrain(timeOfLastEvent,now-this->fadeoutTimeout,now);
this->fadeoutTimer.setStartTime(timeOfLastEvent);
};
#ifdef DEBUG_ECANDLE
Serial.print("ECandle::fadeOut() ID=");
int tmp = getID();
Serial.println(tmp);
#endif
};
void ECandle::off()
{
#ifdef DEBUG_ECANDLE
Serial.print("ECandle::fadeOff() ID=");
int tmp = getID();
Serial.println(tmp);
#endif
setLevel(0);
};
void ECandle::startFlickering()
{
#ifdef DEBUG_ECANDLE
Serial.print("ECandle::fadeOff() ID=");
int tmp = getID();
Serial.println( tmp );
#endif
this->setLevel(0);
};
float ECandle::fscale( float originalMin, float originalMax, float newBegin, float
newEnd, float inputValue, float curve)
/* Floating Point Autoscale Function V0.1
Paul Badger 2007
Modified from code by Greg Shakar
This function will scale one set of floating point numbers (range) to another set of floating point numbers (range)
It has a "curve" parameter so that it can be made to favor either the end of the output. (Logarithmic mapping)
It takes 6 parameters
originalMin - the minimum value of the original range - this MUST be less than origninalMax
originalMax - the maximum value of the original range - this MUST be greater than orginalMin
newBegin - the end of the new range which maps to orginalMin - it can be smaller, or larger, than newEnd, to facilitate inverting the ranges
newEnd - the end of the new range which maps to originalMax - it can be larger, or smaller, than newBegin, to facilitate inverting the ranges
inputValue - the variable for input that will mapped to the given ranges, this variable is constrained to originaMin <= inputValue <= originalMax
curve - curve is the curve which can be made to favor either end of the output scale in the mapping. Parameters are from -10 to 10 with 0 being
a linear mapping (which basically takes curve out of the equation)
*/
{
float OriginalRange = 0;
float NewRange = 0;
float zeroRefCurVal = 0;
float normalizedCurVal = 0;
float rangedValue = 0;
boolean invFlag = 0;
// condition curve parameter
// limit range
if (curve > 10) curve = 10;
if (curve < -10) curve = -10;
curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output
curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function
/*
Serial.println(curve * 100, DEC); // multply by 100 to preserve resolution
Serial.println();
*/
// Check for out of range inputValues
if (inputValue < originalMin) {
inputValue = originalMin;
}
if (inputValue > originalMax) {
inputValue = originalMax;
}
// Zero Refference the values
OriginalRange = originalMax - originalMin;
if (newEnd > newBegin){
NewRange = newEnd - newBegin;
}
else
{
NewRange = newBegin - newEnd;
invFlag = 1;
}
zeroRefCurVal = inputValue - originalMin;
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float
#ifdef DEBUG_ECANDLE
/*
Serial.print(OriginalRange, DEC);
Serial.print(" ");
Serial.print(NewRange, DEC);
Serial.print(" ");
Serial.println(zeroRefCurVal, DEC);
Serial.println();
*/
#endif
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine
if (originalMin > originalMax ) {
return 0;
}
if (invFlag == 0){
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;
}
else // invert the ranges
{
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange);
}
return rangedValue;
};