Skip to content

Commit 41331a6

Browse files
committed
1s->2s packet timeout
+ connection.write now returns a promise
1 parent 44d7acb commit 41331a6

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed

examples/uart.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<button onclick="UART.write('\x03LED1.set();\n');">On!</button>
88
<button onclick="UART.write('\x03LED1.reset();\n');">Off!</button><br>
99
<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>
10+
<button id="temp2" onclick="UART.getConnectionAsync().then(c=>c.espruinoEval('E.getTemperature()',{stmFix:true})).then(function(x) { document.getElementById('temp2').innerHTML = 'Temperature '+x; })">Read Temperature (.espruinoEval)</button><br>
1111
<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>
1212

1313
<button onclick="UART.close();">Disconnect</button>

uart.js

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ ChangeLog:
8989
...
9090
1.08: Add UART.getConnectionAsync()
9191
Add .espruinoEval(... {stmFix:true}) to work around occasional STM32 USB issue in 2v24 and earlier firmwares
92+
1s->2s packet timeout
93+
connection.write now returns a promise
9294
1.07: Added UART.getConnection().espruinoEval
9395
1.06: Added optional serialPort parameter to UART.connect(), allowing a known Web Serial port to be used
9496
Added connectAsync, and write/eval now return promises
@@ -425,9 +427,9 @@ To do:
425427
this.rxDataHandlerPacket = "";
426428
this.rxDataHandlerTimeout = setTimeout(()=>{
427429
this.rxDataHandlerTimeout = undefined;
428-
log(0, "Packet timeout (1s)");
430+
log(0, "Packet timeout (2s)");
429431
this.rxDataHandlerPacket = undefined;
430-
}, 1000);
432+
}, 2000);
431433
ch = undefined;
432434
}
433435
if (ch===undefined) { // if we're supposed to remove the char, do it
@@ -641,45 +643,49 @@ To do:
641643
};
642644

643645
connection.write = function(data, callback) {
644-
if (data) txDataQueue.push({data:data,callback:callback,maxLength:data.length});
645-
if (connection.isOpen && !connection.txInProgress) writeChunk();
646+
return new Promise((resolve,reject) => {
647+
if (data) txDataQueue.push({data:data,callback:callback,maxLength:data.length,resolve:resolve});
648+
if (connection.isOpen && !connection.txInProgress) writeChunk();
646649

647-
function writeChunk() {
648-
if (flowControlXOFF) { // flow control - try again later
649-
setTimeout(writeChunk, 50);
650-
return;
651-
}
652-
var chunk;
653-
if (!txDataQueue.length) {
654-
uart.writeProgress();
655-
return;
656-
}
657-
var txItem = txDataQueue[0];
658-
uart.writeProgress(txItem.maxLength - txItem.data.length, txItem.maxLength);
659-
if (txItem.data.length <= chunkSize) {
660-
chunk = txItem.data;
661-
txItem.data = undefined;
662-
} else {
663-
chunk = txItem.data.substr(0,chunkSize);
664-
txItem.data = txItem.data.substr(chunkSize);
665-
}
666-
connection.txInProgress = true;
667-
log(2, "Sending "+ JSON.stringify(chunk));
668-
txCharacteristic.writeValue(str2ab(chunk)).then(function() {
669-
log(3, "Sent");
670-
if (!txItem.data) {
671-
txDataQueue.shift(); // remove this element
672-
if (txItem.callback)
673-
txItem.callback();
650+
function writeChunk() {
651+
if (flowControlXOFF) { // flow control - try again later
652+
setTimeout(writeChunk, 50);
653+
return;
674654
}
675-
connection.txInProgress = false;
676-
writeChunk();
677-
}).catch(function(error) {
678-
log(1, 'SEND ERROR: ' + error);
679-
txDataQueue = [];
680-
connection.close();
681-
});
682-
}
655+
var chunk;
656+
if (!txDataQueue.length) {
657+
uart.writeProgress();
658+
return;
659+
}
660+
var txItem = txDataQueue[0];
661+
uart.writeProgress(txItem.maxLength - txItem.data.length, txItem.maxLength);
662+
if (txItem.data.length <= chunkSize) {
663+
chunk = txItem.data;
664+
txItem.data = undefined;
665+
} else {
666+
chunk = txItem.data.substr(0,chunkSize);
667+
txItem.data = txItem.data.substr(chunkSize);
668+
}
669+
connection.txInProgress = true;
670+
log(2, "Sending "+ JSON.stringify(chunk));
671+
txCharacteristic.writeValue(str2ab(chunk)).then(function() {
672+
log(3, "Sent");
673+
if (!txItem.data) {
674+
txDataQueue.shift(); // remove this element
675+
if (txItem.callback)
676+
txItem.callback();
677+
if (txItem.resolve)
678+
txItem.resolve();
679+
}
680+
connection.txInProgress = false;
681+
writeChunk();
682+
}).catch(function(error) {
683+
log(1, 'SEND ERROR: ' + error);
684+
txDataQueue = [];
685+
connection.close();
686+
});
687+
}
688+
});
683689
};
684690

685691
return navigator.bluetooth.requestDevice(uart.optionsBluetooth).then(function(device) {
@@ -789,18 +795,22 @@ To do:
789795
// TODO: progress?
790796
writer = serialPort.writable.getWriter();
791797
log(2, "Sending "+ JSON.stringify(data));
792-
writer.write(str2ab(data)).then(function() {
798+
return new Promise((resolve, reject) => {
799+
writer.write(str2ab(data)).then(function() {
793800
writer.releaseLock();
794801
writer = undefined;
795802
log(3, "Sent");
796803
if (callback) callback();
804+
resolve();
797805
}).catch(function(error) {
798806
if (writer) {
799807
writer.releaseLock();
800808
writer.close();
801809
}
802810
writer = undefined;
803811
log(0,'SEND ERROR: ' + error);
812+
reject(error);
813+
});
804814
});
805815
};
806816

0 commit comments

Comments
 (0)