-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanodmx.js
66 lines (58 loc) · 2.12 KB
/
nanodmx.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
module.exports = function(RED) {
"use strict";
var DMX = require('./nanodmx_driver.js');
function DMXout(config) {
RED.nodes.createNode(this, config);
var node = this;
this.configNode = RED.nodes.getNode(config.config);
if (this.configNode) {
this.port = this.configNode.port;
this.dmxOffset = this.configNode.dmxStartingAddress;
this.baudrate = this.configNode.baudrate;
this.DmxUsbDevice = this.configNode.DmxUsbDevice;
} else {
this.error("No configuration node found");
return;
}
var currentUniverse = [];
node.on('input', function(msg) {
var input = Object.keys(msg.payload).length;
if (input) {
currentUniverse = msg.payload;
node.DmxUsbDevice.update(currentUniverse, node.dmxOffset);
} else {
msg.payload = node.DmxUsbDevice.universeToObject();
}
node.send(msg);
});
this.on('close', function(done) {
// Nothing to do here, as the port is managed by the config node
done();
});
}
RED.nodes.registerType("nanodmx", DMXout);
function DMXConfigNode(n) {
RED.nodes.createNode(this, n);
this.port = n.port;
this.dmxStartingAddress = n.dmxStartingAddress;
this.baudrate = n.baudrate;
this.databits = 8;
this.stopbits = 1;
this.parity = "none";
if (!this.DmxUsbDevice) {
var currentUniverseBuffer = Buffer.alloc(512);
this.DmxUsbDevice = new DMX(this.port, this.baudrate, this.databits, this.stopbits, this.parity, currentUniverseBuffer);
}
this.on('close', function(done) {
if (this.DmxUsbDevice) {
this.DmxUsbDevice.close(function(err) {
console.log('[info] DMX4ALL NanoDMX USB device closed', err);
done();
});
} else {
done();
}
});
}
RED.nodes.registerType("nanodmx-config", DMXConfigNode);
}