From ede29216f7919380d1001e12985dd58035af3c52 Mon Sep 17 00:00:00 2001 From: mmcwilliams Date: Fri, 8 Mar 2019 22:32:25 -0500 Subject: [PATCH] Update arduino lib in app and cli --- app/lib/arduino/index.js | 627 +++++++++++++++--------------- app/lib/arduino/index.js.map | 2 +- cli/lib/arduino/index.js | 713 +++++++++++++++++------------------ 3 files changed, 667 insertions(+), 675 deletions(-) diff --git a/app/lib/arduino/index.js b/app/lib/arduino/index.js index 992bebe..e928efc 100644 --- a/app/lib/arduino/index.js +++ b/app/lib/arduino/index.js @@ -7,6 +7,17 @@ const newlineRe = new RegExp('\n', 'g'); const returnRe = new RegExp('\r', 'g'); let eventEmitter; let cfg; +let arduino; +const KNOWN = [ + '/dev/tty.usbmodem1a161', + '/dev/tty.usbserial-A800f8dk', + '/dev/tty.usbserial-A900cebm', + '/dev/tty.usbmodem1a131', + '/dev/tty.usbserial-a900f6de', + '/dev/tty.usbmodem1a141', + '/dev/ttyACM0', + 'COM3' +]; /** * Pause the process for X milliseconds in async/await functions * @@ -20,352 +31,336 @@ async function delay(ms) { }); } /** - * Send a command to an Arduino using async/await - * - * @param {string} device Arduino identifier - * @param {string} cmd Single character command to send - * - * @returns {Promise} Resolves after sending + * Class representing the arduino communication features **/ -async function send(device, cmd) { - return new Promise((resolve, reject) => { - arduino.queue[cmd] = (ms) => { - return resolve(ms); - }; - return arduino.serial[device].write(cmd, (err, results) => { - if (err) { - //console.error(err) - return reject(err); - } - // - }); - }); -} -/** - * Send a string to an Arduino using async/await - * - * @param {string} device Arduino identifier - * @param {string} str String to send - * - * @returns {Promise} Resolves after sending - **/ -async function write(device, str) { - return new Promise((resolve, reject) => { - arduino.serial[device].write(str, function (err, results) { - if (err) { - return reject(err); - } - //console.log('sent: ' + str) - return resolve(results); - }); - }); -} -/** - * Connect to an Arduino using async/await - * - * @param {string} device Arduino identifier - * - * @returns {Promise} Resolves after opening - **/ -async function openArduino(device) { - return new Promise((resolve, reject) => { - return arduino.serial[device].open(error => { - if (error) { - return reject(error); - } - return resolve(true); - }); - }); -} -/** - * Close a connection to an Arduino using async/await - * - * @param {string} device Arduino identifier - * - * @returns {Promise} Resolves after closing - **/ -async function closeArduino(device) { - return new Promise((resolve, reject) => { - return arduino.serial[device].close((err) => { - if (err) { - return reject(err); - } - return resolve(true); - }); - }); -} -/****** - Arduino handlers -*******/ -const arduino = { - path: {}, - known: [ - '/dev/tty.usbmodem1a161', - '/dev/tty.usbserial-A800f8dk', - '/dev/tty.usbserial-A900cebm', - '/dev/tty.usbmodem1a131', - '/dev/tty.usbserial-a900f6de', - '/dev/tty.usbmodem1a141', - '/dev/ttyACM0', - 'COM3' - ], - alias: {}, - serial: { - connect: {}, - projector: {}, - camera: {}, - light: {} - }, - baud: 57600, - queue: {}, - timer: 0, - lock: false, - locks: {} -}; -arduino.enumerate = async function () { - return new Promise((resolve, reject) => { - return SerialPort.list((err, ports) => { - let matches = []; - if (err) { - return reject(err); - } - ports.forEach(port => { - if (arduino.known.indexOf(port.comName) !== -1) { - matches.push(port.comName); +class Arduino { + constructor() { + this.path = {}; + this.known = KNOWN; + this.alias = {}; + this.serial = { connect: {}, projector: {}, camera: {}, light: {} }; + this.baud = 57600; + this.queue = {}; + this.timer = 0; + this.lock = false; + this.locks = {}; + } + async enumerate() { + return new Promise((resolve, reject) => { + return SerialPort.list((err, ports) => { + let matches = []; + if (err) { + return reject(err); } - else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) { - matches.push(port.comName); + ports.forEach((port) => { + if (this.known.indexOf(port.comName) !== -1) { + matches.push(port.comName); + } + else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) { + matches.push(port.comName); + } + else if ((port.comName + '').toLowerCase().indexOf('usbserial') !== -1) { + matches.push(port.comName); + } + else if ((port.comName + '').toLowerCase().indexOf('usbmodem') !== -1) { + matches.push(port.comName); + } + }); + if (matches.length === 0) { + return reject('No USB devices found'); } - else if ((port.comName + '').toLowerCase().indexOf('usbserial') !== -1) { - matches.push(port.comName); - } - else if ((port.comName + '').toLowerCase().indexOf('usbmodem') !== -1) { - matches.push(port.comName); + else if (matches.length > 0) { + return resolve(matches); } }); - if (matches.length === 0) { - return reject('No USB devices found'); - } - else if (matches.length > 0) { - return resolve(matches); - } }); - }); -}; -//commands which respond to a sent char -arduino.send = async function (serial, cmd, res) { - const device = arduino.alias[serial]; - let results; - if (arduino.locks[serial]) { - return false; } - arduino.locks[serial] = true; - await delay(cfg.arduino.serialDelay); - try { - results = await send(device, cmd); + /** + * Send a command to an Arduino using async/await + * + * @param {string} device Arduino identifier + * @param {string} cmd Single character command to send + * + * @returns {Promise} Resolves after sending + **/ + async sendAsync(device, cmd) { + return new Promise((resolve, reject) => { + this.queue[cmd] = (ms) => { + return resolve(ms); + }; + return this.serial[device].write(cmd, (err, results) => { + if (err) { + //console.error(err) + return reject(err); + } + // + }); + }); } - catch (e) { - return console.error(e); - } - arduino.locks[serial] = false; - arduino.timer = new Date().getTime(); - return await eventEmitter.emit('arduino_send', cmd); -}; -//send strings, after char triggers firmware to accept -arduino.string = async function (serial, str) { - const device = arduino.alias[serial]; - let writeSuccess; - await delay(cfg.arduino.serialDelay); - if (typeof arduino.serial[device].fake !== 'undefined' - && arduino.serial[device].fake) { - return arduino.serial[device].string(str); - } - else { + async send(serial, cmd) { + const device = this.alias[serial]; + let results; + if (this.locks[serial]) { + return false; + } + this.locks[serial] = true; + await delay(cfg.arduino.serialDelay); try { - writeSuccess = await write(device, str); + results = await this.sendAsync(device, cmd); } catch (e) { return console.error(e); } - return writeSuccess; + this.locks[serial] = false; + this.timer = new Date().getTime(); + return await eventEmitter.emit('arduino_send', cmd); } -}; -//respond with same char over serial when done -arduino.end = async function (serial, data) { - const end = new Date().getTime(); - const ms = end - arduino.timer; - let complete; - if (arduino.queue[data] !== undefined) { - arduino.locks[serial] = false; - //console.log('Command ' + data + ' took ' + ms + 'ms'); - complete = arduino.queue[data](ms); //execute callback - eventEmitter.emit('arduino_end', data); - delete arduino.queue[data]; - } - else { - //console.log('Received stray "' + data + '"'); //silent to user - } - return complete; -}; -arduino.alias = function (serial, device) { - console.log(`Making "${serial}" an alias of ${device}`); - arduino.alias[serial] = device; -}; -arduino.connect = async function (serial, device, confirm) { - return new Promise(async (resolve, reject) => { - let connectSuccess; - arduino.path[serial] = device; - arduino.alias[serial] = device; - arduino.serial[device] = new SerialPort(arduino.path[serial], { - autoOpen: false, - baudRate: cfg.arduino.baud, - parser: parser - }); - arduino.locks[device] = false; - try { - connectSuccess = await openArduino(device); - } - catch (e) { - console.error('failed to open: ' + e); - return reject(e); - } - console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`); - if (!confirm) { - arduino.serial[device].on('data', async (data) => { - let d = data.toString('utf8'); - d = d.replace(newlineRe, '').replace(returnRe, ''); - return await arduino.end(serial, d); - }); + async string(serial, str) { + const device = this.alias[serial]; + let writeSuccess; + await delay(cfg.arduino.serialDelay); + if (typeof this.serial[device].fake !== 'undefined' + && this.serial[device].fake) { + return this.serial[device].string(str); } else { - arduino.serial[device].on('data', async (data) => { - let d = data.toString('utf8'); - d = d.replace(newlineRe, '').replace(returnRe, ''); - return await arduino.confirmEnd(d); - }); + try { + writeSuccess = await this.writeAsync(device, str); + } + catch (e) { + return console.error(e); + } + return writeSuccess; } - return resolve(arduino.path[serial]); - }); -}; -arduino.confirmExec = {}; -arduino.confirmEnd = function (data) { - //console.dir(data) - if (data === cfg.arduino.cmd.connect - || data === cfg.arduino.cmd.proj_identifier - || data === cfg.arduino.cmd.cam_identifier - || data === cfg.arduino.cmd.light_identifier - || data === cfg.arduino.cmd.proj_light_identifier - || data === cfg.arduino.cmd.proj_cam_light_identifier - || data === cfg.arduino.cmd.proj_cam_identifier) { - arduino.confirmExec(null, data); - arduino.confirmExec = {}; } -}; -arduino.verify = async function () { - return new Promise(async (resolve, reject) => { - const device = arduino.alias['connect']; - let writeSuccess; - arduino.confirmExec = function (err, data) { - if (data === cfg.arduino.cmd.connect) { - return resolve(true); + /** + * Send a string to an Arduino using async/await + * + * @param {string} device Arduino identifier + * @param {string} str String to send + * + * @returns {Promise} Resolves after sending + **/ + async writeAsync(device, str) { + return new Promise((resolve, reject) => { + this.serial[device].write(str, function (err, results) { + if (err) { + return reject(err); + } + //console.log('sent: ' + str) + return resolve(results); + }); + }); + } + end(serial, data) { + const end = new Date().getTime(); + const ms = end - this.timer; + let complete; + if (this.queue[data] !== undefined) { + this.locks[serial] = false; + //console.log('Command ' + data + ' took ' + ms + 'ms'); + complete = this.queue[data](ms); //execute callback + eventEmitter.emit('arduino_end', data); + delete this.queue[data]; + } + else { + //console.log('Received stray "' + data + '"'); //silent to user + } + return complete; + } + aliasSerial(serial, device) { + console.log(`Making "${serial}" an alias of ${device}`); + this.alias[serial] = device; + } + async connect(serial, device, confirm) { + return new Promise(async (resolve, reject) => { + let connectSuccess; + this.path[serial] = device; + this.alias[serial] = device; + this.serial[device] = new SerialPort(this.path[serial], { + autoOpen: false, + baudRate: cfg.arduino.baud, + parser: parser + }); + this.locks[device] = false; + try { + connectSuccess = await this.openArduino(device); + } + catch (e) { + console.error('failed to open: ' + e); + return reject(e); + } + console.log(`Opened connection with ${this.path[serial]} as ${serial}`); + if (!confirm) { + this.serial[device].on('data', async (data) => { + let d = data.toString('utf8'); + d = d.replace(newlineRe, '').replace(returnRe, ''); + return this.end(serial, d); + }); } else { - return reject('Wrong data returned'); + this.serial[device].on('data', async (data) => { + let d = data.toString('utf8'); + d = d.replace(newlineRe, '').replace(returnRe, ''); + return await this.confirmEnd(d); + }); } - }; - await delay(cfg.arduino.serialDelay); - try { - writeSuccess = await send(device, cfg.arduino.cmd.connect); - } - catch (e) { - return reject(e); - } - return resolve(writeSuccess); - }); -}; -arduino.distinguish = async function () { - return new Promise(async (resolve, reject) => { - const device = arduino.alias['connect']; - let writeSuccess; - let type; - arduino.confirmExec = function (err, data) { - if (data === cfg.arduino.cmd.proj_identifier) { - type = 'projector'; - } - else if (data === cfg.arduino.cmd.cam_identifier) { - type = 'camera'; - } - else if (data === cfg.arduino.cmd.light_identifier) { - type = 'light'; - } - else if (data === cfg.arduino.cmd.proj_light_identifier) { - type = 'projector,light'; - } - else if (data === cfg.arduino.cmd.proj_cam_light_identifier) { - type = 'projector,camera,light'; - } - else if (data === cfg.arduino.cmd.proj_cam_identifier) { - type = 'projector,camera'; - } - else if (data === cfg.ardino.cmd.proj_second_identifier) { - type = 'projector_second'; - } - return resolve(type); - }; - await delay(cfg.arduino.serialDelay); - try { - writeSuccess = await send(device, cfg.arduino.cmd.mcopy_identifier); - } - catch (e) { - console.error(e); - return reject(e); - } - }); -}; -arduino.close = async function (callback) { - const device = arduino.alias['connect']; - let closeSuccess; - try { - closeSuccess = await closeArduino(device); + return resolve(this.path[serial]); + }); } - catch (e) { - return console.error(e); + confirmEnd(data) { + //console.dir(data) + if (data === cfg.arduino.cmd.connect + || data === cfg.arduino.cmd.proj_identifier + || data === cfg.arduino.cmd.cam_identifier + || data === cfg.arduino.cmd.light_identifier + || data === cfg.arduino.cmd.proj_light_identifier + || data === cfg.arduino.cmd.proj_cam_light_identifier + || data === cfg.arduino.cmd.proj_cam_identifier) { + this.confirmExec(null, data); + this.confirmExec = {}; + } } - return closeSuccess; -}; -arduino.fakeConnect = async function (serial) { - //console.log('Connecting to fake arduino...'); - const device = '/dev/fake'; - arduino.alias[serial] = device; - arduino.serial[device] = { - write: function (cmd, cb) { - const t = { - c: cfg.arduino.cam.time + cfg.arduino.cam.delay, - p: cfg.arduino.proj.time + cfg.arduino.proj.delay + async verify() { + return new Promise(async (resolve, reject) => { + const device = this.alias['connect']; + let writeSuccess; + this.confirmExec = function (err, data) { + if (data === cfg.arduino.cmd.connect) { + return resolve(true); + } + else { + return reject('Wrong data returned'); + } }; - let timeout = t[cmd]; - let end; - if (typeof timeout === 'undefined') - timeout = 10; - arduino.timer = +new Date(); - setTimeout(() => { + await delay(cfg.arduino.serialDelay); + try { + writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.connect); + } + catch (e) { + return reject(e); + } + return resolve(writeSuccess); + }); + } + async distinguish() { + return new Promise(async (resolve, reject) => { + const device = this.alias['connect']; + let writeSuccess; + let type; + this.confirmExec = function (err, data) { + if (data === cfg.arduino.cmd.proj_identifier) { + type = 'projector'; + } + else if (data === cfg.arduino.cmd.cam_identifier) { + type = 'camera'; + } + else if (data === cfg.arduino.cmd.light_identifier) { + type = 'light'; + } + else if (data === cfg.arduino.cmd.proj_light_identifier) { + type = 'projector,light'; + } + else if (data === cfg.arduino.cmd.proj_cam_light_identifier) { + type = 'projector,camera,light'; + } + else if (data === cfg.arduino.cmd.proj_cam_identifier) { + type = 'projector,camera'; + } + else if (data === cfg.ardino.cmd.proj_second_identifier) { + type = 'projector_second'; + } + return resolve(type); + }; + await delay(cfg.arduino.serialDelay); + try { + writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.mcopy_identifier); + } + catch (e) { + console.error(e); + return reject(e); + } + }); + } + async close() { + const device = this.alias['connect']; + let closeSuccess; + try { + closeSuccess = await this.closeArduino(device); + } + catch (e) { + return console.error(e); + } + return closeSuccess; + } + ; + async fakeConnect(serial) { + //console.log('Connecting to fake arduino...'); + const device = '/dev/fake'; + this.alias[serial] = device; + this.serial[device] = { + write: async function (cmd, cb) { + const t = { + c: cfg.arduino.cam.time + cfg.arduino.cam.delay, + p: cfg.arduino.proj.time + cfg.arduino.proj.delay + }; + let timeout = t[cmd]; + if (typeof timeout === 'undefined') + timeout = 10; + arduino.timer = +new Date(); + await delay(timeout); arduino.end(serial, cmd); return cb(); - }, timeout); - }, - string: async function (str) { - //do nothing - return true; - }, - fake: true - }; - //console.log('Connected to fake arduino! Not real! Doesn\'t exist!'); - return true; -}; + }, + string: async function (str) { + //do nothing + return true; + }, + fake: true + }; + //console.log('Connected to fake arduino! Not real! Does not exist!'); + return true; + } + /** + * Connect to an Arduino using async/await + * + * @param {string} device Arduino identifier + * + * @returns {Promise} Resolves after opening + **/ + async openArduino(device) { + return new Promise((resolve, reject) => { + return this.serial[device].open((err) => { + if (err) { + return reject(err); + } + return resolve(true); + }); + }); + } + /** + * Close a connection to an Arduino using async/await + * + * @param {string} device Arduino identifier + * + * @returns {Promise} Resolves after closing + **/ + async closeArduino(device) { + return new Promise((resolve, reject) => { + return this.serial[device].close((err) => { + if (err) { + return reject(err); + } + return resolve(true); + }); + }); + } +} if (typeof module !== 'undefined' && module.parent) { module.exports = function (c, ee) { eventEmitter = ee; cfg = c; + arduino = new Arduino(); return arduino; }; } diff --git a/app/lib/arduino/index.js.map b/app/lib/arduino/index.js.map index 8e9c98c..d999574 100644 --- a/app/lib/arduino/index.js.map +++ b/app/lib/arduino/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/arduino/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AAEZ,MAAM,UAAU,GAAG,OAAO,CAAC,YAAY,CAAC,CAAA;AACxC,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,QAAQ,CAAA;AAC5C,MAAM,IAAI,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC,IAAI,CAAA;AAE1C,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC,EAAE,CAAC,CAAA;AAC/B,MAAM,SAAS,GAAY,IAAI,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;AAChD,MAAM,QAAQ,GAAY,IAAI,MAAM,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;AAE/C,IAAI,YAAkB,CAAA;AACtB,IAAI,GAAY,CAAA;AAEhB;;;;;;IAMI;AACJ,KAAK,UAAU,KAAK,CAAE,EAAW;IAChC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE;QAC5B,OAAO,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;IAC/B,CAAC,CAAC,CAAA;AACH,CAAC;AAED;;;;;;;IAOI;AACJ,KAAK,UAAU,IAAI,CAAE,MAAe,EAAE,GAAY;IACjD,OAAO,IAAI,OAAO,CAAE,CAAC,OAA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gBACD,OAAO,OAAO,CAAC,IAAI,CAAC,CAAA;YACrB,CAAC,CAAC,CAAA;QACH,CAAC,CAAC,CAAA;IACH,CAAC;CACD;AAED,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,MAAM,EAAE;IACnD,MAAM,CAAC,OAAO,GAAG,UAAU,CAAO,EAAE,EAAQ;QAC3C,YAAY,GAAG,EAAE,CAAA;QACjB,GAAG,GAAG,CAAC,CAAA;QACP,OAAO,GAAG,IAAI,OAAO,EAAE,CAAA;QACvB,OAAO,OAAO,CAAA;IACf,CAAC,CAAA;CACD"} \ No newline at end of file diff --git a/cli/lib/arduino/index.js b/cli/lib/arduino/index.js index d078db5..e928efc 100644 --- a/cli/lib/arduino/index.js +++ b/cli/lib/arduino/index.js @@ -1,15 +1,23 @@ -'use strict' - -const SerialPort = require('serialport') -const Readline = SerialPort.parsers.Readline -const exec = require('child_process').exec -const parser = new Readline('') -const newlineRe = new RegExp('\n', 'g') -const returnRe = new RegExp('\r', 'g') -let eventEmitter - -let cfg - +'use strict'; +const SerialPort = require('serialport'); +const Readline = SerialPort.parsers.Readline; +const exec = require('child_process').exec; +const parser = new Readline(''); +const newlineRe = new RegExp('\n', 'g'); +const returnRe = new RegExp('\r', 'g'); +let eventEmitter; +let cfg; +let arduino; +const KNOWN = [ + '/dev/tty.usbmodem1a161', + '/dev/tty.usbserial-A800f8dk', + '/dev/tty.usbserial-A900cebm', + '/dev/tty.usbmodem1a131', + '/dev/tty.usbserial-a900f6de', + '/dev/tty.usbmodem1a141', + '/dev/ttyACM0', + 'COM3' +]; /** * Pause the process for X milliseconds in async/await functions * @@ -17,354 +25,343 @@ let cfg * * @returns {Promise} Resolves after wait **/ -async function delay (ms) { - return new Promise(resolve => { - return setTimeout(resolve, ms) - }) +async function delay(ms) { + return new Promise(resolve => { + return setTimeout(resolve, ms); + }); } - /** - * Send a command to an Arduino using async/await - * - * @param {string} device Arduino identifier - * @param {string} cmd Single character command to send - * - * @returns {Promise} Resolves after sending + * Class representing the arduino communication features **/ -async function send (device, cmd) { - return new Promise ((resolve, reject) => { - arduino.queue[cmd] = (ms) => { - return resolve(ms) - } - return arduino.serial[device].write(cmd, (err, results) => { - if (err) { - //console.error(err) - return reject(err) - } - // - }) - }) +class Arduino { + constructor() { + this.path = {}; + this.known = KNOWN; + this.alias = {}; + this.serial = { connect: {}, projector: {}, camera: {}, light: {} }; + this.baud = 57600; + this.queue = {}; + this.timer = 0; + this.lock = false; + this.locks = {}; + } + async enumerate() { + return new Promise((resolve, reject) => { + return SerialPort.list((err, ports) => { + let matches = []; + if (err) { + return reject(err); + } + ports.forEach((port) => { + if (this.known.indexOf(port.comName) !== -1) { + matches.push(port.comName); + } + else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) { + matches.push(port.comName); + } + else if ((port.comName + '').toLowerCase().indexOf('usbserial') !== -1) { + matches.push(port.comName); + } + else if ((port.comName + '').toLowerCase().indexOf('usbmodem') !== -1) { + matches.push(port.comName); + } + }); + if (matches.length === 0) { + return reject('No USB devices found'); + } + else if (matches.length > 0) { + return resolve(matches); + } + }); + }); + } + /** + * Send a command to an Arduino using async/await + * + * @param {string} device Arduino identifier + * @param {string} cmd Single character command to send + * + * @returns {Promise} Resolves after sending + **/ + async sendAsync(device, cmd) { + return new Promise((resolve, reject) => { + this.queue[cmd] = (ms) => { + return resolve(ms); + }; + return this.serial[device].write(cmd, (err, results) => { + if (err) { + //console.error(err) + return reject(err); + } + // + }); + }); + } + async send(serial, cmd) { + const device = this.alias[serial]; + let results; + if (this.locks[serial]) { + return false; + } + this.locks[serial] = true; + await delay(cfg.arduino.serialDelay); + try { + results = await this.sendAsync(device, cmd); + } + catch (e) { + return console.error(e); + } + this.locks[serial] = false; + this.timer = new Date().getTime(); + return await eventEmitter.emit('arduino_send', cmd); + } + async string(serial, str) { + const device = this.alias[serial]; + let writeSuccess; + await delay(cfg.arduino.serialDelay); + if (typeof this.serial[device].fake !== 'undefined' + && this.serial[device].fake) { + return this.serial[device].string(str); + } + else { + try { + writeSuccess = await this.writeAsync(device, str); + } + catch (e) { + return console.error(e); + } + return writeSuccess; + } + } + /** + * Send a string to an Arduino using async/await + * + * @param {string} device Arduino identifier + * @param {string} str String to send + * + * @returns {Promise} Resolves after sending + **/ + async writeAsync(device, str) { + return new Promise((resolve, reject) => { + this.serial[device].write(str, function (err, results) { + if (err) { + return reject(err); + } + //console.log('sent: ' + str) + return resolve(results); + }); + }); + } + end(serial, data) { + const end = new Date().getTime(); + const ms = end - this.timer; + let complete; + if (this.queue[data] !== undefined) { + this.locks[serial] = false; + //console.log('Command ' + data + ' took ' + ms + 'ms'); + complete = this.queue[data](ms); //execute callback + eventEmitter.emit('arduino_end', data); + delete this.queue[data]; + } + else { + //console.log('Received stray "' + data + '"'); //silent to user + } + return complete; + } + aliasSerial(serial, device) { + console.log(`Making "${serial}" an alias of ${device}`); + this.alias[serial] = device; + } + async connect(serial, device, confirm) { + return new Promise(async (resolve, reject) => { + let connectSuccess; + this.path[serial] = device; + this.alias[serial] = device; + this.serial[device] = new SerialPort(this.path[serial], { + autoOpen: false, + baudRate: cfg.arduino.baud, + parser: parser + }); + this.locks[device] = false; + try { + connectSuccess = await this.openArduino(device); + } + catch (e) { + console.error('failed to open: ' + e); + return reject(e); + } + console.log(`Opened connection with ${this.path[serial]} as ${serial}`); + if (!confirm) { + this.serial[device].on('data', async (data) => { + let d = data.toString('utf8'); + d = d.replace(newlineRe, '').replace(returnRe, ''); + return this.end(serial, d); + }); + } + else { + this.serial[device].on('data', async (data) => { + let d = data.toString('utf8'); + d = d.replace(newlineRe, '').replace(returnRe, ''); + return await this.confirmEnd(d); + }); + } + return resolve(this.path[serial]); + }); + } + confirmEnd(data) { + //console.dir(data) + if (data === cfg.arduino.cmd.connect + || data === cfg.arduino.cmd.proj_identifier + || data === cfg.arduino.cmd.cam_identifier + || data === cfg.arduino.cmd.light_identifier + || data === cfg.arduino.cmd.proj_light_identifier + || data === cfg.arduino.cmd.proj_cam_light_identifier + || data === cfg.arduino.cmd.proj_cam_identifier) { + this.confirmExec(null, data); + this.confirmExec = {}; + } + } + async verify() { + return new Promise(async (resolve, reject) => { + const device = this.alias['connect']; + let writeSuccess; + this.confirmExec = function (err, data) { + if (data === cfg.arduino.cmd.connect) { + return resolve(true); + } + else { + return reject('Wrong data returned'); + } + }; + await delay(cfg.arduino.serialDelay); + try { + writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.connect); + } + catch (e) { + return reject(e); + } + return resolve(writeSuccess); + }); + } + async distinguish() { + return new Promise(async (resolve, reject) => { + const device = this.alias['connect']; + let writeSuccess; + let type; + this.confirmExec = function (err, data) { + if (data === cfg.arduino.cmd.proj_identifier) { + type = 'projector'; + } + else if (data === cfg.arduino.cmd.cam_identifier) { + type = 'camera'; + } + else if (data === cfg.arduino.cmd.light_identifier) { + type = 'light'; + } + else if (data === cfg.arduino.cmd.proj_light_identifier) { + type = 'projector,light'; + } + else if (data === cfg.arduino.cmd.proj_cam_light_identifier) { + type = 'projector,camera,light'; + } + else if (data === cfg.arduino.cmd.proj_cam_identifier) { + type = 'projector,camera'; + } + else if (data === cfg.ardino.cmd.proj_second_identifier) { + type = 'projector_second'; + } + return resolve(type); + }; + await delay(cfg.arduino.serialDelay); + try { + writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.mcopy_identifier); + } + catch (e) { + console.error(e); + return reject(e); + } + }); + } + async close() { + const device = this.alias['connect']; + let closeSuccess; + try { + closeSuccess = await this.closeArduino(device); + } + catch (e) { + return console.error(e); + } + return closeSuccess; + } + ; + async fakeConnect(serial) { + //console.log('Connecting to fake arduino...'); + const device = '/dev/fake'; + this.alias[serial] = device; + this.serial[device] = { + write: async function (cmd, cb) { + const t = { + c: cfg.arduino.cam.time + cfg.arduino.cam.delay, + p: cfg.arduino.proj.time + cfg.arduino.proj.delay + }; + let timeout = t[cmd]; + if (typeof timeout === 'undefined') + timeout = 10; + arduino.timer = +new Date(); + await delay(timeout); + arduino.end(serial, cmd); + return cb(); + }, + string: async function (str) { + //do nothing + return true; + }, + fake: true + }; + //console.log('Connected to fake arduino! Not real! Does not exist!'); + return true; + } + /** + * Connect to an Arduino using async/await + * + * @param {string} device Arduino identifier + * + * @returns {Promise} Resolves after opening + **/ + async openArduino(device) { + return new Promise((resolve, reject) => { + return this.serial[device].open((err) => { + if (err) { + return reject(err); + } + return resolve(true); + }); + }); + } + /** + * Close a connection to an Arduino using async/await + * + * @param {string} device Arduino identifier + * + * @returns {Promise} Resolves after closing + **/ + async closeArduino(device) { + return new Promise((resolve, reject) => { + return this.serial[device].close((err) => { + if (err) { + return reject(err); + } + return resolve(true); + }); + }); + } } - -/** - * Send a string to an Arduino using async/await - * - * @param {string} device Arduino identifier - * @param {string} str String to send - * - * @returns {Promise} Resolves after sending - **/ -async function write (device, str) { - return new Promise ((resolve, reject) => { - arduino.serial[device].write(str, function (err, results) { - if (err) { - return reject(err) - } - //console.log('sent: ' + str) - return resolve(results) - }) - }) -} - -/** - * Connect to an Arduino using async/await - * - * @param {string} device Arduino identifier - * - * @returns {Promise} Resolves after opening - **/ -async function open (device) { - return new Promise((resolve, reject) => { - return arduino.serial[device].open(error => { - if (error) { - return reject(error) - } - return resolve(true) - }) - }) -} - -/** - * Close a connection to an Arduino using async/await - * - * @param {string} device Arduino identifier - * - * @returns {Promise} Resolves after closing - **/ -async function close (device) { - return new Promise((resolve, reject) => { - return arduino.serial[device].close((err) => { - if (err) { - return reject(err) - } - return resolve(true) - }) - }) -} - -/****** - Arduino handlers -*******/ -const arduino = { - path : {}, - known: [ - '/dev/tty.usbmodem1a161', - '/dev/tty.usbserial-A800f8dk', - '/dev/tty.usbserial-A900cebm', - '/dev/tty.usbmodem1a131', - '/dev/tty.usbserial-a900f6de', - '/dev/tty.usbmodem1a141', - '/dev/ttyACM0', - 'COM3' - ], - alias : { - - }, - serial : { - connect : {}, - projector : {}, - camera : {}, - light : {} - }, - baud : 57600, - queue : {}, - timer : 0, - lock : false, - locks : { - - } -} - -arduino.enumerate = async function () { - return new Promise( (resolve, reject) => { - return SerialPort.list((err, ports) => { - let matches = [] - if (err) { - return reject(err) - } - ports.forEach(port => { - if (arduino.known.indexOf(port.comName) !== -1) { - matches.push(port.comName) - } else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) { - matches.push(port.comName) - } - }) - if (matches.length === 0) { - return reject('No USB devices found'); - } else if (matches.length > 0) { - return resolve(matches) - } - }) - }) -} - -//commands which respond to a sent char -arduino.send = async function (serial, cmd, res) { - const device = arduino.alias[serial] - let results - if (arduino.locks[serial]) { - return false - } - arduino.locks[serial] = true - await delay(cfg.arduino.serialDelay) - try { - results = await send(device, cmd) - } catch (e) { - return console.error(e) - } - arduino.locks[serial] = false - arduino.timer = new Date().getTime() - return await eventEmitter.emit('arduino_send', cmd) -} - -//send strings, after char triggers firmware to accept -arduino.string = async function (serial, str) { - const device = arduino.alias[serial] - let writeSuccess - await delay(cfg.arduino.serialDelay) - if (typeof arduino.serial[device].fake !== 'undefined' - && arduino.serial[device].fake) { - return arduino.serial[device].string(str) - } else { - try { - writeSuccess = await write(device, str) - } catch (e) { - return console.error(e) - } - return writeSuccess - } -} - -//respond with same char over serial when done -arduino.end = async function (serial, data) { - const end = new Date().getTime() - const ms = end - arduino.timer - let complete - if (arduino.queue[data] !== undefined) { - arduino.locks[serial] = false; - //console.log('Command ' + data + ' took ' + ms + 'ms'); - complete = arduino.queue[data](ms) //execute callback - eventEmitter.emit('arduino_end', data) - delete arduino.queue[data] - } else { - //console.log('Received stray "' + data + '"'); //silent to user - } - return complete -}; -arduino.alias = function (serial, device) { - console.log(`Making "${serial}" an alias of ${device}`) - arduino.alias[serial] = device -} -arduino.connect = async function (serial, device, confirm) { - return new Promise(async (resolve, reject) => { - let connectSuccess - arduino.path[serial] = device; - arduino.alias[serial] = device; - arduino.serial[device] = new SerialPort(arduino.path[serial], { - autoOpen : false, - baudRate: cfg.arduino.baud, - parser: parser - }) - arduino.locks[device] = false - try { - connectSuccess = await open(device) - } catch (e) { - console.error('failed to open: ' + e) - return reject(e) - } - console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`); - if (!confirm) { - arduino.serial[device].on('data', async (data) => { - let d = data.toString('utf8') - d = d.replace(newlineRe, '').replace(returnRe, '') - return await arduino.end(serial, d) - }) - } else { - arduino.serial[device].on('data', async (data) => { - let d = data.toString('utf8') - d = d.replace(newlineRe, '').replace(returnRe, '') - return await arduino.confirmEnd(d) - }) - } - return resolve(arduino.path[serial]) - }) - -} - -arduino.confirmExec = {}; -arduino.confirmEnd = function (data) { - //console.dir(data) - if (data === cfg.arduino.cmd.connect - || data === cfg.arduino.cmd.proj_identifier - || data === cfg.arduino.cmd.cam_identifier - || data === cfg.arduino.cmd.light_identifier - || data === cfg.arduino.cmd.proj_light_identifier - || data === cfg.arduino.cmd.proj_cam_light_identifier - || data === cfg.arduino.cmd.proj_cam_identifier ) { - arduino.confirmExec(null, data); - arduino.confirmExec = {}; - } -} - -arduino.verify = async function () { - return new Promise(async (resolve, reject) => { - const device = arduino.alias['connect'] - let writeSuccess - arduino.confirmExec = function (err, data) { - if (data === cfg.arduino.cmd.connect) { - return resolve(true) - } else { - return reject('Wrong data returned') - } - } - await delay(cfg.arduino.serialDelay) - try { - writeSuccess = await send(device, cfg.arduino.cmd.connect) - } catch (e) { - return reject(e) - } - return resolve(writeSuccess) - }) -} - -arduino.distinguish = async function () { - return new Promise(async (resolve, reject) => { - const device = arduino.alias['connect'] - let writeSuccess - let type - arduino.confirmExec = function (err, data) { - if (data === cfg.arduino.cmd.proj_identifier) { - type = 'projector' - } else if (data === cfg.arduino.cmd.cam_identifier) { - type = 'camera' - } else if (data === cfg.arduino.cmd.light_identifier) { - type = 'light' - } else if (data === cfg.arduino.cmd.proj_light_identifier) { - type = 'projector,light' - } else if (data === cfg.arduino.cmd.proj_cam_light_identifier) { - type = 'projector,camera,light' - } else if (data === cfg.arduino.cmd.proj_cam_identifier) { - type = 'projector,camera' - } else if (data === cfg.ardino.cmd.proj_second_identifier) { - type = 'projector_second' - } - return resolve(type) - } - await delay(cfg.arduino.serialDelay) - try { - writeSuccess = await send(device, cfg.arduino.cmd.mcopy_identifier) - } catch (e) { - console.error(e) - return reject(e) - } - }) -} - -arduino.close = async function (callback) { - const device = arduino.alias['connect'] - let closeSuccess - try { - closeSuccess = await close(device) - } catch (e) { - return console.error(e) - } - return closeSuccess -}; - -arduino.fakeConnect = async function (serial) { - //console.log('Connecting to fake arduino...'); - const device = '/dev/fake' - arduino.alias[serial] = device - arduino.serial[device] = { - write : function (cmd, cb) { - const t = { - c : cfg.arduino.cam.time + cfg.arduino.cam.delay, - p : cfg.arduino.proj.time + cfg.arduino.proj.delay - } - let timeout = t[cmd] - let end - if (typeof timeout === 'undefined') timeout = 10 - arduino.timer = +new Date() - setTimeout(() => { - arduino.end(serial, cmd) - return cb() - }, timeout) - - }, - string : async function (str) { - //do nothing - return true - }, - fake : true - }; - //console.log('Connected to fake arduino! Not real! Doesn\'t exist!'); - return true -} - if (typeof module !== 'undefined' && module.parent) { - module.exports = function (c, ee) { - eventEmitter = ee - cfg = c - return arduino - } -} \ No newline at end of file + module.exports = function (c, ee) { + eventEmitter = ee; + cfg = c; + arduino = new Arduino(); + return arduino; + }; +} +//# sourceMappingURL=index.js.map \ No newline at end of file