dmx-flok/drivers/dmx4all.js

67 lines
1.3 KiB
JavaScript
Raw Normal View History

const SerialPort = require('serialport');
2016-11-12 16:31:45 +00:00
const UNIVERSE_LEN = 512;
2016-11-12 16:31:45 +00:00
function DMX4ALL(deviceId, options = {}) {
const self = this;
this.universe = new Buffer(UNIVERSE_LEN + 1);
this.universe.fill(0);
2016-11-12 16:31:45 +00:00
this.dev = new SerialPort(deviceId, {
'baudRate': 38400,
'dataBits': 8,
'stopBits': 1,
'parity': 'none',
}, err => {
if (!err) {
self.sendUniverse();
}
});
this.dev.on('data', data => {
// process.stdout.write(data.toString('ascii'))
});
2016-11-12 16:31:45 +00:00
}
DMX4ALL.prototype.sendUniverse = function () {
if (!this.dev.writable) {
return;
}
2016-11-12 16:31:45 +00:00
const msg = Buffer(UNIVERSE_LEN * 3);
2016-11-12 16:31:45 +00:00
for (let i = 0; i < UNIVERSE_LEN; i++) {
msg[i * 3 + 0] = (i < 256) ? 0xE2 : 0xE3;
msg[i * 3 + 1] = i;
msg[i * 3 + 2] = this.universe[i + 1];
}
this.dev.write(msg);
};
2016-11-12 16:31:45 +00:00
DMX4ALL.prototype.start = () => {};
DMX4ALL.prototype.stop = () => {};
2016-11-12 16:31:45 +00:00
DMX4ALL.prototype.close = function (cb) {
this.dev.close(cb);
};
2016-11-12 16:31:45 +00:00
DMX4ALL.prototype.update = function (u) {
for (const c in u) {
this.universe[c] = u[c];
}
this.sendUniverse();
};
2016-11-12 16:31:45 +00:00
DMX4ALL.prototype.updateAll = function (v) {
for (let i = 1; i <= 512; i++) {
this.universe[i] = v;
}
this.sendUniverse();
};
DMX4ALL.prototype.get = function (c) {
return this.universe[c];
};
2016-11-12 16:31:45 +00:00
module.exports = DMX4ALL;