Skip to content

Commit

Permalink
1.08: Add UART.getConnectionAsync()
Browse files Browse the repository at this point in the history
      Add .espruinoEval(... {stmFix:true}) to work around occasional STM32 USB issue in 2v24 and earlier firmwares
  • Loading branch information
gfwilliams committed Oct 7, 2024
1 parent bfd7e46 commit 44d7acb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion examples/uart.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
<script>console.log("Use UART.debug=3 for full debug info")</script>
<button onclick="UART.write('\x03LED1.set();\n');">On!</button>
<button onclick="UART.write('\x03LED1.reset();\n');">Off!</button><br>
<button id="temp" onclick="UART.eval('E.getTemperature()', function(x) { document.getElementById('temp').innerHTML = 'Temperature '+x; })">Read Temperature</button><br>
<button id="temp" onclick="UART.eval('E.getTemperature()', function(x) { document.getElementById('temp').innerHTML = 'Temperature '+x; })">Read Temperature (UART.eval)</button><br>
<button id="temp2" onclick="UART.getConnectionAsync().then(c=>c.espruinoEval('E.getTemperature()',{workaround:true})).then(function(x) { document.getElementById('temp2').innerHTML = 'Temperature '+x; })">Read Temperature (.espruinoEval)</button><br>
<button onclick="UART.write('\x03var t=getTime()+5;while(getTime()<t);LED1.set();\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\n\'Lots and lots of text Lots and lots of text Lots and lots of text\'\nLED1.reset();\n');">Test flow control</button><br>

<button onclick="UART.close();">Disconnect</button>
<script>UART.debug=3;</script>
</body>
</html>
26 changes: 21 additions & 5 deletions uart.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ UART.getConnection().espruinoEval("1+2").then(res => console.log("=",res));
ChangeLog:
...
1.08: Add UART.getConnectionAsync()
Add .espruinoEval(... {stmFix:true}) to work around occasional STM32 USB issue in 2v24 and earlier firmwares
1.07: Added UART.getConnection().espruinoEval
1.06: Added optional serialPort parameter to UART.connect(), allowing a known Web Serial port to be used
Added connectAsync, and write/eval now return promises
Expand Down Expand Up @@ -535,19 +537,25 @@ To do:
/* Send a JS expression to be evaluated on Espruino using using 2v25 packets.
options = {
timeout : int // milliseconds timeout
stmFix : bool // if set, this works around an issue in Espruino STM32 2v24 and earlier where USB could get in a state where it only sent small chunks of data at a time
}*/
espruinoEval(expr, options) {
options = options || {};
if ("string"!=typeof expr) throw new Error("'expr' must be a String");
let connection = this;
let resolve, reject, timeout;
return new Promise((resolve,reject) => {
let prodInterval;

function cleanup() {
connection.removeListener("packet", onPacket);
if (timeout) {
clearTimeout(timeout);
timeout = undefined;
}
if (prodInterval) {
clearInterval(prodInterval);
prodInterval = undefined;
}
}
function onPacket(type,data) {
if (type!=0) return; // ignore things that are not a response
Expand All @@ -556,13 +564,17 @@ To do:
}
connection.parsePackets = true;
connection.on("packet", onPacket);
timeout = setTimeout(() => {
let timeout = setTimeout(() => {
timeout = undefined;
cleanup();
reject("espruinoEval Timeout");
}, options.timeout || 1000);
connection.espruinoSendPacket("EVAL",expr).then(()=>{
connection.espruinoSendPacket("EVAL",expr,{noACK:options.stmFix}).then(()=>{
// resolved/rejected with 'packet' event or timeout
if (options.stmFix)
prodInterval = setInterval(function() {
connection.write(" \x08"); // space+backspace
}, 50);
}, err => {
cleanup();
reject(err);
Expand Down Expand Up @@ -1050,7 +1062,7 @@ To do:
// ----------------------------------------------------------

var uart = {
version : "1.07",
version : "1.08",
/// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
debug : 1,
/// Should we use flow control? Default is true
Expand Down Expand Up @@ -1088,10 +1100,14 @@ To do:
isConnected : function() {
return connection!==undefined;
},
/// get the connection used by `write` and `eval`
/// get the connection used by `write` and `eval`, or return undefined
getConnection : function() {
return connection;
},
/// Return a promise with the connection used by `write` and `eval`, and if there's no connection attempt to get one
getConnectionAsync : function() {
return connection ? Promise.resolve(connection) : uart.connectAsync();
},
/// Close the connection used by `write` and `eval`
close : function() {
if (connection)
Expand Down

0 comments on commit 44d7acb

Please sign in to comment.