dmx-flok/drivers/enttec-open-usb-dmx.js

76 lines
1.5 KiB
JavaScript
Raw Normal View History

const SerialPort = require('serialport');
2014-02-02 04:16:30 +00:00
function EnttecOpenUsbDMX(deviceId, options) {
const self = this;
2014-02-02 04:16:30 +00:00
options = options || {};
2015-05-15 12:34:24 +00:00
this.universe = new Buffer(513);
this.universe.fill(0);
2015-05-15 12:34:24 +00:00
self.interval = 46;
2015-05-15 12:34:24 +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
}
EnttecOpenUsbDMX.prototype.sendUniverse = function () {
const self = this;
if (!this.dev.writable) {
return;
}
2014-04-19 21:49:17 +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
EnttecOpenUsbDMX.prototype.start = function () {
this.intervalhandle = setInterval(this.sendUniverse.bind(this), this.interval);
};
EnttecOpenUsbDMX.prototype.stop = function () {
clearInterval(this.intervalhandle);
};
2014-02-02 04:16:30 +00:00
EnttecOpenUsbDMX.prototype.close = function (cb) {
this.stop();
this.dev.close(cb);
};
2014-02-02 04:16:30 +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
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
module.exports = EnttecOpenUsbDMX;