Skip to content

Commit 44d7acb

Browse files
committed
1.08: Add UART.getConnectionAsync()
Add .espruinoEval(... {stmFix:true}) to work around occasional STM32 USB issue in 2v24 and earlier firmwares
1 parent bfd7e46 commit 44d7acb

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

examples/uart.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
<script>console.log("Use UART.debug=3 for full debug info")</script>
77
<button onclick="UART.write('\x03LED1.set();\n');">On!</button>
88
<button onclick="UART.write('\x03LED1.reset();\n');">Off!</button><br>
9-
<button id="temp" onclick="UART.eval('E.getTemperature()', function(x) { document.getElementById('temp').innerHTML = 'Temperature '+x; })">Read Temperature</button><br>
9+
<button id="temp" onclick="UART.eval('E.getTemperature()', function(x) { document.getElementById('temp').innerHTML = 'Temperature '+x; })">Read Temperature (UART.eval)</button><br>
10+
<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>
1011
<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>
1112

1213
<button onclick="UART.close();">Disconnect</button>
14+
<script>UART.debug=3;</script>
1315
</body>
1416
</html>

uart.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ UART.getConnection().espruinoEval("1+2").then(res => console.log("=",res));
8787
ChangeLog:
8888
8989
...
90+
1.08: Add UART.getConnectionAsync()
91+
Add .espruinoEval(... {stmFix:true}) to work around occasional STM32 USB issue in 2v24 and earlier firmwares
9092
1.07: Added UART.getConnection().espruinoEval
9193
1.06: Added optional serialPort parameter to UART.connect(), allowing a known Web Serial port to be used
9294
Added connectAsync, and write/eval now return promises
@@ -535,19 +537,25 @@ To do:
535537
/* Send a JS expression to be evaluated on Espruino using using 2v25 packets.
536538
options = {
537539
timeout : int // milliseconds timeout
540+
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
538541
}*/
539542
espruinoEval(expr, options) {
540543
options = options || {};
541544
if ("string"!=typeof expr) throw new Error("'expr' must be a String");
542545
let connection = this;
543-
let resolve, reject, timeout;
544546
return new Promise((resolve,reject) => {
547+
let prodInterval;
548+
545549
function cleanup() {
546550
connection.removeListener("packet", onPacket);
547551
if (timeout) {
548552
clearTimeout(timeout);
549553
timeout = undefined;
550554
}
555+
if (prodInterval) {
556+
clearInterval(prodInterval);
557+
prodInterval = undefined;
558+
}
551559
}
552560
function onPacket(type,data) {
553561
if (type!=0) return; // ignore things that are not a response
@@ -556,13 +564,17 @@ To do:
556564
}
557565
connection.parsePackets = true;
558566
connection.on("packet", onPacket);
559-
timeout = setTimeout(() => {
567+
let timeout = setTimeout(() => {
560568
timeout = undefined;
561569
cleanup();
562570
reject("espruinoEval Timeout");
563571
}, options.timeout || 1000);
564-
connection.espruinoSendPacket("EVAL",expr).then(()=>{
572+
connection.espruinoSendPacket("EVAL",expr,{noACK:options.stmFix}).then(()=>{
565573
// resolved/rejected with 'packet' event or timeout
574+
if (options.stmFix)
575+
prodInterval = setInterval(function() {
576+
connection.write(" \x08"); // space+backspace
577+
}, 50);
566578
}, err => {
567579
cleanup();
568580
reject(err);
@@ -1050,7 +1062,7 @@ To do:
10501062
// ----------------------------------------------------------
10511063

10521064
var uart = {
1053-
version : "1.07",
1065+
version : "1.08",
10541066
/// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
10551067
debug : 1,
10561068
/// Should we use flow control? Default is true
@@ -1088,10 +1100,14 @@ To do:
10881100
isConnected : function() {
10891101
return connection!==undefined;
10901102
},
1091-
/// get the connection used by `write` and `eval`
1103+
/// get the connection used by `write` and `eval`, or return undefined
10921104
getConnection : function() {
10931105
return connection;
10941106
},
1107+
/// Return a promise with the connection used by `write` and `eval`, and if there's no connection attempt to get one
1108+
getConnectionAsync : function() {
1109+
return connection ? Promise.resolve(connection) : uart.connectAsync();
1110+
},
10951111
/// Close the connection used by `write` and `eval`
10961112
close : function() {
10971113
if (connection)

0 commit comments

Comments
 (0)