2018-01-02 06:15:29 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const SerialPort = require('serialport')
|
|
|
|
const exec = require('child_process').exec
|
|
|
|
let eventEmitter
|
|
|
|
|
|
|
|
const mcopy = {}
|
2016-04-12 03:05:05 +00:00
|
|
|
|
|
|
|
/******
|
|
|
|
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',
|
2018-01-02 06:15:29 +00:00
|
|
|
'/dev/tty.usbmodem1a141',
|
|
|
|
'/dev/ttyACM0'
|
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,
|
|
|
|
lock : false
|
2018-01-02 06:15:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.enumerate = function (callback) {
|
2018-01-02 06:15:29 +00:00
|
|
|
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)
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
2018-01-02 06:15:29 +00:00
|
|
|
})
|
2016-04-12 03:05:05 +00:00
|
|
|
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
|
|
|
}
|
2018-01-02 06:15:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-01-02 06:15:29 +00:00
|
|
|
const device = mcopy.arduino.alias[serial]
|
2016-04-12 03:05:05 +00:00
|
|
|
if (!mcopy.arduino.lock) {
|
2018-01-02 06:15:29 +00:00
|
|
|
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)
|
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) {
|
2018-01-02 06:15:29 +00:00
|
|
|
const device = mcopy.arduino.alias[serial]
|
2016-04-12 03:05:05 +00:00
|
|
|
setTimeout(function () {
|
2018-01-02 06:15:29 +00:00
|
|
|
if (typeof mcopy.arduino.serial[device].fake !== 'undefined'
|
|
|
|
&& mcopy.arduino.serial[device].fake) {
|
|
|
|
mcopy.arduino.serial[device].string(str);
|
2016-04-19 01:26:45 +00:00
|
|
|
} else {
|
2018-01-02 06:15:29 +00:00
|
|
|
mcopy.arduino.serial[device].write(str, function (err, results) {
|
2016-04-19 01:26:45 +00:00
|
|
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
2018-01-02 06:15:29 +00:00
|
|
|
mcopy.arduino.alias = function (serial, device) {
|
|
|
|
console.log(`Making "${serial}" an alias of ${device}`)
|
|
|
|
mcopy.arduino.alias[serial] = device
|
|
|
|
}
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.connect = function (serial, device, confirm, callback) {
|
|
|
|
mcopy.arduino.path[serial] = device;
|
2018-01-02 06:15:29 +00:00
|
|
|
mcopy.arduino.alias[serial] = device;
|
|
|
|
mcopy.arduino.serial[device] = new SerialPort(mcopy.arduino.path[serial], {
|
|
|
|
autoOpen : false,
|
2016-04-19 01:26:45 +00:00
|
|
|
baudrate: mcopy.cfg.arduino.baud,
|
2018-01-02 06:15:29 +00:00
|
|
|
parser: SerialPort.parsers.readline("\n")
|
|
|
|
});
|
|
|
|
mcopy.arduino.serial[device].open(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 {
|
2018-01-02 06:15:29 +00:00
|
|
|
console.log(`Opened connection with ${mcopy.arduino.path[serial]} as ${serial}`);
|
2016-04-19 01:26:45 +00:00
|
|
|
if (!confirm) {
|
2018-01-02 06:15:29 +00:00
|
|
|
mcopy.arduino.serial[device].on('data', data => {
|
|
|
|
data = data.replace('\r', '')
|
|
|
|
mcopy.arduino.end(data)
|
|
|
|
})
|
2016-04-19 01:26:45 +00:00
|
|
|
} else {
|
2018-01-02 06:15:29 +00:00
|
|
|
mcopy.arduino.serial[device].on('data', data => {
|
|
|
|
data = data.replace('\r', '')
|
|
|
|
mcopy.arduino.confirmEnd(data)
|
|
|
|
})
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
|
|
|
if (callback) {
|
2018-01-02 06:15:29 +00:00
|
|
|
callback(null, mcopy.arduino.path[serial])
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.confirmExec = {};
|
|
|
|
mcopy.arduino.confirmEnd = function (data) {
|
|
|
|
if (data === mcopy.cfg.arduino.cmd.connect
|
|
|
|
|| data === mcopy.cfg.arduino.cmd.proj_identifier
|
2018-01-02 06:15:29 +00:00
|
|
|
|| 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 ) {
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.confirmExec(null, data);
|
|
|
|
mcopy.arduino.confirmExec = {};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
mcopy.arduino.verify = function (callback) {
|
2018-01-02 06:15:29 +00:00
|
|
|
const device = mcopy.arduino.alias['connect']
|
2016-04-19 01:26:45 +00:00
|
|
|
mcopy.arduino.confirmExec = function (err, data) {
|
|
|
|
if (data === mcopy.cfg.arduino.cmd.connect) {
|
|
|
|
callback(null, true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setTimeout(function () {
|
2018-01-02 06:15:29 +00:00
|
|
|
mcopy.arduino.serial[device].write(mcopy.cfg.arduino.cmd.connect, (err, results) => {
|
2016-04-19 01:26:45 +00:00
|
|
|
if (err) {
|
|
|
|
return console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, mcopy.cfg.arduino.serialDelay);
|
|
|
|
};
|
|
|
|
mcopy.arduino.distinguish = function (callback) {
|
2018-01-02 06:15:29 +00:00
|
|
|
const device = mcopy.arduino.alias['connect']
|
2016-04-19 01:26:45 +00:00
|
|
|
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');
|
2018-01-02 06:15:29 +00:00
|
|
|
} 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')
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
2018-01-02 06:15:29 +00:00
|
|
|
}
|
2016-04-19 01:26:45 +00:00
|
|
|
setTimeout(function () {
|
2018-01-02 06:15:29 +00:00
|
|
|
mcopy.arduino.serial[device].write(mcopy.cfg.arduino.cmd.mcopy_identifier, function (err, results) {
|
2016-04-19 01:26:45 +00:00
|
|
|
if (err) {
|
|
|
|
return console.log(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, mcopy.cfg.arduino.serialDelay);
|
2018-01-02 06:15:29 +00:00
|
|
|
}
|
2016-04-19 01:26:45 +00:00
|
|
|
|
|
|
|
mcopy.arduino.close = function (callback) {
|
2018-01-02 06:15:29 +00:00
|
|
|
let device = mcopy.arduino.alias['connect']
|
|
|
|
mcopy.arduino.serial[device].close((err) => {
|
2016-04-19 01:26:45 +00:00
|
|
|
if (callback) {
|
2018-01-02 06:15:29 +00:00
|
|
|
callback(err)
|
2016-04-19 01:26:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
mcopy.arduino.fakeConnect = function (serial, callback) {
|
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'
|
|
|
|
mcopy.arduino.alias[serial] = device
|
|
|
|
mcopy.arduino.serial[device] = {
|
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();
|
2018-01-02 06:15:29 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
mcopy.arduino.end(cmd)
|
|
|
|
}, timeout)
|
2016-04-14 04:17:42 +00:00
|
|
|
},
|
|
|
|
string : function (str) {
|
|
|
|
//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-01-02 06:15:29 +00:00
|
|
|
if (callback) callback()
|
|
|
|
}
|
2016-04-14 04:17:42 +00:00
|
|
|
|
2016-04-12 06:19:35 +00:00
|
|
|
if (typeof module !== 'undefined' && module.parent) {
|
2016-06-21 16:12:14 +00:00
|
|
|
module.exports = function (cfg, ee) {
|
2018-01-02 06:15:29 +00:00
|
|
|
eventEmitter = ee
|
|
|
|
mcopy.cfg = cfg
|
|
|
|
return mcopy.arduino
|
2016-04-12 03:05:05 +00:00
|
|
|
}
|
|
|
|
}
|