UNPKG

fivetwelve-driver-arduino-serial

Version:

DMX-Driver for the fivetwelve-library that uses an Arduino to generate a DMX-Signal.

113 lines (94 loc) 3.64 kB
/** * Simple serial to DMX driver that only sends changes to the serialport. */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } var DmxSerialDriver = (function () { function DmxSerialDriver(serialport) { var numChannels = arguments.length <= 1 || arguments[1] === undefined ? 512 : arguments[1]; _classCallCheck(this, DmxSerialDriver); this.serialport = serialport; this.dmxBuffer = new Buffer(numChannels); this.dmxBuffer.fill(0); /** * Will be set to true once the first packet has been sent. * @type {boolean} */ this.initialized = false; this.ready = this.openSerialport(); } _createClass(DmxSerialDriver, [{ key: 'openSerialport', value: function openSerialport() { var _this = this; if (this.serialport.isOpen()) { return Promise.resolve(this.serialport); } return new Promise(function (resolve, reject) { _this.serialport.on('open', function (error) { if (error) { return reject(error); } return resolve(_this.serialport); }); }); } /** * sends the given values to the dmx-interface over the serialport * @param {Buffer} values A buffer with the dmx-values to be sent. * @returns {Promise} A promise that will be resolved when the buffer is * completely sent. */ }, { key: 'send', value: function send(values) { var diff = [], n = Math.min(values.length, this.dmxBuffer.length); // determine changes and update local state for (var i = 0; i < n; i++) { if (!this.initialized || this.dmxBuffer[i] !== values[i]) { diff.push([i + 1, values[i]]); this.dmxBuffer[i] = values[i]; } } this.initialized = true; if (diff.length === 0) { return Promise.resolve(); } // bring diff into binary format var msgBuf = new Buffer(diff.length * 3); for (var i = 0; i < diff.length; i++) { msgBuf.writeUInt16BE(diff[i][0], i * 3); msgBuf.writeUInt8(diff[i][1], i * 3 + 2); } return this.write(msgBuf); } /** * Writes the raw data to the serialport. * @param {Buffer} buffer A buffer to be sent to the serialport. * @returns {Promise} a promise that is resolved when the buffer was * completely sent. * @private */ }, { key: 'write', value: function write(buffer) { return this.ready.then(function (serialport) { return new Promise(function (resolve) { serialport.write(buffer, function () { return serialport.drain(function () { return resolve(); }); }); }); }); } }]); return DmxSerialDriver; })(); exports['default'] = DmxSerialDriver; module.exports = exports['default'];