Refactored copy arduino into library with real tests.

Optimized copy light firmware from 1000ms response times to 10ms
This commit is contained in:
Matt 2016-04-11 23:05:05 -04:00
parent 9990943aed
commit 2ca59c0be8
5 changed files with 174 additions and 125 deletions

123
app/lib/mcopy-arduino.js Normal file
View File

@ -0,0 +1,123 @@
var serialport = require('serialport'),
SerialPort = serialport.SerialPort,
exec = require('child_process').exec,
mcopy = {};
/******
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) {
'use strict';
console.log('Searching for devices...');
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.');
if (callback) { callback(false); }
} else if (matches.length > 0) {
console.log('Found ' + matches[0]);
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) {
'use strict';
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); }
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) {
'use strict';
setTimeout(function () {
mcopy.arduino.serial.write(str, function (err, results) {
if (err) { console.log(err); }
//console.log('sent: ' + str);
});
}, mcopy.cfg.arduino.serialDelay);
};
//with same over serial when done
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;
console.log('Command ' + data + ' took ' + ms + 'ms');
mcopy.arduino.queue[data](ms);
delete mcopy.arduino.queue[data]; //add timestamp?
} else {
console.log('Received stray "' + data + '" from ' + mcopy.arduino.path); //silent to user
}
};
mcopy.arduino.connect = function (callback) {
'use strict';
console.log('Connecting to ' + mcopy.arduino.path + '...');
mcopy.arduino.serial = new SerialPort(mcopy.arduino.path, {
baudrate: mcopy.cfg.arduino.baud,
parser: serialport.parsers.readline("\n")
}, false);
mcopy.arduino.serial.open(function (error) {
if (error) {
return console.log('failed to open: '+ error);
} else {
console.log('Opened connection with ' + mcopy.arduino.path);
mcopy.arduino.serial.on('data', function (data) {
data = data.replace('\r', '');
mcopy.arduino.end(data);
});
setTimeout(function () {
console.log('Verifying firmware...');
mcopy.arduino.send(mcopy.cfg.arduino.cmd.connect, function () {
console.log('Firmware verified');
console.log('Optical printer ready!');
if (callback) { callback(); }
});
}, 2000);
}
});
};
if (typeof module !== 'undefined') {
module.exports = function (cfg) {
mcopy.cfg = cfg;
return mcopy.arduino;
}
}

View File

@ -1,24 +1,22 @@
'use strict';
const electron = require('electron'),
var electron = require('electron'),
fs = require('fs'),
Menu = require('menu'),
ipcMain = require('electron').ipcMain,
app = electron.app,
exec = require('child_process').exec,
BrowserWindow = electron.BrowserWindow,
uuid = require('node-uuid'),
serialport = require('serialport'),
SerialPort = serialport.SerialPort,
mcopy = {};
let mainWindow;
mcopy.cfg = JSON.parse(fs.readFileSync('./cfg.json', 'utf8'));
mcopy.arduino = require('./lib/mcopy-arduino.js')(mcopy.cfg);
var mainWindow;
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();
//
});
});
};
@ -54,118 +52,3 @@ 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...');
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.');
if (callback) { callback(false); }
} else if (matches.length > 0) {
console.log('Found ' + matches[0]);
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); }
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);
};
//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');
mcopy.arduino.queue[data](ms);
delete mcopy.arduino.queue[data]; //add timestamp?
} else {
console.log('Received stray "' + data + '" from ' + mcopy.arduino.path); //silent to user
}
};
mcopy.arduino.connect = function (callback) {
console.log('Connecting to ' + mcopy.arduino.path + '...');
mcopy.arduino.serial = new SerialPort(mcopy.arduino.path, {
baudrate: mcopy.cfg.arduino.baud,
parser: serialport.parsers.readline("\n")
}, false);
mcopy.arduino.serial.open(function (error) {
if (error) {
return console.log('failed to open: '+ error);
} else {
console.log('Opened connection with ' + mcopy.arduino.path);
mcopy.arduino.serial.on('data', function (data) {
data = data.replace('\r', '');
mcopy.arduino.end(data);
});
setTimeout(function () {
console.log('Verifying firmware...');
mcopy.arduino.send(mcopy.cfg.arduino.cmd.connect, function () {
console.log('Firmware verified');
console.log('Optical printer ready!');
if (callback) { callback(); }
});
}, 2000);
}
});
};
mcopy.arduino.colorTest = function () {
var color = '255,140,70';
mcopy.arduino.send(mcopy.cfg.arduino.cmd.light, function () {
console.log('Light set to ' + color);
});
mcopy.arduino.string(color);
};

View File

@ -0,0 +1,29 @@
var fs = require('fs'),
mcopy = {};
mcopy.cfg = JSON.parse(fs.readFileSync('./cfg.json', 'utf8'));
mcopy.arduino = require('../lib/mcopy-arduino.js')(mcopy.cfg);
mcopy.arduino.colorTest = function (color, cb) {
mcopy.arduino.send(mcopy.cfg.arduino.cmd.light, function () {
console.log('Light set to ' + color);
if (cb) setTimeout(cb, 20);
});
mcopy.arduino.string(color);
};
mcopy.arduino.init(function (success) {
mcopy.arduino.connect(function () {
mcopy.arduino.colorTest('255,140,70', function () {
mcopy.arduino.colorTest('5,0,0', function () {
mcopy.arduino.colorTest('255,255,255', function () {
mcopy.arduino.colorTest('160,14,250', function () {
mcopy.arduino.colorTest('5,32,200', function () {
});//5
});//4
});//3
});//2
});//1
});
});

View File

@ -21,10 +21,12 @@ volatile int g = 0;
volatile int b = 0;
volatile char cmd_char = 'z';
const int serialDelay = 5;
void setup () {
Serial.begin(57600);
Serial.flush();
Serial.setTimeout(serialDelay);
pixieSerial.begin(115200); // Pixie REQUIRES this baud rate
light.setPixelColor(0, 0, 0, 0);
}

View File

@ -29,13 +29,15 @@ volatile int g = 0;
volatile int b = 0;
volatile char cmd_char = 'z';
const int serialDelay = 5;
void setup () {
Serial.begin(57600);
Serial.flush();
Serial.setTimeout(serialDelay);
//pixieSerial.begin(115200); // Pixie REQUIRES this baud rate
light.begin();
light.setPixelColor(0, 0, 0, 0);
light.setPixelColor(0, 0, 0, 0);
light.show();
}
@ -62,6 +64,16 @@ void cmd (char val) {
}
}
//takes 1004ms w/ string method
//takes 2ms(!!!!!) w/o readString() ugh
//WAIT!! Serial.setTimeout(1000) by default!
//500 - WORKS
//250 - WORKS
//100 - WORKS
//25? - WORKS
//5 - WORKS - STAY HERE FOR NOW
//got down to ~10ms, will take hours off 4000 frame roll
//timing may also change when switching to pixie
void colorString () {
while (Serial.available() == 0) {
//Wait for color string
@ -81,5 +93,5 @@ void colorString () {
b = strB.toInt();
light.setPixelColor(0, r, g, b);
light.show();
light.show();
}