-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (95 loc) · 2.53 KB
/
index.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
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
'use strict';
const FadeCandy = require('node-fadecandy');
let ws = require('nodejs-websocket');
let ndFC = new FadeCandy();
let isFadeCandyReady = false;
let debug = true;
let port = 1337;
let config = {
stripes : 8,
led : 30,
max_led : 64
}
/**
* Fill the gabs from input with 0 using the config
*/
function convertData(input, config) {
let result = [];
let fill = new Array((config.max_led - config.led) * 3);
fill.fill(0);
if (debug) {
console.log('input: ', input.length);
}
let tmp;
let start_input = 0;
let start_output = 0;
if (debug) {
console.log('fill:', fill.length);
}
for (var i = 0; i < config.stripes; i++) {
let end_input = ((i + 1) * config.led) * 3;
if (debug) {
console.log('from', start_input, 'to', end_input);
}
tmp = input.slice(start_input, end_input);
result = result.concat(tmp);
result = result.concat(fill);
start_input = end_input;
}
return result;
}
/**
* FadeCandy is ready
*/
ndFC.on(FadeCandy.events.READY, function () {
if (debug) {
console.log('FadeCandy.events.READY');
}
// create default color look up table
ndFC.clut.create()
// set fadecandy led to manual mode
ndFC.config.set(ndFC.Configuration.schema.LED_MODE, 1)
// Update the status LED
let state = false
setInterval(() => {
state = !state;
ndFC.config.set(ndFC.Configuration.schema.LED_STATUS, +state)
}, 100);
});
/**
* FadeCandy Color_LUT is ready
*/
ndFC.on(FadeCandy.events.COLOR_LUT_READY, function () {
isFadeCandyReady = true;
// Turn off every pixel
let pixels = new Uint8Array(config.max_led * config.stripes * 3);
pixels.fill(0);
ndFC.send(pixels);
});
/**
* Create a WebSocket server
*/
let server = ws.createServer(function (connection) {
console.log('New connection');
connection.on('text', function (data) {
if (debug) {
console.log('nerdV - LED:', 'Receiving data');
}
data = JSON.parse(data);
let converted_data = convertData(data, config);
let pixels = new Uint8Array(converted_data);
// Send data when FadeCandy is ready
if (isFadeCandyReady) {
ndFC.send(pixels);
}
})
connection.on('close', function (code, reason) {
console.log('Connection closed');
});
connection.on('error', function (error) {
if (error.code !== 'ECONNRESET') {
// Ignore ECONNRESET and re throw anything else
throw err;
}
});
}).listen(port);