forked from bitluni/bitluniHomeAutomation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFader.h
52 lines (47 loc) · 1009 Bytes
/
Fader.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
/*
Released under Creative Commons Attribution 4.0
by bitluni 2016
https://creativecommons.org/licenses/by/4.0/
Attribution means you can use it however you like as long you
mention that it's base on my stuff.
I'll be pleased if you'd do it by sharing http://youtube.com/bitlunislab
*/
template<class T>
class Fader
{
public:
bool active = false;
long startTime = 0;
long duration = 0;
T &from;
T &to;
Fader(T &fromState, T &toState)
:from(fromState)
,to(toState)
{
}
bool start(int duration)
{
if(active)
return false;
active = true;
startTime = millis();
this->duration = duration;
}
bool fade()
{
if(!active)
return false;
long t = millis() - startTime;
if(t >= duration || t < 0 || duration == 0)
{
from.setValues(to);
active = false;
return false;
}
float f1 = (float)t / duration;
float f0 = 1 - f1;
from.fade(to, long(f0 * 0x10000), long(f1 * 0x10000));
return true;
}
};