mirror of
https://github.com/betaflight/betaflight-configurator.git
synced 2025-07-19 14:25:14 +03:00
Add progress dialogs for flash save and erase
This commit is contained in:
parent
0f22b92e35
commit
49741b45a8
7 changed files with 357 additions and 63 deletions
|
@ -2,39 +2,93 @@
|
|||
|
||||
TABS.dataflash = {};
|
||||
TABS.dataflash.initialize = function (callback) {
|
||||
var self = this;
|
||||
var
|
||||
self = this,
|
||||
saveCancelled, eraseCancelled;
|
||||
|
||||
if (GUI.active_tab != 'dataflash') {
|
||||
GUI.active_tab = 'dataflash';
|
||||
googleAnalytics.sendAppView('dataflash');
|
||||
}
|
||||
|
||||
var requested_properties = [],
|
||||
var
|
||||
requested_properties = [],
|
||||
samples = 0,
|
||||
requests = 0,
|
||||
log_buffer = [];
|
||||
|
||||
if (CONFIGURATOR.connectionValid) {
|
||||
MSP.send_message(MSP_codes.MSP_DATAFLASH_SUMMARY, false, false, function() {
|
||||
$('#content').load("./tabs/dataflash.html", process_html);
|
||||
$('#content').load("./tabs/dataflash.html", function() {
|
||||
create_html();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function process_html() {
|
||||
function formatFilesize(bytes) {
|
||||
if (bytes < 1024) {
|
||||
return bytes + "B";
|
||||
}
|
||||
|
||||
var kilobytes = bytes / 1024;
|
||||
|
||||
if (kilobytes < 1024) {
|
||||
return Math.round(kilobytes) + "kB";
|
||||
}
|
||||
|
||||
var megabytes = kilobytes / 1024;
|
||||
|
||||
return megabytes.toFixed(1) + "MB";
|
||||
}
|
||||
|
||||
function update_html() {
|
||||
if (DATAFLASH.usedSize > 0) {
|
||||
$(".tab-dataflash .dataflash-used").css({
|
||||
width: (DATAFLASH.usedSize / DATAFLASH.totalSize * 100) + "%",
|
||||
display: 'block'
|
||||
});
|
||||
|
||||
$(".tab-dataflash .dataflash-used div").text('Used space ' + formatFilesize(DATAFLASH.usedSize));
|
||||
} else {
|
||||
$(".tab-dataflash .dataflash-used").css({
|
||||
display: 'none'
|
||||
});
|
||||
}
|
||||
|
||||
if (DATAFLASH.totalSize - DATAFLASH.usedSize > 0) {
|
||||
$(".tab-dataflash .dataflash-free").css({
|
||||
width: ((DATAFLASH.totalSize - DATAFLASH.usedSize) / DATAFLASH.totalSize * 100) + "%",
|
||||
display: 'block'
|
||||
});
|
||||
$(".tab-dataflash .dataflash-free div").text('Free space ' + formatFilesize(DATAFLASH.totalSize - DATAFLASH.usedSize));
|
||||
} else {
|
||||
$(".tab-dataflash .dataflash-free").css({
|
||||
display: 'none'
|
||||
});
|
||||
}
|
||||
|
||||
$(".tab-dataflash a.erase-flash, .tab-dataflash a.save-flash").toggleClass("disabled", DATAFLASH.usedSize == 0);
|
||||
}
|
||||
|
||||
function create_html() {
|
||||
// translate to user-selected language
|
||||
localize();
|
||||
|
||||
$(".tab-dataflash .dataflash-capacity").text(DATAFLASH.totalSize);
|
||||
$(".tab-dataflash .dataflash-sectors").text(DATAFLASH.sectors);
|
||||
|
||||
// UI hooks
|
||||
$('.tab-dataflash a.erase_flash').click(erase_flash);
|
||||
|
||||
$('.tab-dataflash a.save_to_file').click(stream_flash_to_file);
|
||||
$('.tab-dataflash a.erase-flash').click(ask_to_erase_flash);
|
||||
|
||||
$('.tab-dataflash a.erase-flash-confirm').click(flash_erase);
|
||||
$('.tab-dataflash a.erase-flash-cancel').click(flash_erase_cancel);
|
||||
|
||||
$('.tab-dataflash a.save-flash').click(flash_save_begin);
|
||||
$('.tab-dataflash a.save-flash-cancel').click(flash_save_cancel);
|
||||
$('.tab-dataflash a.save-flash-dismiss').click(dismiss_saving_dialog);
|
||||
|
||||
update_html();
|
||||
|
||||
if (callback) callback();
|
||||
}
|
||||
|
||||
|
||||
// IO related methods
|
||||
function zeroPad(value, width) {
|
||||
value = "" + value;
|
||||
|
@ -46,21 +100,60 @@ TABS.dataflash.initialize = function (callback) {
|
|||
return value;
|
||||
}
|
||||
|
||||
function stream_flash_to_file() {
|
||||
function flash_save_cancel() {
|
||||
saveCancelled = true;
|
||||
}
|
||||
|
||||
function show_saving_dialog() {
|
||||
$(".dataflash-saving progress").attr("value", 0);
|
||||
saveCancelled = false;
|
||||
$(".dataflash-saving").removeClass("done");
|
||||
|
||||
$(".dataflash-saving")[0].showModal();
|
||||
}
|
||||
|
||||
function dismiss_saving_dialog() {
|
||||
$(".dataflash-saving")[0].close();
|
||||
}
|
||||
|
||||
function mark_saving_dialog_done() {
|
||||
$(".dataflash-saving").addClass("done");
|
||||
}
|
||||
|
||||
function flash_save_begin() {
|
||||
var
|
||||
maxBytes = DATAFLASH.usedSize;
|
||||
|
||||
if (GUI.connected_to) {
|
||||
prepare_file(function(fileWriter) {
|
||||
var
|
||||
nextAddress = 0;
|
||||
|
||||
show_saving_dialog();
|
||||
|
||||
function onChunkRead(chunkAddress, chunkDataView) {
|
||||
// If we didn't get a zero-byte chunk (indicating end-of-file), request more
|
||||
if (chunkDataView.byteLength > 0) {
|
||||
var blob = new Blob([chunkDataView]);
|
||||
nextAddress += chunkDataView.byteLength;
|
||||
|
||||
$(".dataflash-saving progress").attr("value", nextAddress / maxBytes * 100);
|
||||
|
||||
var
|
||||
blob = new Blob([chunkDataView]);
|
||||
|
||||
fileWriter.write(blob);
|
||||
|
||||
nextAddress += chunkDataView.byteLength;
|
||||
MSP.dataflashRead(nextAddress, onChunkRead);
|
||||
|
||||
if (saveCancelled || nextAddress >= maxBytes) {
|
||||
if (saveCancelled) {
|
||||
dismiss_saving_dialog();
|
||||
} else {
|
||||
mark_saving_dialog_done();
|
||||
}
|
||||
} else {
|
||||
MSP.dataflashRead(nextAddress, onChunkRead);
|
||||
}
|
||||
} else {
|
||||
mark_saving_dialog_done();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -88,41 +181,51 @@ TABS.dataflash.initialize = function (callback) {
|
|||
console.log('Dataflash dump file path: ' + path);
|
||||
});
|
||||
|
||||
prepare_writer(fileEntry, onComplete);
|
||||
});
|
||||
}
|
||||
fileEntry.createWriter(function (fileWriter) {
|
||||
fileWriter.onerror = function (e) {
|
||||
console.error(e);
|
||||
|
||||
function prepare_writer(fileEntry, onComplete) {
|
||||
fileEntry.createWriter(function (fileWriter) {
|
||||
fileWriter.onerror = function (e) {
|
||||
// stop logging if the procedure was/is still running
|
||||
};
|
||||
|
||||
onComplete(fileWriter);
|
||||
}, function (e) {
|
||||
// File is not readable or does not exist!
|
||||
console.error(e);
|
||||
|
||||
// stop logging if the procedure was/is still running
|
||||
};
|
||||
|
||||
fileWriter.onwriteend = function () {
|
||||
};
|
||||
|
||||
onComplete(fileWriter);
|
||||
}, function (e) {
|
||||
// File is not readable or does not exist!
|
||||
console.error(e);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function erase_flash() {
|
||||
/* var dialog = $("<dialog>lol</dialog>");
|
||||
|
||||
$("body").append(dialog);
|
||||
|
||||
dialog[0].showModal();
|
||||
|
||||
TODO modal dialog to confirm erase */
|
||||
|
||||
MSP.send_message(MSP_codes.MSP_DATAFLASH_ERASE, false, false, function(data) {
|
||||
|
||||
function ask_to_erase_flash() {
|
||||
eraseCancelled = false;
|
||||
$(".dataflash-confirm-erase").removeClass('erasing');
|
||||
|
||||
$(".dataflash-confirm-erase")[0].showModal();
|
||||
}
|
||||
|
||||
function poll_for_erase_completion() {
|
||||
MSP.send_message(MSP_codes.MSP_DATAFLASH_SUMMARY, false, false, function() {
|
||||
update_html();
|
||||
if (!eraseCancelled) {
|
||||
if (DATAFLASH.ready) {
|
||||
$(".dataflash-confirm-erase")[0].close();
|
||||
} else {
|
||||
setTimeout(poll_for_erase_completion, 500);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function flash_erase() {
|
||||
$(".dataflash-confirm-erase").addClass('erasing');
|
||||
|
||||
MSP.send_message(MSP_codes.MSP_DATAFLASH_ERASE, false, false, poll_for_erase_completion);
|
||||
}
|
||||
|
||||
function flash_erase_cancel() {
|
||||
eraseCancelled = true;
|
||||
$(".dataflash-confirm-erase")[0].close();
|
||||
}
|
||||
};
|
||||
|
||||
TABS.dataflash.cleanup = function (callback) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue