1
0
Fork 0
mirror of https://github.com/betaflight/betaflight-configurator.git synced 2025-07-15 12:25:15 +03:00

Refactor flashing progress bar code (#1523)

Refactor flashing progress bar code
This commit is contained in:
Michael Keller 2019-07-07 15:20:13 +12:00 committed by GitHub
commit 9b8b17b219
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 97 additions and 59 deletions

View file

@ -143,10 +143,8 @@ STM32_protocol.prototype.initialize = function () {
self.upload_process_alive = false;
// reset progress bar to initial state
self.progress_bar_e = $('.progress');
self.progress_bar_e.val(0);
self.progress_label_e = $('span.progressLabel');
self.progress_label_e.removeClass('valid invalid actionRequired');
TABS.firmware_flasher.flashingMessage(null, TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL)
.flashProgress(0);
// lock some UI elements TODO needs rework
$('select[name="release"]').prop('disabled', true);
@ -161,8 +159,7 @@ STM32_protocol.prototype.initialize = function () {
} else {
console.log('STM32 - timed out, programming failed ...');
$('span.progressLabel').text(i18n.getMessage('stm32TimedOut'));
self.progress_label_e.addClass('invalid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32TimedOut'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
// protocol got stuck, clear timer and disconnect
GUI.interval_remove('STM32_timeout');
@ -244,8 +241,7 @@ STM32_protocol.prototype.verify_response = function (val, data) {
if (val != data[0]) {
var message = 'STM32 Communication failed, wrong response, expected: ' + val + ' (0x' + val.toString(16) + ') received: ' + data[0] + ' (0x' + data[0].toString(16) + ')';
console.error(message);
$('span.progressLabel').text(i18n.getMessage('stm32WrongResponse',[val, val.toString(16), data[0], data[0].toString(16)]));
self.progress_label_e.addClass('invalid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32WrongResponse',[val, val.toString(16), data[0], data[0].toString(16)]), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
// disconnect
this.upload_procedure(99);
@ -357,7 +353,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
switch (step) {
case 1:
// initialize serial interface on the MCU side, auto baud rate settings
$('span.progressLabel').text(i18n.getMessage('stm32ContactingBootloader'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ContactingBootloader'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
var send_counter = 0;
GUI.interval_add('stm32_initialize_mcu', function () { // 200 ms interval (just in case mcu was already initialized), we need to break the 2 bytes command requirement
@ -369,8 +365,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
// proceed to next step
self.upload_procedure(2);
} else {
$('span.progressLabel').text(i18n.getMessage('stm32ContactingBootloaderFailed'));
self.progress_label_e.addClass('invalid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ContactingBootloaderFailed'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
GUI.interval_remove('stm32_initialize_mcu');
@ -383,8 +378,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
// stop retrying, its too late to get any response from MCU
console.log('STM32 - no response from bootloader, disconnecting');
$('span.progressLabel').text(i18n.getMessage('stm32ResponseBootloaderFailed'));
self.progress_label_e.addClass('invalid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ResponseBootloaderFailed'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
GUI.interval_remove('stm32_initialize_mcu');
GUI.interval_remove('STM32_timeout');
@ -436,7 +430,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
var message = 'Executing global chip erase (via extended erase)';
console.log(message);
$('span.progressLabel').text(i18n.getMessage('stm32GlobalEraseExtended'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32GlobalEraseExtended'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
self.send([self.command.extended_erase, 0xBB], 1, function (reply) {
if (self.verify_response(self.status.ACK, reply)) {
@ -452,7 +446,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
} else {
var message = 'Executing local erase (via extended erase)';
console.log(message);
$('span.progressLabel').text(i18n.getMessage('stm32LocalEraseExtended'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32LocalEraseExtended'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
self.send([self.command.extended_erase, 0xBB], 1, function (reply) {
if (self.verify_response(self.status.ACK, reply)) {
@ -504,7 +498,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
if (self.options.erase_chip) {
var message = 'Executing global chip erase' ;
console.log(message);
$('span.progressLabel').text(i18n.getMessage('stm32GlobalErase'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32GlobalErase'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
self.send([self.command.erase, 0xBC], 1, function (reply) { // 0x43 ^ 0xFF
if (self.verify_response(self.status.ACK, reply)) {
@ -520,7 +514,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
} else {
var message = 'Executing local erase';
console.log(message);
$('span.progressLabel').text(i18n.getMessage('stm32LocalErase'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32LocalErase'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
self.send([self.command.erase, 0xBC], 1, function (reply) { // 0x43 ^ 0xFF
if (self.verify_response(self.status.ACK, reply)) {
@ -553,7 +547,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
case 5:
// upload
console.log('Writing data ...');
$('span.progressLabel').text(i18n.getMessage('stm32Flashing'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Flashing'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
var blocks = self.hex.data.length - 1,
flashing_block = 0,
@ -598,7 +592,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
});
// update progress bar
self.progress_bar_e.val(Math.round(bytes_flashed_total / (self.hex.bytes_total * 2) * 100));
TABS.firmware_flasher.flashProgress(Math.round(bytes_flashed_total / (self.hex.bytes_total * 2) * 100));
}
});
}
@ -628,7 +622,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
case 6:
// verify
console.log('Verifying data ...');
$('span.progressLabel').text(i18n.getMessage('stm32Verifying'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Verifying'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
var blocks = self.hex.data.length - 1,
reading_block = 0,
@ -674,7 +668,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
});
// update progress bar
self.progress_bar_e.val(Math.round((self.hex.bytes_total + bytes_verified_total) / (self.hex.bytes_total * 2) * 100));
TABS.firmware_flasher.flashProgress(Math.round((self.hex.bytes_total + bytes_verified_total) / (self.hex.bytes_total * 2) * 100));
}
});
}
@ -701,16 +695,14 @@ STM32_protocol.prototype.upload_procedure = function (step) {
if (verify) {
console.log('Programming: SUCCESSFUL');
// update progress bar
$('span.progressLabel').text(i18n.getMessage('stm32ProgrammingSuccessful'));
self.progress_label_e.addClass('valid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ProgrammingSuccessful'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.VALID);
// proceed to next step
self.upload_procedure(7);
} else {
console.log('Programming: FAILED');
// update progress bar
$('span.progressLabel').text(i18n.getMessage('stm32ProgrammingFailed'));
self.progress_label_e.addClass('invalid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ProgrammingFailed'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
// disconnect
self.upload_procedure(99);

View file

@ -85,10 +85,9 @@ STM32DFU_protocol.prototype.connect = function (device, hex, options, callback)
self.verify_hex = [];
// reset progress bar to initial state
self.progress_bar_e = $('.progress');
self.progress_bar_e.val(0);
self.progress_label_e = $('span.progressLabel');
self.progress_label_e.removeClass('valid invalid actionRequired');
TABS.firmware_flasher.flashingMessage(null, TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL)
.flashProgress(0);
chrome.usb.getDevices(device, function (result) {
if (result.length) {
@ -579,9 +578,9 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
var unprotect = function() {
console.log('Initiate read unprotect');
GUI.log(i18n.getMessage('stm32ReadProtected'));
$('span.progressLabel').text(i18n.getMessage('stm32ReadProtected'));
self.progress_label_e.addClass('actionRequired');
let messageReadProtected = i18n.getMessage('stm32ReadProtected');
GUI.log(messageReadProtected);
TABS.firmware_flasher.flashingMessage(messageReadProtected, TABS.firmware_flasher.FLASH_MESSAGE_TYPES.ACTION)
self.controlTransfer('out', self.request.DNLOAD, 0, 0, 0, [0x92], function () { // 0x92 initiates read unprotect
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
@ -591,7 +590,9 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
var timeSpentWaiting = 0;
var incr = 1000; // one sec incements
var waitForErase = setInterval(function () {
self.progress_bar_e.val( Math.min(timeSpentWaiting/total_delay,1) * 100);
TABS.firmware_flasher.flashProgress(Math.min(timeSpentWaiting / total_delay, 1) * 100);
if(timeSpentWaiting < total_delay)
{
timeSpentWaiting += incr;
@ -602,15 +603,18 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
if(error) { // we encounter an error, but this is expected. should be a stall.
console.log('Unprotect memory command ran successfully. Unplug flight controller. Connect again in DFU mode and try flashing again.');
GUI.log(i18n.getMessage('stm32UnprotectSuccessful'));
GUI.log(i18n.getMessage('stm32UnprotectUnplug'));
$('span.progressLabel').text(i18n.getMessage('stm32UnprotectUnplug'));
self.progress_bar_e.val(0);
self.progress_label_e.addClass('actionRequired');
let messageUnprotectUnplug = i18n.getMessage('stm32UnprotectUnplug');
GUI.log(messageUnprotectUnplug);
TABS.firmware_flasher.flashingMessage(messageUnprotectUnplug, TABS.firmware_flasher.FLASH_MESSAGE_TYPES.ACTION)
.flashProgress(0);
} else { // unprotecting the flight controller did not work. It did not reboot.
console.log('Failed to execute unprotect memory command');
GUI.log(i18n.getMessage('stm32UnprotectFailed'));
$('span.progressLabel').text(i18n.getMessage('stm32UnprotectFailed'));
self.progress_label_e.addClass('invalid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32UnprotectFailed'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
console.log(data);
self.upload_procedure(99);
}
@ -618,9 +622,9 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
}, incr);
} else {
console.log('Failed to initiate unprotect memory command');
GUI.log(i18n.getMessage('stm32UnprotectInitFailed'));
$('span.progressLabel').text(i18n.getMessage('stm32UnprotectInitFailed'));
self.progress_label_e.addClass('invalid');
let messageUnprotectInitFailed = i18n.getMessage('stm32UnprotectInitFailed')
GUI.log(messageUnprotectInitFailed);
TABS.firmware_flasher.flashingMessage(messageUnprotectInitFailed, TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID)
self.upload_procedure(99);
}
});
@ -735,7 +739,8 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
}
}
}
$('span.progressLabel').text(i18n.getMessage('stm32Erase'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Erase'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
console.log('Executing local chip erase');
var page = 0;
@ -758,7 +763,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
if (data[4] == self.state.dfuDNLOAD_IDLE) {
// update progress bar
self.progress_bar_e.val((page + 1) / erase_pages.length * 100);
TABS.firmware_flasher.flashProgress((page + 1) / erase_pages.length * 100);
page++;
if(page == erase_pages.length) {
@ -790,7 +795,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
// upload
// we dont need to clear the state as we are already using DFU_DNLOAD
console.log('Writing data ...');
$('span.progressLabel').text(i18n.getMessage('stm32Flashing'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Flashing'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
var blocks = self.hex.data.length - 1;
var flashing_block = 0;
@ -819,7 +824,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
self.controlTransfer('in', self.request.GETSTATUS, 0, 0, 6, 0, function (data) {
if (data[4] == self.state.dfuDNLOAD_IDLE) {
// update progress bar
self.progress_bar_e.val(bytes_flashed_total / (self.hex.bytes_total * 2) * 100);
TABS.firmware_flasher.flashProgress(bytes_flashed_total / (self.hex.bytes_total * 2) * 100);
// flash another page
write();
@ -862,7 +867,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
case 5:
// verify
console.log('Verifying data ...');
$('span.progressLabel').text(i18n.getMessage('stm32Verifying'));
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32Verifying'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.NEUTRAL);
var blocks = self.hex.data.length - 1;
var reading_block = 0;
@ -898,7 +903,7 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
bytes_verified_total += bytes_to_read;
// update progress bar
self.progress_bar_e.val((self.hex.bytes_total + bytes_verified_total) / (self.hex.bytes_total * 2) * 100);
TABS.firmware_flasher.flashProgress((self.hex.bytes_total + bytes_verified_total) / (self.hex.bytes_total * 2) * 100);
// verify another page
read();
@ -930,16 +935,14 @@ STM32DFU_protocol.prototype.upload_procedure = function (step) {
if (verify) {
console.log('Programming: SUCCESSFUL');
// update progress bar
$('span.progressLabel').text(i18n.getMessage('stm32ProgrammingSuccessful'));
self.progress_label_e.addClass('valid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ProgrammingSuccessful'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.VALID);
// proceed to next step
self.upload_procedure(6);
} else {
console.log('Programming: FAILED');
// update progress bar
$('span.progressLabel').text(i18n.getMessage('stm32ProgrammingFailed'));
self.progress_label_e.addClass('invalid');
TABS.firmware_flasher.flashingMessage(i18n.getMessage('stm32ProgrammingFailed'), TABS.firmware_flasher.FLASH_MESSAGE_TYPES.INVALID);
// disconnect
self.upload_procedure(99);

View file

@ -63,7 +63,8 @@ TABS.firmware_flasher.initialize = function (callback) {
FirmwareCache.put(summary, intel_hex);
}
$('span.progressLabel').html('<a class="save_firmware" href="#" title="Save Firmware">' + i18n.getMessage('firmwareFlasherFirmwareOnlineLoaded', parsed_hex.bytes_total) + '</a>');
self.flashingMessage('<a class="save_firmware" href="#" title="Save Firmware">' + i18n.getMessage('firmwareFlasherFirmwareOnlineLoaded', parsed_hex.bytes_total) + '</a>',
self.FLASH_MESSAGE_TYPES.NEUTRAL);
self.enableFlashing(true);
@ -83,7 +84,7 @@ TABS.firmware_flasher.initialize = function (callback) {
$('div.release_info').slideDown();
} else {
$('span.progressLabel').text(i18n.getMessage('firmwareFlasherHexCorrupted'));
self.flashingMessage(i18n.getMessage('firmwareFlasherHexCorrupted'), self.FLASH_MESSAGE_TYPES.INVALID);
}
});
}
@ -316,8 +317,9 @@ TABS.firmware_flasher.initialize = function (callback) {
var target = $(this).val();
if (!GUI.connect_lock) {
$('.progress').val(0).removeClass('valid invalid');
$('span.progressLabel').text(i18n.getMessage('firmwareFlasherLoadFirmwareFile'));
self.flashingMessage(i18n.getMessage('firmwareFlasherLoadFirmwareFile'), self.FLASH_MESSAGE_TYPES.NEUTRAL)
.flashProgress(0);
$('div.git_info').slideUp();
$('div.release_info').slideUp();
@ -336,7 +338,7 @@ TABS.firmware_flasher.initialize = function (callback) {
$("<option value='{0}'>{0} - {1} - {2}</option>".format(
descriptor.version,
descriptor.target,
descriptor.date,
descriptor.date
))
.css("font-weight", FirmwareCache.has(descriptor)
? "bold"
@ -397,9 +399,9 @@ TABS.firmware_flasher.initialize = function (callback) {
self.enableFlashing(true);
$('span.progressLabel').text(i18n.getMessage('firmwareFlasherFirmwareLocalLoaded', parsed_hex.bytes_total));
self.flashingMessage(i18n.getMessage('firmwareFlasherFirmwareLocalLoaded', parsed_hex.bytes_total), self.FLASH_MESSAGE_TYPES.NEUTRAL);
} else {
$('span.progressLabel').text(i18n.getMessage('firmwareFlasherHexCorrupted'));
self.flashingMessage(i18n.getMessage('firmwareFlasherHexCorrupted'), self.FLASH_MESSAGE_TYPES.INVALID);
}
});
}
@ -708,3 +710,44 @@ TABS.firmware_flasher.enableFlashing = function (enabled) {
$('a.flash_firmware').addClass('disabled');
}
}
TABS.firmware_flasher.FLASH_MESSAGE_TYPES = {NEUTRAL : 'NEUTRAL',
VALID : 'VALID',
INVALID : 'INVALID',
ACTION : 'ACTION'};
TABS.firmware_flasher.flashingMessage = function(message, type) {
let self = this;
let progressLabel_e = $('span.progressLabel');
switch (type) {
case self.FLASH_MESSAGE_TYPES.VALID:
progressLabel_e.removeClass('invalid actionRequired')
.addClass('valid');
break;
case self.FLASH_MESSAGE_TYPES.INVALID:
progressLabel_e.removeClass('valid actionRequired')
.addClass('invalid');
break;
case self.FLASH_MESSAGE_TYPES.ACTION:
progressLabel_e.removeClass('valid invalid')
.addClass('actionRequired');
break;
case self.FLASH_MESSAGE_TYPES.NEUTRAL:
default:
progressLabel_e.removeClass('valid invalid actionRequired');
break;
}
if (message != null) {
progressLabel_e.html(message);
}
return self;
};
TABS.firmware_flasher.flashProgress = function(value) {
$('.progress').val(value);
return this;
};