2016-04-12 03:05:05 +00:00
|
|
|
var serialport = require('serialport'),
|
|
|
|
SerialPort = serialport.SerialPort,
|
|
|
|
exec = require('child_process').exec,
|
2016-06-21 15:04:57 +00:00
|
|
|
events = require('events'),
|
|
|
|
eventEmitter = new events.EventEmitter(),
|
2016-04-12 03:05:05 +00:00
|
|
|
mcopy = {};
|
|
|
|
|
|
|
|
/******
|
|
|
|
Arduino handlers
|
|
|
|
*******/
|
|
|
|
mcopy.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',
|
|
|
|
'/dev/tty.usbmodem1a141'
|
|
|
|
],
|
2016-04-19 01:26:45 +00:00
|
|
|
serial : {
|
|
|
|
connect : {},
|
|
|
|
projector : {},
|
|
|
|
camera : {}
|
|
|
|
},
|
2016-04-12 03:05:05 +00:00
|
|
|
baud : 57600,
|
|
|
|
queue : {},
|
|
|
|
timer : 0,
|
|
|
|
lock : false
|
|
|
|
};
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.enumerate = function (callback) {
|
2016-04-12 03:05:05 +00:00
|
|
|
'use strict';
|
2016-04-19 01:52:38 +00:00
|
|
|
//console.log('Searching for devices...');
|
2016-04-12 03:05:05 +00:00
|
|
|
var cmd = 'ls /dev/tty.*';
|
|
|
|
exec(cmd, function (e, std) {
|
|
|
|
var devices = std.split('\n'),
|
|
|
|
matches = [];
|
2016-04-19 01:26:45 +00:00
|
|
|
if (e) { return callback(e); }
|
2016-04-12 03:05:05 +00:00
|
|
|
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) {
|
2016-04-13 03:14:27 +00:00
|
|
|
if (callback) { callback('No USB devices found'); }
|
2016-04-12 03:05:05 +00:00
|
|
|
} else if (matches.length > 0) {
|
2016-04-19 01:26:45 +00:00
|
|
|
if (callback) { callback(null, matches); }
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
//commands which respond to a sent char
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.send = function (serial, cmd, res) {
|
2016-04-12 03:05:05 +00:00
|
|
|
'use strict';
|
|
|
|
if (!mcopy.arduino.lock) {
|
|
|
|
mcopy.arduino.lock = true;
|
|
|
|
mcopy.arduino.queue[cmd] = res;
|
|
|
|
setTimeout(function () {
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.serial[serial].write(cmd, function (err, results) {
|
2016-04-12 03:05:05 +00:00
|
|
|
if (err) { console.log(err); }
|
|
|
|
mcopy.arduino.lock = false;
|
|
|
|
mcopy.arduino.timer = new Date().getTime();
|
|
|
|
});
|
|
|
|
}, mcopy.cfg.arduino.serialDelay);
|
2016-06-21 15:04:57 +00:00
|
|
|
eventEmitter.emit('arduino_send', cmd);
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
//send strings, after char triggers firmware to accept
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.string = function (serial, str) {
|
2016-04-12 03:05:05 +00:00
|
|
|
'use strict';
|
|
|
|
setTimeout(function () {
|
2016-04-19 01:26:45 +00:00
|
|
|
if (typeof mcopy.arduino.serial[serial].fake !== 'undefined'
|
|
|
|
&& mcopy.arduino.serial[serial].fake) {
|
|
|
|
mcopy.arduino.serial[serial].string(str);
|
|
|
|
} else {
|
|
|
|
mcopy.arduino.serial[serial].write(str, function (err, results) {
|
|
|
|
if (err) { console.log(err); }
|
|
|
|
//console.log('sent: ' + str);
|
|
|
|
});
|
|
|
|
}
|
2016-04-12 03:05:05 +00:00
|
|
|
}, mcopy.cfg.arduino.serialDelay);
|
|
|
|
};
|
2016-04-19 01:26:45 +00:00
|
|
|
//respond with same char over serial when done
|
2016-04-12 03:05:05 +00:00
|
|
|
mcopy.arduino.end = function (data) {
|
|
|
|
'use strict';
|
|
|
|
var end = new Date().getTime(),
|
|
|
|
ms = end - mcopy.arduino.timer;
|
|
|
|
if (mcopy.arduino.queue[data] !== undefined) {
|
|
|
|
mcopy.arduino.lock = false;
|
2016-04-19 01:52:38 +00:00
|
|
|
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.queue[data](ms); //execute callback
|
2016-06-21 15:04:57 +00:00
|
|
|
eventEmitter.emit('arduino_end', data);
|
2016-04-19 01:26:45 +00:00
|
|
|
delete mcopy.arduino.queue[data];
|
2016-04-12 03:05:05 +00:00
|
|
|
} else {
|
2016-04-19 01:26:45 +00:00
|
|
|
console.log('Received stray "' + data + '"'); //silent to user
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
|
|
|
};
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.connect = function (serial, device, confirm, callback) {
|
2016-04-12 03:05:05 +00:00
|
|
|
'use strict';
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.path[serial] = device;
|
|
|
|
mcopy.arduino.serial[serial] = new SerialPort(mcopy.arduino.path[serial], {
|
|
|
|
baudrate: mcopy.cfg.arduino.baud,
|
|
|
|
parser: serialport.parsers.readline("\n")
|
2016-04-12 03:05:05 +00:00
|
|
|
}, false);
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.serial[serial].open(function (error) {
|
2016-04-12 03:05:05 +00:00
|
|
|
if (error) {
|
2016-04-19 01:26:45 +00:00
|
|
|
if (callback) { callback(error); }
|
2016-04-12 03:05:05 +00:00
|
|
|
return console.log('failed to open: '+ error);
|
|
|
|
} else {
|
2016-04-19 01:52:38 +00:00
|
|
|
//console.log('Opened connection with ' + mcopy.arduino.path[serial]);
|
2016-04-19 01:26:45 +00:00
|
|
|
if (!confirm) {
|
|
|
|
mcopy.arduino.serial[serial].on('data', function (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);
|
2016-04-12 03:05:05 +00:00
|
|
|
});
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
|
|
|
if (callback) {
|
|
|
|
callback(null, mcopy.arduino.path[serial]);
|
|
|
|
}
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-04-19 01:26:45 +00:00
|
|
|
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) {
|
|
|
|
mcopy.arduino.confirmExec(null, data);
|
|
|
|
mcopy.arduino.confirmExec = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
mcopy.arduino.verify = function (callback) {
|
|
|
|
'use strict';
|
|
|
|
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) {
|
|
|
|
if (err) {
|
|
|
|
return console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, mcopy.cfg.arduino.serialDelay);
|
|
|
|
};
|
|
|
|
mcopy.arduino.distinguish = function (callback) {
|
|
|
|
'use strict';
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setTimeout(function () {
|
|
|
|
mcopy.arduino.serial['connect'].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) {
|
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
mcopy.arduino.fakeConnect = function (serial, callback) {
|
2016-04-19 01:52:38 +00:00
|
|
|
//console.log('Connecting to fake arduino...');
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.serial[serial] = {
|
2016-04-14 04:17:42 +00:00
|
|
|
write : function (cmd, res) {
|
|
|
|
var t = {
|
2016-04-19 01:26:45 +00:00
|
|
|
c : mcopy.cfg.arduino.cam.time + mcopy.cfg.arduino.cam.delay,
|
|
|
|
p : mcopy.cfg.arduino.proj.time + mcopy.cfg.arduino.proj.delay
|
|
|
|
},
|
|
|
|
timeout = t[cmd];
|
|
|
|
if (typeof timeout === 'undefined') timeout = 10;
|
|
|
|
mcopy.arduino.timer = +new Date();
|
|
|
|
setTimeout(function () {
|
|
|
|
mcopy.arduino.end(cmd);
|
|
|
|
}, timeout);
|
2016-04-14 04:17:42 +00:00
|
|
|
},
|
|
|
|
string : function (str) {
|
|
|
|
//do nothing
|
|
|
|
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!');
|
2016-04-14 04:17:42 +00:00
|
|
|
if (callback) callback();
|
|
|
|
};
|
|
|
|
|
2016-04-12 06:19:35 +00:00
|
|
|
if (typeof module !== 'undefined' && module.parent) {
|
2016-04-12 03:05:05 +00:00
|
|
|
module.exports = function (cfg) {
|
|
|
|
mcopy.cfg = cfg;
|
|
|
|
return mcopy.arduino;
|
|
|
|
}
|
|
|
|
}
|