diff --git a/CHANGES.md b/CHANGES.md index 08cf701..596b6b9 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,9 +1,10 @@ ### 1.1.0 ### -Added: NodeMCU-Tool to use the connector as well as all CLI functions programmatic +Added: NodeMCU-Tool.js to use the connector as well as all CLI functions programmatically Added: Colorized terminal messages -Changed: the bin tool has been separated in two files (CLI and API layer) -Changed: all errors are handled as callbacks (node.js standard) -Bugfix: Replaced the progress bar to avoid random empty line terminal output +Added: Low Level API for direct interactions with the LUA interpreter +Changed: The bin tool has been separated in two files (CLI and API layer) +Changed: all errors are handled as callbacks (Node.js standard) +Bugfix: Replaced the progress bar with [cli-progress](https://www.npmjs.com/package/cli-progress) to avoid random empty lines on the terminal ### 1.0.0 ### Initial public release \ No newline at end of file diff --git a/README.md b/README.md index 95ee998..0841ee5 100755 --- a/README.md +++ b/README.md @@ -266,6 +266,37 @@ Therefore keep attention to the following limitations: * You cannot upload files over the serial connection with more than **235 characters per line** * Your script must not use the string initialization sequence **[===[** + +Low Level API +------------- +It's possible to use the underlying "NodeMcuConnector" in your own projects to communicate with a NodeMCU based device. +For more details, take a look into the sources! + +**Low Level Example** +Run `node.compile()` on NodeMCU and display the output + +```js +var _connector = require('nodemcu-tool').Connector; + +// create a new connector instance +var con = new _connector('/dev/ttyUSB4', 9600); + +// open the serial connection +con.connect(function(err, response){ + // get version, flashid ... message + console.log(response); + + // run a command on the LUA command line + con.executeCommand('node.compile("testfile.lua");', function(err, echo, response){ + if (err){ + console.error('IO Error - ', err); + }else{ + console.log(response); + } + }); +}); +``` + Any Questions ? Report a Bug ? Enhancements ? --------------------------------------------- Please open a new issue on [GitHub](https://github.com/AndiDittrich/NodeMCU-Tool/issues) diff --git a/bin/nodemcu-tool.js b/bin/nodemcu-tool.js index a06e107..27bcea7 100755 --- a/bin/nodemcu-tool.js +++ b/bin/nodemcu-tool.js @@ -2,7 +2,7 @@ // load utils var _cli = require('commander'); -var _progressbar = require('progress'); +var _progressbar = require('cli-progress'); var _pkg = require('../package.json') var _prompt = require('prompt') var _nodemcutool = require('../lib/NodeMCU-Tool'); @@ -62,14 +62,23 @@ _cli .option('-c, --compile', 'Compile LUA file to bytecode (.lc) and remove the original file after upload', false) .action(function(localFile, options){ - var p = null; + // initialize a new progress bar + var bar = new _progressbar.Bar({ + format: 'Upload Status {percentage}% [{bar}] | ETA {eta}s', + clearOnComplete: true + }); _nodemcutool.upload(_cli.port, _cli.baud, localFile, options, function(current, total){ - - if (p){ - - }else{ - + // bar initialized ? + if (current == 0) { + bar.start(total, 0); + }else { + bar.update(current); + + // finished ? + if (current >= total) { + bar.stop(); + } } }); }); diff --git a/lib/NodeMCU-Tool.js b/lib/NodeMCU-Tool.js index 0376e0d..497dfae 100755 --- a/lib/NodeMCU-Tool.js +++ b/lib/NodeMCU-Tool.js @@ -49,6 +49,11 @@ var Tool = { connector.onError(errorHandler); connector.connect(function(error, response){ + if (error){ + logError('NodeMCU-Tool', 'Unable to establish connection'); + return; + } + logStatus('NodeMCU-Tool', 'Connected'); logStatus('NodeMCU', response); @@ -86,6 +91,11 @@ var Tool = { connector.onError(errorHandler); connector.connect(function(error, response){ + if (error){ + logError('NodeMCU-Tool', 'Unable to establish connection'); + return; + } + logStatus('NodeMCU-Tool', 'Connected'); logStatus('NodeMCU', response); logStatus('NodeMCU-Tool', 'Uploading "' + localFile + '" ...'); @@ -137,6 +147,11 @@ var Tool = { connector.onError(errorHandler); connector.connect(function(error, response){ + if (error){ + logError('NodeMCU-Tool', 'Unable to establish connection'); + return; + } + logStatus('NodeMCU-Tool', 'Connected'); logStatus('NodeMCU', response); @@ -166,6 +181,11 @@ var Tool = { connector.onError(errorHandler); connector.connect(function(error, response){ + if (error){ + logError('NodeMCU-Tool', 'Unable to establish connection'); + return; + } + logStatus('NodeMCU-Tool', 'Connected'); logStatus('NodeMCU', response); @@ -192,6 +212,11 @@ var Tool = { connector.onError(errorHandler); connector.connect(function(error, response){ + if (error){ + logError('NodeMCU-Tool', 'Unable to establish connection'); + return; + } + logStatus('NodeMCU-Tool', 'Connected'); logStatus('NodeMCU', response); logStatus('NodeMCU', 'Formatting the file system...this will take around ~30s'); diff --git a/lib/NodeMcuConnector.js b/lib/NodeMcuConnector.js index c6d3c26..1824d57 100755 --- a/lib/NodeMcuConnector.js +++ b/lib/NodeMcuConnector.js @@ -359,4 +359,16 @@ NodeMcuConnector.prototype.run = function(filename, cb){ }.bind(this)); }; +// LOW LEVEL - run a lua command directly +NodeMcuConnector.prototype.executeCommand = function(cmd, cb){ + // check connect flag + if (!this.isConnected){ + cb('Cannot execute remote file - device offline', null); + return; + } + + // run the lua interpreter + this.device.executeCommand(cmd, cb); +}; + module.exports = NodeMcuConnector; \ No newline at end of file diff --git a/package.json b/package.json old mode 100644 new mode 100755 index 5b31ed2..b06973f --- a/package.json +++ b/package.json @@ -27,9 +27,9 @@ "author": "Andi Dittrich (http://andidittrich.de)", "license": "MIT", "dependencies": { + "cli-progress": "^1.0.1", "colors": "^1.1.2", "commander": "^2.9.0", - "progress": "^1.1.8", "prompt": "^0.2.14", "serialport": "^2.0.5" }