diff --git a/index.html b/index.html index 7ee5494..6575c29 100644 --- a/index.html +++ b/index.html @@ -18,23 +18,41 @@ -
+
-
- - + +
2. Select firmware.zip to flash
+
+ +
+ +
+ +
+ +
+ Flashing: {{progress}}% + Flashing: please wait... +
+
+
+
+
-
@@ -45,7 +63,8 @@ Vue.createApp({ data() { return { - + isFlashing: false, + progress: 0, }; }, mounted() { @@ -94,8 +113,16 @@ } // flash file + this.isFlashing = true; + this.progress = 0; const flasher = new Nrf52DfuFlasher(serialPort); - await flasher.flash(file); + await flasher.flash(file, (percentage) => { + this.progress = percentage; + if(this.progress === 100){ + this.isFlashing = false; + alert("Firmware has been flashed!"); + } + }); }, async detect() { diff --git a/nrf52_dfu_flasher.js b/nrf52_dfu_flasher.js index dbae1f1..6db69dc 100644 --- a/nrf52_dfu_flasher.js +++ b/nrf52_dfu_flasher.js @@ -86,9 +86,10 @@ class Nrf52DfuFlasher { /** * Flashes the provided firmware zip. * @param firmwareZipBlob + * @param progressCallback * @returns {Promise} */ - async flash(firmwareZipBlob) { + async flash(firmwareZipBlob, progressCallback) { // read zip file const blobReader = new window.zip.BlobReader(firmwareZipBlob); @@ -122,7 +123,7 @@ class Nrf52DfuFlasher { // flash application image if(manifest.application){ - await this.dfuSendImage(this.HEX_TYPE_APPLICATION, zipEntries, manifest.application); + await this.dfuSendImage(this.HEX_TYPE_APPLICATION, zipEntries, manifest.application, progressCallback); } } @@ -132,9 +133,10 @@ class Nrf52DfuFlasher { * @param programMode * @param zipEntries * @param firmwareManifest + * @param progressCallback * @returns {Promise} */ - async dfuSendImage(programMode, zipEntries, firmwareManifest) { + async dfuSendImage(programMode, zipEntries, firmwareManifest, progressCallback) { // open port await this.serialPort.open({ @@ -174,7 +176,7 @@ class Nrf52DfuFlasher { await this.sendInitPacket(init_packet); console.log("Sending firmware file") - await this.sendFirmware(firmware); + await this.sendFirmware(firmware, progressCallback); // close port console.log("Closing Port"); @@ -353,11 +355,13 @@ class Nrf52DfuFlasher { /** * Sends the firmware file to the device in multiple chunks. * @param firmware + * @param progressCallback * @returns {Promise} */ - async sendFirmware(firmware) { + async sendFirmware(firmware, progressCallback) { const packets = []; + var packetsSent = 0; // chunk firmware into separate packets for(let i = 0; i < firmware.length; i += this.DFU_PACKET_MAX_SIZE){ @@ -367,6 +371,11 @@ class Nrf52DfuFlasher { ])); } + // send initial progress + if(progressCallback){ + progressCallback(0); + } + // send each packet one after the other for(var i = 0; i < packets.length; i++){ @@ -376,6 +385,13 @@ class Nrf52DfuFlasher { // wait a bit to allow device to write before sending next packet await this.sleepMillis(this.FLASH_PAGE_WRITE_TIME * 1000); + // update progress + packetsSent++; + if(progressCallback){ + const progress = Math.floor((packetsSent / packets.length) * 100); + progressCallback(progress); + } + } // finished sending firmware, send DFU Stop Data packet @@ -383,6 +399,11 @@ class Nrf52DfuFlasher { ...this.int32ToBytes(this.DFU_STOP_DATA_PACKET), ])); + // send final progress + if(progressCallback){ + progressCallback(100); + } + } /**