Comparing version 0.0.9 to 0.0.10
@@ -15,5 +15,13 @@ #!/usr/bin/env node | ||
}; | ||
var isNextInvalid = function(next) { | ||
return !next || next.indexOf("-") !== -1 || next.indexOf(".js") !== -1; | ||
var isNextValidPort = function(next) { | ||
return next && next.indexOf("-") == -1 && next.indexOf(".js") == -1; | ||
} | ||
var isNextValidJS = function(next) { | ||
return next && next.indexOf("-") == -1 && next.indexOf(".js") >= 0; | ||
} | ||
var isNextValid = function(next) { | ||
return next && next.indexOf("-") == -1; | ||
} | ||
for (var i=2;i<process.argv.length;i++) { | ||
@@ -31,13 +39,16 @@ var arg = process.argv[i]; | ||
var j = (++i) + 1; | ||
while (!isNextInvalid(process.argv[j])) { | ||
while (isNextValidPort(process.argv[j])) { | ||
args.ports.push(process.argv[j++]); | ||
i++; | ||
} | ||
if (isNextInvalid(next)) throw new Error("Expecting a port argument to -p, --port"); | ||
if (!isNextValidPort(next)) throw new Error("Expecting a port argument to -p, --port"); | ||
} else if (arg=="-e") { | ||
i++; args.expr = next; | ||
if (isNextInvalid(next)) throw new Error("Expecting an expression argument to -e"); | ||
if (!isNextValid(next)) throw new Error("Expecting an expression argument to -e"); | ||
} else if (arg=="-o") { | ||
i++; args.outputJS = next; | ||
if (!isNextValidJS(next)) throw new Error("Expecting a JS filename argument to -o"); | ||
} else if (arg=="-f") { | ||
i++; args.updateFirmware = next; | ||
if (isNextInvalid(next)) throw new Error("Expecting a filename argument to -f"); | ||
if (!isNextValid(next)) throw new Error("Expecting a filename argument to -f"); | ||
} else throw new Error("Unknown Argument '"+arg+"', try --help"); | ||
@@ -54,3 +65,3 @@ } else { | ||
//Extra argument stuff | ||
args.espruinoPrefix = args.quiet?"":"--]"; | ||
args.espruinoPrefix = args.quiet?"":"--] "; | ||
args.espruinoPostfix = ""; | ||
@@ -86,2 +97,3 @@ if (args.color) { | ||
" -p,--port /dev/ttyX : Specify port(s) to connect to", | ||
" -o out.js : Write the actual JS code sent to Espruino to a file", | ||
" -f firmware.bin : Update Espruino's firmware to the given file", | ||
@@ -93,2 +105,6 @@ " Espruino must be in bootloader mode", | ||
"", | ||
"If no file, command, or firmware update is specified, this will act", | ||
"as a terminal for communicating directly with Espruino. Press Ctrl-C", | ||
"twice to exit.", | ||
"", | ||
"Please report bugs via https://github.com/espruino/EspruinoTool/issues", | ||
@@ -100,2 +116,3 @@ ""]. | ||
/* Connect and send file/expression/etc */ | ||
function connect(port, exitCallback) { | ||
@@ -151,2 +168,6 @@ if (!args.quiet) log("Connecting to '"+port+"'"); | ||
Espruino.callProcessor("transformForEspruino", code, function(code) { | ||
if (args.outputJS) { | ||
log("Writing output to "+args.outputJS); | ||
require("fs").writeFileSync(args.outputJS, code); | ||
} | ||
Espruino.Core.CodeWriter.writeToEspruino(code, function() { | ||
@@ -159,14 +180,75 @@ exitTimeout = setTimeout(exitCallback, 500); | ||
}, function() { | ||
log("Disconnected"); | ||
log("Disconnected."); | ||
}); | ||
} | ||
function main() { | ||
setupConfig(Espruino); | ||
/* Connect and enter terminal mode */ | ||
function terminal(port, exitCallback) { | ||
if (!args.quiet) log("Connecting to '"+port+"'"); | ||
var hadCtrlC = false; | ||
var hadCR = false; | ||
process.stdin.setRawMode(true); | ||
Espruino.Core.Serial.startListening(function(data) { | ||
data = new Uint8Array(data); | ||
process.stdout.write(String.fromCharCode.apply(null, data)); | ||
/* If Espruino responds after a Ctrl-C with anything other | ||
than a blank prompt, make sure the next Ctrl-C will exit */ | ||
for (var i=0;i<data.length;i++) { | ||
var ch = data[i]; | ||
if (ch==8) hadCR = true; | ||
else { | ||
if (hadCtrlC && (ch!=62 /*>*/ || !hadCR)) { | ||
//process.stdout.write("\nCTRLC RESET BECAUSE OF "+JSON.stringify(String.fromCharCode.apply(null, data))+" "+hadCR+" "+ch+"\n"); | ||
hadCtrlC = false; | ||
} | ||
hadCR = false; | ||
} | ||
} | ||
}); | ||
Espruino.Core.Serial.open(port, function(status) { | ||
if (status === undefined) { | ||
console.error("Unable to connect!"); | ||
return exitCallback(); | ||
} | ||
if (!args.quiet) log("Connected"); | ||
process.stdin.on('readable', function() { | ||
var chunk = process.stdin.read(); | ||
if (chunk !== null) { | ||
chunk = chunk.toString(); | ||
Espruino.Core.Serial.write(chunk); | ||
// Check for two Ctrl-C in a row (without Espruino doing anything inbetween) | ||
for (var i=0;i<chunk.length;i++) { | ||
var ch = chunk.charCodeAt(i); | ||
if (ch==3) { | ||
if (hadCtrlC) { | ||
process.stdout.write("\r\n"); | ||
exitCallback(); | ||
} else { | ||
setTimeout(function() { | ||
if (hadCtrlC) process.stdout.write("\nPress Ctrl-C again to exit\n>"); | ||
}, 200); | ||
} | ||
hadCtrlC = true; | ||
} | ||
} | ||
} | ||
}); | ||
if (Espruino.Core.Serial === undefined) { | ||
console.error("No serial driver found"); | ||
return; | ||
} | ||
if (args.ports.length > 0) { | ||
process.stdin.on('end', function() { | ||
console.log("STDIN ended. exiting..."); | ||
exitCallback(); | ||
}); | ||
}, function() { | ||
log("\nDisconnected."); | ||
exitCallback(); | ||
}); | ||
} | ||
function startConnect() { | ||
if (!args.file && !args.updateFirmware && !args.expr) { | ||
if (args.ports.length != 1) | ||
throw new Error("Can only have one port when using terminal mode"); | ||
terminal(args.ports[0], function() { process.exit(0); }); | ||
} else { | ||
//closure for stepping through each port | ||
@@ -183,12 +265,20 @@ //and connect + upload (use timeout callback [iterate] for proceeding) | ||
})(args.ports, connect); | ||
} else { | ||
log("Searching for serial ports..."); | ||
} | ||
} | ||
function main() { | ||
setupConfig(Espruino); | ||
if (args.ports.length == 0) { | ||
console.log("Searching for serial ports..."); | ||
Espruino.Core.Serial.getPorts(function(ports) { | ||
console.log(ports); | ||
if (ports.length>0) | ||
connect(ports[0], function() { process.exit(0); }); | ||
else | ||
console.log("PORTS:\n "+ports.join("\n ")); | ||
if (ports.length>0) { | ||
log("Using first port, "+ports[0]); | ||
args.ports = [ports[0]]; | ||
startConnect(); | ||
} else | ||
throw new Error("No Ports Found"); | ||
}); | ||
} | ||
} else startConnect(); | ||
} | ||
@@ -195,0 +285,0 @@ |
{ | ||
"name": "espruino", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "Command Line Interface and library for Communications with Espruino JavaScript Microcontrollers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -6,3 +6,3 @@ Espruino Tools | ||
While it is used directly by the [Espruino Web IDE](http://www.github.com/espruino/EspruinoWebIDE), there's are also simple command-lune and `node.js` interfaces. | ||
While it is used directly by the [Espruino Web IDE](http://www.github.com/espruino/EspruinoWebIDE), there's are also simple command-line and `node.js` interfaces. | ||
@@ -23,2 +23,3 @@ | ||
-p,--port /dev/ttyX : Specify port(s) to connect to | ||
-o out.js : Write the actual JS code sent to Espruino to a file | ||
-f firmware.bin : Update Espruino's firmware to the given file | ||
@@ -29,2 +30,6 @@ Espruino must be in bootloader mode | ||
Espruino will not be reset | ||
If no file, command, or firmware update is specified, this will act | ||
as a terminal for communicating directly with Espruino. Press Ctrl-C | ||
twice to exit. | ||
``` | ||
@@ -35,2 +40,8 @@ | ||
``` | ||
# Connect to Espruno and act as a terminal app (IF Espruino is the only serial port reported) | ||
espruino | ||
# Connect to Espruino on the specified port, act as a terminal | ||
espruino -p /dev/ttyACM0 | ||
# Write a program to Espruino (IF Espruino is the only serial port reported) | ||
@@ -112,3 +123,2 @@ espruino myprogram.js | ||
* Remove usage of `console.log` and replace it with something else that can be easily disabled when used as a module | ||
* Proper 'terminal mode' where you can interact with Espruino directly from the command-line | ||
@@ -118,2 +128,3 @@ Cool stuff would be: | ||
* Add a command-line option (`-w`?) to watch a file and re-upload it when it has changed | ||
* Allow the final code that's sent to Espruino to be written to a file (useful when there's Compilation/assembly/modules/minification involved). | ||
* Add an option to expose the serial connection via WebSockets. Something [like the online Web IDE](http://espruino.github.io/EspruinoWebIDE/) could the communicate directly. | ||
@@ -120,0 +131,0 @@ * Support for Nordic UART via [bleat](https://www.npmjs.com/package/bleat) |
Sorry, the diff of this file is not supported yet
730478
17666
163
25