/
fadeRgbLEDs_Espruino.js
103 lines (90 loc) · 2.86 KB
/
fadeRgbLEDs_Espruino.js
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
/*
The following code is a quick test on how to implement fading colors with an RGB LED
Setup:
2 x 220 Ohm resistors ( Green & Blue channels)
1 x 1k resistor ( Red channel )
Espruino B9 --/\/\/-- R
Espruino B4 --/\/\/-- G
Espruino B8 --/\/\/-- B
Espruino B9 --------- Common cathode Gnd
/!\ R: verify that your RGB LED is either common anode or common cathode
the first 'd be connected to +V, while the later 'd be to Gnd
StephaneAG - 2016
*/
var redPin = B9;
var bluePin = B8;
var greenPin = B4;
var rgbLED = {red: 0, green: 0, blue: 0 };
var colorTweenTimeout = undefined;
// desired logic
function blueToViolet(){
tweenColor(redPin, 'red', 0, 1, 0, violetToRed);
}
function violetToRed(){
tweenColor(bluePin, 'blue', 1, 0, 1, redToYellow);
}
function redToYellow(){
tweenColor(greenPin, 'green', 0, 1, 0, yellowToGreen);
}
function yellowToGreen(){
tweenColor(redPin, 'red', 1, 0, 1, greenToAqua);
}
function greenToAqua(){
tweenColor(bluePin, 'blue', 0, 1, 0, aquaToBlue);
}
function aquaToBlue(){
tweenColor(greenPin, 'green', 1, 0, 1, blueToViolet);
}
// helper(s)
function tweenColor(pin, channel, from, to, dir, callback){
if( from < to && dir === 0 ){
from+= 0.05;
rgbLED[channel] = Number(from.toFixed(3));
analogWrite(pin, rgbLED[channel]); // Espruino call
colorTweenTimeout = setTimeout(function(){ tweenColor(pin, channel, from, to, dir, callback) }, 100);
}
else if( from > to && dir === 1 ){
from-= 0.05;
rgbLED[channel] = Number(from.toFixed(3));
analogWrite(pin, rgbLED[channel]); // Espruino call
colorTweenTimeout = setTimeout(function(){ tweenColor(pin, channel, from, to, dir, callback) }, 100);
}
else { callback(); }
}
function stopColorFade(){
clearTimeout( colorTweenTimeout );
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
// specific fcnality "smoothed" color feedback ;p ( circular stepping between states )
function deviceConnected_colorFeedback(){
tweenColor(bluePin, 'blue', 0, 1, 0, function(){
console.log('Device connected color feedback done !');
});
}
function deviceCharging_colorFeedback(){
tweenColor(greenPin, 'green', 1, 0, 1, function(){
tweenColor(redPin, 'red', 0, 1, 0, function(){
tweenColor(bluePin, 'blue', 1, 0, 1, function(){
console.log('Device charging color feedback done !');
})
})
});
}
function deviceCharged_colorFeedback(){
tweenColor(greenPin, 'green', 0, 1, 0, function(){
tweenColor(redPin, 'red', 1, 0, 1, function(){
console.log('Device charged color feedback done !');
})
});
}
function deviceDisconnected_colorFeedback(){
tweenColor(greenPin, 'green', 1, 0, 1, function(){
console.log('Device disconnected color feedback done !');
});
}
// run looping color fade
blueToViolet();
// to cancel ( ex: on disconnecting device )
clearTimeout( colorTweenTimeout );