Skip to content

Commit

Permalink
1s->2s packet timeout
Browse files Browse the repository at this point in the history
+      connection.write now returns a promise
  • Loading branch information
gfwilliams committed Oct 7, 2024
1 parent 44d7acb commit 41331a6
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 41 deletions.
2 changes: 1 addition & 1 deletion examples/uart.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<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 (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 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>
<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>
Expand Down
90 changes: 50 additions & 40 deletions uart.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ ChangeLog:
...
1.08: Add UART.getConnectionAsync()
Add .espruinoEval(... {stmFix:true}) to work around occasional STM32 USB issue in 2v24 and earlier firmwares
1s->2s packet timeout
connection.write now returns a promise
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 @@ -425,9 +427,9 @@ To do:
this.rxDataHandlerPacket = "";
this.rxDataHandlerTimeout = setTimeout(()=>{
this.rxDataHandlerTimeout = undefined;
log(0, "Packet timeout (1s)");
log(0, "Packet timeout (2s)");
this.rxDataHandlerPacket = undefined;
}, 1000);
}, 2000);
ch = undefined;
}
if (ch===undefined) { // if we're supposed to remove the char, do it
Expand Down Expand Up @@ -641,45 +643,49 @@ To do:
};

connection.write = function(data, callback) {
if (data) txDataQueue.push({data:data,callback:callback,maxLength:data.length});
if (connection.isOpen && !connection.txInProgress) writeChunk();
return new Promise((resolve,reject) => {
if (data) txDataQueue.push({data:data,callback:callback,maxLength:data.length,resolve:resolve});
if (connection.isOpen && !connection.txInProgress) writeChunk();

function writeChunk() {
if (flowControlXOFF) { // flow control - try again later
setTimeout(writeChunk, 50);
return;
}
var chunk;
if (!txDataQueue.length) {
uart.writeProgress();
return;
}
var txItem = txDataQueue[0];
uart.writeProgress(txItem.maxLength - txItem.data.length, txItem.maxLength);
if (txItem.data.length <= chunkSize) {
chunk = txItem.data;
txItem.data = undefined;
} else {
chunk = txItem.data.substr(0,chunkSize);
txItem.data = txItem.data.substr(chunkSize);
}
connection.txInProgress = true;
log(2, "Sending "+ JSON.stringify(chunk));
txCharacteristic.writeValue(str2ab(chunk)).then(function() {
log(3, "Sent");
if (!txItem.data) {
txDataQueue.shift(); // remove this element
if (txItem.callback)
txItem.callback();
function writeChunk() {
if (flowControlXOFF) { // flow control - try again later
setTimeout(writeChunk, 50);
return;
}
connection.txInProgress = false;
writeChunk();
}).catch(function(error) {
log(1, 'SEND ERROR: ' + error);
txDataQueue = [];
connection.close();
});
}
var chunk;
if (!txDataQueue.length) {
uart.writeProgress();
return;
}
var txItem = txDataQueue[0];
uart.writeProgress(txItem.maxLength - txItem.data.length, txItem.maxLength);
if (txItem.data.length <= chunkSize) {
chunk = txItem.data;
txItem.data = undefined;
} else {
chunk = txItem.data.substr(0,chunkSize);
txItem.data = txItem.data.substr(chunkSize);
}
connection.txInProgress = true;
log(2, "Sending "+ JSON.stringify(chunk));
txCharacteristic.writeValue(str2ab(chunk)).then(function() {
log(3, "Sent");
if (!txItem.data) {
txDataQueue.shift(); // remove this element
if (txItem.callback)
txItem.callback();
if (txItem.resolve)
txItem.resolve();
}
connection.txInProgress = false;
writeChunk();
}).catch(function(error) {
log(1, 'SEND ERROR: ' + error);
txDataQueue = [];
connection.close();
});
}
});
};

return navigator.bluetooth.requestDevice(uart.optionsBluetooth).then(function(device) {
Expand Down Expand Up @@ -789,18 +795,22 @@ To do:
// TODO: progress?
writer = serialPort.writable.getWriter();
log(2, "Sending "+ JSON.stringify(data));
writer.write(str2ab(data)).then(function() {
return new Promise((resolve, reject) => {
writer.write(str2ab(data)).then(function() {
writer.releaseLock();
writer = undefined;
log(3, "Sent");
if (callback) callback();
resolve();
}).catch(function(error) {
if (writer) {
writer.releaseLock();
writer.close();
}
writer = undefined;
log(0,'SEND ERROR: ' + error);
reject(error);
});
});
};

Expand Down

0 comments on commit 41331a6

Please sign in to comment.