-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwled-serial.js
More file actions
102 lines (91 loc) · 2.44 KB
/
wled-serial.js
File metadata and controls
102 lines (91 loc) · 2.44 KB
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
const debug = require('debug')('kcapp-colors:wled-serial');
const { SerialPort } = require('serialport')
exports.EFFECTS = {
SOLID: 0,
BLINK: 1,
TWO_DOTS: 50,
DANCING_SHADOW: 112,
PRIDE2015: 63,
LOADING: 47,
HEARTBEAT: 100,
BREATH: 2,
BPM: 68,
LIGHTHOUSE: 41, // 240 effect, 220 fade
}
exports.PALLETE = {
RAINBOW: 11
}
function write(message) {
message = JSON.stringify(message);
debug(`Message: ${message}`);
this.port.write(message, (err) => {
if (err) {
debug('Error on write: ', err.message)
return;
}
});
}
/**
* Set the LEDs to the given hex color
* @param {string} color - Hex color to set
*/
exports.setColor = async (color) => {
debug(`Setting lights to ${color}`);
const message = {
on: true,
transition: 0,
seg: [ {
fx: this.EFFECTS.SOLID,
col: [ color.substring(1) ],
bri: 255
},
{
fx: this.EFFECTS.SOLID,
col: [ color.substring(1) ],
bri: 255
},
{
fx: this.EFFECTS.SOLID,
col: [ color.substring(1) ],
bri: 255
} ]
}
write.bind(this)(message);
}
/**
* Turn off the LEDs
*/
exports.turnOff = () => {
debug("Disabled lights");
write.bind(this)({ on :false });
}
exports.blink = (color, time, callback) => {
this.effect(this.EFFECTS.BLINK, color, null, 245, 75, time, callback);
}
exports.effect = (effect, color, pallete, sx = 100, ix = 100, time, callback) => {
debug(`Setting effect ${effect}!`);
const message = { on: true, seg: [ { sx: sx, ix: ix, fx: effect }, { sx: sx, ix: ix, fx: effect }, { sx: sx, ix: ix, fx: effect } ] };
if (color) {
message.seg.forEach(seg => seg.col = [ color.substring(1) ] );
}
if (pallete) {
message.seg.forEach(seg => seg.pal = pallete );
}
write.bind(this)(message);
if (time) {
setTimeout(() => {
debug("Stopping effect...");
write.bind(this)({ on: false, seg: [ { fx: this.EFFECTS.SOLID }, { fx: this.EFFECTS.SOLID }, { fx: this.EFFECTS.SOLID } ]});
if (callback) {
callback();
}
}, time);
}
}
module.exports = (path) => {
this.port = new SerialPort({ path: path, baudRate: 115200 });
this.port.on('error', (err) => {
debug('Error: ', err.message)
});
return this;
}