mcopy/app/main.js

172 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-04-11 06:01:26 +00:00
'use strict';
2016-04-11 14:49:57 +00:00
const electron = require('electron'),
fs = require('fs'),
Menu = require('menu'),
ipcMain = require('electron').ipcMain,
app = electron.app,
exec = require('child_process').exec,
2016-04-11 14:49:57 +00:00
BrowserWindow = electron.BrowserWindow,
uuid = require('node-uuid'),
serialport = require('serialport'),
SerialPort = serialport.SerialPort,
mcopy = {};
2016-04-11 06:01:26 +00:00
let mainWindow;
2016-04-11 14:49:57 +00:00
var init = function () {
mcopy.cfg = JSON.parse(fs.readFileSync('./cfg.json', 'utf8'));
createWindow();
mcopy.arduino.init(function (success) {
mcopy.arduino.connect(function () {
mcopy.arduino.colorTest();
});
});
2016-04-11 14:49:57 +00:00
};
2016-04-11 06:01:26 +00:00
2016-04-11 14:49:57 +00:00
var createMenu = function () {
2016-04-11 06:01:26 +00:00
2016-04-11 14:49:57 +00:00
};
2016-04-11 06:01:26 +00:00
2016-04-11 14:49:57 +00:00
var createWindow = function () {
mainWindow = new BrowserWindow({width: 800, height: 600});
mainWindow.loadURL('file://' + __dirname + '/index.html');
mainWindow.webContents.openDevTools();
mainWindow.on('closed', function() {
mainWindow = null;
});
2016-04-11 06:01:26 +00:00
}
2016-04-11 14:49:57 +00:00
app.on('ready', init);
2016-04-11 06:01:26 +00:00
app.on('window-all-closed', function () {
2016-04-11 14:49:57 +00:00
if (process.platform !== 'darwin') {
app.quit();
}
2016-04-11 06:01:26 +00:00
});
app.on('activate', function () {
2016-04-11 14:49:57 +00:00
if (mainWindow === null) {
createWindow();
}
});
ipcMain.on('light', function(event, arg) {
//
event.returnValue = true;
});
/******
Arduino handlers
*******/
mcopy.arduino = {
path : '',
known: [
'/dev/tty.usbmodem1a161',
'/dev/tty.usbserial-A800f8dk',
'/dev/tty.usbserial-A900cebm',
'/dev/tty.usbmodem1a131',
'/dev/tty.usbserial-a900f6de',
'/dev/tty.usbmodem1a141'
],
serial : {},
baud : 57600,
queue : {},
timer : 0,
lock : false
};
mcopy.arduino.init = function (callback) {
console.log('Searching for devices...');
2016-04-11 14:49:57 +00:00
var cmd = 'ls /dev/tty.*';
exec(cmd, function (e, std) {
var devices = std.split('\n'),
matches = [];
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) {
console.log('No devices found.');
2016-04-11 14:49:57 +00:00
if (callback) { callback(false); }
} else if (matches.length > 0) {
console.log('Found ' + matches[0]);
2016-04-11 14:49:57 +00:00
mcopy.arduino.path = matches[0];
//once connected to the arduino
//start user interface
if (callback) { callback(true); }
}
});
};
//commands which respond to a sent char
mcopy.arduino.send = function (cmd, res) {
if (!mcopy.arduino.lock) {
mcopy.arduino.lock = true;
mcopy.arduino.queue[cmd] = res;
setTimeout(function () {
mcopy.arduino.serial.write(cmd, function (err, results) {
if (err) { console.log(err); }
2016-04-11 14:49:57 +00:00
mcopy.arduino.lock = false;
mcopy.arduino.timer = new Date().getTime();
});
}, mcopy.cfg.arduino.serialDelay);
}
};
//send strings, after char triggers firmware to accept
mcopy.arduino.string = function (str) {
setTimeout(function () {
mcopy.arduino.serial.write(str, function (err, results) {
if (err) { console.log(err); }
//console.log('sent: ' + str);
});
}, mcopy.cfg.arduino.serialDelay);
};
2016-04-11 14:49:57 +00:00
//with same over serial when done
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;
console.log('Command ' + data + ' took ' + ms + 'ms');
2016-04-11 14:49:57 +00:00
mcopy.arduino.queue[data](ms);
delete mcopy.arduino.queue[data]; //add timestamp?
2016-04-11 14:49:57 +00:00
} else {
console.log('Received stray "' + data + '" from ' + mcopy.arduino.path); //silent to user
2016-04-11 14:49:57 +00:00
}
};
mcopy.arduino.connect = function (callback) {
console.log('Connecting to ' + mcopy.arduino.path + '...');
2016-04-11 14:49:57 +00:00
mcopy.arduino.serial = new SerialPort(mcopy.arduino.path, {
baudrate: mcopy.cfg.arduino.baud,
parser: serialport.parsers.readline("\n")
}, false);
2016-04-11 14:49:57 +00:00
mcopy.arduino.serial.open(function (error) {
if (error) {
return console.log('failed to open: '+ error);
2016-04-11 14:49:57 +00:00
} else {
console.log('Opened connection with ' + mcopy.arduino.path);
2016-04-11 14:49:57 +00:00
mcopy.arduino.serial.on('data', function (data) {
data = data.replace('\r', '');
mcopy.arduino.end(data);
});
setTimeout(function () {
console.log('Verifying firmware...');
2016-04-11 14:49:57 +00:00
mcopy.arduino.send(mcopy.cfg.arduino.cmd.connect, function () {
console.log('Firmware verified');
console.log('Optical printer ready!');
2016-04-11 14:49:57 +00:00
if (callback) { callback(); }
});
}, 2000);
}
});
};
mcopy.arduino.colorTest = function () {
2016-04-12 00:12:08 +00:00
var color = '255,140,70';
mcopy.arduino.send(mcopy.cfg.arduino.cmd.light, function () {
2016-04-12 00:12:08 +00:00
console.log('Light set to ' + color);
});
2016-04-12 00:12:08 +00:00
mcopy.arduino.string(color);
};