Refactor arduino module to not use the mcopy. prefix. This should really be a class, not an object. We'll get there.
This commit is contained in:
parent
b481906c86
commit
a5a9a4ea86
|
@ -8,7 +8,7 @@ const newlineRe = new RegExp('\n', 'g')
|
||||||
const returnRe = new RegExp('\r', 'g')
|
const returnRe = new RegExp('\r', 'g')
|
||||||
let eventEmitter
|
let eventEmitter
|
||||||
|
|
||||||
const mcopy = {}
|
let cfg
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pause the process for X milliseconds in async/await functions
|
* Pause the process for X milliseconds in async/await functions
|
||||||
|
@ -33,10 +33,10 @@ async function delay (ms) {
|
||||||
**/
|
**/
|
||||||
async function send (device, cmd) {
|
async function send (device, cmd) {
|
||||||
return new Promise ((resolve, reject) => {
|
return new Promise ((resolve, reject) => {
|
||||||
mcopy.arduino.queue[cmd] = (ms) => {
|
arduino.queue[cmd] = (ms) => {
|
||||||
return resolve(ms)
|
return resolve(ms)
|
||||||
}
|
}
|
||||||
return mcopy.arduino.serial[device].write(cmd, (err, results) => {
|
return arduino.serial[device].write(cmd, (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
//console.error(err)
|
//console.error(err)
|
||||||
return reject(err)
|
return reject(err)
|
||||||
|
@ -56,7 +56,7 @@ async function send (device, cmd) {
|
||||||
**/
|
**/
|
||||||
async function write (device, str) {
|
async function write (device, str) {
|
||||||
return new Promise ((resolve, reject) => {
|
return new Promise ((resolve, reject) => {
|
||||||
mcopy.arduino.serial[device].write(str, function (err, results) {
|
arduino.serial[device].write(str, function (err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ async function write (device, str) {
|
||||||
**/
|
**/
|
||||||
async function open (device) {
|
async function open (device) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
return mcopy.arduino.serial[device].open(error => {
|
return arduino.serial[device].open(error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
return reject(error)
|
return reject(error)
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ async function open (device) {
|
||||||
**/
|
**/
|
||||||
async function close (device) {
|
async function close (device) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
return mcopy.arduino.serial[device].close((err) => {
|
return arduino.serial[device].close((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ async function close (device) {
|
||||||
/******
|
/******
|
||||||
Arduino handlers
|
Arduino handlers
|
||||||
*******/
|
*******/
|
||||||
mcopy.arduino = {
|
const arduino = {
|
||||||
path : {},
|
path : {},
|
||||||
known: [
|
known: [
|
||||||
'/dev/tty.usbmodem1a161',
|
'/dev/tty.usbmodem1a161',
|
||||||
|
@ -129,10 +129,13 @@ mcopy.arduino = {
|
||||||
baud : 57600,
|
baud : 57600,
|
||||||
queue : {},
|
queue : {},
|
||||||
timer : 0,
|
timer : 0,
|
||||||
lock : false
|
lock : false,
|
||||||
|
locks : {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.enumerate = async function () {
|
arduino.enumerate = async function () {
|
||||||
return new Promise( (resolve, reject) => {
|
return new Promise( (resolve, reject) => {
|
||||||
return SerialPort.list((err, ports) => {
|
return SerialPort.list((err, ports) => {
|
||||||
let matches = []
|
let matches = []
|
||||||
|
@ -140,7 +143,7 @@ mcopy.arduino.enumerate = async function () {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
ports.forEach(port => {
|
ports.forEach(port => {
|
||||||
if (mcopy.arduino.known.indexOf(port.comName) !== -1) {
|
if (arduino.known.indexOf(port.comName) !== -1) {
|
||||||
matches.push(port.comName)
|
matches.push(port.comName)
|
||||||
} else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
} else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
||||||
matches.push(port.comName)
|
matches.push(port.comName)
|
||||||
|
@ -156,32 +159,32 @@ mcopy.arduino.enumerate = async function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
//commands which respond to a sent char
|
//commands which respond to a sent char
|
||||||
mcopy.arduino.send = async function (serial, cmd, res) {
|
arduino.send = async function (serial, cmd, res) {
|
||||||
const device = mcopy.arduino.alias[serial]
|
const device = arduino.alias[serial]
|
||||||
let results
|
let results
|
||||||
if (mcopy.arduino.lock) {
|
if (arduino.locks[serial]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
mcopy.arduino.lock = true
|
arduino.locks[serial] = true
|
||||||
await delay(mcopy.cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
try {
|
try {
|
||||||
results = await send(device, cmd)
|
results = await send(device, cmd)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return console.error(e)
|
return console.error(e)
|
||||||
}
|
}
|
||||||
mcopy.arduino.lock = false
|
arduino.locks[serial] = false
|
||||||
mcopy.arduino.timer = new Date().getTime()
|
arduino.timer = new Date().getTime()
|
||||||
return await eventEmitter.emit('arduino_send', cmd)
|
return await eventEmitter.emit('arduino_send', cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
//send strings, after char triggers firmware to accept
|
//send strings, after char triggers firmware to accept
|
||||||
mcopy.arduino.string = async function (serial, str) {
|
arduino.string = async function (serial, str) {
|
||||||
const device = mcopy.arduino.alias[serial]
|
const device = arduino.alias[serial]
|
||||||
let writeSuccess
|
let writeSuccess
|
||||||
await delay(mcopy.cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
if (typeof mcopy.arduino.serial[device].fake !== 'undefined'
|
if (typeof arduino.serial[device].fake !== 'undefined'
|
||||||
&& mcopy.arduino.serial[device].fake) {
|
&& arduino.serial[device].fake) {
|
||||||
return mcopy.arduino.serial[device].string(str)
|
return arduino.serial[device].string(str)
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
writeSuccess = await write(device, str)
|
writeSuccess = await write(device, str)
|
||||||
|
@ -193,89 +196,90 @@ mcopy.arduino.string = async function (serial, str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//respond with same char over serial when done
|
//respond with same char over serial when done
|
||||||
mcopy.arduino.end = async function (data) {
|
arduino.end = async function (serial, data) {
|
||||||
const end = new Date().getTime()
|
const end = new Date().getTime()
|
||||||
const ms = end - mcopy.arduino.timer
|
const ms = end - arduino.timer
|
||||||
let complete
|
let complete
|
||||||
if (mcopy.arduino.queue[data] !== undefined) {
|
if (arduino.queue[data] !== undefined) {
|
||||||
mcopy.arduino.lock = false;
|
arduino.locks[serial] = false;
|
||||||
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
||||||
complete = mcopy.arduino.queue[data](ms) //execute callback
|
complete = arduino.queue[data](ms) //execute callback
|
||||||
eventEmitter.emit('arduino_end', data)
|
eventEmitter.emit('arduino_end', data)
|
||||||
delete mcopy.arduino.queue[data]
|
delete arduino.queue[data]
|
||||||
} else {
|
} else {
|
||||||
//console.log('Received stray "' + data + '"'); //silent to user
|
//console.log('Received stray "' + data + '"'); //silent to user
|
||||||
}
|
}
|
||||||
return complete
|
return complete
|
||||||
};
|
};
|
||||||
mcopy.arduino.alias = function (serial, device) {
|
arduino.alias = function (serial, device) {
|
||||||
console.log(`Making "${serial}" an alias of ${device}`)
|
console.log(`Making "${serial}" an alias of ${device}`)
|
||||||
mcopy.arduino.alias[serial] = device
|
arduino.alias[serial] = device
|
||||||
}
|
}
|
||||||
mcopy.arduino.connect = async function (serial, device, confirm) {
|
arduino.connect = async function (serial, device, confirm) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let connectSuccess
|
let connectSuccess
|
||||||
mcopy.arduino.path[serial] = device;
|
arduino.path[serial] = device;
|
||||||
mcopy.arduino.alias[serial] = device;
|
arduino.alias[serial] = device;
|
||||||
mcopy.arduino.serial[device] = new SerialPort(mcopy.arduino.path[serial], {
|
arduino.serial[device] = new SerialPort(arduino.path[serial], {
|
||||||
autoOpen : false,
|
autoOpen : false,
|
||||||
baudRate: mcopy.cfg.arduino.baud,
|
baudRate: cfg.arduino.baud,
|
||||||
parser: parser
|
parser: parser
|
||||||
})
|
})
|
||||||
|
arduino.locks[device] = false
|
||||||
try {
|
try {
|
||||||
connectSuccess = await open(device)
|
connectSuccess = await open(device)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('failed to open: ' + e)
|
console.error('failed to open: ' + e)
|
||||||
return reject(e)
|
return reject(e)
|
||||||
}
|
}
|
||||||
console.log(`Opened connection with ${mcopy.arduino.path[serial]} as ${serial}`);
|
console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`);
|
||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
mcopy.arduino.serial[device].on('data', async (data) => {
|
arduino.serial[device].on('data', async (data) => {
|
||||||
let d = data.toString('utf8')
|
let d = data.toString('utf8')
|
||||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
d = d.replace(newlineRe, '').replace(returnRe, '')
|
||||||
return await mcopy.arduino.end(d)
|
return await arduino.end(serial, d)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
mcopy.arduino.serial[device].on('data', async (data) => {
|
arduino.serial[device].on('data', async (data) => {
|
||||||
let d = data.toString('utf8')
|
let d = data.toString('utf8')
|
||||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
d = d.replace(newlineRe, '').replace(returnRe, '')
|
||||||
return await mcopy.arduino.confirmEnd(d)
|
return await arduino.confirmEnd(d)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return resolve(mcopy.arduino.path[serial])
|
return resolve(arduino.path[serial])
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.confirmExec = {};
|
arduino.confirmExec = {};
|
||||||
mcopy.arduino.confirmEnd = function (data) {
|
arduino.confirmEnd = function (data) {
|
||||||
//console.dir(data)
|
//console.dir(data)
|
||||||
if (data === mcopy.cfg.arduino.cmd.connect
|
if (data === cfg.arduino.cmd.connect
|
||||||
|| data === mcopy.cfg.arduino.cmd.proj_identifier
|
|| data === cfg.arduino.cmd.proj_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.cam_identifier
|
|| data === cfg.arduino.cmd.cam_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.light_identifier
|
|| data === cfg.arduino.cmd.light_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.proj_light_identifier
|
|| data === cfg.arduino.cmd.proj_light_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.proj_cam_light_identifier
|
|| data === cfg.arduino.cmd.proj_cam_light_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.proj_cam_identifier ) {
|
|| data === cfg.arduino.cmd.proj_cam_identifier ) {
|
||||||
mcopy.arduino.confirmExec(null, data);
|
arduino.confirmExec(null, data);
|
||||||
mcopy.arduino.confirmExec = {};
|
arduino.confirmExec = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.verify = async function () {
|
arduino.verify = async function () {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const device = mcopy.arduino.alias['connect']
|
const device = arduino.alias['connect']
|
||||||
let writeSuccess
|
let writeSuccess
|
||||||
mcopy.arduino.confirmExec = function (err, data) {
|
arduino.confirmExec = function (err, data) {
|
||||||
if (data === mcopy.cfg.arduino.cmd.connect) {
|
if (data === cfg.arduino.cmd.connect) {
|
||||||
return resolve(true)
|
return resolve(true)
|
||||||
} else {
|
} else {
|
||||||
return reject('Wrong data returned')
|
return reject('Wrong data returned')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await delay(mcopy.cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
try {
|
try {
|
||||||
writeSuccess = await send(device, mcopy.cfg.arduino.cmd.connect)
|
writeSuccess = await send(device, cfg.arduino.cmd.connect)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return reject(e)
|
return reject(e)
|
||||||
}
|
}
|
||||||
|
@ -283,32 +287,32 @@ mcopy.arduino.verify = async function () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.distinguish = async function () {
|
arduino.distinguish = async function () {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const device = mcopy.arduino.alias['connect']
|
const device = arduino.alias['connect']
|
||||||
let writeSuccess
|
let writeSuccess
|
||||||
let type
|
let type
|
||||||
mcopy.arduino.confirmExec = function (err, data) {
|
arduino.confirmExec = function (err, data) {
|
||||||
if (data === mcopy.cfg.arduino.cmd.proj_identifier) {
|
if (data === cfg.arduino.cmd.proj_identifier) {
|
||||||
type = 'projector'
|
type = 'projector'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.cam_identifier) {
|
} else if (data === cfg.arduino.cmd.cam_identifier) {
|
||||||
type = 'camera'
|
type = 'camera'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.light_identifier) {
|
} else if (data === cfg.arduino.cmd.light_identifier) {
|
||||||
type = 'light'
|
type = 'light'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.proj_light_identifier) {
|
} else if (data === cfg.arduino.cmd.proj_light_identifier) {
|
||||||
type = 'projector,light'
|
type = 'projector,light'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.proj_cam_light_identifier) {
|
} else if (data === cfg.arduino.cmd.proj_cam_light_identifier) {
|
||||||
type = 'projector,camera,light'
|
type = 'projector,camera,light'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.proj_cam_identifier) {
|
} else if (data === cfg.arduino.cmd.proj_cam_identifier) {
|
||||||
type = 'projector,camera'
|
type = 'projector,camera'
|
||||||
} else if (data === mcopy.cfg.ardino.cmd.proj_second_identifier) {
|
} else if (data === cfg.ardino.cmd.proj_second_identifier) {
|
||||||
type = 'projector_second'
|
type = 'projector_second'
|
||||||
}
|
}
|
||||||
return resolve(type)
|
return resolve(type)
|
||||||
}
|
}
|
||||||
await delay(mcopy.cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
try {
|
try {
|
||||||
writeSuccess = await send(device, mcopy.cfg.arduino.cmd.mcopy_identifier)
|
writeSuccess = await send(device, cfg.arduino.cmd.mcopy_identifier)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
return reject(e)
|
return reject(e)
|
||||||
|
@ -316,8 +320,8 @@ mcopy.arduino.distinguish = async function () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.close = async function (callback) {
|
arduino.close = async function (callback) {
|
||||||
const device = mcopy.arduino.alias['connect']
|
const device = arduino.alias['connect']
|
||||||
let closeSuccess
|
let closeSuccess
|
||||||
try {
|
try {
|
||||||
closeSuccess = await close(device)
|
closeSuccess = await close(device)
|
||||||
|
@ -327,22 +331,22 @@ mcopy.arduino.close = async function (callback) {
|
||||||
return closeSuccess
|
return closeSuccess
|
||||||
};
|
};
|
||||||
|
|
||||||
mcopy.arduino.fakeConnect = async function (serial) {
|
arduino.fakeConnect = async function (serial) {
|
||||||
//console.log('Connecting to fake arduino...');
|
//console.log('Connecting to fake arduino...');
|
||||||
const device = '/dev/fake'
|
const device = '/dev/fake'
|
||||||
mcopy.arduino.alias[serial] = device
|
arduino.alias[serial] = device
|
||||||
mcopy.arduino.serial[device] = {
|
arduino.serial[device] = {
|
||||||
write : function (cmd, cb) {
|
write : function (cmd, cb) {
|
||||||
const t = {
|
const t = {
|
||||||
c : mcopy.cfg.arduino.cam.time + mcopy.cfg.arduino.cam.delay,
|
c : cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
||||||
p : mcopy.cfg.arduino.proj.time + mcopy.cfg.arduino.proj.delay
|
p : cfg.arduino.proj.time + cfg.arduino.proj.delay
|
||||||
}
|
}
|
||||||
let timeout = t[cmd]
|
let timeout = t[cmd]
|
||||||
let end
|
let end
|
||||||
if (typeof timeout === 'undefined') timeout = 10
|
if (typeof timeout === 'undefined') timeout = 10
|
||||||
mcopy.arduino.timer = +new Date()
|
arduino.timer = +new Date()
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
mcopy.arduino.end(cmd)
|
arduino.end(serial, cmd)
|
||||||
return cb()
|
return cb()
|
||||||
}, timeout)
|
}, timeout)
|
||||||
|
|
||||||
|
@ -358,9 +362,9 @@ mcopy.arduino.fakeConnect = async function (serial) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof module !== 'undefined' && module.parent) {
|
if (typeof module !== 'undefined' && module.parent) {
|
||||||
module.exports = function (cfg, ee) {
|
module.exports = function (c, ee) {
|
||||||
eventEmitter = ee
|
eventEmitter = ee
|
||||||
mcopy.cfg = cfg
|
cfg = c
|
||||||
return mcopy.arduino
|
return arduino
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -8,7 +8,7 @@ const newlineRe = new RegExp('\n', 'g')
|
||||||
const returnRe = new RegExp('\r', 'g')
|
const returnRe = new RegExp('\r', 'g')
|
||||||
let eventEmitter
|
let eventEmitter
|
||||||
|
|
||||||
const mcopy = {}
|
let cfg
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pause the process for X milliseconds in async/await functions
|
* Pause the process for X milliseconds in async/await functions
|
||||||
|
@ -33,10 +33,10 @@ async function delay (ms) {
|
||||||
**/
|
**/
|
||||||
async function send (device, cmd) {
|
async function send (device, cmd) {
|
||||||
return new Promise ((resolve, reject) => {
|
return new Promise ((resolve, reject) => {
|
||||||
mcopy.arduino.queue[cmd] = (ms) => {
|
arduino.queue[cmd] = (ms) => {
|
||||||
return resolve(ms)
|
return resolve(ms)
|
||||||
}
|
}
|
||||||
return mcopy.arduino.serial[device].write(cmd, (err, results) => {
|
return arduino.serial[device].write(cmd, (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
//console.error(err)
|
//console.error(err)
|
||||||
return reject(err)
|
return reject(err)
|
||||||
|
@ -56,7 +56,7 @@ async function send (device, cmd) {
|
||||||
**/
|
**/
|
||||||
async function write (device, str) {
|
async function write (device, str) {
|
||||||
return new Promise ((resolve, reject) => {
|
return new Promise ((resolve, reject) => {
|
||||||
mcopy.arduino.serial[device].write(str, function (err, results) {
|
arduino.serial[device].write(str, function (err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
@ -75,7 +75,7 @@ async function write (device, str) {
|
||||||
**/
|
**/
|
||||||
async function open (device) {
|
async function open (device) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
return mcopy.arduino.serial[device].open(error => {
|
return arduino.serial[device].open(error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
return reject(error)
|
return reject(error)
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ async function open (device) {
|
||||||
**/
|
**/
|
||||||
async function close (device) {
|
async function close (device) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
return mcopy.arduino.serial[device].close((err) => {
|
return arduino.serial[device].close((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ async function close (device) {
|
||||||
/******
|
/******
|
||||||
Arduino handlers
|
Arduino handlers
|
||||||
*******/
|
*******/
|
||||||
mcopy.arduino = {
|
const arduino = {
|
||||||
path : {},
|
path : {},
|
||||||
known: [
|
known: [
|
||||||
'/dev/tty.usbmodem1a161',
|
'/dev/tty.usbmodem1a161',
|
||||||
|
@ -129,10 +129,13 @@ mcopy.arduino = {
|
||||||
baud : 57600,
|
baud : 57600,
|
||||||
queue : {},
|
queue : {},
|
||||||
timer : 0,
|
timer : 0,
|
||||||
lock : false
|
lock : false,
|
||||||
|
locks : {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.enumerate = async function () {
|
arduino.enumerate = async function () {
|
||||||
return new Promise( (resolve, reject) => {
|
return new Promise( (resolve, reject) => {
|
||||||
return SerialPort.list((err, ports) => {
|
return SerialPort.list((err, ports) => {
|
||||||
let matches = []
|
let matches = []
|
||||||
|
@ -140,7 +143,7 @@ mcopy.arduino.enumerate = async function () {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
ports.forEach(port => {
|
ports.forEach(port => {
|
||||||
if (mcopy.arduino.known.indexOf(port.comName) !== -1) {
|
if (arduino.known.indexOf(port.comName) !== -1) {
|
||||||
matches.push(port.comName)
|
matches.push(port.comName)
|
||||||
} else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
} else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
||||||
matches.push(port.comName)
|
matches.push(port.comName)
|
||||||
|
@ -156,32 +159,32 @@ mcopy.arduino.enumerate = async function () {
|
||||||
}
|
}
|
||||||
|
|
||||||
//commands which respond to a sent char
|
//commands which respond to a sent char
|
||||||
mcopy.arduino.send = async function (serial, cmd, res) {
|
arduino.send = async function (serial, cmd, res) {
|
||||||
const device = mcopy.arduino.alias[serial]
|
const device = arduino.alias[serial]
|
||||||
let results
|
let results
|
||||||
if (mcopy.arduino.lock) {
|
if (arduino.locks[serial]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
mcopy.arduino.lock = true
|
arduino.locks[serial] = true
|
||||||
await delay(mcopy.cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
try {
|
try {
|
||||||
results = await send(device, cmd)
|
results = await send(device, cmd)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return console.error(e)
|
return console.error(e)
|
||||||
}
|
}
|
||||||
mcopy.arduino.lock = false
|
arduino.locks[serial] = false
|
||||||
mcopy.arduino.timer = new Date().getTime()
|
arduino.timer = new Date().getTime()
|
||||||
return await eventEmitter.emit('arduino_send', cmd)
|
return await eventEmitter.emit('arduino_send', cmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
//send strings, after char triggers firmware to accept
|
//send strings, after char triggers firmware to accept
|
||||||
mcopy.arduino.string = async function (serial, str) {
|
arduino.string = async function (serial, str) {
|
||||||
const device = mcopy.arduino.alias[serial]
|
const device = arduino.alias[serial]
|
||||||
let writeSuccess
|
let writeSuccess
|
||||||
await delay(mcopy.cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
if (typeof mcopy.arduino.serial[device].fake !== 'undefined'
|
if (typeof arduino.serial[device].fake !== 'undefined'
|
||||||
&& mcopy.arduino.serial[device].fake) {
|
&& arduino.serial[device].fake) {
|
||||||
return mcopy.arduino.serial[device].string(str)
|
return arduino.serial[device].string(str)
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
writeSuccess = await write(device, str)
|
writeSuccess = await write(device, str)
|
||||||
|
@ -193,89 +196,90 @@ mcopy.arduino.string = async function (serial, str) {
|
||||||
}
|
}
|
||||||
|
|
||||||
//respond with same char over serial when done
|
//respond with same char over serial when done
|
||||||
mcopy.arduino.end = async function (data) {
|
arduino.end = async function (serial, data) {
|
||||||
const end = new Date().getTime()
|
const end = new Date().getTime()
|
||||||
const ms = end - mcopy.arduino.timer
|
const ms = end - arduino.timer
|
||||||
let complete
|
let complete
|
||||||
if (mcopy.arduino.queue[data] !== undefined) {
|
if (arduino.queue[data] !== undefined) {
|
||||||
mcopy.arduino.lock = false;
|
arduino.locks[serial] = false;
|
||||||
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
||||||
complete = mcopy.arduino.queue[data](ms) //execute callback
|
complete = arduino.queue[data](ms) //execute callback
|
||||||
eventEmitter.emit('arduino_end', data)
|
eventEmitter.emit('arduino_end', data)
|
||||||
delete mcopy.arduino.queue[data]
|
delete arduino.queue[data]
|
||||||
} else {
|
} else {
|
||||||
//console.log('Received stray "' + data + '"'); //silent to user
|
//console.log('Received stray "' + data + '"'); //silent to user
|
||||||
}
|
}
|
||||||
return complete
|
return complete
|
||||||
};
|
};
|
||||||
mcopy.arduino.alias = function (serial, device) {
|
arduino.alias = function (serial, device) {
|
||||||
console.log(`Making "${serial}" an alias of ${device}`)
|
console.log(`Making "${serial}" an alias of ${device}`)
|
||||||
mcopy.arduino.alias[serial] = device
|
arduino.alias[serial] = device
|
||||||
}
|
}
|
||||||
mcopy.arduino.connect = async function (serial, device, confirm) {
|
arduino.connect = async function (serial, device, confirm) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let connectSuccess
|
let connectSuccess
|
||||||
mcopy.arduino.path[serial] = device;
|
arduino.path[serial] = device;
|
||||||
mcopy.arduino.alias[serial] = device;
|
arduino.alias[serial] = device;
|
||||||
mcopy.arduino.serial[device] = new SerialPort(mcopy.arduino.path[serial], {
|
arduino.serial[device] = new SerialPort(arduino.path[serial], {
|
||||||
autoOpen : false,
|
autoOpen : false,
|
||||||
baudRate: mcopy.cfg.arduino.baud,
|
baudRate: cfg.arduino.baud,
|
||||||
parser: parser
|
parser: parser
|
||||||
})
|
})
|
||||||
|
arduino.locks[device] = false
|
||||||
try {
|
try {
|
||||||
connectSuccess = await open(device)
|
connectSuccess = await open(device)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('failed to open: ' + e)
|
console.error('failed to open: ' + e)
|
||||||
return reject(e)
|
return reject(e)
|
||||||
}
|
}
|
||||||
console.log(`Opened connection with ${mcopy.arduino.path[serial]} as ${serial}`);
|
console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`);
|
||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
mcopy.arduino.serial[device].on('data', async (data) => {
|
arduino.serial[device].on('data', async (data) => {
|
||||||
let d = data.toString('utf8')
|
let d = data.toString('utf8')
|
||||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
d = d.replace(newlineRe, '').replace(returnRe, '')
|
||||||
return await mcopy.arduino.end(d)
|
return await arduino.end(serial, d)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
mcopy.arduino.serial[device].on('data', async (data) => {
|
arduino.serial[device].on('data', async (data) => {
|
||||||
let d = data.toString('utf8')
|
let d = data.toString('utf8')
|
||||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
d = d.replace(newlineRe, '').replace(returnRe, '')
|
||||||
return await mcopy.arduino.confirmEnd(d)
|
return await arduino.confirmEnd(d)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return resolve(mcopy.arduino.path[serial])
|
return resolve(arduino.path[serial])
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.confirmExec = {};
|
arduino.confirmExec = {};
|
||||||
mcopy.arduino.confirmEnd = function (data) {
|
arduino.confirmEnd = function (data) {
|
||||||
//console.dir(data)
|
//console.dir(data)
|
||||||
if (data === mcopy.cfg.arduino.cmd.connect
|
if (data === cfg.arduino.cmd.connect
|
||||||
|| data === mcopy.cfg.arduino.cmd.proj_identifier
|
|| data === cfg.arduino.cmd.proj_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.cam_identifier
|
|| data === cfg.arduino.cmd.cam_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.light_identifier
|
|| data === cfg.arduino.cmd.light_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.proj_light_identifier
|
|| data === cfg.arduino.cmd.proj_light_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.proj_cam_light_identifier
|
|| data === cfg.arduino.cmd.proj_cam_light_identifier
|
||||||
|| data === mcopy.cfg.arduino.cmd.proj_cam_identifier ) {
|
|| data === cfg.arduino.cmd.proj_cam_identifier ) {
|
||||||
mcopy.arduino.confirmExec(null, data);
|
arduino.confirmExec(null, data);
|
||||||
mcopy.arduino.confirmExec = {};
|
arduino.confirmExec = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.verify = async function () {
|
arduino.verify = async function () {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const device = mcopy.arduino.alias['connect']
|
const device = arduino.alias['connect']
|
||||||
let writeSuccess
|
let writeSuccess
|
||||||
mcopy.arduino.confirmExec = function (err, data) {
|
arduino.confirmExec = function (err, data) {
|
||||||
if (data === mcopy.cfg.arduino.cmd.connect) {
|
if (data === cfg.arduino.cmd.connect) {
|
||||||
return resolve(true)
|
return resolve(true)
|
||||||
} else {
|
} else {
|
||||||
return reject('Wrong data returned')
|
return reject('Wrong data returned')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
await delay(mcopy.cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
try {
|
try {
|
||||||
writeSuccess = await send(device, mcopy.cfg.arduino.cmd.connect)
|
writeSuccess = await send(device, cfg.arduino.cmd.connect)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return reject(e)
|
return reject(e)
|
||||||
}
|
}
|
||||||
|
@ -283,30 +287,32 @@ mcopy.arduino.verify = async function () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.distinguish = async function () {
|
arduino.distinguish = async function () {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const device = mcopy.arduino.alias['connect']
|
const device = arduino.alias['connect']
|
||||||
let writeSuccess
|
let writeSuccess
|
||||||
let type
|
let type
|
||||||
mcopy.arduino.confirmExec = function (err, data) {
|
arduino.confirmExec = function (err, data) {
|
||||||
if (data === mcopy.cfg.arduino.cmd.proj_identifier) {
|
if (data === cfg.arduino.cmd.proj_identifier) {
|
||||||
type = 'projector'
|
type = 'projector'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.cam_identifier) {
|
} else if (data === cfg.arduino.cmd.cam_identifier) {
|
||||||
type = 'camera'
|
type = 'camera'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.light_identifier) {
|
} else if (data === cfg.arduino.cmd.light_identifier) {
|
||||||
type = 'light'
|
type = 'light'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.proj_light_identifier) {
|
} else if (data === cfg.arduino.cmd.proj_light_identifier) {
|
||||||
type = 'projector,light'
|
type = 'projector,light'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.proj_cam_light_identifier) {
|
} else if (data === cfg.arduino.cmd.proj_cam_light_identifier) {
|
||||||
type = 'projector,camera,light'
|
type = 'projector,camera,light'
|
||||||
} else if (data === mcopy.cfg.arduino.cmd.proj_cam_identifier) {
|
} else if (data === cfg.arduino.cmd.proj_cam_identifier) {
|
||||||
type = 'projector,camera'
|
type = 'projector,camera'
|
||||||
|
} else if (data === cfg.ardino.cmd.proj_second_identifier) {
|
||||||
|
type = 'projector_second'
|
||||||
}
|
}
|
||||||
return resolve(type)
|
return resolve(type)
|
||||||
}
|
}
|
||||||
await delay(mcopy.cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay)
|
||||||
try {
|
try {
|
||||||
writeSuccess = await send(device, mcopy.cfg.arduino.cmd.mcopy_identifier)
|
writeSuccess = await send(device, cfg.arduino.cmd.mcopy_identifier)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e)
|
console.error(e)
|
||||||
return reject(e)
|
return reject(e)
|
||||||
|
@ -314,8 +320,8 @@ mcopy.arduino.distinguish = async function () {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
mcopy.arduino.close = async function (callback) {
|
arduino.close = async function (callback) {
|
||||||
const device = mcopy.arduino.alias['connect']
|
const device = arduino.alias['connect']
|
||||||
let closeSuccess
|
let closeSuccess
|
||||||
try {
|
try {
|
||||||
closeSuccess = await close(device)
|
closeSuccess = await close(device)
|
||||||
|
@ -325,22 +331,22 @@ mcopy.arduino.close = async function (callback) {
|
||||||
return closeSuccess
|
return closeSuccess
|
||||||
};
|
};
|
||||||
|
|
||||||
mcopy.arduino.fakeConnect = async function (serial) {
|
arduino.fakeConnect = async function (serial) {
|
||||||
//console.log('Connecting to fake arduino...');
|
//console.log('Connecting to fake arduino...');
|
||||||
const device = '/dev/fake'
|
const device = '/dev/fake'
|
||||||
mcopy.arduino.alias[serial] = device
|
arduino.alias[serial] = device
|
||||||
mcopy.arduino.serial[device] = {
|
arduino.serial[device] = {
|
||||||
write : function (cmd, cb) {
|
write : function (cmd, cb) {
|
||||||
const t = {
|
const t = {
|
||||||
c : mcopy.cfg.arduino.cam.time + mcopy.cfg.arduino.cam.delay,
|
c : cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
||||||
p : mcopy.cfg.arduino.proj.time + mcopy.cfg.arduino.proj.delay
|
p : cfg.arduino.proj.time + cfg.arduino.proj.delay
|
||||||
}
|
}
|
||||||
let timeout = t[cmd]
|
let timeout = t[cmd]
|
||||||
let end
|
let end
|
||||||
if (typeof timeout === 'undefined') timeout = 10
|
if (typeof timeout === 'undefined') timeout = 10
|
||||||
mcopy.arduino.timer = +new Date()
|
arduino.timer = +new Date()
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
mcopy.arduino.end(cmd)
|
arduino.end(serial, cmd)
|
||||||
return cb()
|
return cb()
|
||||||
}, timeout)
|
}, timeout)
|
||||||
|
|
||||||
|
@ -356,9 +362,9 @@ mcopy.arduino.fakeConnect = async function (serial) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof module !== 'undefined' && module.parent) {
|
if (typeof module !== 'undefined' && module.parent) {
|
||||||
module.exports = function (cfg, ee) {
|
module.exports = function (c, ee) {
|
||||||
eventEmitter = ee
|
eventEmitter = ee
|
||||||
mcopy.cfg = cfg
|
cfg = c
|
||||||
return mcopy.arduino
|
return arduino
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue