-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMXController.js
86 lines (69 loc) · 1.62 KB
/
DMXController.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
/*
* https://github.com/andypotato/dmx-controller-demo/
*
*/
const DMX = require('dmx');
const EventEmitter = require('events');
const config_default = {
driver: 'enttec-open-usb-dmx',
device: '/dev/tty.usbserial-AD0JL34E',
startChannel: 1,
nbChannels: 11,
channels: {
x: 1,
y: 3,
speed: 9
}
};
module.exports = class DMXController extends EventEmitter {
// construction
constructor(config) {
super();
this.universe = null;
this.channels = [];
this.config = Object.assign(config_default, config);
for (var i = 0; i < this.config.nbChannels; i++) {
this.channels[i] = this.config.startChannel+i;
}
this.connect();
}
connect() {
let self = this;
// create universe
const dmx = new DMX();
this.universe = dmx.addUniverse('demo', this.config.driver, this.config.device);
this.universe.dev.on('open', function(err) {
self.emit('open');
})
this.universe.dev.on('error', function(err) {
console.log('Error: ', err.message);
})
this.universe.dev.on('close', (stream) => {
console.log('close!');
});
// let there be (no) light
this.universe.updateAll(0);
}
update(channels) {
if (!this.universe.dev.isOpen) {
this.connect();
}
this.universe.update(channels);
}
// accessors
setSpeed(speed) {
this.update({
[this.config.channels.speed]: speed
});
}
setPos(x, y, speed) {
var payload = {
[this.config.channels.x]: x,
[this.config.channels.y]: y
};
if (speed) {
payload[this.config.channels.speed] = speed;
}
this.update(payload);
}
}