Refactor arduino library as class, rather than object.
This commit is contained in:
parent
febb39aef7
commit
f0fae8ce1b
|
@ -7,6 +7,17 @@ const newlineRe = new RegExp('\n', 'g');
|
|||
const returnRe = new RegExp('\r', 'g');
|
||||
let eventEmitter;
|
||||
let cfg;
|
||||
let arduino;
|
||||
const KNOWN = [
|
||||
'/dev/tty.usbmodem1a161',
|
||||
'/dev/tty.usbserial-A800f8dk',
|
||||
'/dev/tty.usbserial-A900cebm',
|
||||
'/dev/tty.usbmodem1a131',
|
||||
'/dev/tty.usbserial-a900f6de',
|
||||
'/dev/tty.usbmodem1a141',
|
||||
'/dev/ttyACM0',
|
||||
'COM3'
|
||||
];
|
||||
/**
|
||||
* Pause the process for X milliseconds in async/await functions
|
||||
*
|
||||
|
@ -20,352 +31,336 @@ async function delay(ms) {
|
|||
});
|
||||
}
|
||||
/**
|
||||
* Send a command to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
* @param {string} cmd Single character command to send
|
||||
*
|
||||
* @returns {Promise} Resolves after sending
|
||||
* Class representing the arduino communication features
|
||||
**/
|
||||
async function send(device, cmd) {
|
||||
return new Promise((resolve, reject) => {
|
||||
arduino.queue[cmd] = (ms) => {
|
||||
return resolve(ms);
|
||||
};
|
||||
return arduino.serial[device].write(cmd, (err, results) => {
|
||||
if (err) {
|
||||
//console.error(err)
|
||||
return reject(err);
|
||||
}
|
||||
//
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Send a string to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
* @param {string} str String to send
|
||||
*
|
||||
* @returns {Promise} Resolves after sending
|
||||
**/
|
||||
async function write(device, str) {
|
||||
return new Promise((resolve, reject) => {
|
||||
arduino.serial[device].write(str, function (err, results) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
//console.log('sent: ' + str)
|
||||
return resolve(results);
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Connect to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
*
|
||||
* @returns {Promise} Resolves after opening
|
||||
**/
|
||||
async function openArduino(device) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return arduino.serial[device].open(error => {
|
||||
if (error) {
|
||||
return reject(error);
|
||||
}
|
||||
return resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Close a connection to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
*
|
||||
* @returns {Promise} Resolves after closing
|
||||
**/
|
||||
async function closeArduino(device) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return arduino.serial[device].close((err) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
/******
|
||||
Arduino handlers
|
||||
*******/
|
||||
const arduino = {
|
||||
path: {},
|
||||
known: [
|
||||
'/dev/tty.usbmodem1a161',
|
||||
'/dev/tty.usbserial-A800f8dk',
|
||||
'/dev/tty.usbserial-A900cebm',
|
||||
'/dev/tty.usbmodem1a131',
|
||||
'/dev/tty.usbserial-a900f6de',
|
||||
'/dev/tty.usbmodem1a141',
|
||||
'/dev/ttyACM0',
|
||||
'COM3'
|
||||
],
|
||||
alias: {},
|
||||
serial: {
|
||||
connect: {},
|
||||
projector: {},
|
||||
camera: {},
|
||||
light: {}
|
||||
},
|
||||
baud: 57600,
|
||||
queue: {},
|
||||
timer: 0,
|
||||
lock: false,
|
||||
locks: {}
|
||||
};
|
||||
arduino.enumerate = async function () {
|
||||
return new Promise((resolve, reject) => {
|
||||
return SerialPort.list((err, ports) => {
|
||||
let matches = [];
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
ports.forEach(port => {
|
||||
if (arduino.known.indexOf(port.comName) !== -1) {
|
||||
matches.push(port.comName);
|
||||
class Arduino {
|
||||
constructor() {
|
||||
this.path = {};
|
||||
this.known = KNOWN;
|
||||
this.alias = {};
|
||||
this.serial = { connect: {}, projector: {}, camera: {}, light: {} };
|
||||
this.baud = 57600;
|
||||
this.queue = {};
|
||||
this.timer = 0;
|
||||
this.lock = false;
|
||||
this.locks = {};
|
||||
}
|
||||
async enumerate() {
|
||||
return new Promise((resolve, reject) => {
|
||||
return SerialPort.list((err, ports) => {
|
||||
let matches = [];
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
||||
matches.push(port.comName);
|
||||
ports.forEach((port) => {
|
||||
if (this.known.indexOf(port.comName) !== -1) {
|
||||
matches.push(port.comName);
|
||||
}
|
||||
else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
||||
matches.push(port.comName);
|
||||
}
|
||||
else if ((port.comName + '').toLowerCase().indexOf('usbserial') !== -1) {
|
||||
matches.push(port.comName);
|
||||
}
|
||||
else if ((port.comName + '').toLowerCase().indexOf('usbmodem') !== -1) {
|
||||
matches.push(port.comName);
|
||||
}
|
||||
});
|
||||
if (matches.length === 0) {
|
||||
return reject('No USB devices found');
|
||||
}
|
||||
else if ((port.comName + '').toLowerCase().indexOf('usbserial') !== -1) {
|
||||
matches.push(port.comName);
|
||||
}
|
||||
else if ((port.comName + '').toLowerCase().indexOf('usbmodem') !== -1) {
|
||||
matches.push(port.comName);
|
||||
else if (matches.length > 0) {
|
||||
return resolve(matches);
|
||||
}
|
||||
});
|
||||
if (matches.length === 0) {
|
||||
return reject('No USB devices found');
|
||||
}
|
||||
else if (matches.length > 0) {
|
||||
return resolve(matches);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
//commands which respond to a sent char
|
||||
arduino.send = async function (serial, cmd, res) {
|
||||
const device = arduino.alias[serial];
|
||||
let results;
|
||||
if (arduino.locks[serial]) {
|
||||
return false;
|
||||
}
|
||||
arduino.locks[serial] = true;
|
||||
await delay(cfg.arduino.serialDelay);
|
||||
try {
|
||||
results = await send(device, cmd);
|
||||
/**
|
||||
* Send a command to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
* @param {string} cmd Single character command to send
|
||||
*
|
||||
* @returns {Promise} Resolves after sending
|
||||
**/
|
||||
async sendAsync(device, cmd) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.queue[cmd] = (ms) => {
|
||||
return resolve(ms);
|
||||
};
|
||||
return this.serial[device].write(cmd, (err, results) => {
|
||||
if (err) {
|
||||
//console.error(err)
|
||||
return reject(err);
|
||||
}
|
||||
//
|
||||
});
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
return console.error(e);
|
||||
}
|
||||
arduino.locks[serial] = false;
|
||||
arduino.timer = new Date().getTime();
|
||||
return await eventEmitter.emit('arduino_send', cmd);
|
||||
};
|
||||
//send strings, after char triggers firmware to accept
|
||||
arduino.string = async function (serial, str) {
|
||||
const device = arduino.alias[serial];
|
||||
let writeSuccess;
|
||||
await delay(cfg.arduino.serialDelay);
|
||||
if (typeof arduino.serial[device].fake !== 'undefined'
|
||||
&& arduino.serial[device].fake) {
|
||||
return arduino.serial[device].string(str);
|
||||
}
|
||||
else {
|
||||
async send(serial, cmd) {
|
||||
const device = this.alias[serial];
|
||||
let results;
|
||||
if (this.locks[serial]) {
|
||||
return false;
|
||||
}
|
||||
this.locks[serial] = true;
|
||||
await delay(cfg.arduino.serialDelay);
|
||||
try {
|
||||
writeSuccess = await write(device, str);
|
||||
results = await this.sendAsync(device, cmd);
|
||||
}
|
||||
catch (e) {
|
||||
return console.error(e);
|
||||
}
|
||||
return writeSuccess;
|
||||
this.locks[serial] = false;
|
||||
this.timer = new Date().getTime();
|
||||
return await eventEmitter.emit('arduino_send', cmd);
|
||||
}
|
||||
};
|
||||
//respond with same char over serial when done
|
||||
arduino.end = async function (serial, data) {
|
||||
const end = new Date().getTime();
|
||||
const ms = end - arduino.timer;
|
||||
let complete;
|
||||
if (arduino.queue[data] !== undefined) {
|
||||
arduino.locks[serial] = false;
|
||||
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
||||
complete = arduino.queue[data](ms); //execute callback
|
||||
eventEmitter.emit('arduino_end', data);
|
||||
delete arduino.queue[data];
|
||||
}
|
||||
else {
|
||||
//console.log('Received stray "' + data + '"'); //silent to user
|
||||
}
|
||||
return complete;
|
||||
};
|
||||
arduino.alias = function (serial, device) {
|
||||
console.log(`Making "${serial}" an alias of ${device}`);
|
||||
arduino.alias[serial] = device;
|
||||
};
|
||||
arduino.connect = async function (serial, device, confirm) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let connectSuccess;
|
||||
arduino.path[serial] = device;
|
||||
arduino.alias[serial] = device;
|
||||
arduino.serial[device] = new SerialPort(arduino.path[serial], {
|
||||
autoOpen: false,
|
||||
baudRate: cfg.arduino.baud,
|
||||
parser: parser
|
||||
});
|
||||
arduino.locks[device] = false;
|
||||
try {
|
||||
connectSuccess = await openArduino(device);
|
||||
}
|
||||
catch (e) {
|
||||
console.error('failed to open: ' + e);
|
||||
return reject(e);
|
||||
}
|
||||
console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`);
|
||||
if (!confirm) {
|
||||
arduino.serial[device].on('data', async (data) => {
|
||||
let d = data.toString('utf8');
|
||||
d = d.replace(newlineRe, '').replace(returnRe, '');
|
||||
return await arduino.end(serial, d);
|
||||
});
|
||||
async string(serial, str) {
|
||||
const device = this.alias[serial];
|
||||
let writeSuccess;
|
||||
await delay(cfg.arduino.serialDelay);
|
||||
if (typeof this.serial[device].fake !== 'undefined'
|
||||
&& this.serial[device].fake) {
|
||||
return this.serial[device].string(str);
|
||||
}
|
||||
else {
|
||||
arduino.serial[device].on('data', async (data) => {
|
||||
let d = data.toString('utf8');
|
||||
d = d.replace(newlineRe, '').replace(returnRe, '');
|
||||
return await arduino.confirmEnd(d);
|
||||
});
|
||||
try {
|
||||
writeSuccess = await this.writeAsync(device, str);
|
||||
}
|
||||
catch (e) {
|
||||
return console.error(e);
|
||||
}
|
||||
return writeSuccess;
|
||||
}
|
||||
return resolve(arduino.path[serial]);
|
||||
});
|
||||
};
|
||||
arduino.confirmExec = {};
|
||||
arduino.confirmEnd = function (data) {
|
||||
//console.dir(data)
|
||||
if (data === cfg.arduino.cmd.connect
|
||||
|| data === cfg.arduino.cmd.proj_identifier
|
||||
|| data === cfg.arduino.cmd.cam_identifier
|
||||
|| data === cfg.arduino.cmd.light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_cam_light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_cam_identifier) {
|
||||
arduino.confirmExec(null, data);
|
||||
arduino.confirmExec = {};
|
||||
}
|
||||
};
|
||||
arduino.verify = async function () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device = arduino.alias['connect'];
|
||||
let writeSuccess;
|
||||
arduino.confirmExec = function (err, data) {
|
||||
if (data === cfg.arduino.cmd.connect) {
|
||||
return resolve(true);
|
||||
/**
|
||||
* Send a string to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
* @param {string} str String to send
|
||||
*
|
||||
* @returns {Promise} Resolves after sending
|
||||
**/
|
||||
async writeAsync(device, str) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.serial[device].write(str, function (err, results) {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
//console.log('sent: ' + str)
|
||||
return resolve(results);
|
||||
});
|
||||
});
|
||||
}
|
||||
end(serial, data) {
|
||||
const end = new Date().getTime();
|
||||
const ms = end - this.timer;
|
||||
let complete;
|
||||
if (this.queue[data] !== undefined) {
|
||||
this.locks[serial] = false;
|
||||
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
||||
complete = this.queue[data](ms); //execute callback
|
||||
eventEmitter.emit('arduino_end', data);
|
||||
delete this.queue[data];
|
||||
}
|
||||
else {
|
||||
//console.log('Received stray "' + data + '"'); //silent to user
|
||||
}
|
||||
return complete;
|
||||
}
|
||||
aliasSerial(serial, device) {
|
||||
console.log(`Making "${serial}" an alias of ${device}`);
|
||||
this.alias[serial] = device;
|
||||
}
|
||||
async connect(serial, device, confirm) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let connectSuccess;
|
||||
this.path[serial] = device;
|
||||
this.alias[serial] = device;
|
||||
this.serial[device] = new SerialPort(this.path[serial], {
|
||||
autoOpen: false,
|
||||
baudRate: cfg.arduino.baud,
|
||||
parser: parser
|
||||
});
|
||||
this.locks[device] = false;
|
||||
try {
|
||||
connectSuccess = await this.openArduino(device);
|
||||
}
|
||||
catch (e) {
|
||||
console.error('failed to open: ' + e);
|
||||
return reject(e);
|
||||
}
|
||||
console.log(`Opened connection with ${this.path[serial]} as ${serial}`);
|
||||
if (!confirm) {
|
||||
this.serial[device].on('data', async (data) => {
|
||||
let d = data.toString('utf8');
|
||||
d = d.replace(newlineRe, '').replace(returnRe, '');
|
||||
return this.end(serial, d);
|
||||
});
|
||||
}
|
||||
else {
|
||||
return reject('Wrong data returned');
|
||||
this.serial[device].on('data', async (data) => {
|
||||
let d = data.toString('utf8');
|
||||
d = d.replace(newlineRe, '').replace(returnRe, '');
|
||||
return await this.confirmEnd(d);
|
||||
});
|
||||
}
|
||||
};
|
||||
await delay(cfg.arduino.serialDelay);
|
||||
try {
|
||||
writeSuccess = await send(device, cfg.arduino.cmd.connect);
|
||||
}
|
||||
catch (e) {
|
||||
return reject(e);
|
||||
}
|
||||
return resolve(writeSuccess);
|
||||
});
|
||||
};
|
||||
arduino.distinguish = async function () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device = arduino.alias['connect'];
|
||||
let writeSuccess;
|
||||
let type;
|
||||
arduino.confirmExec = function (err, data) {
|
||||
if (data === cfg.arduino.cmd.proj_identifier) {
|
||||
type = 'projector';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.cam_identifier) {
|
||||
type = 'camera';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.light_identifier) {
|
||||
type = 'light';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.proj_light_identifier) {
|
||||
type = 'projector,light';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.proj_cam_light_identifier) {
|
||||
type = 'projector,camera,light';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.proj_cam_identifier) {
|
||||
type = 'projector,camera';
|
||||
}
|
||||
else if (data === cfg.ardino.cmd.proj_second_identifier) {
|
||||
type = 'projector_second';
|
||||
}
|
||||
return resolve(type);
|
||||
};
|
||||
await delay(cfg.arduino.serialDelay);
|
||||
try {
|
||||
writeSuccess = await send(device, cfg.arduino.cmd.mcopy_identifier);
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e);
|
||||
return reject(e);
|
||||
}
|
||||
});
|
||||
};
|
||||
arduino.close = async function (callback) {
|
||||
const device = arduino.alias['connect'];
|
||||
let closeSuccess;
|
||||
try {
|
||||
closeSuccess = await closeArduino(device);
|
||||
return resolve(this.path[serial]);
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
return console.error(e);
|
||||
confirmEnd(data) {
|
||||
//console.dir(data)
|
||||
if (data === cfg.arduino.cmd.connect
|
||||
|| data === cfg.arduino.cmd.proj_identifier
|
||||
|| data === cfg.arduino.cmd.cam_identifier
|
||||
|| data === cfg.arduino.cmd.light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_cam_light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_cam_identifier) {
|
||||
this.confirmExec(null, data);
|
||||
this.confirmExec = {};
|
||||
}
|
||||
}
|
||||
return closeSuccess;
|
||||
};
|
||||
arduino.fakeConnect = async function (serial) {
|
||||
//console.log('Connecting to fake arduino...');
|
||||
const device = '/dev/fake';
|
||||
arduino.alias[serial] = device;
|
||||
arduino.serial[device] = {
|
||||
write: function (cmd, cb) {
|
||||
const t = {
|
||||
c: cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
||||
p: cfg.arduino.proj.time + cfg.arduino.proj.delay
|
||||
async verify() {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device = this.alias['connect'];
|
||||
let writeSuccess;
|
||||
this.confirmExec = function (err, data) {
|
||||
if (data === cfg.arduino.cmd.connect) {
|
||||
return resolve(true);
|
||||
}
|
||||
else {
|
||||
return reject('Wrong data returned');
|
||||
}
|
||||
};
|
||||
let timeout = t[cmd];
|
||||
let end;
|
||||
if (typeof timeout === 'undefined')
|
||||
timeout = 10;
|
||||
arduino.timer = +new Date();
|
||||
setTimeout(() => {
|
||||
await delay(cfg.arduino.serialDelay);
|
||||
try {
|
||||
writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.connect);
|
||||
}
|
||||
catch (e) {
|
||||
return reject(e);
|
||||
}
|
||||
return resolve(writeSuccess);
|
||||
});
|
||||
}
|
||||
async distinguish() {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device = this.alias['connect'];
|
||||
let writeSuccess;
|
||||
let type;
|
||||
this.confirmExec = function (err, data) {
|
||||
if (data === cfg.arduino.cmd.proj_identifier) {
|
||||
type = 'projector';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.cam_identifier) {
|
||||
type = 'camera';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.light_identifier) {
|
||||
type = 'light';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.proj_light_identifier) {
|
||||
type = 'projector,light';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.proj_cam_light_identifier) {
|
||||
type = 'projector,camera,light';
|
||||
}
|
||||
else if (data === cfg.arduino.cmd.proj_cam_identifier) {
|
||||
type = 'projector,camera';
|
||||
}
|
||||
else if (data === cfg.ardino.cmd.proj_second_identifier) {
|
||||
type = 'projector_second';
|
||||
}
|
||||
return resolve(type);
|
||||
};
|
||||
await delay(cfg.arduino.serialDelay);
|
||||
try {
|
||||
writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.mcopy_identifier);
|
||||
}
|
||||
catch (e) {
|
||||
console.error(e);
|
||||
return reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
async close() {
|
||||
const device = this.alias['connect'];
|
||||
let closeSuccess;
|
||||
try {
|
||||
closeSuccess = await this.closeArduino(device);
|
||||
}
|
||||
catch (e) {
|
||||
return console.error(e);
|
||||
}
|
||||
return closeSuccess;
|
||||
}
|
||||
;
|
||||
async fakeConnect(serial) {
|
||||
//console.log('Connecting to fake arduino...');
|
||||
const device = '/dev/fake';
|
||||
this.alias[serial] = device;
|
||||
this.serial[device] = {
|
||||
write: async function (cmd, cb) {
|
||||
const t = {
|
||||
c: cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
||||
p: cfg.arduino.proj.time + cfg.arduino.proj.delay
|
||||
};
|
||||
let timeout = t[cmd];
|
||||
if (typeof timeout === 'undefined')
|
||||
timeout = 10;
|
||||
arduino.timer = +new Date();
|
||||
await delay(timeout);
|
||||
arduino.end(serial, cmd);
|
||||
return cb();
|
||||
}, timeout);
|
||||
},
|
||||
string: async function (str) {
|
||||
//do nothing
|
||||
return true;
|
||||
},
|
||||
fake: true
|
||||
};
|
||||
//console.log('Connected to fake arduino! Not real! Doesn\'t exist!');
|
||||
return true;
|
||||
};
|
||||
},
|
||||
string: async function (str) {
|
||||
//do nothing
|
||||
return true;
|
||||
},
|
||||
fake: true
|
||||
};
|
||||
//console.log('Connected to fake arduino! Not real! Does not exist!');
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* Connect to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
*
|
||||
* @returns {Promise} Resolves after opening
|
||||
**/
|
||||
async openArduino(device) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return this.serial[device].open((err) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
/**
|
||||
* Close a connection to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
*
|
||||
* @returns {Promise} Resolves after closing
|
||||
**/
|
||||
async closeArduino(device) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return this.serial[device].close((err) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
return resolve(true);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
if (typeof module !== 'undefined' && module.parent) {
|
||||
module.exports = function (c, ee) {
|
||||
eventEmitter = ee;
|
||||
cfg = c;
|
||||
arduino = new Arduino();
|
||||
return arduino;
|
||||
};
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -4,12 +4,24 @@ const SerialPort = require('serialport')
|
|||
const Readline = SerialPort.parsers.Readline
|
||||
const exec = require('child_process').exec
|
||||
|
||||
const parser = new Readline('')
|
||||
const parser : any = new Readline('')
|
||||
const newlineRe : RegExp = new RegExp('\n', 'g')
|
||||
const returnRe : RegExp = new RegExp('\r', 'g')
|
||||
|
||||
let eventEmitter : any
|
||||
let cfg : object
|
||||
let cfg : any
|
||||
let arduino : any
|
||||
|
||||
const KNOWN : string[] = [
|
||||
'/dev/tty.usbmodem1a161',
|
||||
'/dev/tty.usbserial-A800f8dk',
|
||||
'/dev/tty.usbserial-A900cebm',
|
||||
'/dev/tty.usbmodem1a131',
|
||||
'/dev/tty.usbserial-a900f6de',
|
||||
'/dev/tty.usbmodem1a141',
|
||||
'/dev/ttyACM0',
|
||||
'COM3'
|
||||
]
|
||||
|
||||
/**
|
||||
* Pause the process for X milliseconds in async/await functions
|
||||
|
@ -25,351 +37,339 @@ async function delay (ms : number) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Send a command to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
* @param {string} cmd Single character command to send
|
||||
*
|
||||
* @returns {Promise} Resolves after sending
|
||||
**/
|
||||
async function send (device : string, cmd : string) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
arduino.queue[cmd] = (ms : number) => {
|
||||
return resolve(ms)
|
||||
}
|
||||
return arduino.serial[device].write(cmd, (err : any, results : any) => {
|
||||
if (err) {
|
||||
//console.error(err)
|
||||
return reject(err)
|
||||
}
|
||||
//
|
||||
})
|
||||
})
|
||||
}
|
||||
* Class representing the arduino communication features
|
||||
**/
|
||||
|
||||
/**
|
||||
* Send a string to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
* @param {string} str String to send
|
||||
*
|
||||
* @returns {Promise} Resolves after sending
|
||||
**/
|
||||
async function write (device : string, str : string) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
arduino.serial[device].write(str, function (err, results) {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
//console.log('sent: ' + str)
|
||||
return resolve(results)
|
||||
})
|
||||
})
|
||||
}
|
||||
class Arduino {
|
||||
|
||||
/**
|
||||
* Connect to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
*
|
||||
* @returns {Promise} Resolves after opening
|
||||
**/
|
||||
async function openArduino (device : string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return arduino.serial[device].open(error => {
|
||||
if (error) {
|
||||
return reject(error)
|
||||
}
|
||||
return resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a connection to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
*
|
||||
* @returns {Promise} Resolves after closing
|
||||
**/
|
||||
async function closeArduino (device : string) {
|
||||
return new Promise((resolve : any, reject : any) => {
|
||||
return arduino.serial[device].close((err) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
return resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/******
|
||||
Arduino handlers
|
||||
*******/
|
||||
const arduino = {
|
||||
path : {},
|
||||
known: [
|
||||
'/dev/tty.usbmodem1a161',
|
||||
'/dev/tty.usbserial-A800f8dk',
|
||||
'/dev/tty.usbserial-A900cebm',
|
||||
'/dev/tty.usbmodem1a131',
|
||||
'/dev/tty.usbserial-a900f6de',
|
||||
'/dev/tty.usbmodem1a141',
|
||||
'/dev/ttyACM0',
|
||||
'COM3'
|
||||
],
|
||||
alias : {
|
||||
|
||||
},
|
||||
serial : {
|
||||
connect : {},
|
||||
projector : {},
|
||||
camera : {},
|
||||
light : {}
|
||||
},
|
||||
baud : 57600,
|
||||
queue : {},
|
||||
timer : 0,
|
||||
lock : false,
|
||||
locks : {
|
||||
path : any = {}
|
||||
known : string[] = KNOWN
|
||||
alias : any = {}
|
||||
serial : any = { connect : {}, projector : {}, camera : {}, light : {} }
|
||||
baud : number = 57600
|
||||
queue : any = {}
|
||||
timer : number = 0
|
||||
lock : boolean = false
|
||||
locks : any = {}
|
||||
confirmExec : any
|
||||
|
||||
constructor () {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
arduino.enumerate = async function () {
|
||||
return new Promise( (resolve, reject) => {
|
||||
return SerialPort.list((err, ports) => {
|
||||
let matches = []
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
ports.forEach(port => {
|
||||
if (arduino.known.indexOf(port.comName) !== -1) {
|
||||
matches.push(port.comName)
|
||||
} else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
||||
matches.push(port.comName)
|
||||
} else if ((port.comName + '').toLowerCase().indexOf('usbserial') !== -1) {
|
||||
matches.push(port.comName)
|
||||
} else if ((port.comName + '').toLowerCase().indexOf('usbmodem') !== -1) {
|
||||
matches.push(port.comName)
|
||||
async enumerate () {
|
||||
return new Promise( (resolve, reject) => {
|
||||
return SerialPort.list((err : any, ports : any[]) => {
|
||||
let matches : string[] = []
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
ports.forEach((port : any) => {
|
||||
if (this.known.indexOf(port.comName) !== -1) {
|
||||
matches.push(port.comName)
|
||||
} else if ((port.manufacturer + '').toLowerCase().indexOf('arduino') !== -1) {
|
||||
matches.push(port.comName)
|
||||
} else if ((port.comName + '').toLowerCase().indexOf('usbserial') !== -1) {
|
||||
matches.push(port.comName)
|
||||
} else if ((port.comName + '').toLowerCase().indexOf('usbmodem') !== -1) {
|
||||
matches.push(port.comName)
|
||||
}
|
||||
})
|
||||
if (matches.length === 0) {
|
||||
return reject('No USB devices found');
|
||||
} else if (matches.length > 0) {
|
||||
return resolve(matches)
|
||||
}
|
||||
})
|
||||
if (matches.length === 0) {
|
||||
return reject('No USB devices found');
|
||||
} else if (matches.length > 0) {
|
||||
return resolve(matches)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
//commands which respond to a sent char
|
||||
arduino.send = async function (serial, cmd, res) {
|
||||
const device = arduino.alias[serial]
|
||||
let results
|
||||
if (arduino.locks[serial]) {
|
||||
return false
|
||||
}
|
||||
arduino.locks[serial] = true
|
||||
await delay(cfg.arduino.serialDelay)
|
||||
try {
|
||||
results = await send(device, cmd)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
}
|
||||
arduino.locks[serial] = false
|
||||
arduino.timer = new Date().getTime()
|
||||
return await eventEmitter.emit('arduino_send', cmd)
|
||||
}
|
||||
|
||||
//send strings, after char triggers firmware to accept
|
||||
arduino.string = async function (serial, str) {
|
||||
const device = arduino.alias[serial]
|
||||
let writeSuccess
|
||||
await delay(cfg.arduino.serialDelay)
|
||||
if (typeof arduino.serial[device].fake !== 'undefined'
|
||||
&& arduino.serial[device].fake) {
|
||||
return arduino.serial[device].string(str)
|
||||
} else {
|
||||
/**
|
||||
* Send a command to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
* @param {string} cmd Single character command to send
|
||||
*
|
||||
* @returns {Promise} Resolves after sending
|
||||
**/
|
||||
async sendAsync (device : string, cmd : string) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
this.queue[cmd] = (ms : number) => {
|
||||
return resolve(ms)
|
||||
}
|
||||
return this.serial[device].write(cmd, (err : any, results : any) => {
|
||||
if (err) {
|
||||
//console.error(err)
|
||||
return reject(err)
|
||||
}
|
||||
//
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async send (serial : string, cmd : string) {
|
||||
const device : any = this.alias[serial]
|
||||
let results : any
|
||||
if (this.locks[serial]) {
|
||||
return false
|
||||
}
|
||||
this.locks[serial] = true
|
||||
await delay(cfg.arduino.serialDelay)
|
||||
try {
|
||||
writeSuccess = await write(device, str)
|
||||
results = await this.sendAsync(device, cmd)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
}
|
||||
return writeSuccess
|
||||
this.locks[serial] = false
|
||||
this.timer = new Date().getTime()
|
||||
return await eventEmitter.emit('arduino_send', cmd)
|
||||
}
|
||||
}
|
||||
|
||||
//respond with same char over serial when done
|
||||
arduino.end = async function (serial, data) {
|
||||
const end = new Date().getTime()
|
||||
const ms = end - arduino.timer
|
||||
let complete
|
||||
if (arduino.queue[data] !== undefined) {
|
||||
arduino.locks[serial] = false;
|
||||
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
||||
complete = arduino.queue[data](ms) //execute callback
|
||||
eventEmitter.emit('arduino_end', data)
|
||||
delete arduino.queue[data]
|
||||
} else {
|
||||
//console.log('Received stray "' + data + '"'); //silent to user
|
||||
}
|
||||
return complete
|
||||
};
|
||||
arduino.alias = function (serial, device) {
|
||||
console.log(`Making "${serial}" an alias of ${device}`)
|
||||
arduino.alias[serial] = device
|
||||
}
|
||||
arduino.connect = async function (serial, device, confirm) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let connectSuccess
|
||||
arduino.path[serial] = device;
|
||||
arduino.alias[serial] = device;
|
||||
arduino.serial[device] = new SerialPort(arduino.path[serial], {
|
||||
autoOpen : false,
|
||||
baudRate: cfg.arduino.baud,
|
||||
parser: parser
|
||||
})
|
||||
arduino.locks[device] = false
|
||||
try {
|
||||
connectSuccess = await openArduino(device)
|
||||
} catch (e) {
|
||||
console.error('failed to open: ' + e)
|
||||
return reject(e)
|
||||
}
|
||||
console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`);
|
||||
if (!confirm) {
|
||||
arduino.serial[device].on('data', async (data) => {
|
||||
let d = data.toString('utf8')
|
||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
||||
return await arduino.end(serial, d)
|
||||
})
|
||||
async string (serial : string, str : string) {
|
||||
const device : any = this.alias[serial]
|
||||
let writeSuccess : any
|
||||
await delay(cfg.arduino.serialDelay)
|
||||
if (typeof this.serial[device].fake !== 'undefined'
|
||||
&& this.serial[device].fake) {
|
||||
return this.serial[device].string(str)
|
||||
} else {
|
||||
arduino.serial[device].on('data', async (data) => {
|
||||
let d = data.toString('utf8')
|
||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
||||
return await arduino.confirmEnd(d)
|
||||
try {
|
||||
writeSuccess = await this.writeAsync(device, str)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
}
|
||||
return writeSuccess
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a string to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
* @param {string} str String to send
|
||||
*
|
||||
* @returns {Promise} Resolves after sending
|
||||
**/
|
||||
async writeAsync (device : string, str : string) {
|
||||
return new Promise ((resolve, reject) => {
|
||||
this.serial[device].write(str, function (err : any, results : any) {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
//console.log('sent: ' + str)
|
||||
return resolve(results)
|
||||
})
|
||||
}
|
||||
return resolve(arduino.path[serial])
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
arduino.confirmExec = {};
|
||||
arduino.confirmEnd = function (data) {
|
||||
//console.dir(data)
|
||||
if (data === cfg.arduino.cmd.connect
|
||||
|| data === cfg.arduino.cmd.proj_identifier
|
||||
|| data === cfg.arduino.cmd.cam_identifier
|
||||
|| data === cfg.arduino.cmd.light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_cam_light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_cam_identifier ) {
|
||||
arduino.confirmExec(null, data);
|
||||
arduino.confirmExec = {};
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
arduino.verify = async function () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device = arduino.alias['connect']
|
||||
let writeSuccess
|
||||
arduino.confirmExec = function (err, data) {
|
||||
if (data === cfg.arduino.cmd.connect) {
|
||||
return resolve(true)
|
||||
end (serial : string, data : string) {
|
||||
const end = new Date().getTime()
|
||||
const ms = end - this.timer
|
||||
let complete
|
||||
if (this.queue[data] !== undefined) {
|
||||
this.locks[serial] = false;
|
||||
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
||||
complete = this.queue[data](ms) //execute callback
|
||||
eventEmitter.emit('arduino_end', data)
|
||||
delete this.queue[data]
|
||||
} else {
|
||||
//console.log('Received stray "' + data + '"'); //silent to user
|
||||
}
|
||||
return complete
|
||||
}
|
||||
|
||||
aliasSerial (serial : string, device : string) {
|
||||
console.log(`Making "${serial}" an alias of ${device}`)
|
||||
this.alias[serial] = device
|
||||
}
|
||||
|
||||
async connect (serial : string, device : string, confirm : any) {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let connectSuccess : any
|
||||
this.path[serial] = device;
|
||||
this.alias[serial] = device;
|
||||
this.serial[device] = new SerialPort(this.path[serial], {
|
||||
autoOpen : false,
|
||||
baudRate: cfg.arduino.baud,
|
||||
parser: parser
|
||||
})
|
||||
this.locks[device] = false
|
||||
try {
|
||||
connectSuccess = await this.openArduino(device)
|
||||
} catch (e) {
|
||||
console.error('failed to open: ' + e)
|
||||
return reject(e)
|
||||
}
|
||||
console.log(`Opened connection with ${this.path[serial]} as ${serial}`);
|
||||
if (!confirm) {
|
||||
this.serial[device].on('data', async (data : Buffer) => {
|
||||
let d = data.toString('utf8')
|
||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
||||
return this.end(serial, d)
|
||||
})
|
||||
} else {
|
||||
return reject('Wrong data returned')
|
||||
this.serial[device].on('data', async (data : Buffer) => {
|
||||
let d = data.toString('utf8')
|
||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
||||
return await this.confirmEnd(d)
|
||||
})
|
||||
}
|
||||
}
|
||||
await delay(cfg.arduino.serialDelay)
|
||||
try {
|
||||
writeSuccess = await send(device, cfg.arduino.cmd.connect)
|
||||
} catch (e) {
|
||||
return reject(e)
|
||||
}
|
||||
return resolve(writeSuccess)
|
||||
})
|
||||
}
|
||||
|
||||
arduino.distinguish = async function () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device = arduino.alias['connect']
|
||||
let writeSuccess
|
||||
let type
|
||||
arduino.confirmExec = function (err, data) {
|
||||
if (data === cfg.arduino.cmd.proj_identifier) {
|
||||
type = 'projector'
|
||||
} else if (data === cfg.arduino.cmd.cam_identifier) {
|
||||
type = 'camera'
|
||||
} else if (data === cfg.arduino.cmd.light_identifier) {
|
||||
type = 'light'
|
||||
} else if (data === cfg.arduino.cmd.proj_light_identifier) {
|
||||
type = 'projector,light'
|
||||
} else if (data === cfg.arduino.cmd.proj_cam_light_identifier) {
|
||||
type = 'projector,camera,light'
|
||||
} else if (data === cfg.arduino.cmd.proj_cam_identifier) {
|
||||
type = 'projector,camera'
|
||||
} else if (data === cfg.ardino.cmd.proj_second_identifier) {
|
||||
type = 'projector_second'
|
||||
}
|
||||
return resolve(type)
|
||||
}
|
||||
await delay(cfg.arduino.serialDelay)
|
||||
try {
|
||||
writeSuccess = await send(device, cfg.arduino.cmd.mcopy_identifier)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return reject(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
arduino.close = async function (callback) {
|
||||
const device = arduino.alias['connect']
|
||||
let closeSuccess
|
||||
try {
|
||||
closeSuccess = await closeArduino(device)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
return resolve(this.path[serial])
|
||||
})
|
||||
}
|
||||
return closeSuccess
|
||||
};
|
||||
|
||||
arduino.fakeConnect = async function (serial) {
|
||||
//console.log('Connecting to fake arduino...');
|
||||
const device = '/dev/fake'
|
||||
arduino.alias[serial] = device
|
||||
arduino.serial[device] = {
|
||||
write : function (cmd, cb) {
|
||||
const t = {
|
||||
c : cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
||||
p : cfg.arduino.proj.time + cfg.arduino.proj.delay
|
||||
confirmEnd (data : string) {
|
||||
//console.dir(data)
|
||||
if (data === cfg.arduino.cmd.connect
|
||||
|| data === cfg.arduino.cmd.proj_identifier
|
||||
|| data === cfg.arduino.cmd.cam_identifier
|
||||
|| data === cfg.arduino.cmd.light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_cam_light_identifier
|
||||
|| data === cfg.arduino.cmd.proj_cam_identifier ) {
|
||||
this.confirmExec(null, data);
|
||||
this.confirmExec = {};
|
||||
}
|
||||
}
|
||||
|
||||
async verify () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device : any = this.alias['connect']
|
||||
let writeSuccess : any
|
||||
this.confirmExec = function (err : any, data : string) {
|
||||
if (data === cfg.arduino.cmd.connect) {
|
||||
return resolve(true)
|
||||
} else {
|
||||
return reject('Wrong data returned')
|
||||
}
|
||||
}
|
||||
let timeout = t[cmd]
|
||||
let end
|
||||
if (typeof timeout === 'undefined') timeout = 10
|
||||
arduino.timer = +new Date()
|
||||
setTimeout(() => {
|
||||
|
||||
await delay(cfg.arduino.serialDelay)
|
||||
|
||||
try {
|
||||
writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.connect)
|
||||
} catch (e) {
|
||||
return reject(e)
|
||||
}
|
||||
return resolve(writeSuccess)
|
||||
})
|
||||
}
|
||||
|
||||
async distinguish () {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const device : any = this.alias['connect']
|
||||
let writeSuccess : any
|
||||
let type : string
|
||||
this.confirmExec = function (err : any, data : string) {
|
||||
if (data === cfg.arduino.cmd.proj_identifier) {
|
||||
type = 'projector'
|
||||
} else if (data === cfg.arduino.cmd.cam_identifier) {
|
||||
type = 'camera'
|
||||
} else if (data === cfg.arduino.cmd.light_identifier) {
|
||||
type = 'light'
|
||||
} else if (data === cfg.arduino.cmd.proj_light_identifier) {
|
||||
type = 'projector,light'
|
||||
} else if (data === cfg.arduino.cmd.proj_cam_light_identifier) {
|
||||
type = 'projector,camera,light'
|
||||
} else if (data === cfg.arduino.cmd.proj_cam_identifier) {
|
||||
type = 'projector,camera'
|
||||
} else if (data === cfg.ardino.cmd.proj_second_identifier) {
|
||||
type = 'projector_second'
|
||||
}
|
||||
return resolve(type)
|
||||
}
|
||||
await delay(cfg.arduino.serialDelay)
|
||||
try {
|
||||
writeSuccess = await this.sendAsync(device, cfg.arduino.cmd.mcopy_identifier)
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
return reject(e)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async close () {
|
||||
const device = this.alias['connect']
|
||||
let closeSuccess
|
||||
try {
|
||||
closeSuccess = await this.closeArduino(device)
|
||||
} catch (e) {
|
||||
return console.error(e)
|
||||
}
|
||||
return closeSuccess
|
||||
};
|
||||
|
||||
async fakeConnect (serial : string) {
|
||||
//console.log('Connecting to fake arduino...');
|
||||
const device : string = '/dev/fake'
|
||||
this.alias[serial] = device
|
||||
this.serial[device] = {
|
||||
write : async function (cmd : string, cb : any) {
|
||||
const t : any = {
|
||||
c : cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
||||
p : cfg.arduino.proj.time + cfg.arduino.proj.delay
|
||||
}
|
||||
let timeout : number = t[cmd]
|
||||
if (typeof timeout === 'undefined') timeout = 10
|
||||
arduino.timer = +new Date()
|
||||
|
||||
await delay(timeout)
|
||||
|
||||
arduino.end(serial, cmd)
|
||||
return cb()
|
||||
}, timeout)
|
||||
|
||||
},
|
||||
string : async function (str) {
|
||||
//do nothing
|
||||
return true
|
||||
},
|
||||
fake : true
|
||||
};
|
||||
//console.log('Connected to fake arduino! Not real! Doesn\'t exist!');
|
||||
return true
|
||||
},
|
||||
string : async function (str : string) {
|
||||
//do nothing
|
||||
return true
|
||||
},
|
||||
fake : true
|
||||
};
|
||||
//console.log('Connected to fake arduino! Not real! Does not exist!');
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
*
|
||||
* @returns {Promise} Resolves after opening
|
||||
**/
|
||||
async openArduino (device : string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
return this.serial[device].open((err : any) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
return resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a connection to an Arduino using async/await
|
||||
*
|
||||
* @param {string} device Arduino identifier
|
||||
*
|
||||
* @returns {Promise} Resolves after closing
|
||||
**/
|
||||
async closeArduino (device : string) {
|
||||
return new Promise((resolve : any, reject : any) => {
|
||||
return this.serial[device].close((err : any) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
return resolve(true)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && module.parent) {
|
||||
module.exports = function (c, ee) {
|
||||
module.exports = function (c : any, ee : any) {
|
||||
eventEmitter = ee
|
||||
cfg = c
|
||||
arduino = new Arduino()
|
||||
return arduino
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue