betaflight-configurator/tabs/firmware_flasher.js

217 lines
9.5 KiB
JavaScript
Raw Normal View History

function tab_initialize_firmware_flasher() {
ga_tracker.sendAppView('Firmware Flasher');
GUI.active_tab = 'firmware_flasher';
2013-11-13 08:13:24 +00:00
var intel_hex = false; // standard intel hex in string format
2013-11-15 15:17:43 +00:00
var parsed_hex = false; // parsed raw hex in array format
$('#content').load("./tabs/firmware_flasher.html", function() {
// UI Hooks
$('a.load_file').click(function() {
chrome.fileSystem.chooseEntry({type: 'openFile', accepts: [{extensions: ['hex']}]}, function(fileEntry) {
if (!fileEntry) {
// no "valid" file selected/created, aborting
console.log('No valid file selected, aborting');
return;
}
chrome.fileSystem.getDisplayPath(fileEntry, function(path) {
console.log('Loading file from: ' + path);
$('span.path').html(path);
fileEntry.file(function(file) {
var reader = new FileReader();
reader.onprogress = function(e) {
if (e.total > 1048576) { // 1 MB
// dont allow reading files bigger then 1 MB
console.log('File limit (1 MB) exceeded, aborting');
reader.abort();
}
};
reader.onloadend = function(e) {
if (e.total != 0 && e.total == e.loaded) {
console.log('File loaded');
intel_hex = e.target.result;
parse_hex(intel_hex, function(data) {
parsed_hex = data;
if (parsed_hex) {
STM32.GUI_status('<span style="color: green">Firmware loaded, ready for flashing</span>');
$('a.flash_firmware').removeClass('locked');
$('span.size').html(parsed_hex.bytes_total + ' bytes');
} else {
STM32.GUI_status('<span style="color: red">HEX file appears to be corrupted</span>');
}
});
}
};
reader.readAsText(file);
});
});
});
});
$('a.load_remote_file').click(function() {
2013-11-13 12:53:44 +00:00
/* for future use
$.get('https://api.github.com/repos/multiwii/baseflight/tags', function(data) {
console.log(data)
});
*/
$.get('https://raw.github.com/multiwii/baseflight/master/obj/baseflight.hex', function(data) {
intel_hex = data;
parse_hex(intel_hex, function(data) {
parsed_hex = data;
if (parsed_hex) {
STM32.GUI_status('<span style="color: green">Remote Firmware loaded, ready for flashing</span>');
$('a.flash_firmware').removeClass('locked');
$('span.path').html('Using remote Firmware');
$('span.size').html(parsed_hex.bytes_total + ' bytes');
} else {
STM32.GUI_status('<span style="color: red">HEX file appears to be corrupted</span>');
}
});
}).fail(function() {
STM32.GUI_status('<span style="color: red">Failed to load remote firmware</span>');
$('a.flash_firmware').addClass('locked');
});
});
$('a.flash_firmware').click(function() {
if (!$(this).hasClass('locked')) {
if (!GUI.connect_lock) { // button disabled while flashing is in progress
2013-12-10 07:19:18 +00:00
if (parsed_hex != false) {
STM32.connect(parsed_hex);
} else {
STM32.GUI_status('<span style="color: red">Firmware not loaded</span>');
}
2013-11-14 06:39:39 +00:00
}
}
});
2013-11-14 06:49:02 +00:00
chrome.storage.local.get('no_reboot_sequence', function(result) {
if (typeof result.no_reboot_sequence === 'undefined') {
// wasn't saved yet, save and push false to the GUI
chrome.storage.local.set({'no_reboot_sequence': false});
$('input.updating').prop('checked', false);
} else {
if (result.no_reboot_sequence) {
$('input.updating').prop('checked', true);
$('label.flash_on_connect_wrapper').show();
} else {
$('input.updating').prop('checked', false);
}
}
// bind UI hook so the status is saved on change
$('input.updating').change(function() {
var status = $(this).is(':checked');
if (status) {
$('label.flash_on_connect_wrapper').show();
} else {
$('label.flash_on_connect_wrapper').hide();
2014-02-26 07:32:57 +00:00
$('input.flash_on_connect').prop('checked', false).change();
}
chrome.storage.local.set({'no_reboot_sequence': status}, function() {});
});
});
chrome.storage.local.get('flash_on_connect', function(result) {
if (typeof result.flash_on_connect === 'undefined') {
// wasn't saved yet, save and push false to the GUI
chrome.storage.local.set({'flash_on_connect': false});
$('input.flash_on_connect').prop('checked', false);
} else {
if (result.flash_on_connect) {
$('input.flash_on_connect').prop('checked', true);
} else {
$('input.flash_on_connect').prop('checked', false);
}
}
$('input.flash_on_connect').change(function() {
var status = $(this).is(':checked');
if (status) {
var flashing_port;
var start = function() {
PortHandler.port_detected('flash_next_device', function(result) {
flashing_port = result[0];
2014-02-26 07:32:57 +00:00
GUI.log('Detected: <strong>' + flashing_port + '</strong> - triggering flash on connect');
console.log('Detected: ' + flashing_port + ' - triggering flash on connect');
// Trigger regular Flashing sequence
$('a.flash_firmware').click();
// Detect port removal to create a new callback
end();
}, false, true);
};
var end = function() {
PortHandler.port_removed('flashed_device_removed', function(result) {
for (var i = 0; i < result.length; i++) {
if (result[i] == flashing_port) {
// flashed device removed
2014-02-26 07:32:57 +00:00
GUI.log('Removed: <strong>' + flashing_port + '</strong> - ready for next device');
console.log('Removed: ' + flashing_port + ' - ready for next device');
flashing_port = false;
start();
return;
}
}
// different device removed, we need to retry
end();
}, false, true);
};
start();
} else {
PortHandler.flush_callbacks();
}
chrome.storage.local.set({'flash_on_connect': status}, function() {});
}).change();
});
2013-11-14 06:49:02 +00:00
$('a.back').click(function() {
if (!GUI.connect_lock) { // button disabled while flashing is in progress
GUI.tab_switch_cleanup(function() {
tab_initialize_default();
});
2013-11-14 06:49:02 +00:00
} else {
GUI.log('You <span style="color: red">can\'t</span> do this right now, please wait for current operation to finish ...');
2013-11-14 06:49:02 +00:00
}
});
});
}
function parse_hex(str, callback) {
// parsing hex in different thread
2014-02-07 20:48:04 +00:00
var worker = new Worker('./js/workers/hex_parser.js');
// "callback"
worker.onmessage = function (event) {
callback(event.data);
};
// send data/string over for processing
worker.postMessage(str);
}