Skip to content

Commit

Permalink
added run option to the terminal command
Browse files Browse the repository at this point in the history
  • Loading branch information
AndiDittrich committed May 28, 2016
1 parent 9cdcc40 commit 761348b
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 1.6.0-BETA1 ###
* Added: `--run` option to the terminal command to execute a file on the nodemcu when opening a new terminal session - feature requested by [blezek on GitHub](https://github.com/AndiDittrich/NodeMCU-Tool/issues/11)
* Added: [gulp](http://gulpjs.com/) based build example - thanks to [remcoder](https://github.com/AndiDittrich/NodeMCU-Tool/commits/master?author=remcoder)

### 1.5.0 ###
* Added: Bulk/Multi File upload
* Added: `devices` command to display a list of all connected NodeMCU Devices
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,11 @@ You can quit the terminal session by pressing <kbd>ctrl+c</kbd>

**Syntax** `nodemcu-tool [options] terminal`

**Example**
**Options**

* `--run <filename>` | Executes a command on start of the terminal session - useful for testing/debugging

**Example 1**

```shell
$ nodemcu-tool terminal
Expand All @@ -532,6 +536,29 @@ String: Lorem ipsum dolor sit amet, consetetur sadipscing elitr
> [SerialTerminal] Connection closed
```

**Example 2**
```shell
$ nodemcu-tool terminal --run helloworld.lua
[SerialTerminal] Starting Terminal Mode - press ctrl+c to exit
dofile("helloworld.lua")
Hello World!
|---|
| H |
| E |
| L |
| L |
| O |
| | |
| W |
| O |
| R |
| L |
| D |
|---|
YEAH!!! HELLO WORLD!!!
String: Lorem ipsum dolor sit amet, consetetur sadipscing elitr
```

Behind the Scene
----------------
Many beginners may ask how the tool is working because there is no binary interface documented like FTP to access files.
Expand Down
13 changes: 11 additions & 2 deletions bin/nodemcu-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var _progressbar = require('cli-progress');
var _pkg = require('../package.json')
var _prompt = require('prompt')
var _nodemcutool = require('../lib/NodeMCU-Tool');
var _luaCommandBuilder = require('../lib/LuaCommandBuilder');
var _colors = require('colors');
var _fs = require('fs');

Expand Down Expand Up @@ -263,11 +264,19 @@ _cli
_cli
.command('terminal')
.description('Opens a Terminal connection to NodeMCU')
.action(function(){
.option('--run <filename>', 'Running a file on NodeMCU before starting the terminal session', false)
.action(function(options){
// silent mode ?
SilentMode(_cli.silent===true);

_nodemcutool.terminal(_cli.port, _cli.baud);
// run a initial command on startup ?
var initialCommand = null;
if (options.run){
initialCommand = _luaCommandBuilder.prepare('run', [options.run]);
}

// start terminal session
_nodemcutool.terminal(_cli.port, _cli.baud, initialCommand);
});

_cli
Expand Down
9 changes: 8 additions & 1 deletion lib/NodeMCU-Tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ Tool.mkfs = function(port, baud){
};

// serial terminal <> console session
Tool.terminal = function(port, baud){
Tool.terminal = function(port, baud, initialCommand){
// create new connector
var terminal = new _serialTerminal();

Expand All @@ -380,6 +380,13 @@ Tool.terminal = function(port, baud){

logStatus('SerialTerminal', 'Starting Terminal Mode - press ctrl+c to exit');

// run initial command before starting terminal session ?
if (initialCommand){
terminal.onConnect(function(device){
device.write(initialCommand + '\n');
});
}

// start
terminal.passthrough(port, baud, function(err){
if (err){
Expand Down
12 changes: 12 additions & 0 deletions lib/SerialTerminal.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ var _serialport = require('serialport');
function SerialTerminal(){
this.device = null;
this.errorHandler = null;
this.connectHandler = null;
this.encoding = 'utf8';
}

SerialTerminal.prototype.onError = function(cb){
this.errorHandler = cb;
};

SerialTerminal.prototype.onConnect = function(cb){
this.connectHandler = cb;
};

SerialTerminal.prototype.passthrough = function(devicename, baudrate, cb){

// try to open the serial port
Expand Down Expand Up @@ -46,9 +51,11 @@ SerialTerminal.prototype.passthrough = function(devicename, baudrate, cb){
cb(error);
}else{

// prepare
process.stdin.setRawMode(true);
process.stdin.setEncoding('utf8');

// pass-through
process.stdin.on('data', function(data){
// ctrl-c
if (data == '\u0003'){
Expand All @@ -58,6 +65,11 @@ SerialTerminal.prototype.passthrough = function(devicename, baudrate, cb){
}
this.device.write(data);
}.bind(this));

// run connect handler ?
if (this.connectHandler){
this.connectHandler.apply(this.connectHandler, [this.device]);
}
}
}.bind(this));
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodemcu-tool",
"version": "1.5.0",
"version": "1.6.0",
"description": "Command line tool to upload and manage files on your NodeMCU / ESP8266 Module.",
"keywords": [
"cli",
Expand Down

0 comments on commit 761348b

Please sign in to comment.