Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var fs = require('fs');
var Q = require('q');
var readline = require('readline');

var ihex = require('./ihex');
var programmer = require('./programmer');
Expand All @@ -22,8 +23,47 @@ var device = firmware.then(function() {
function find_device() {
return programmer.list_devices().then(function(devs) {
if (devs.length == 0) throw new Error('No devices found.');
if (devs.length > 1) throw new Error('More than one device found.');
console.log('Found device in ' + devs[0].mode + ' mode on bus ' + devs[0].device.busNumber + ', device ' + devs[0].device.deviceAddress + '.');

for (var i = 0; i < devs.length; i++) {
console.log('#' + i + ': Found device in ' + devs[i].mode + ' mode on bus ' + devs[i].device.busNumber + ', device ' + devs[i].device.deviceAddress + ':');
console.log(devs[i]);
console.log();
}

if (devs.length > 1) {
var programDevices = devs.filter(function(dev) {
return dev.mode === 'program';
});
if (programDevices.length === 1) {
var programDeviceIndex = devs.findIndex(function(dev) {
return dev.mode === 'program';
});
console.log(`Automatically selected device #${programDeviceIndex} (only device in program mode)`);
return devs[programDeviceIndex];
}

var deferred = Q.defer();

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question('Multiple devices found. Please select a device by ID from 0 to ' + (devs.length - 1) + ': ', function(answer) {
const deviceIndex = parseInt(answer, 10);
rl.close();

if (isNaN(deviceIndex) || deviceIndex < 0 || deviceIndex >= devs.length) {
deferred.reject(new Error('Invalid device selection.'));
} else {
console.log(`Selected device #${deviceIndex}`);
deferred.resolve(devs[deviceIndex]);
}
});

return deferred.promise;
}

return devs[0];
}).then(function(dev) {
if (dev.mode == 'normal') {
Expand Down Expand Up @@ -56,6 +96,7 @@ Q.all([firmware, device]).spread(function(firmware, device) {
});
}).then(function() {
console.log('Done.');
process.exit(0);
}).catch(function(err) {
console.log(err.toString());
process.exit(1);
Expand Down