Skip to content

Commit 9d80b4d

Browse files
authored
Add Save and Erase button for onboard logging (#4673)
* Add Save and Erase button for onboard logging * Add missing handler * Update disabled state toggle * Nitpicks * Nitpicks 2 * Remove unsupported * Repeat check for not cancelled for consistency * Only close dialog when open
1 parent 6ad6894 commit 9d80b4d

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

locales/en/messages.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,15 +3549,16 @@
35493549
"dataflashButtonSaveFile": {
35503550
"message": "Save flash to file..."
35513551
},
3552-
"dataflashButtonSaveFileDeprecated": {
3553-
"message": "Save flash to file... (unsupported)"
3554-
},
35553552
"dataflashSavetoFileNote": {
35563553
"message": "Directly saving flash to file is slow and inherently prone to error / file corruption.<br>In some cases it will work for small files, but this is not supported and support requests for it will be closed without comment - use Mass Storage mode instead."
35573554
},
35583555
"dataflashSaveFileDepreciationHint": {
35593556
"message": "This method is slow and inherently prone to error / file corruption, because the MSP connection itself has intrinsic, fundamental limitations that make it unsuitable for file transfers. It may work for small log files only. Do not create support requests if file transfers fail when saved using this method. The recommended method is to use '<b>$t(onboardLoggingRebootMscText.message)</b>' (below) to activate the Mass Storage Mode, and access your flight controller as a storage device to download the log files."
35603557
},
3558+
"dataflashButtonSaveAndErase": {
3559+
"message": "Save & Erase",
3560+
"description": "Button text to save blackbox logs to file and then erase the flash"
3561+
},
35613562
"dataflashButtonErase": {
35623563
"message": "Erase flash"
35633564
},

src/js/tabs/onboard_logging.js

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,16 @@ onboard_logging.initialize = function (callback) {
102102
$(".tab-onboard_logging a.erase-flash-confirm").click(flash_erase);
103103
$(".tab-onboard_logging a.erase-flash-cancel").click(flash_erase_cancel);
104104

105-
$(".tab-onboard_logging a.save-flash").click(flash_save_begin);
105+
$(".tab-onboard_logging a.save-flash").on("click", (e) => {
106+
e.preventDefault();
107+
flash_save_begin(false);
108+
});
109+
110+
$(".tab-onboard_logging a.save-flash-erase").on("click", (e) => {
111+
e.preventDefault();
112+
flash_save_begin(true);
113+
});
114+
106115
$(".tab-onboard_logging a.save-flash-cancel").click(flash_save_cancel);
107116
$(".tab-onboard_logging a.save-flash-dismiss").click(dismiss_saving_dialog);
108117
}
@@ -355,10 +364,9 @@ onboard_logging.initialize = function (callback) {
355364
true,
356365
);
357366

358-
$("a.regular-button erase-flash, a.regular-button.require-msc-supported.save-flash").toggleClass(
359-
"disabled",
360-
FC.DATAFLASH.usedSize === 0,
361-
);
367+
$(
368+
"a.regular-button.erase-flash, a.regular-button.save-flash-erase, a.regular-button.require-msc-supported.save-flash",
369+
).toggleClass("disabled", FC.DATAFLASH.usedSize === 0);
362370

363371
$(".tab-onboard_logging")
364372
.toggleClass("sdcard-error", FC.SDCARD.state === MSP.SDCARD_STATE_FATAL)
@@ -478,7 +486,20 @@ onboard_logging.initialize = function (callback) {
478486
});
479487
}
480488

481-
function flash_save_begin() {
489+
function conditionallyEraseFlash(maxBytes, nextAddress) {
490+
if (Number.isFinite(maxBytes) && nextAddress >= maxBytes) {
491+
eraseCancelled = false;
492+
$(".dataflash-confirm-erase").addClass("erasing");
493+
MSP.send_message(MSPCodes.MSP_DATAFLASH_ERASE, false, false, poll_for_erase_completion);
494+
} else {
495+
gui_log(
496+
i18n.getMessage("dataflashSaveIncompleteWarning") ||
497+
"Downloaded size did not match expected size - not erasing flash.",
498+
);
499+
}
500+
}
501+
502+
function flash_save_begin(alsoErase = false) {
482503
if (GUI.connected_to) {
483504
self.blockSize = self.BLOCK_SIZE;
484505

@@ -498,7 +519,7 @@ onboard_logging.initialize = function (callback) {
498519
// Did we receive any data?
499520
if (chunkDataView.byteLength > 0) {
500521
nextAddress += chunkDataView.byteLength;
501-
if (isNaN(bytesCompressed) || isNaN(totalBytesCompressed)) {
522+
if (Number.isNaN(bytesCompressed) || Number.isNaN(totalBytesCompressed)) {
502523
totalBytesCompressed = null;
503524
} else {
504525
totalBytesCompressed += bytesCompressed;
@@ -515,6 +536,10 @@ onboard_logging.initialize = function (callback) {
515536
mark_saving_dialog_done(startTime, nextAddress, totalBytesCompressed);
516537
}
517538
FileSystem.closeFile(openedFile);
539+
// Optionally erase after successful full download
540+
if (!saveCancelled && alsoErase) {
541+
conditionallyEraseFlash(maxBytes, nextAddress);
542+
}
518543
} else {
519544
if (!self.writeError) {
520545
mspHelper.dataflashRead(nextAddress, self.blockSize, onChunkRead);
@@ -528,6 +553,9 @@ onboard_logging.initialize = function (callback) {
528553
// A zero-byte block indicates end-of-file, so we're done
529554
mark_saving_dialog_done(startTime, nextAddress, totalBytesCompressed);
530555
FileSystem.closeFile(openedFile);
556+
if (!saveCancelled && alsoErase) {
557+
conditionallyEraseFlash(maxBytes, nextAddress);
558+
}
531559
}
532560
} else {
533561
// There was an error with the received block (address didn't match the one we asked for), retry
@@ -582,7 +610,10 @@ onboard_logging.initialize = function (callback) {
582610
flash_update_summary(function () {
583611
if (CONFIGURATOR.connectionValid && !eraseCancelled) {
584612
if (FC.DATAFLASH.ready) {
585-
$(".dataflash-confirm-erase")[0].close();
613+
const dialog = $(".dataflash-confirm-erase")[0];
614+
if (dialog?.open) {
615+
dialog.close();
616+
}
586617
if (getConfig("showNotifications").showNotifications) {
587618
NotificationManager.showNotification("Betaflight Configurator", {
588619
body: i18n.getMessage("flashEraseDoneNotification"),

src/tabs/onboard_logging.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ <h3 i18n="dataflashSavingTitle"></h3>
110110

111111
<div class="dataflash-buttons">
112112
<a class="regular-button erase-flash" href="#" i18n="dataflashButtonErase"></a>
113+
<a class="regular-button require-msc-not-supported save-flash-erase" href="#" i18n="dataflashButtonSaveAndErase"></a>
113114
<a class="regular-button require-msc-not-supported save-flash" href="#" i18n="dataflashButtonSaveFile"></a>
114-
<a class="regular-button require-msc-supported save-flash" href="#"><span i18n="dataflashButtonSaveFileDeprecated"></span><span class="helpicon cf_tip" i18n_title="dataflashSaveFileDepreciationHint"></span></a>
115+
<a class="regular-button require-msc-supported save-flash-erase" href="#" i18n="dataflashButtonSaveAndErase"></a>
116+
<a class="regular-button require-msc-supported save-flash" href="#"><span i18n="dataflashButtonSaveFile"></span><span class="helpicon cf_tip" i18n_title="dataflashSaveFileDepreciationHint"></span></a>
115117
<p i18n="dataflashSavetoFileNote"></p>
116118
</div>
117119
</div>

0 commit comments

Comments
 (0)