ES6 refactor

Can support multiple devices
Uses new types of mcopy firmware distinguishing characters
This commit is contained in:
mmcw-dev 2018-01-02 01:15:29 -05:00
parent 21ec669b20
commit 2bde6c9a8e
1 changed files with 97 additions and 75 deletions

View File

@ -1,7 +1,10 @@
var SerialPort = require('serialport'),
exec = require('child_process').exec,
eventEmitter,
mcopy = {};
'use strict'
const SerialPort = require('serialport')
const exec = require('child_process').exec
let eventEmitter
const mcopy = {}
/******
Arduino handlers
@ -14,65 +17,67 @@ mcopy.arduino = {
'/dev/tty.usbserial-A900cebm',
'/dev/tty.usbmodem1a131',
'/dev/tty.usbserial-a900f6de',
'/dev/tty.usbmodem1a141'
'/dev/tty.usbmodem1a141',
'/dev/ttyACM0'
],
alias : {
},
serial : {
connect : {},
projector : {},
camera : {}
camera : {},
light : {}
},
baud : 57600,
queue : {},
timer : 0,
lock : false
};
}
mcopy.arduino.enumerate = function (callback) {
'use strict';
//console.log('Searching for devices...');
var cmd = 'ls /dev/tty.*';
exec(cmd, function (e, std) {
var devices = std.split('\n'),
matches = [];
if (e) { return callback(e); }
devices.pop();
for (var i = 0; i < devices.length; i++) {
if (devices[i].indexOf('usbserial') !== -1
||devices[i].indexOf('usbmodem') !== -1){
matches.push(devices[i]);
}
let matches = []
SerialPort.list((err, ports) => {
ports.forEach(port => {
if (mcopy.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) {
if (callback) { callback('No USB devices found'); }
} else if (matches.length > 0) {
if (callback) { callback(null, matches); }
}
});
};
})
}
//commands which respond to a sent char
mcopy.arduino.send = function (serial, cmd, res) {
'use strict';
const device = mcopy.arduino.alias[serial]
if (!mcopy.arduino.lock) {
mcopy.arduino.lock = true;
mcopy.arduino.queue[cmd] = res;
setTimeout(function () {
mcopy.arduino.serial[serial].write(cmd, function (err, results) {
if (err) { console.log(err); }
mcopy.arduino.lock = false;
mcopy.arduino.timer = new Date().getTime();
});
}, mcopy.cfg.arduino.serialDelay);
eventEmitter.emit('arduino_send', cmd);
mcopy.arduino.lock = true
mcopy.arduino.queue[cmd] = res
setTimeout(() => {
mcopy.arduino.serial[device].write(cmd, (err, results) => {
if (err) { console.log(err) }
mcopy.arduino.lock = false
mcopy.arduino.timer = new Date().getTime()
})
}, mcopy.cfg.arduino.serialDelay)
eventEmitter.emit('arduino_send', cmd)
}
};
//send strings, after char triggers firmware to accept
mcopy.arduino.string = function (serial, str) {
'use strict';
const device = mcopy.arduino.alias[serial]
setTimeout(function () {
if (typeof mcopy.arduino.serial[serial].fake !== 'undefined'
&& mcopy.arduino.serial[serial].fake) {
mcopy.arduino.serial[serial].string(str);
if (typeof mcopy.arduino.serial[device].fake !== 'undefined'
&& mcopy.arduino.serial[device].fake) {
mcopy.arduino.serial[device].string(str);
} else {
mcopy.arduino.serial[serial].write(str, function (err, results) {
mcopy.arduino.serial[device].write(str, function (err, results) {
if (err) { console.log(err); }
//console.log('sent: ' + str);
});
@ -81,7 +86,6 @@ mcopy.arduino.string = function (serial, str) {
};
//respond with same char over serial when done
mcopy.arduino.end = function (data) {
'use strict';
var end = new Date().getTime(),
ms = end - mcopy.arduino.timer;
if (mcopy.arduino.queue[data] !== undefined) {
@ -94,32 +98,37 @@ mcopy.arduino.end = function (data) {
console.log('Received stray "' + data + '"'); //silent to user
}
};
mcopy.arduino.alias = function (serial, device) {
console.log(`Making "${serial}" an alias of ${device}`)
mcopy.arduino.alias[serial] = device
}
mcopy.arduino.connect = function (serial, device, confirm, callback) {
'use strict';
mcopy.arduino.path[serial] = device;
mcopy.arduino.serial[serial] = new SerialPort(mcopy.arduino.path[serial], {
mcopy.arduino.alias[serial] = device;
mcopy.arduino.serial[device] = new SerialPort(mcopy.arduino.path[serial], {
autoOpen : false,
baudrate: mcopy.cfg.arduino.baud,
parser: serialport.parsers.readline("\n")
}, false);
mcopy.arduino.serial[serial].open(function (error) {
parser: SerialPort.parsers.readline("\n")
});
mcopy.arduino.serial[device].open(error => {
if (error) {
if (callback) { callback(error); }
return console.log('failed to open: '+ error);
} else {
//console.log('Opened connection with ' + mcopy.arduino.path[serial]);
console.log(`Opened connection with ${mcopy.arduino.path[serial]} as ${serial}`);
if (!confirm) {
mcopy.arduino.serial[serial].on('data', function (data) {
data = data.replace('\r', '');
mcopy.arduino.end(data);
});
mcopy.arduino.serial[device].on('data', data => {
data = data.replace('\r', '')
mcopy.arduino.end(data)
})
} else {
mcopy.arduino.serial[serial].on('data', function (data) {
data = data.replace('\r', '');
mcopy.arduino.confirmEnd(data);
});
mcopy.arduino.serial[device].on('data', data => {
data = data.replace('\r', '')
mcopy.arduino.confirmEnd(data)
})
}
if (callback) {
callback(null, mcopy.arduino.path[serial]);
callback(null, mcopy.arduino.path[serial])
}
}
});
@ -127,23 +136,26 @@ mcopy.arduino.connect = function (serial, device, confirm, callback) {
mcopy.arduino.confirmExec = {};
mcopy.arduino.confirmEnd = function (data) {
'use strict';
if (data === mcopy.cfg.arduino.cmd.connect
|| data === mcopy.cfg.arduino.cmd.proj_identifier
|| data === mcopy.cfg.arduino.cmd.cam_identifier) {
|| data === mcopy.cfg.arduino.cmd.cam_identifier
|| data === mcopy.cfg.arduino.cmd.light_identifier
|| data === mcopy.cfg.arduino.cmd.proj_light_identifier
|| data === mcopy.cfg.arduino.cmd.proj_cam_light_identifier
|| data === mcopy.cfg.arduino.cmd.proj_cam_identifier ) {
mcopy.arduino.confirmExec(null, data);
mcopy.arduino.confirmExec = {};
}
};
mcopy.arduino.verify = function (callback) {
'use strict';
const device = mcopy.arduino.alias['connect']
mcopy.arduino.confirmExec = function (err, data) {
if (data === mcopy.cfg.arduino.cmd.connect) {
callback(null, true);
}
};
setTimeout(function () {
mcopy.arduino.serial['connect'].write(mcopy.cfg.arduino.cmd.connect, function (err, results) {
mcopy.arduino.serial[device].write(mcopy.cfg.arduino.cmd.connect, (err, results) => {
if (err) {
return console.log(err);
}
@ -151,35 +163,45 @@ mcopy.arduino.verify = function (callback) {
}, mcopy.cfg.arduino.serialDelay);
};
mcopy.arduino.distinguish = function (callback) {
'use strict';
const device = mcopy.arduino.alias['connect']
mcopy.arduino.confirmExec = function (err, data) {
if (data === mcopy.cfg.arduino.cmd.proj_identifier) {
callback(null, 'projector');
} else if (data === mcopy.cfg.arduino.cmd.cam_identifier) {
callback(null, 'camera');
} else if (data === mcopy.cfg.arduino.cmd.light_identifier) {
callback(null, 'light')
} else if (data === mcopy.cfg.arduino.cmd.proj_light_identifier) {
callback(null, 'projector,light')
} else if (data === mcopy.cfg.arduino.cmd.proj_cam_light_identifier) {
callback(null, 'projector,camera,light')
} else if (data === mcopy.cfg.arduino.cmd.proj_cam_identifier) {
callback(null, 'projector,camera')
}
}
};
setTimeout(function () {
mcopy.arduino.serial['connect'].write(mcopy.cfg.arduino.cmd.mcopy_identifier, function (err, results) {
mcopy.arduino.serial[device].write(mcopy.cfg.arduino.cmd.mcopy_identifier, function (err, results) {
if (err) {
return console.log(err);
}
});
}, mcopy.cfg.arduino.serialDelay);
};
}
mcopy.arduino.close = function (callback) {
'use strict';
mcopy.arduino.serial['connect'].close(function (err) {
let device = mcopy.arduino.alias['connect']
mcopy.arduino.serial[device].close((err) => {
if (callback) {
callback(err);
callback(err)
}
});
};
mcopy.arduino.fakeConnect = function (serial, callback) {
//console.log('Connecting to fake arduino...');
mcopy.arduino.serial[serial] = {
const device = '/dev/fake'
mcopy.arduino.alias[serial] = device
mcopy.arduino.serial[device] = {
write : function (cmd, res) {
var t = {
c : mcopy.cfg.arduino.cam.time + mcopy.cfg.arduino.cam.delay,
@ -188,24 +210,24 @@ mcopy.arduino.fakeConnect = function (serial, callback) {
timeout = t[cmd];
if (typeof timeout === 'undefined') timeout = 10;
mcopy.arduino.timer = +new Date();
setTimeout(function () {
mcopy.arduino.end(cmd);
}, timeout);
setTimeout(() => {
mcopy.arduino.end(cmd)
}, timeout)
},
string : function (str) {
//do nothing
return true;
return true
},
fake : true
};
//console.log('Connected to fake arduino! Not real! Doesn\'t exist!');
if (callback) callback();
};
if (callback) callback()
}
if (typeof module !== 'undefined' && module.parent) {
module.exports = function (cfg, ee) {
eventEmitter = ee;
mcopy.cfg = cfg;
return mcopy.arduino;
eventEmitter = ee
mcopy.cfg = cfg
return mcopy.arduino
}
}