2018-12-19 19:24:06 +00:00
|
|
|
const SerialPort = require('serialport');
|
2014-02-02 04:16:30 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
function EnttecOpenUsbDMX(deviceId, options) {
|
|
|
|
const self = this;
|
2014-02-02 04:16:30 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
options = options || {};
|
2015-05-15 12:34:24 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
this.universe = new Buffer(513);
|
|
|
|
this.universe.fill(0);
|
2015-05-15 12:34:24 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
self.interval = 46;
|
2015-05-15 12:34:24 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
this.dev = new SerialPort(deviceId, {
|
|
|
|
'baudRate': 250000,
|
|
|
|
'dataBits': 8,
|
|
|
|
'stopBits': 2,
|
|
|
|
'parity': 'none',
|
|
|
|
}, err => {
|
|
|
|
if (err) {
|
|
|
|
console.log(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.start();
|
|
|
|
});
|
2014-04-19 21:49:17 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.sendUniverse = function () {
|
|
|
|
const self = this;
|
2016-04-09 16:08:19 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
if (!this.dev.writable) {
|
|
|
|
return;
|
|
|
|
}
|
2014-04-19 21:49:17 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
// toggle break
|
|
|
|
self.dev.set({brk: true, rts: true}, (err, r) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
self.dev.set({brk: false, rts: true}, (err, r) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
self.dev.write(Buffer.concat([Buffer([0]), self.universe.slice(1)]));
|
|
|
|
}, 1);
|
|
|
|
});
|
|
|
|
}, 1);
|
|
|
|
});
|
|
|
|
};
|
2014-02-02 04:16:30 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.start = function () {
|
|
|
|
this.intervalhandle = setInterval(this.sendUniverse.bind(this), this.interval);
|
|
|
|
};
|
2014-04-24 17:53:07 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.stop = function () {
|
|
|
|
clearInterval(this.intervalhandle);
|
|
|
|
};
|
2014-02-02 04:16:30 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.close = function (cb) {
|
|
|
|
this.stop();
|
|
|
|
this.dev.close(cb);
|
|
|
|
};
|
2014-02-02 04:16:30 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.update = function (u) {
|
|
|
|
for (const c in u) {
|
|
|
|
this.universe[c] = u[c];
|
|
|
|
}
|
|
|
|
};
|
2014-04-19 21:49:17 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
EnttecOpenUsbDMX.prototype.updateAll = function (v) {
|
|
|
|
for (let i = 1; i <= 512; i++) {
|
|
|
|
this.universe[i] = v;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EnttecOpenUsbDMX.prototype.get = function (c) {
|
|
|
|
return this.universe[c];
|
|
|
|
};
|
2014-02-02 04:16:30 +00:00
|
|
|
|
2018-12-19 19:24:06 +00:00
|
|
|
module.exports = EnttecOpenUsbDMX;
|