2018-01-02 06:15:29 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const SerialPort = require('serialport')
|
2018-01-23 16:35:39 +00:00
|
|
|
const Readline = SerialPort.parsers.Readline
|
2018-01-02 06:15:29 +00:00
|
|
|
const exec = require('child_process').exec
|
2018-01-23 16:35:39 +00:00
|
|
|
const parser = new Readline('')
|
|
|
|
const newlineRe = new RegExp('\n', 'g')
|
2018-01-23 16:41:42 +00:00
|
|
|
const returnRe = new RegExp('\r', 'g')
|
2018-01-02 06:15:29 +00:00
|
|
|
let eventEmitter
|
|
|
|
|
2019-02-27 02:55:52 +00:00
|
|
|
let cfg
|
2016-04-12 03:05:05 +00:00
|
|
|
|
2019-02-22 21:31:53 +00:00
|
|
|
/**
|
|
|
|
* Pause the process for X milliseconds in async/await functions
|
|
|
|
*
|
|
|
|
* @param {integer} ms milliseconds
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after wait
|
|
|
|
**/
|
2018-03-03 03:42:15 +00:00
|
|
|
async function delay (ms) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
return setTimeout(resolve, ms)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:31:53 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
**/
|
2018-03-03 03:42:15 +00:00
|
|
|
async function send (device, cmd) {
|
|
|
|
return new Promise ((resolve, reject) => {
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.queue[cmd] = (ms) => {
|
2018-03-03 05:27:49 +00:00
|
|
|
return resolve(ms)
|
|
|
|
}
|
2019-02-27 02:55:52 +00:00
|
|
|
return arduino.serial[device].write(cmd, (err, results) => {
|
2018-03-03 03:42:15 +00:00
|
|
|
if (err) {
|
|
|
|
//console.error(err)
|
|
|
|
return reject(err)
|
|
|
|
}
|
2018-03-03 05:27:49 +00:00
|
|
|
//
|
2018-03-03 03:42:15 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:31:53 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
**/
|
2018-03-03 03:42:15 +00:00
|
|
|
async function write (device, str) {
|
|
|
|
return new Promise ((resolve, reject) => {
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.serial[device].write(str, function (err, results) {
|
2018-03-03 03:42:15 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
//console.log('sent: ' + str)
|
|
|
|
return resolve(results)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:31:53 +00:00
|
|
|
/**
|
|
|
|
* Connect to an Arduino using async/await
|
|
|
|
*
|
|
|
|
* @param {string} device Arduino identifier
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after opening
|
|
|
|
**/
|
2018-03-03 03:42:15 +00:00
|
|
|
async function open (device) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-02-27 02:55:52 +00:00
|
|
|
return arduino.serial[device].open(error => {
|
2018-03-03 03:42:15 +00:00
|
|
|
if (error) {
|
|
|
|
return reject(error)
|
|
|
|
}
|
|
|
|
return resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:31:53 +00:00
|
|
|
/**
|
|
|
|
* Close a connection to an Arduino using async/await
|
|
|
|
*
|
|
|
|
* @param {string} device Arduino identifier
|
|
|
|
*
|
|
|
|
* @returns {Promise} Resolves after closing
|
|
|
|
**/
|
2018-03-03 03:42:15 +00:00
|
|
|
async function close (device) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2019-02-27 02:55:52 +00:00
|
|
|
return arduino.serial[device].close((err) => {
|
2018-03-03 03:42:15 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
return resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-12 03:05:05 +00:00
|
|
|
/******
|
|
|
|
Arduino handlers
|
|
|
|
*******/
|
2019-02-27 02:55:52 +00:00
|
|
|
const arduino = {
|
2016-04-19 01:26:45 +00:00
|
|
|
path : {},
|
2016-04-12 03:05:05 +00:00
|
|
|
known: [
|
|
|
|
'/dev/tty.usbmodem1a161',
|
|
|
|
'/dev/tty.usbserial-A800f8dk',
|
|
|
|
'/dev/tty.usbserial-A900cebm',
|
|
|
|
'/dev/tty.usbmodem1a131',
|
|
|
|
'/dev/tty.usbserial-a900f6de',
|
2018-01-02 06:15:29 +00:00
|
|
|
'/dev/tty.usbmodem1a141',
|
2018-01-30 09:07:52 +00:00
|
|
|
'/dev/ttyACM0',
|
|
|
|
'COM3'
|
2016-04-12 03:05:05 +00:00
|
|
|
],
|
2018-01-02 06:15:29 +00:00
|
|
|
alias : {
|
|
|
|
|
|
|
|
},
|
2016-04-19 01:26:45 +00:00
|
|
|
serial : {
|
|
|
|
connect : {},
|
|
|
|
projector : {},
|
2018-01-02 06:15:29 +00:00
|
|
|
camera : {},
|
|
|
|
light : {}
|
2016-04-19 01:26:45 +00:00
|
|
|
},
|
2016-04-12 03:05:05 +00:00
|
|
|
baud : 57600,
|
|
|
|
queue : {},
|
|
|
|
timer : 0,
|
2019-02-27 02:55:52 +00:00
|
|
|
lock : false,
|
|
|
|
locks : {
|
|
|
|
|
|
|
|
}
|
2018-01-02 06:15:29 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.enumerate = async function () {
|
2018-03-03 03:42:15 +00:00
|
|
|
return new Promise( (resolve, reject) => {
|
|
|
|
return SerialPort.list((err, ports) => {
|
|
|
|
let matches = []
|
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
ports.forEach(port => {
|
2019-02-27 02:55:52 +00:00
|
|
|
if (arduino.known.indexOf(port.comName) !== -1) {
|
2018-03-03 03:42:15 +00:00
|
|
|
matches.push(port.comName)
|
|
|
|
} else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
|
|
|
matches.push(port.comName)
|
2019-03-01 23:01:16 +00:00
|
|
|
} else if ((port.comName + '').toLowerCase().indexOf('usbserial') !== -1) {
|
|
|
|
matches.push(port.comName)
|
|
|
|
} else if ((port.comName + '').toLowerCase().indexOf('usbmodem') !== -1) {
|
|
|
|
matches.push(port.comName)
|
2018-03-03 03:42:15 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if (matches.length === 0) {
|
|
|
|
return reject('No USB devices found');
|
|
|
|
} else if (matches.length > 0) {
|
|
|
|
return resolve(matches)
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
2018-01-02 06:15:29 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-12 03:05:05 +00:00
|
|
|
//commands which respond to a sent char
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.send = async function (serial, cmd, res) {
|
|
|
|
const device = arduino.alias[serial]
|
2018-03-03 03:42:15 +00:00
|
|
|
let results
|
2019-02-27 02:55:52 +00:00
|
|
|
if (arduino.locks[serial]) {
|
2018-03-03 03:42:15 +00:00
|
|
|
return false
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.locks[serial] = true
|
|
|
|
await delay(cfg.arduino.serialDelay)
|
2018-03-03 03:42:15 +00:00
|
|
|
try {
|
|
|
|
results = await send(device, cmd)
|
|
|
|
} catch (e) {
|
|
|
|
return console.error(e)
|
|
|
|
}
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.locks[serial] = false
|
|
|
|
arduino.timer = new Date().getTime()
|
2018-03-03 03:42:15 +00:00
|
|
|
return await eventEmitter.emit('arduino_send', cmd)
|
|
|
|
}
|
|
|
|
|
2016-04-12 03:05:05 +00:00
|
|
|
//send strings, after char triggers firmware to accept
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.string = async function (serial, str) {
|
|
|
|
const device = arduino.alias[serial]
|
2018-03-03 03:42:15 +00:00
|
|
|
let writeSuccess
|
2019-02-27 02:55:52 +00:00
|
|
|
await delay(cfg.arduino.serialDelay)
|
|
|
|
if (typeof arduino.serial[device].fake !== 'undefined'
|
|
|
|
&& arduino.serial[device].fake) {
|
|
|
|
return arduino.serial[device].string(str)
|
2018-03-03 03:42:15 +00:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
writeSuccess = await write(device, str)
|
|
|
|
} catch (e) {
|
|
|
|
return console.error(e)
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2018-03-03 03:42:15 +00:00
|
|
|
return writeSuccess
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:26:45 +00:00
|
|
|
//respond with same char over serial when done
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.end = async function (serial, data) {
|
2018-03-03 05:27:49 +00:00
|
|
|
const end = new Date().getTime()
|
2019-02-27 02:55:52 +00:00
|
|
|
const ms = end - arduino.timer
|
2018-03-03 05:27:49 +00:00
|
|
|
let complete
|
2019-02-27 02:55:52 +00:00
|
|
|
if (arduino.queue[data] !== undefined) {
|
|
|
|
arduino.locks[serial] = false;
|
2016-04-19 01:52:38 +00:00
|
|
|
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
2019-02-27 02:55:52 +00:00
|
|
|
complete = arduino.queue[data](ms) //execute callback
|
2018-03-03 03:42:15 +00:00
|
|
|
eventEmitter.emit('arduino_end', data)
|
2019-02-27 02:55:52 +00:00
|
|
|
delete arduino.queue[data]
|
2016-04-12 03:05:05 +00:00
|
|
|
} else {
|
2018-03-03 03:42:15 +00:00
|
|
|
//console.log('Received stray "' + data + '"'); //silent to user
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
2018-03-03 05:27:49 +00:00
|
|
|
return complete
|
2016-04-12 03:05:05 +00:00
|
|
|
};
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.alias = function (serial, device) {
|
2018-01-02 06:15:29 +00:00
|
|
|
console.log(`Making "${serial}" an alias of ${device}`)
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.alias[serial] = device
|
2018-01-02 06:15:29 +00:00
|
|
|
}
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.connect = async function (serial, device, confirm) {
|
2018-03-03 03:42:15 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
let connectSuccess
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.path[serial] = device;
|
|
|
|
arduino.alias[serial] = device;
|
|
|
|
arduino.serial[device] = new SerialPort(arduino.path[serial], {
|
2018-03-03 03:42:15 +00:00
|
|
|
autoOpen : false,
|
2019-02-27 02:55:52 +00:00
|
|
|
baudRate: cfg.arduino.baud,
|
2018-03-03 03:42:15 +00:00
|
|
|
parser: parser
|
|
|
|
})
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.locks[device] = false
|
2018-03-03 03:42:15 +00:00
|
|
|
try {
|
|
|
|
connectSuccess = await open(device)
|
|
|
|
} catch (e) {
|
|
|
|
console.error('failed to open: ' + e)
|
|
|
|
return reject(e)
|
|
|
|
}
|
2019-02-27 02:55:52 +00:00
|
|
|
console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`);
|
2018-03-03 03:42:15 +00:00
|
|
|
if (!confirm) {
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.serial[device].on('data', async (data) => {
|
2018-03-03 03:42:15 +00:00
|
|
|
let d = data.toString('utf8')
|
|
|
|
d = d.replace(newlineRe, '').replace(returnRe, '')
|
2019-02-27 02:55:52 +00:00
|
|
|
return await arduino.end(serial, d)
|
2018-03-03 03:42:15 +00:00
|
|
|
})
|
2016-04-12 03:05:05 +00:00
|
|
|
} else {
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.serial[device].on('data', async (data) => {
|
2018-03-03 03:42:15 +00:00
|
|
|
let d = data.toString('utf8')
|
|
|
|
d = d.replace(newlineRe, '').replace(returnRe, '')
|
2019-02-27 02:55:52 +00:00
|
|
|
return await arduino.confirmEnd(d)
|
2018-03-03 03:42:15 +00:00
|
|
|
})
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
2019-02-27 02:55:52 +00:00
|
|
|
return resolve(arduino.path[serial])
|
2018-03-03 03:42:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2016-04-12 03:05:05 +00:00
|
|
|
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.confirmExec = {};
|
|
|
|
arduino.confirmEnd = function (data) {
|
2018-01-23 16:41:42 +00:00
|
|
|
//console.dir(data)
|
2019-02-27 02:55:52 +00:00
|
|
|
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 = {};
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2018-03-03 03:42:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.verify = async function () {
|
2018-03-03 03:42:15 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2019-02-27 02:55:52 +00:00
|
|
|
const device = arduino.alias['connect']
|
2018-03-03 03:42:15 +00:00
|
|
|
let writeSuccess
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.confirmExec = function (err, data) {
|
|
|
|
if (data === cfg.arduino.cmd.connect) {
|
2018-03-03 03:42:15 +00:00
|
|
|
return resolve(true)
|
|
|
|
} else {
|
|
|
|
return reject('Wrong data returned')
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2018-03-03 03:42:15 +00:00
|
|
|
}
|
2019-02-27 02:55:52 +00:00
|
|
|
await delay(cfg.arduino.serialDelay)
|
2018-03-03 03:42:15 +00:00
|
|
|
try {
|
2019-02-27 02:55:52 +00:00
|
|
|
writeSuccess = await send(device, cfg.arduino.cmd.connect)
|
2018-03-03 03:42:15 +00:00
|
|
|
} catch (e) {
|
|
|
|
return reject(e)
|
|
|
|
}
|
|
|
|
return resolve(writeSuccess)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.distinguish = async function () {
|
2018-03-03 05:27:49 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2019-02-27 02:55:52 +00:00
|
|
|
const device = arduino.alias['connect']
|
2018-03-03 05:27:49 +00:00
|
|
|
let writeSuccess
|
|
|
|
let type
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.confirmExec = function (err, data) {
|
|
|
|
if (data === cfg.arduino.cmd.proj_identifier) {
|
2018-03-03 05:27:49 +00:00
|
|
|
type = 'projector'
|
2019-02-27 02:55:52 +00:00
|
|
|
} else if (data === cfg.arduino.cmd.cam_identifier) {
|
2018-03-03 05:27:49 +00:00
|
|
|
type = 'camera'
|
2019-02-27 02:55:52 +00:00
|
|
|
} else if (data === cfg.arduino.cmd.light_identifier) {
|
2018-03-03 05:27:49 +00:00
|
|
|
type = 'light'
|
2019-02-27 02:55:52 +00:00
|
|
|
} else if (data === cfg.arduino.cmd.proj_light_identifier) {
|
2018-03-03 05:27:49 +00:00
|
|
|
type = 'projector,light'
|
2019-02-27 02:55:52 +00:00
|
|
|
} else if (data === cfg.arduino.cmd.proj_cam_light_identifier) {
|
2018-03-03 05:27:49 +00:00
|
|
|
type = 'projector,camera,light'
|
2019-02-27 02:55:52 +00:00
|
|
|
} else if (data === cfg.arduino.cmd.proj_cam_identifier) {
|
2018-03-03 05:27:49 +00:00
|
|
|
type = 'projector,camera'
|
2019-02-27 02:55:52 +00:00
|
|
|
} else if (data === cfg.ardino.cmd.proj_second_identifier) {
|
2019-02-25 23:14:19 +00:00
|
|
|
type = 'projector_second'
|
2018-03-03 05:27:49 +00:00
|
|
|
}
|
|
|
|
return resolve(type)
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2019-02-27 02:55:52 +00:00
|
|
|
await delay(cfg.arduino.serialDelay)
|
2018-03-03 05:27:49 +00:00
|
|
|
try {
|
2019-02-27 02:55:52 +00:00
|
|
|
writeSuccess = await send(device, cfg.arduino.cmd.mcopy_identifier)
|
2018-03-03 05:27:49 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
return reject(e)
|
|
|
|
}
|
|
|
|
})
|
2018-01-02 06:15:29 +00:00
|
|
|
}
|
2016-04-19 01:26:45 +00:00
|
|
|
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.close = async function (callback) {
|
|
|
|
const device = arduino.alias['connect']
|
2018-03-03 03:42:15 +00:00
|
|
|
let closeSuccess
|
|
|
|
try {
|
|
|
|
closeSuccess = await close(device)
|
|
|
|
} catch (e) {
|
|
|
|
return console.error(e)
|
|
|
|
}
|
|
|
|
return closeSuccess
|
2016-04-19 01:26:45 +00:00
|
|
|
};
|
|
|
|
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.fakeConnect = async function (serial) {
|
2016-04-19 01:52:38 +00:00
|
|
|
//console.log('Connecting to fake arduino...');
|
2018-01-02 06:15:29 +00:00
|
|
|
const device = '/dev/fake'
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.alias[serial] = device
|
|
|
|
arduino.serial[device] = {
|
2018-03-03 05:27:49 +00:00
|
|
|
write : function (cmd, cb) {
|
|
|
|
const t = {
|
2019-02-27 02:55:52 +00:00
|
|
|
c : cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
|
|
|
p : cfg.arduino.proj.time + cfg.arduino.proj.delay
|
2018-03-03 05:27:49 +00:00
|
|
|
}
|
|
|
|
let timeout = t[cmd]
|
|
|
|
let end
|
|
|
|
if (typeof timeout === 'undefined') timeout = 10
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.timer = +new Date()
|
2018-03-05 03:35:21 +00:00
|
|
|
setTimeout(() => {
|
2019-02-27 02:55:52 +00:00
|
|
|
arduino.end(serial, cmd)
|
2018-03-03 05:27:49 +00:00
|
|
|
return cb()
|
|
|
|
}, timeout)
|
2018-03-05 03:35:21 +00:00
|
|
|
|
2016-04-14 04:17:42 +00:00
|
|
|
},
|
2018-03-03 03:42:15 +00:00
|
|
|
string : async function (str) {
|
2016-04-14 04:17:42 +00:00
|
|
|
//do nothing
|
2018-01-02 06:15:29 +00:00
|
|
|
return true
|
2016-04-19 01:26:45 +00:00
|
|
|
},
|
|
|
|
fake : true
|
2016-04-14 04:17:42 +00:00
|
|
|
};
|
2016-04-19 01:52:38 +00:00
|
|
|
//console.log('Connected to fake arduino! Not real! Doesn\'t exist!');
|
2018-03-03 03:42:15 +00:00
|
|
|
return true
|
2018-01-02 06:15:29 +00:00
|
|
|
}
|
2016-04-14 04:17:42 +00:00
|
|
|
|
2016-04-12 06:19:35 +00:00
|
|
|
if (typeof module !== 'undefined' && module.parent) {
|
2019-02-27 02:55:52 +00:00
|
|
|
module.exports = function (c, ee) {
|
2018-01-02 06:15:29 +00:00
|
|
|
eventEmitter = ee
|
2019-02-27 02:55:52 +00:00
|
|
|
cfg = c
|
|
|
|
return arduino
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
|
|
|
}
|