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