function tab_initialize_firmware_flasher() { ga_tracker.sendAppView('Firmware Flasher'); GUI.active_tab = 'firmware_flasher'; var intel_hex = false; // standard intel hex in string format 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('Firmware loaded, ready for flashing'); $('a.flash_firmware').removeClass('locked'); $('span.size').html(parsed_hex.bytes + ' bytes'); } else { STM32.GUI_status('HEX file appears to be corrupted'); } }); } }; reader.readAsText(file); }); }); }); }); $('a.load_remote_file').click(function() { /* 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('Remote Firmware loaded, ready for flashing'); $('a.flash_firmware').removeClass('locked'); $('span.path').html('Using remote Firmware'); $('span.size').html(parsed_hex.bytes + ' bytes'); } else { STM32.GUI_status('HEX file appears to be corrupted'); } }); }).fail(function() { STM32.GUI_status('Failed to load remote firmware'); $('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 if (parsed_hex != false) { STM32.connect(parsed_hex); } else { STM32.GUI_status('Firmware not loaded'); } } } }); $('a.back').click(function() { if (!GUI.connect_lock) { // button disabled while flashing is in progress tab_initialize_default(); } else { GUI.log('You can\'t do this right now, please wait for current operation to finish ...'); } }); }); } function parse_hex(str, callback) { // parsing hex in different thread var worker = new Worker('./workers/hex_parser.js'); // "callback" worker.onmessage = function (event) { callback(event.data); }; // send data/string over for processing worker.postMessage(str); }