Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assets/
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ As a convenience, if you would like to install StandardFirmata you can do so by:
interchange install StandardFirmata -a <board> -p <port>
```

For an interactive interface that will prompt you with choices for each option, use
```
interchange install --interactive
```

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably include a screen shot here as well showing what that interactive version looks like,

![Interactive CLI demo](https://github.com/ajfisher/nodebots-interchange/raw/master/assets/interchange.gif)

### Usage examples

Get help:
Expand Down
Binary file added assets/interchange.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var program = require('commander');
var version = require('../package.json').version;
var Interchange = require('../lib/interchange');
var Inquire = require('../lib/inquire');
var interchange = new Interchange();

program
Expand Down Expand Up @@ -32,6 +33,13 @@ program.command("install [firmware]")
.option("-p, --port <port>", "Serial port board is attached to")
.option("-f, --firmata [firmata]", "Install firmata version of firmware")
.option("-i, --address <address>", "Specify I2C address, eg 0x67")
.action(interchange.install_firmware.bind(interchange));
.option("--interactive", "Interactive mode will prompt for input")
.action(function (firmware, opts) {
if (opts.interactive) {
var inquire = new Inquire(interchange.install_firmware.bind(interchange));
} else {
interchange.install_firmware(firmware, opts);
}
});

program.parse(process.argv);
107 changes: 107 additions & 0 deletions lib/inquire.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
var inquirer = require('inquirer');
var Interchange = require('../lib/interchange');
var Inquire = require('../lib/inquire');
var firmwares = require('../lib/firmwares.json').firmwares;

var Inquire = function (cb) {

this.interchange = new Interchange();

this.callback = cb;

this.interchange.get_ports(function (err, ports) {

if (err) {
console.error(err);
return;
}

this.promptQuestions(ports);

}.bind(this));

};

Inquire.prototype.promptQuestions = function (ports) {

var questions = [
{
type: "list",
name: "firmware",
message: "Choose a firmware",
choices: this.interchange.firmwares.map(function (el) {
return el.name
})
},
{
type: "confirm",
name: "firmata",
message: "Install firmata version?",
default: function (answers) {
return answers.firmware.indexOf('Firmata') > -1;
},
when: function (answers) {
var firmware = firmwares.filter(function(obj) {
return obj.name === answers.firmware
});

return firmware.length && firmware[0].firmata && answers.firmware.indexOf('Firmata') === -1;
}
},
{
type: "input",
name: "firmataType",
message: "Firmata name [optional]",
default: null,
when: function (answers) {
return answers.firmata;
}
},
{
type: "list",
name: "avr",
message: "Choose a board",
choices: [
"uno",
"nano",
"promini",
],
default: "nano"
},
{
type: "list",
name: "port",
message: "Choose a port",
choices: ports.map(function (el) {
return el.comName;
}),
default: null
},
{
type: "input",
name: "address",
message: "Choose an I2C address [optional]",
default: null,
when: function (answers) {
return !answers.firmata && answers.firmware.indexOf('Firmata') === -1;
}
}
];

inquirer.prompt(questions, function(answers) {
var firmware = answers.firmware;
var opts = {
board : answers.avr,
port : answers.port,
address : answers.address,
firmata : answers.firmataType || answers.firmata
};

if (this.callback && typeof this.callback === 'function') {
this.callback(firmware, opts);
}
}.bind(this));

};

module.exports = Inquire;
12 changes: 10 additions & 2 deletions lib/interchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var fw = null; // used to hold details of the firmware we want in it.

var Interchange = function () {

this.firmwares = firmwares;

};

Interchange.prototype.clean_temp_dir = function (tmpdir) {
Expand All @@ -44,12 +46,18 @@ Interchange.prototype.list_devices = function () {
});
};

Interchange.prototype.get_ports = function (cb) {
Serialport.list(function (err, ports) {
cb(err, ports);
});
};

Interchange.prototype.list_ports = function (opts) {
// this function lists out all the ports available to flash firmware

var verbose = opts.verbose;

Serialport.list(function (err, ports) {
this.get_ports(function (err, ports) {
if (err) {
console.error(err);
return;
Expand Down Expand Up @@ -345,7 +353,7 @@ Interchange.prototype.check_firmware = function (firmware, options, cb) {
// to a temporary location

var board = options.board || "nano"; // assumes nano if none provided
var useFirmata = (firmware.indexOf('Firmata') > 0) || (options.firmata != null) || false;
var useFirmata = (firmware.indexOf('Firmata') > 0) || options.firmata || false;
var firmataName = options.firmata || "";
// check for default where no firmata name is supplied or default is implied.
if (firmataName === true) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"commander": "^2.9.0",
"download": "^4.4.1",
"fs-extra": "^0.24.0",
"inquirer": "^0.11.0",
"lodash": "^3.10.1",
"minimist": "^1.2.0",
"serialport": "^2.0.2",
Expand Down