Skip to content

Commit 1fd2962

Browse files
committed
Updated connection class, also fix smooth upload percentage indicator
1 parent 4dd5048 commit 1fd2962

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

core/serial.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,32 @@ To add a new serial device, you must add an object to
4141
var slowWrite = true;
4242

4343
// filler to allow us to use EspruinoWebTools' Connection class
44-
var uart = { writeProgress : () => {} };
44+
var uart = {
45+
showProgress : false,
46+
writeProgress : function(chars, charsMax) {
47+
if (chars===undefined) {
48+
this.lastProgress = 0;
49+
} else {
50+
var diff = chars-this.lastProgress;
51+
if (chars==0) diff=0;
52+
this.lastProgress = chars;
53+
if (this.showProgress)
54+
Espruino.Core.Status.incrementProgress(diff);
55+
}
56+
} };
4557
function log(level, str) { if (level<3) console.log("serial:", str); }
4658
function ab2str(buf) { return String.fromCharCode.apply(null, new Uint8Array(buf)); }
4759
var parseRJSON = data => Espruino.Core.Utils.parseRJSON(data);
4860
// ---------------
4961

50-
// From https://github.com/espruino/EspruinoWebTools/blob/master/uart.js
5162
/// Base connection class - BLE/Serial add writeLowLevel/closeLowLevel/etc on top of this
5263
class Connection {
5364
endpoint = undefined; // Set to the endpoint used for this connection - eg maybe endpoint.name=="Web Bluetooth"
5465
// on/emit work for close/data/open/error/ack/nak/packet events
5566
on(evt,cb) { let e = "on"+evt; if (!this[e]) this[e]=[]; this[e].push(cb); } // on only works with a single handler
5667
emit(evt,data1,data2) { let e = "on"+evt; if (this[e]) this[e].forEach(fn=>fn(data1,data2)); }
5768
removeListener(evt,callback) { let e = "on"+evt; if (this[e]) this[e]=this[e].filter(fn=>fn!=callback); }
69+
removeAllListeners(evt) { let e = "on"+evt; delete this[e]; }
5870
// on("open", () => ... ) connection opened
5971
// on("close", () => ... ) connection closed
6072
// on("data", (data) => ... ) when data is received (as string)
@@ -228,12 +240,10 @@ To add a new serial device, you must add an object to
228240
}
229241
var chunk;
230242
if (!connection.txDataQueue.length) {
231-
uart.writeProgress();
232243
connection.updateProgress();
233244
return;
234245
}
235246
var txItem = connection.txDataQueue[0];
236-
uart.writeProgress(txItem.maxLength - (txItem.data?txItem.data.length:0), txItem.maxLength);
237247
connection.updateProgress(txItem.maxLength - (txItem.data?txItem.data.length:0), txItem.maxLength);
238248
if (txItem.data.length <= connection.chunkSize) {
239249
chunk = txItem.data;
@@ -260,7 +270,6 @@ To add a new serial device, you must add an object to
260270
promise.then(writeChunk); // if txItem.callback() returned a promise, wait until it completes before continuing
261271
}, function(error) {
262272
log(1, 'SEND ERROR: ' + error);
263-
uart.writeProgress();
264273
connection.updateProgress();
265274
connection.txDataQueue = [];
266275
connection.close();
@@ -364,7 +373,7 @@ To add a new serial device, you must add an object to
364373
let connection = this;
365374
let packetCount = 0, packetTotal = Math.ceil(data.length/CHUNK)+1;
366375
connection.progressAmt = 0;
367-
connection.progressMax = data.length;
376+
connection.progressMax = 100 + data.length;
368377
// always ack the FILE_SEND
369378
progressHandler(0, packetTotal);
370379
return connection.espruinoSendPacket("FILE_SEND",JSON.stringify(options)).then(sendData, err=> {
@@ -374,7 +383,7 @@ To add a new serial device, you must add an object to
374383
});
375384
// but if noACK don't ack for data
376385
function sendData() {
377-
connection.progressAmt += CHUNK;
386+
connection.progressAmt += connection.progressAmt?CHUNK:100;
378387
progressHandler(++packetCount, packetTotal);
379388
if (data.length==0) {
380389
connection.progressAmt = 0;
@@ -738,6 +747,7 @@ To add a new serial device, you must add an object to
738747
if (slowWrite) blockSize=19;
739748

740749
writeData.showStatus &= writeData.data.length>blockSize;
750+
uart.showProgress = writeData.showStatus;
741751
if (writeData.showStatus) {
742752
Espruino.Core.Status.setStatus("Sending...", writeData.data.length);
743753
console.log("serial: ---> "+JSON.stringify(writeData.data));
@@ -779,10 +789,11 @@ To add a new serial device, you must add an object to
779789
Espruino.Core.Serial.connection.chunkSize = blockSize;
780790
Espruino.Core.Serial.connection.write(d, function() { // write data, but the callback returns a promise that delays
781791
// update status
782-
if (writeData.showStatus)
783-
Espruino.Core.Status.incrementProgress(d.length);
792+
/*if (writeData.showStatus)
793+
Espruino.Core.Status.incrementProgress(d.length);*/
784794
return new Promise(resolve => setTimeout(function() {
785795
if (isLast && writeData.showStatus) {
796+
uart.showProgress = false;
786797
Espruino.Core.Status.setStatus("Sent");
787798
if (writeData.callback)
788799
writeData.callback();

0 commit comments

Comments
 (0)