Typescript adding commas to compiled code.
This commit is contained in:
parent
ec0b8e3b4f
commit
fcdc6ebb1d
|
@ -1,15 +1,12 @@
|
||||||
'use strict'
|
'use strict';
|
||||||
|
const SerialPort = require('serialport');
|
||||||
const SerialPort = require('serialport')
|
const Readline = SerialPort.parsers.Readline;
|
||||||
const Readline = SerialPort.parsers.Readline
|
const exec = require('child_process').exec;
|
||||||
const exec = require('child_process').exec
|
const parser = new Readline('');
|
||||||
const parser = new Readline('')
|
const newlineRe = new RegExp('\n', 'g');
|
||||||
const newlineRe = new RegExp('\n', 'g')
|
const returnRe = new RegExp('\r', 'g');
|
||||||
const returnRe = new RegExp('\r', 'g')
|
let eventEmitter;
|
||||||
let eventEmitter
|
let cfg;
|
||||||
|
|
||||||
let cfg
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pause the process for X milliseconds in async/await functions
|
* Pause the process for X milliseconds in async/await functions
|
||||||
*
|
*
|
||||||
|
@ -19,10 +16,9 @@ let cfg
|
||||||
**/
|
**/
|
||||||
async function delay(ms) {
|
async function delay(ms) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
return setTimeout(resolve, ms)
|
return setTimeout(resolve, ms);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a command to an Arduino using async/await
|
* Send a command to an Arduino using async/await
|
||||||
*
|
*
|
||||||
|
@ -34,18 +30,17 @@ async function delay (ms) {
|
||||||
async function send(device, cmd) {
|
async function send(device, cmd) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
arduino.queue[cmd] = (ms) => {
|
arduino.queue[cmd] = (ms) => {
|
||||||
return resolve(ms)
|
return resolve(ms);
|
||||||
}
|
};
|
||||||
return arduino.serial[device].write(cmd, (err, results) => {
|
return arduino.serial[device].write(cmd, (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
//console.error(err)
|
//console.error(err)
|
||||||
return reject(err)
|
return reject(err);
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a string to an Arduino using async/await
|
* Send a string to an Arduino using async/await
|
||||||
*
|
*
|
||||||
|
@ -58,14 +53,13 @@ async function write (device, str) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
arduino.serial[device].write(str, function (err, results) {
|
arduino.serial[device].write(str, function (err, results) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err);
|
||||||
}
|
}
|
||||||
//console.log('sent: ' + str)
|
//console.log('sent: ' + str)
|
||||||
return resolve(results)
|
return resolve(results);
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connect to an Arduino using async/await
|
* Connect to an Arduino using async/await
|
||||||
*
|
*
|
||||||
|
@ -73,17 +67,16 @@ async function write (device, str) {
|
||||||
*
|
*
|
||||||
* @returns {Promise} Resolves after opening
|
* @returns {Promise} Resolves after opening
|
||||||
**/
|
**/
|
||||||
async function open (device) {
|
async function openArduino(device) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
return arduino.serial[device].open(error => {
|
return arduino.serial[device].open(error => {
|
||||||
if (error) {
|
if (error) {
|
||||||
return reject(error)
|
return reject(error);
|
||||||
}
|
}
|
||||||
return resolve(true)
|
return resolve(true);
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close a connection to an Arduino using async/await
|
* Close a connection to an Arduino using async/await
|
||||||
*
|
*
|
||||||
|
@ -91,17 +84,16 @@ async function open (device) {
|
||||||
*
|
*
|
||||||
* @returns {Promise} Resolves after closing
|
* @returns {Promise} Resolves after closing
|
||||||
**/
|
**/
|
||||||
async function close (device) {
|
async function closeArduino(device) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
return arduino.serial[device].close((err) => {
|
return arduino.serial[device].close((err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err);
|
||||||
}
|
}
|
||||||
return resolve(true)
|
return resolve(true);
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/******
|
/******
|
||||||
Arduino handlers
|
Arduino handlers
|
||||||
*******/
|
*******/
|
||||||
|
@ -117,9 +109,7 @@ const arduino = {
|
||||||
'/dev/ttyACM0',
|
'/dev/ttyACM0',
|
||||||
'COM3'
|
'COM3'
|
||||||
],
|
],
|
||||||
alias : {
|
alias: {},
|
||||||
|
|
||||||
},
|
|
||||||
serial: {
|
serial: {
|
||||||
connect: {},
|
connect: {},
|
||||||
projector: {},
|
projector: {},
|
||||||
|
@ -130,131 +120,133 @@ const arduino = {
|
||||||
queue: {},
|
queue: {},
|
||||||
timer: 0,
|
timer: 0,
|
||||||
lock: false,
|
lock: false,
|
||||||
locks : {
|
locks: {}
|
||||||
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
arduino.enumerate = async function () {
|
arduino.enumerate = async function () {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
return SerialPort.list((err, ports) => {
|
return SerialPort.list((err, ports) => {
|
||||||
let matches = []
|
let matches = [];
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err);
|
||||||
}
|
}
|
||||||
ports.forEach(port => {
|
ports.forEach(port => {
|
||||||
if (arduino.known.indexOf(port.comName) !== -1) {
|
if (arduino.known.indexOf(port.comName) !== -1) {
|
||||||
matches.push(port.comName)
|
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)
|
|
||||||
}
|
}
|
||||||
})
|
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) {
|
if (matches.length === 0) {
|
||||||
return reject('No USB devices found');
|
return reject('No USB devices found');
|
||||||
} else if (matches.length > 0) {
|
|
||||||
return resolve(matches)
|
|
||||||
}
|
}
|
||||||
})
|
else if (matches.length > 0) {
|
||||||
})
|
return resolve(matches);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
//commands which respond to a sent char
|
//commands which respond to a sent char
|
||||||
arduino.send = async function (serial, cmd, res) {
|
arduino.send = async function (serial, cmd, res) {
|
||||||
const device = arduino.alias[serial]
|
const device = arduino.alias[serial];
|
||||||
let results
|
let results;
|
||||||
if (arduino.locks[serial]) {
|
if (arduino.locks[serial]) {
|
||||||
return false
|
return false;
|
||||||
}
|
}
|
||||||
arduino.locks[serial] = true
|
arduino.locks[serial] = true;
|
||||||
await delay(cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay);
|
||||||
try {
|
try {
|
||||||
results = await send(device, cmd)
|
results = await send(device, cmd);
|
||||||
} catch (e) {
|
|
||||||
return console.error(e)
|
|
||||||
}
|
}
|
||||||
arduino.locks[serial] = false
|
catch (e) {
|
||||||
arduino.timer = new Date().getTime()
|
return console.error(e);
|
||||||
return await eventEmitter.emit('arduino_send', cmd)
|
|
||||||
}
|
}
|
||||||
|
arduino.locks[serial] = false;
|
||||||
|
arduino.timer = new Date().getTime();
|
||||||
|
return await eventEmitter.emit('arduino_send', cmd);
|
||||||
|
};
|
||||||
//send strings, after char triggers firmware to accept
|
//send strings, after char triggers firmware to accept
|
||||||
arduino.string = async function (serial, str) {
|
arduino.string = async function (serial, str) {
|
||||||
const device = arduino.alias[serial]
|
const device = arduino.alias[serial];
|
||||||
let writeSuccess
|
let writeSuccess;
|
||||||
await delay(cfg.arduino.serialDelay)
|
await delay(cfg.arduino.serialDelay);
|
||||||
if (typeof arduino.serial[device].fake !== 'undefined'
|
if (typeof arduino.serial[device].fake !== 'undefined'
|
||||||
&& arduino.serial[device].fake) {
|
&& arduino.serial[device].fake) {
|
||||||
return arduino.serial[device].string(str)
|
return arduino.serial[device].string(str);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
try {
|
try {
|
||||||
writeSuccess = await write(device, str)
|
writeSuccess = await write(device, str);
|
||||||
} catch (e) {
|
|
||||||
return console.error(e)
|
|
||||||
}
|
}
|
||||||
return writeSuccess
|
catch (e) {
|
||||||
|
return console.error(e);
|
||||||
}
|
}
|
||||||
|
return writeSuccess;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
//respond with same char over serial when done
|
//respond with same char over serial when done
|
||||||
arduino.end = async function (serial, data) {
|
arduino.end = async function (serial, data) {
|
||||||
const end = new Date().getTime()
|
const end = new Date().getTime();
|
||||||
const ms = end - arduino.timer
|
const ms = end - arduino.timer;
|
||||||
let complete
|
let complete;
|
||||||
if (arduino.queue[data] !== undefined) {
|
if (arduino.queue[data] !== undefined) {
|
||||||
arduino.locks[serial] = false;
|
arduino.locks[serial] = false;
|
||||||
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
//console.log('Command ' + data + ' took ' + ms + 'ms');
|
||||||
complete = arduino.queue[data](ms) //execute callback
|
complete = arduino.queue[data](ms); //execute callback
|
||||||
eventEmitter.emit('arduino_end', data)
|
eventEmitter.emit('arduino_end', data);
|
||||||
delete arduino.queue[data]
|
delete arduino.queue[data];
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
//console.log('Received stray "' + data + '"'); //silent to user
|
//console.log('Received stray "' + data + '"'); //silent to user
|
||||||
}
|
}
|
||||||
return complete
|
return complete;
|
||||||
};
|
};
|
||||||
arduino.alias = function (serial, device) {
|
arduino.alias = function (serial, device) {
|
||||||
console.log(`Making "${serial}" an alias of ${device}`)
|
console.log(`Making "${serial}" an alias of ${device}`);
|
||||||
arduino.alias[serial] = device
|
arduino.alias[serial] = device;
|
||||||
}
|
};
|
||||||
arduino.connect = async function (serial, device, confirm) {
|
arduino.connect = async function (serial, device, confirm) {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let connectSuccess
|
let connectSuccess;
|
||||||
arduino.path[serial] = device;
|
arduino.path[serial] = device;
|
||||||
arduino.alias[serial] = device;
|
arduino.alias[serial] = device;
|
||||||
arduino.serial[device] = new SerialPort(arduino.path[serial], {
|
arduino.serial[device] = new SerialPort(arduino.path[serial], {
|
||||||
autoOpen: false,
|
autoOpen: false,
|
||||||
baudRate: cfg.arduino.baud,
|
baudRate: cfg.arduino.baud,
|
||||||
parser: parser
|
parser: parser
|
||||||
})
|
});
|
||||||
arduino.locks[device] = false
|
arduino.locks[device] = false;
|
||||||
try {
|
try {
|
||||||
connectSuccess = await open(device)
|
connectSuccess = await openArduino(device);
|
||||||
} catch (e) {
|
}
|
||||||
console.error('failed to open: ' + e)
|
catch (e) {
|
||||||
return reject(e)
|
console.error('failed to open: ' + e);
|
||||||
|
return reject(e);
|
||||||
}
|
}
|
||||||
console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`);
|
console.log(`Opened connection with ${arduino.path[serial]} as ${serial}`);
|
||||||
if (!confirm) {
|
if (!confirm) {
|
||||||
arduino.serial[device].on('data', async (data) => {
|
arduino.serial[device].on('data', async (data) => {
|
||||||
let d = data.toString('utf8')
|
let d = data.toString('utf8');
|
||||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
d = d.replace(newlineRe, '').replace(returnRe, '');
|
||||||
return await arduino.end(serial, d)
|
return await arduino.end(serial, d);
|
||||||
})
|
});
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
arduino.serial[device].on('data', async (data) => {
|
arduino.serial[device].on('data', async (data) => {
|
||||||
let d = data.toString('utf8')
|
let d = data.toString('utf8');
|
||||||
d = d.replace(newlineRe, '').replace(returnRe, '')
|
d = d.replace(newlineRe, '').replace(returnRe, '');
|
||||||
return await arduino.confirmEnd(d)
|
return await arduino.confirmEnd(d);
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
return resolve(arduino.path[serial])
|
return resolve(arduino.path[serial]);
|
||||||
})
|
});
|
||||||
|
};
|
||||||
}
|
|
||||||
|
|
||||||
arduino.confirmExec = {};
|
arduino.confirmExec = {};
|
||||||
arduino.confirmEnd = function (data) {
|
arduino.confirmEnd = function (data) {
|
||||||
//console.dir(data)
|
//console.dir(data)
|
||||||
|
@ -268,107 +260,113 @@ arduino.confirmEnd = function (data) {
|
||||||
arduino.confirmExec(null, data);
|
arduino.confirmExec(null, data);
|
||||||
arduino.confirmExec = {};
|
arduino.confirmExec = {};
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
arduino.verify = async function () {
|
arduino.verify = async function () {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const device = arduino.alias['connect']
|
const device = arduino.alias['connect'];
|
||||||
let writeSuccess
|
let writeSuccess;
|
||||||
arduino.confirmExec = function (err, data) {
|
arduino.confirmExec = function (err, data) {
|
||||||
if (data === cfg.arduino.cmd.connect) {
|
if (data === cfg.arduino.cmd.connect) {
|
||||||
return resolve(true)
|
return resolve(true);
|
||||||
} else {
|
|
||||||
return reject('Wrong data returned')
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return reject('Wrong data returned');
|
||||||
}
|
}
|
||||||
await delay(cfg.arduino.serialDelay)
|
};
|
||||||
|
await delay(cfg.arduino.serialDelay);
|
||||||
try {
|
try {
|
||||||
writeSuccess = await send(device, cfg.arduino.cmd.connect)
|
writeSuccess = await send(device, cfg.arduino.cmd.connect);
|
||||||
} catch (e) {
|
|
||||||
return reject(e)
|
|
||||||
}
|
}
|
||||||
return resolve(writeSuccess)
|
catch (e) {
|
||||||
})
|
return reject(e);
|
||||||
}
|
}
|
||||||
|
return resolve(writeSuccess);
|
||||||
|
});
|
||||||
|
};
|
||||||
arduino.distinguish = async function () {
|
arduino.distinguish = async function () {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
const device = arduino.alias['connect']
|
const device = arduino.alias['connect'];
|
||||||
let writeSuccess
|
let writeSuccess;
|
||||||
let type
|
let type;
|
||||||
arduino.confirmExec = function (err, data) {
|
arduino.confirmExec = function (err, data) {
|
||||||
if (data === cfg.arduino.cmd.proj_identifier) {
|
if (data === cfg.arduino.cmd.proj_identifier) {
|
||||||
type = 'projector'
|
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)
|
else if (data === cfg.arduino.cmd.cam_identifier) {
|
||||||
|
type = 'camera';
|
||||||
}
|
}
|
||||||
await delay(cfg.arduino.serialDelay)
|
else if (data === cfg.arduino.cmd.light_identifier) {
|
||||||
try {
|
type = 'light';
|
||||||
writeSuccess = await send(device, cfg.arduino.cmd.mcopy_identifier)
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e)
|
|
||||||
return reject(e)
|
|
||||||
}
|
}
|
||||||
})
|
else if (data === cfg.arduino.cmd.proj_light_identifier) {
|
||||||
|
type = 'projector,light';
|
||||||
}
|
}
|
||||||
|
else if (data === cfg.arduino.cmd.proj_cam_light_identifier) {
|
||||||
arduino.close = async function (callback) {
|
type = 'projector,camera,light';
|
||||||
const device = arduino.alias['connect']
|
|
||||||
let closeSuccess
|
|
||||||
try {
|
|
||||||
closeSuccess = await close(device)
|
|
||||||
} catch (e) {
|
|
||||||
return console.error(e)
|
|
||||||
}
|
}
|
||||||
return closeSuccess
|
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 closeSuccess;
|
||||||
};
|
};
|
||||||
|
|
||||||
arduino.fakeConnect = async function (serial) {
|
arduino.fakeConnect = async function (serial) {
|
||||||
//console.log('Connecting to fake arduino...');
|
//console.log('Connecting to fake arduino...');
|
||||||
const device = '/dev/fake'
|
const device = '/dev/fake';
|
||||||
arduino.alias[serial] = device
|
arduino.alias[serial] = device;
|
||||||
arduino.serial[device] = {
|
arduino.serial[device] = {
|
||||||
write: function (cmd, cb) {
|
write: function (cmd, cb) {
|
||||||
const t = {
|
const t = {
|
||||||
c: cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
c: cfg.arduino.cam.time + cfg.arduino.cam.delay,
|
||||||
p: cfg.arduino.proj.time + cfg.arduino.proj.delay
|
p: cfg.arduino.proj.time + cfg.arduino.proj.delay
|
||||||
}
|
};
|
||||||
let timeout = t[cmd]
|
let timeout = t[cmd];
|
||||||
let end
|
let end;
|
||||||
if (typeof timeout === 'undefined') timeout = 10
|
if (typeof timeout === 'undefined')
|
||||||
arduino.timer = +new Date()
|
timeout = 10;
|
||||||
|
arduino.timer = +new Date();
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
arduino.end(serial, cmd)
|
arduino.end(serial, cmd);
|
||||||
return cb()
|
return cb();
|
||||||
}, timeout)
|
}, timeout);
|
||||||
|
|
||||||
},
|
},
|
||||||
string: async function (str) {
|
string: async 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!');
|
||||||
return true
|
return true;
|
||||||
}
|
};
|
||||||
|
|
||||||
if (typeof module !== 'undefined' && module.parent) {
|
if (typeof module !== 'undefined' && module.parent) {
|
||||||
module.exports = function (c, ee) {
|
module.exports = function (c, ee) {
|
||||||
eventEmitter = ee
|
eventEmitter = ee;
|
||||||
cfg = c
|
cfg = c;
|
||||||
return arduino
|
return arduino;
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
//# sourceMappingURL=index.js.map
|
Loading…
Reference in New Issue