Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fastupload option #34

Closed
wants to merge 4 commits into from
Closed
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ The most important task of this tool: upload local files to the module.
* `--compile` | Compiles the uploaded .lua file into executable bytecode and removes the source .lua file (performance)
* `--keeppath` | Keeps the relative file path in the destination filename (i.e: static/test.html will be named static/test.html)
* `--remotename` | Set the destination file name
* `--fastupload` | Boost the upload speed by using base64 encoding. **IMPORTANT**: The _NodeMCU_ module "**encode**" must be present on the firmware (which is not the case by default).

**Example 1**

Expand Down
7 changes: 6 additions & 1 deletion bin/nodemcu-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ function cliPrepare(options){
all: options.all || false,
json: options.json || false,
raw: options.raw || false,
softreset: options.softreset || false
softreset: options.softreset || false,
fastupload: options.fastupload || false
};

// project based configuration
Expand All @@ -94,6 +95,7 @@ function cliPrepare(options){
defaultConfig.optimize = (d.optimize && d.optimize === true);
defaultConfig.compile = (d.compile && d.compile === true);
defaultConfig.keeppath = (d.keeppath && d.keeppath === true);
defaultConfig.fastupload = (d.fastupload && d.fastupload === true);
_logger.log('Project based configuration loaded');
}
}catch (err){
Expand Down Expand Up @@ -181,6 +183,9 @@ _cli
// sets the remote filename
.option('-n, --remotename <remotename>', 'Set destination file name. Default is same as original. Only available when uploading a single file!', false)

// use fast upload
.option('-f, --fastupload', 'Boost the upload speed by using base64 encoding. IMPORTANT: The NodeMCU module "encode" must be present on the firmware (which is not the case by default)!', false)

.action(function(localFiles, opt){
var options = cliPrepare(opt);

Expand Down
3 changes: 3 additions & 0 deletions lib/LuaCommandBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ var lua_commands = {
// helper function to write hex encoded content to file
hexWriteHelper: "function __hexwrite(s) for c in s:gmatch('..') do file.write(string.char(tonumber(c, 16))) end end",

// helper function to write base64 encoded content to file. The module "encoder" must be available in the NodeMCU Firmeware.
base64WriteHelper: "function __hexwrite(s) file.write(encoder.fromBase64(s)) end",

// helper function to read file as hex and write content to uart
hexReadHelper: "function __hexread() while true do c = file.read(1) if c == nil then print('') break end uart.write(0, string.format('%02X', string.byte(c))) end end"
};
Expand Down
13 changes: 7 additions & 6 deletions lib/NodeMcuConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function NodeMcuConnector(devicename, baudrate){
this.isConnected = false;
this.name = devicename;
this.baudrate = baudrate;
this.isHexWriteHelperUploaded = false;
this.isWriteHelperUploaded = false;

// handle low level errors
this.device.onError(function(err){
Expand Down Expand Up @@ -212,7 +212,7 @@ NodeMcuConnector.prototype.upload = function(localName, remoteName, options, com
}

// convert buffer to hex
var content = rawContent.toString('hex');
var content = options.fastupload ? rawContent.toString('base64') : rawContent.toString('hex');

// get absolute filesize
var absoluteFilesize = rawContent.length;
Expand Down Expand Up @@ -241,7 +241,7 @@ NodeMcuConnector.prototype.upload = function(localName, remoteName, options, com
var l = chunks.shift();

// increment size counter
currentUploadSize += l.length/2 ;
currentUploadSize += (options.fastupload ? l.length : l.length/2);

// write first element to file
this.device.executeCommand('__hexwrite("' + l + '")', function(err, echo, response){
Expand Down Expand Up @@ -278,22 +278,23 @@ NodeMcuConnector.prototype.upload = function(localName, remoteName, options, com


// hex write helper already uploaded within current session ?
if (this.isHexWriteHelperUploaded){
if (this.isWriteHelperUploaded){
// start transfer directly
startTransfer();

// otherwise upload helper
}else{
// transfer helper function to decode hex data
this.device.executeCommand(_luaCommandBuilder.command.hexWriteHelper, function(err, echo, response) {
var command = options.fastupload ? _luaCommandBuilder.command.base64WriteHelper : _luaCommandBuilder.command.hexWriteHelper;
this.device.executeCommand(command, function(err, echo, response) {
// successful opened ?
if (err) {
completeCb('Cannot transfer hex.decode helper function - ' + err);
return;
}

// set flag
this.isHexWriteHelperUploaded = true;
this.isWriteHelperUploaded = true;

// start file transfer on upload complete
startTransfer();
Expand Down