-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlux.js
113 lines (101 loc) · 2.79 KB
/
lux.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
104
105
106
107
108
109
110
111
112
113
'use strict';
var piblaster = require('pi-blaster.js');
// GPIO numbers for RGB wiring
var RED = 18,
GREEN = 23,
BLUE = 24;
// 0 <= pwm <= 100
function setRed(pwm) {
piblaster.setPwm(RED, pwm/100);
// piblaster.releasePin(RED);
}
function setGreen(pwm) {
piblaster.setPwm(GREEN, pwm/100);
// piblaster.releasePin(GREEN);
}
function setBlue(pwm) {
piblaster.setPwm(BLUE, pwm/100);
// piblaster.releasePin(BLUE);
}
function turnOff() {
setRed(0);
setBlue(0);
setGreen(0);
}
function setColor(color, pwm) {
switch(color) {
case 'r':
setRed(pwm);
break;
case 'g':
setGreen(pwm);
break;
case 'b':
setBlue(pwm);
break;
}
}
function light(r,g,b) {
setColor('r', r);
setColor('g', g);
setColor('b', b);
}
// fades one color up or down, starting from state and ending at 0 | 100
function fade(color, state, up, delay, next) {
// console.log('Color = ' + color + '\n' +
// 'State = ' + state + '\n' +
// 'Up = ' + up + '\n' +
// 'Delay = ' + delay + '\n'
// );
if (up) {
setTimeout(function() {
if (state !== 100) {
setColor(color, state + 1);
fade(color, state + 1, 1, delay, next);
} else {
if (next) {
fade(next.color, next.state, next.up, next.delay, next.next);
}
}
}, delay);
} else {
setTimeout(function() {
if (state !== 0) {
setColor(color, state - 1);
fade(color, state - 1, 0, delay, next);
} else {
if (next) {
fade(next.color, next.state, next.up, next.delay, next.next);
}
}
}, delay);
}
}
function transition(delay) {
var phase6 = { color: 'g', state: 0, up: 1, delay: delay, next: null};
var phase5 = { color: 'r', state: 0, up: 1, delay: delay, next: phase6};
var phase4 = { color: 'g', state: 100, up: 0, delay: delay, next: phase5};
var phase3 = { color: 'b', state: 0, up: 1, delay: delay, next: phase4};
var phase2 = { color: 'r', state: 100, up: 0, delay: delay, next: phase3};
var phase1 = { color: 'g', state: 0, up: 1, delay: delay, next: phase2};
fade('r', 0, 1, delay, phase1);
}
function mapColor(i) {
if ( i <= 100 ) {
light(100, i, 0);
} else if ( i <= 200 ) {
light(200 - i, 100, 0);
} else if ( i <= 300 ) {
light(0, 100, i - 200);
} else {
light(0, 400 - i, 100);
}
}
module.exports = {
light: light,
setGreen: setGreen,
mapColor: mapColor,
turnOff: turnOff,
transition: transition,
setColor: setColor
}