-
Notifications
You must be signed in to change notification settings - Fork 595
/
76-blinkstick.js
341 lines (305 loc) · 14.6 KB
/
76-blinkstick.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
module.exports = function(RED) {
"use strict";
var blinkstick = require("blinkstick");
var availableModes = ["normal", "inverted", "neopixel"];
var availableTasks = ["set_color", "blink", "pulse", "morph"];
Object.size = function(obj) {
var size = 0;
for (var key in obj) { if (obj.hasOwnProperty(key)) { size++; } }
return size;
};
//Helper function to convert decimal number to hex with padding
function decimalToHex(d, padding) {
var hex = Number(d).toString(16);
padding = typeof (padding) === "undefined" || padding === null ? padding = 2 : padding;
while (hex.length < padding) {
hex = "0" + hex;
}
return hex;
}
function validateInt(value, defaultValue) {
return typeof (value) === "undefined" || value === null ? value = defaultValue : parseInt(value);
}
function validate(value, defaultValue) {
return typeof (value) === "undefined" || value === null ? value = defaultValue : value;
}
function validateArray(value, defaultValue) {
return typeof (value) === "undefined" || Array.isArray(value) ? value : defaultValue;
}
function validatePayloadObject (obj) {
var
task = validate(obj.task),
delay = validateInt(obj.delay),
repeats = validateInt(obj.repeats),
duration = validateInt(obj.duration),
steps = validateInt(obj.steps),
repeat = validate(obj.repeat),
color = validate(obj.color),
channel = validateInt(obj.channel),
index = validateInt(obj.index),
row = validateArray(obj.row);
if (typeof(task) !== 'undefined' && availableTasks.indexOf(task) === -1) {
return "Task is invalid";
}
if (typeof(color) === 'undefined' && typeof(row) === 'undefined') {
return "Color parameter is not set";
}
return { 'task': task, 'delay': delay, 'repeats': repeats, 'duration': duration, 'steps': steps,
'repeat': repeat, 'color': color, 'channel': channel, 'index': index, 'row': row };
}
function BlinkStick(n) {
RED.nodes.createNode(this,n);
this.name = n.name;
this.serial = n.serial;
this.mode = n.mode || "normal";
this.task = n.task || "set_color";
this.delay = n.delay || 500;
this.repeats = n.repeats || 1;
this.duration = n.duration || 1000;
this.steps = n.steps || 50;
this.repeat = n.repeat;
this.channel = 0;
this.index = 0;
this.row = [];
this.closing = false;
this.color = '';
this.previousColor = '';
var p1 = /[0-9]+,[0-9]+,[0-9]+/;
var node = this;
//Keeps track for active animations
var animationComplete = true;
//Find BlinkStick based on serial number if supplied, otherwise look for first
//Blinkstick in the USB device list
var findBlinkStick = function (callback) {
if (typeof(node.serial) == 'string' && node.serial.replace(/\s+/g,'') !== '') {
blinkstick.findBySerial(node.serial, function (device) {
node.led = device;
if (Object.size(node.led) === 0) {
node.status({fill:"red",shape:"ring",text:"not found"});
node.error("BlinkStick with serial number " + node.serial + " not found");
}
else {
node.status({fill:"green",shape:"dot",text:"connected"});
if (node.mode == "normal") {node.led.setMode(0);}
else if (node.mode == "inverted") {node.led.setMode(1);}
else if (node.mode == "neopixel") {node.led.setMode(2);}
if (callback) { callback(); }
}
});
}
else {
node.led = blinkstick.findFirst();
if (Object.size(node.led) === 0) {
node.status({fill:"red",shape:"ring",text:"not found"});
node.error("No BlinkStick found");
}
else {
node.status({fill:"green",shape:"dot",text:"connected"});
if (node.mode == "normal") {node.led.setMode(0);}
else if (node.mode == "inverted") {node.led.setMode(1);}
else if (node.mode == "neopixel") {node.led.setMode(2);}
if (callback) { callback(); }
}
}
};
//Check if repeat is enabled. Works only for pulse and blink tasks
var canRepeat = function () {
return node.task == "pulse" || node.task == "blink";
};
//Event handler for all animation complete events
var blinkstickAnimationComplete = function (err) {
if (typeof(err) !== 'undefined') {
node.warn(err);
if (typeof(err.name) === 'undefined' || err.name !== 'ReferenceError') {
//USB error occurred when BlinkStick was animating
node.led.close(function () {
node.led = undefined;
findBlinkStick();
});
}
}
animationComplete = true;
//Apply queued color animation
if (!node.closing && node.color !== '') {
//Apply new color only if there was no error or queued color is different from the previous color
if (typeof(err) === 'undefined' || typeof(err) !== 'undefined' && node.color != node.previousColor) {
applyColor();
}
}
};
var applyColor = function () {
animationComplete = false;
//Store the value of color to check if it has changed
node.previousColor = node.color;
try {
//Select animation to perform
if (node.task == "pulse") {
node.led.pulse(node.color, {'duration': node.duration, 'steps': node.steps, 'channel': node.channel, 'index': node.index }, blinkstickAnimationComplete);
}
else if (node.task == "morph") {
node.led.morph(node.color, {'duration': node.duration, 'steps': node.steps, 'channel': node.channel, 'index': node.index }, blinkstickAnimationComplete);
}
else if (node.task == "blink") {
node.led.blink(node.color,{'repeats': node.repeats, 'delay': node.delay, 'channel': node.channel, 'index': node.index }, blinkstickAnimationComplete);
}
else {
if (node.row.length > 0) {
var dat = [];
for (var i = 0; i < node.row.length; i++) {
if (typeof node.row[i] === "string") { // if string then assume must be colour names
var params = node.led.interpretParameters(node.row[i]); // lookup colour code from name
if (params) {
dat.push(params.green);
dat.push(params.red);
dat.push(params.blue);
}
else { node.warn("invalid colour: "+node.row[i]); }
}
else { // otherwise lets use numbers 0-255
dat.push(node.row[i+1]);
dat.push(node.row[i]);
dat.push(node.row[i+2]);
i += 2;
}
}
if ((dat.length % 3) === 0) { // by now length must be a multiple of 3
node.led.setColors(node.channel, dat, blinkstickAnimationComplete);
}
else {
node.warn("Colour array length not / 3");
}
}
else {
node.led.setColor(node.color, {'channel': node.channel, 'index': node.index}, blinkstickAnimationComplete);
}
}
}
catch (err) {
if (err.toString().indexOf("setColor") !== -1) {
node.led.setColour(node.color, blinkstickAnimationComplete);
node.warn("Old version - please upgrade Blinkstick npm");
}
else {
node.warn("BlinkStick missing ? " + err);
}
node.log(err);
//Reset animation
animationComplete = true;
//Clear color
node.color = '';
//Look for a BlinkStick
findBlinkStick();
return;
}
//Clear color value until next one is received, unless repeat option is set to true
if (!node.repeat || !canRepeat()) {
node.color = '';
}
};
findBlinkStick();
this.on("input", function(msg) {
if (typeof(node.led) !== "undefined") {
if (typeof(msg.payload) === 'object' ) {
// if it's an array then hopefully it's r,g,b,r,g,b or name,name,name
if (Array.isArray(msg.payload)) {
if (Object.size(node.led) !== 0) {
var dat = [];
for (var i = 0; i < msg.payload.length; i++) {
if (typeof msg.payload[i] === "string") { // if string then assume must be colour names
var params = node.led.interpretParameters(msg.payload[i]); // lookup colour code from name
if (params) {
dat.push(params.green);
dat.push(params.red);
dat.push(params.blue);
}
else { node.warn("invalid colour: "+msg.payload[i]); }
}
else { // otherwise lets use numbers 0-255
dat.push(msg.payload[i+1]);
dat.push(msg.payload[i]);
dat.push(msg.payload[i+2]);
i += 2;
}
}
if ((dat.length % 3) === 0) { // by now length must be a multiple of 3
node.led.setColors(0, dat, function(err) {
if (err) { node.log(err); }
});
}
else {
node.warn("Colour array length not / 3");
}
return;
} // else if no blinkstick let it get caught below
}
// not an array - must be the "normal" object....
else {
var data = validatePayloadObject(msg.payload);
if (typeof(data) === 'object') {
node.task = data.task ? data.task : node.task;
node.delay = data.delay ? data.delay : node.delay;
node.repeats = data.repeats ? data.repeats : node.repeats;
node.duration = data.duration ? data.duration : node.duration;
node.steps = data.steps ? data.steps : node.steps;
node.repeat = data.repeat ? data.repeat : node.repeat;
node.color = data.color ? data.color : node.color;
node.channel = typeof(data.channel) !== 'undefined' ? data.channel : node.channel;
node.index = data.index ? data.index : node.index;
node.row = data.row ? data.row : node.row;
}
else {
node.error(data);
return;
}
}
}
else if (p1.test(msg.payload)) {
//Color value is represented as "red,green,blue" string of bytes
var rgb = msg.payload.split(",");
//Convert color value back to HEX string for easier implementation
node.color = "#" + decimalToHex(parseInt(rgb[0])&255) +
decimalToHex(parseInt(rgb[1])&255) + decimalToHex(parseInt(rgb[2])&255);
}
else {
//Sanitize color value
node.color = msg.payload.toLowerCase().replace(/\s+/g,'');
if (node.color === "amber") { node.color = "#FFBF00"; }
if (node.color === "off") { node.color = "#000000"; }
}
if (Object.size(node.led) !== 0) {
//Start color animation, otherwise the color is queued until current animation completes
if (animationComplete) {
applyColor();
}
}
else {
//Attempt to find BlinkStick and start animation if it's found
findBlinkStick(function() {
if (animationComplete) {
applyColor();
}
});
}
}
else {
node.status({fill:"red",shape:"ring",text:"not found"});
node.error("BlinkStick not found",msg);
}
});
this.on("close", function() {
//Set the flag to finish all animations
this.closing = true;
if (Object.size(node.led) !== 0) {
//Close device and stop animations
if (typeof this.led.close === "function") { this.led.close(); }
else { node.warn("Please upgrade blinkstick npm"); }
}
});
}
RED.nodes.registerType("blinkstick",BlinkStick);
RED.httpAdmin.get("/blinksticklist", RED.auth.needsPermission("blinkstick.read"), function(req,res) {
blinkstick.findAllSerials(function(serials) {
res.json(serials);
});
});
};