Skip to content

Commit 80080dd

Browse files
committed
1.20: Added options as 3rd arg to UART.write instead of just callbackNewline, added noWait option
1 parent 90c69cf commit 80080dd

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

uart.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ UART.getConnection().espruinoEval("1+2").then(res => console.log("=",res));
9191
ChangeLog:
9292
9393
...
94+
1.20: Added options as 3rd arg to UART.write instead of just callbackNewline, added noWait option
9495
1.19: Add this.removeAllListeners
9596
1.18: Add connection.on("line"...) event, export parseRJSON, handle 'NaN' in RJSON
9697
1.17: Work around Linux Web Bluetooth Bug (connect-disconnect-connect to same device)
@@ -391,7 +392,7 @@ To do:
391392
var q = queue.shift();
392393
log(3,"Executing "+JSON.stringify(q)+" from queue");
393394
if (q.type=="eval") uart.eval(q.expr, q.cb).then(q.resolve, q.reject);
394-
else if (q.type=="write") uart.write(q.data, q.callback, q.callbackNewline).then(q.resolve, q.reject);
395+
else if (q.type=="write") uart.write(q.data, q.callback, q.options).then(q.resolve, q.reject);
395396
else log(1,"Unknown queue item "+JSON.stringify(q));
396397
}
397398

@@ -585,7 +586,6 @@ To do:
585586
return;
586587
}
587588
var txItem = connection.txDataQueue[0];
588-
uart.writeProgress(txItem.maxLength - (txItem.data?txItem.data.length:0), txItem.maxLength);
589589
connection.updateProgress(txItem.maxLength - (txItem.data?txItem.data.length:0), txItem.maxLength);
590590
if (txItem.data.length <= connection.chunkSize) {
591591
chunk = txItem.data;
@@ -716,7 +716,7 @@ To do:
716716
let connection = this;
717717
let packetCount = 0, packetTotal = Math.ceil(data.length/CHUNK)+1;
718718
connection.progressAmt = 0;
719-
connection.progressMax = data.length;
719+
connection.progressMax = 100 + data.length;
720720
// always ack the FILE_SEND
721721
progressHandler(0, packetTotal);
722722
return connection.espruinoSendPacket("FILE_SEND",JSON.stringify(options)).then(sendData, err=> {
@@ -726,7 +726,7 @@ To do:
726726
});
727727
// but if noACK don't ack for data
728728
function sendData() {
729-
connection.progressAmt += CHUNK;
729+
connection.progressAmt += connection.progressAmt?CHUNK:100;
730730
progressHandler(++packetCount, packetTotal);
731731
if (data.length==0) {
732732
connection.progressAmt = 0;
@@ -1189,16 +1189,24 @@ To do:
11891189
}
11901190
// ======================================================================
11911191
/* convenience function... Write data, call the callback with data:
1192-
callbackNewline = false => if no new data received for ~0.2 sec
1193-
callbackNewline = true => after a newline */
1194-
function write(data, callback, callbackNewline) {
1192+
options = true/false => same as setting {waitNewline:true/false}
1193+
options.waitNewline = false => return if no new data received for ~0.2 sec
1194+
true => return only after a newline
1195+
options.noWait : bool => don't wait for any response, just return immediately
1196+
*/
1197+
function write(data, callback, options) {
1198+
if ("boolean" == typeof options)
1199+
options = {waitNewline:options};
1200+
else if (options === undefined)
1201+
options = {};
1202+
11951203
if (isBusy)
1196-
return pushToQueue({type:"write", data:data, callback:callback, callbackNewline:callbackNewline});
1204+
return pushToQueue({type:"write", data:data, callback:callback, options:options});
11971205

11981206
return new Promise((resolve,reject) => {
11991207
var cbTimeout;
12001208
function onWritten() {
1201-
if (callbackNewline) {
1209+
if (options.waitNewline) {
12021210
connection.cb = function(d) {
12031211
// if we hadn't got a newline this time (even if we had one before)
12041212
// then ignore it (https://github.com/espruino/BangleApps/issues/3771)
@@ -1222,9 +1230,12 @@ To do:
12221230
}
12231231
// wait for any received data if we have a callback...
12241232
var maxTime = uart.timeoutMax; // Max time we wait in total, even if getting data
1225-
var dataWaitTime = callbackNewline ? uart.timeoutNewline : uart.timeoutNormal;
1233+
var dataWaitTime = options.waitNewline ? uart.timeoutNewline : uart.timeoutNormal;
12261234
var maxDataTime = dataWaitTime; // max time we wait after having received data
12271235
const POLLINTERVAL = 100;
1236+
if (options.noWait) { // just return immediately, as soon as written
1237+
maxTime = dataWaitTime = maxDataTime = 0;
1238+
}
12281239
cbTimeout = setTimeout(function timeout() {
12291240
cbTimeout = undefined;
12301241
if (connection===undefined) {
@@ -1238,14 +1249,14 @@ To do:
12381249
cbTimeout = setTimeout(timeout, 100);
12391250
} else {
12401251
connection.cb = undefined;
1241-
if (callbackNewline)
1252+
if (options.waitNewline)
12421253
log(2, "write waiting for newline timed out");
12431254
if (callback)
12441255
callback(connection.received);
12451256
resolve(connection.received);
12461257
isBusy = false;
1247-
handleQueue();
12481258
connection.received = "";
1259+
handleQueue();
12491260
}
12501261
connection.hadData = false;
12511262
}, 100);
@@ -1282,13 +1293,13 @@ To do:
12821293
if (cb) cb(null, err);
12831294
return Promise.reject(err);
12841295
}
1285-
}, true/*callbackNewline*/);
1296+
}, {waitNewline:true});
12861297
}
12871298

12881299
// ----------------------------------------------------------
12891300

12901301
var uart = {
1291-
version : "1.19",
1302+
version : "1.20",
12921303
/// Are we writing debug information? 0 is no, 1 is some, 2 is more, 3 is all.
12931304
debug : 1,
12941305
/// Should we use flow control? Default is true

0 commit comments

Comments
 (0)