Skip to content

Commit 6061e44

Browse files
committed
add --sleep option to leave the connection open for a while after writing
1 parent ae4fc7c commit 6061e44

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ USAGE: espruino ...options... [file_to_upload.js]
3030
-e command : Evaluate the given expression on Espruino
3131
If no file to upload is specified but you use -e,
3232
Espruino will not be reset
33+
--sleep 10 : Sleep for the given number of seconds after uploading code
3334
-n : Do not connect to Espruino to upload code
3435
--board BRDNAME/BRD.json : Rather than checking on connect, use the given board name or file
3536
--ide [8080] : Serve up the Espruino Web IDE on the given port. If not specified, 8080 is the default.

bin/espruino-cli.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function getHelp() {
1919
" -e command : Evaluate the given expression on Espruino",
2020
" If no file to upload is specified but you use -e,",
2121
" Espruino will not be reset",
22+
" --sleep 10 : Sleep for the given number of seconds after uploading code",
2223
" -n : Do not connect to Espruino to upload code",
2324
" --board BRDNAME/BRD.json : Rather than checking on connect, use the given board name or file",
2425
" --ide [8080] : Serve up the Espruino Web IDE on the given port. If not specified, 8080 is the default.",
@@ -64,6 +65,9 @@ var args = {
6465
storageContents : {} // Storage files to save when using ohex
6566
};
6667

68+
var isNextValidNumber = function(next) {
69+
return next && isFinite(parseFloat(next));
70+
}
6771
var isNextValidPort = function(next) {
6872
return next && next[0]!=='-' && next.indexOf(".js") == -1;
6973
}
@@ -154,6 +158,9 @@ for (var i=2;i<process.argv.length;i++) {
154158
args.ideServer = parseInt(next);
155159
i++;
156160
}
161+
} else if (arg=="--sleep") {
162+
i++; args.sleepAfterUpload = parseFloat(next);
163+
if (!isNextValidNumber(next)) throw new Error("Expecting a number argument to --sleep");
157164
} else throw new Error("Unknown Argument '"+arg+"', try --help");
158165
} else {
159166
if ("file" in args)
@@ -440,7 +447,15 @@ function sendCode(callback) {
440447
require("fs").writeFileSync(args.outputHEX, toIntelHex(storage));
441448
}
442449
if (!args.nosend)
443-
Espruino.Core.CodeWriter.writeToEspruino(code, callback);
450+
Espruino.Core.CodeWriter.writeToEspruino(code, function() {
451+
if (args.sleepAfterUpload) {
452+
log("Upload Complete. Sleeping for "+args.sleepAfterUpload+"s");
453+
setTimeout(callback, args.sleepAfterUpload*1000);
454+
} else {
455+
log("Upload Complete");
456+
callback();
457+
}
458+
});
444459
else
445460
callback();
446461
});
@@ -455,10 +470,9 @@ function connect(devicePath, exitCallback) {
455470
if (!args.quiet) if (! args.nosend) log("Connecting to '"+devicePath+"'");
456471
var currentLine = "";
457472
var exitTimeout;
473+
// Handle received data
458474
Espruino.Core.Serial.startListening(function(data) {
459-
// convert ArrayBuffer to string
460475
data = String.fromCharCode.apply(null, new Uint8Array(data));
461-
// Now handle...
462476
currentLine += data;
463477
while (currentLine.indexOf("\n")>=0) {
464478
var i = currentLine.indexOf("\n");
@@ -684,13 +698,18 @@ function getPortPath(port, callback) {
684698
} else throw new Error("Unknown port type! "+JSON.stringify(port));
685699
}
686700

701+
function tasksComplete() {
702+
console.log("Done");
703+
process.exit(0);
704+
}
705+
687706
function startConnect() {
688707
if ((!args.file && !args.updateFirmware && !args.expr) || (args.file && args.watchFile)) {
689708
if (args.ports.length != 1)
690709
throw new Error("Can only have one port when using terminal mode");
691710

692-
getPortPath(args.ports[0], function(path) {
693-
terminal(path, function() { process.exit(0); });
711+
getPortPath(args.ports[0], function(path) {
712+
terminal(path, tasksComplete);
694713
});
695714
} else {
696715
//closure for stepping through each port
@@ -700,7 +719,7 @@ function startConnect() {
700719
this.idx = 0;
701720
this.connect = connect;
702721
this.iterate = function() {
703-
if (idx>=ports.length) process.exit(0);
722+
if (idx>=ports.length) tasksComplete();
704723
else getPortPath(ports[idx++], function(path) {
705724
connect(path,iterate);
706725
});
@@ -716,10 +735,7 @@ function main() {
716735
if (args.ports.length == 0 && (args.outputJS || args.outputHEX)) {
717736
console.log("No port supplied, but output file listed - not connecting");
718737
args.nosend = true;
719-
sendCode(function() {
720-
log("File written. Exiting.");
721-
process.exit(1);
722-
});
738+
sendCode(tasksComplete);
723739
} else if (args.ports.length == 0 || args.showDevices) {
724740
console.log("Searching for serial ports...");
725741
Espruino.Core.Serial.getPorts(function(ports, shouldCallAgain) {

0 commit comments

Comments
 (0)