mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-24 16:55:36 +03:00
lots of work on removing global variables
This commit is contained in:
parent
377853b58a
commit
ec44b77ff2
8 changed files with 156 additions and 265 deletions
|
@ -47,7 +47,7 @@ function configuration_backup() {
|
|||
var now = d.getUTCFullYear() + '.' + d.getDate() + '.' + (d.getMonth() + 1) + '.' + d.getHours() + '.' + d.getMinutes();
|
||||
|
||||
// create or load the file
|
||||
chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: 'bf_mw_backup_' + now, accepts: accepts}, function(fileEntry) {
|
||||
chrome.fileSystem.chooseEntry({type: 'saveFile', suggestedName: 'bf_mw_backup_' + now, accepts: accepts}, function (fileEntry) {
|
||||
if (!fileEntry) {
|
||||
console.log('No file selected, backup aborted.');
|
||||
|
||||
|
@ -57,14 +57,14 @@ function configuration_backup() {
|
|||
chosenFileEntry = fileEntry;
|
||||
|
||||
// echo/console log path specified
|
||||
chrome.fileSystem.getDisplayPath(chosenFileEntry, function(path) {
|
||||
chrome.fileSystem.getDisplayPath(chosenFileEntry, function (path) {
|
||||
console.log('Backup file path: ' + path);
|
||||
});
|
||||
|
||||
// change file entry from read only to read/write
|
||||
chrome.fileSystem.getWritableEntry(chosenFileEntry, function(fileEntryWritable) {
|
||||
chrome.fileSystem.getWritableEntry(chosenFileEntry, function (fileEntryWritable) {
|
||||
// check if file is writable
|
||||
chrome.fileSystem.isWritableEntry(fileEntryWritable, function(isWritable) {
|
||||
chrome.fileSystem.isWritableEntry(fileEntryWritable, function (isWritable) {
|
||||
if (isWritable) {
|
||||
chosenFileEntry = fileEntryWritable;
|
||||
|
||||
|
@ -83,13 +83,13 @@ function configuration_backup() {
|
|||
var serialized_config_object = JSON.stringify(configuration);
|
||||
var blob = new Blob([serialized_config_object], {type: 'text/plain'}); // first parameter for Blob needs to be an array
|
||||
|
||||
chosenFileEntry.createWriter(function(writer) {
|
||||
chosenFileEntry.createWriter(function (writer) {
|
||||
writer.onerror = function (e) {
|
||||
console.error(e);
|
||||
};
|
||||
|
||||
var truncated = false;
|
||||
writer.onwriteend = function() {
|
||||
writer.onwriteend = function () {
|
||||
if (!truncated) {
|
||||
// onwriteend will be fired again when truncation is finished
|
||||
truncated = true;
|
||||
|
@ -126,7 +126,7 @@ function configuration_restore() {
|
|||
}];
|
||||
|
||||
// load up the file
|
||||
chrome.fileSystem.chooseEntry({type: 'openFile', accepts: accepts}, function(fileEntry) {
|
||||
chrome.fileSystem.chooseEntry({type: 'openFile', accepts: accepts}, function (fileEntry) {
|
||||
if (!fileEntry) {
|
||||
console.log('No file selected, restore aborted.');
|
||||
|
||||
|
@ -136,15 +136,15 @@ function configuration_restore() {
|
|||
chosenFileEntry = fileEntry;
|
||||
|
||||
// echo/console log path specified
|
||||
chrome.fileSystem.getDisplayPath(chosenFileEntry, function(path) {
|
||||
chrome.fileSystem.getDisplayPath(chosenFileEntry, function (path) {
|
||||
console.log('Restore file path: ' + path);
|
||||
});
|
||||
|
||||
// read contents into variable
|
||||
chosenFileEntry.file(function(file) {
|
||||
chosenFileEntry.file(function (file) {
|
||||
var reader = new FileReader();
|
||||
|
||||
reader.onprogress = function(e) {
|
||||
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');
|
||||
|
@ -152,7 +152,7 @@ function configuration_restore() {
|
|||
}
|
||||
};
|
||||
|
||||
reader.onloadend = function(e) {
|
||||
reader.onloadend = function (e) {
|
||||
if (e.total != 0 && e.total == e.loaded) {
|
||||
console.log('Read SUCCESSFUL');
|
||||
|
||||
|
@ -295,9 +295,9 @@ function configuration_upload() {
|
|||
buffer_out[21] = 0; // vbatlevel_crit (unused)
|
||||
|
||||
// Send ove the new MISC
|
||||
MSP.send_message(MSP_codes.MSP_SET_MISC, buffer_out, false, function() {
|
||||
MSP.send_message(MSP_codes.MSP_SET_MISC, buffer_out, false, function () {
|
||||
// Save changes to EEPROM
|
||||
MSP.send_message(MSP_codes.MSP_EEPROM_WRITE, false, false, function() {
|
||||
MSP.send_message(MSP_codes.MSP_EEPROM_WRITE, false, false, function () {
|
||||
GUI.log(chrome.i18n.getMessage('eeprom_saved_ok'));
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
'use strict';
|
||||
|
||||
var firmware_version_accepted = 2.3;
|
||||
var CONFIGURATOR = {
|
||||
'firmwareVersionAccepted': 2.3,
|
||||
'connectionValid': false,
|
||||
'mspPassThrough': false,
|
||||
'cliActive': false,
|
||||
'cliValid': false
|
||||
};
|
||||
|
||||
var CONFIG = {
|
||||
version: 0,
|
||||
|
|
24
js/gui.js
24
js/gui.js
|
@ -2,7 +2,7 @@
|
|||
|
||||
var tabs = {}; // filled by individual tab js file
|
||||
|
||||
var GUI_control = function() {
|
||||
var GUI_control = function () {
|
||||
this.auto_connect = false;
|
||||
this.connecting_to = false;
|
||||
this.connected_to = false;
|
||||
|
@ -29,7 +29,7 @@ var GUI_control = function() {
|
|||
// code = function reference (code to be executed)
|
||||
// interval = time interval in miliseconds
|
||||
// first = true/false if code should be ran initially before next timer interval hits
|
||||
GUI_control.prototype.interval_add = function(name, code, interval, first) {
|
||||
GUI_control.prototype.interval_add = function (name, code, interval, first) {
|
||||
var data = {'name': name, 'timer': undefined, 'code': code, 'interval': interval, 'fired': 0, 'paused': false};
|
||||
|
||||
if (first == true) {
|
||||
|
@ -50,7 +50,7 @@ GUI_control.prototype.interval_add = function(name, code, interval, first) {
|
|||
};
|
||||
|
||||
// name = string
|
||||
GUI_control.prototype.interval_remove = function(name) {
|
||||
GUI_control.prototype.interval_remove = function (name) {
|
||||
for (var i = 0; i < this.interval_array.length; i++) {
|
||||
if (this.interval_array[i].name == name) {
|
||||
clearInterval(this.interval_array[i].timer); // stop timer
|
||||
|
@ -65,7 +65,7 @@ GUI_control.prototype.interval_remove = function(name) {
|
|||
};
|
||||
|
||||
// name = string
|
||||
GUI_control.prototype.interval_pause = function(name) {
|
||||
GUI_control.prototype.interval_pause = function (name) {
|
||||
for (var i = 0; i < this.interval_array.length; i++) {
|
||||
if (this.interval_array[i].name == name) {
|
||||
clearInterval(this.interval_array[i].timer);
|
||||
|
@ -79,7 +79,7 @@ GUI_control.prototype.interval_pause = function(name) {
|
|||
};
|
||||
|
||||
// name = string
|
||||
GUI_control.prototype.interval_resume = function(name) {
|
||||
GUI_control.prototype.interval_resume = function (name) {
|
||||
for (var i = 0; i < this.interval_array.length; i++) {
|
||||
if (this.interval_array[i].name == name && this.interval_array[i].paused) {
|
||||
var obj = this.interval_array[i];
|
||||
|
@ -101,14 +101,14 @@ GUI_control.prototype.interval_resume = function(name) {
|
|||
|
||||
// input = array of timers thats meant to be kept, or nothing
|
||||
// return = returns timers killed in last call
|
||||
GUI_control.prototype.interval_kill_all = function(keep_array) {
|
||||
GUI_control.prototype.interval_kill_all = function (keep_array) {
|
||||
var self = this;
|
||||
var timers_killed = 0;
|
||||
|
||||
for (var i = (this.interval_array.length - 1); i >= 0; i--) { // reverse iteration
|
||||
var keep = false;
|
||||
if (keep_array) { // only run through the array if it exists
|
||||
keep_array.forEach(function(name) {
|
||||
keep_array.forEach(function (name) {
|
||||
if (self.interval_array[i].name == name) {
|
||||
keep = true;
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ GUI_control.prototype.interval_kill_all = function(keep_array) {
|
|||
// name = string
|
||||
// code = function reference (code to be executed)
|
||||
// timeout = timeout in miliseconds
|
||||
GUI_control.prototype.timeout_add = function(name, code, timeout) {
|
||||
GUI_control.prototype.timeout_add = function (name, code, timeout) {
|
||||
var self = this;
|
||||
var data = {'name': name, 'timer': undefined, 'timeout': timeout};
|
||||
|
||||
|
@ -149,7 +149,7 @@ GUI_control.prototype.timeout_add = function(name, code, timeout) {
|
|||
};
|
||||
|
||||
// name = string
|
||||
GUI_control.prototype.timeout_remove = function(name) {
|
||||
GUI_control.prototype.timeout_remove = function (name) {
|
||||
for (var i = 0; i < this.timeout_array.length; i++) {
|
||||
if (this.timeout_array[i].name == name) {
|
||||
clearTimeout(this.timeout_array[i].timer); // stop timer
|
||||
|
@ -165,7 +165,7 @@ GUI_control.prototype.timeout_remove = function(name) {
|
|||
|
||||
// no input paremeters
|
||||
// return = returns timers killed in last call
|
||||
GUI_control.prototype.timeout_kill_all = function() {
|
||||
GUI_control.prototype.timeout_kill_all = function () {
|
||||
var timers_killed = 0;
|
||||
|
||||
for (var i = 0; i < this.timeout_array.length; i++) {
|
||||
|
@ -180,7 +180,7 @@ GUI_control.prototype.timeout_kill_all = function() {
|
|||
};
|
||||
|
||||
// message = string
|
||||
GUI_control.prototype.log = function(message) {
|
||||
GUI_control.prototype.log = function (message) {
|
||||
var command_log = $('div#log');
|
||||
var d = new Date();
|
||||
var time = ((d.getHours() < 10) ? '0' + d.getHours(): d.getHours())
|
||||
|
@ -194,7 +194,7 @@ GUI_control.prototype.log = function(message) {
|
|||
// Method is called every time a valid tab change event is received
|
||||
// callback = code to run when cleanup is finished
|
||||
// default switch doesn't require callback to be set
|
||||
GUI_control.prototype.tab_switch_cleanup = function(callback) {
|
||||
GUI_control.prototype.tab_switch_cleanup = function (callback) {
|
||||
MSP.callbacks_cleanup(); // we don't care about any old data that might or might not arrive
|
||||
GUI.interval_kill_all(); // all intervals (mostly data pulling) needs to be removed on tab switch
|
||||
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
'use strict';
|
||||
|
||||
var configuration_received = false;
|
||||
|
||||
$(document).ready(function() {
|
||||
$('div#port-picker a.connect').click(function() {
|
||||
if (GUI.connect_lock != true) { // GUI control overrides the user control
|
||||
|
@ -36,8 +34,8 @@ $(document).ready(function() {
|
|||
|
||||
MSP.disconnect_cleanup();
|
||||
PortUsage.reset();
|
||||
configuration_received = false; // reset valid config received variable (used to block tabs while not connected properly)
|
||||
MSP_pass_through = false;
|
||||
CONFIGURATOR.connectionValid = false;
|
||||
CONFIGURATOR.mspPassThrough = false;
|
||||
|
||||
// unlock port select & baud
|
||||
$('div#port-picker #port').prop('disabled', false);
|
||||
|
@ -127,10 +125,10 @@ function onOpen(openInfo) {
|
|||
|
||||
serial.onReceive.addListener(read_serial);
|
||||
|
||||
if (!MSP_pass_through) {
|
||||
if (!CONFIGURATOR.mspPassThrough) {
|
||||
// disconnect after 10 seconds with error if we don't get IDENT data
|
||||
GUI.timeout_add('connecting', function() {
|
||||
if (!configuration_received) {
|
||||
if (!CONFIGURATOR.connectionValid) {
|
||||
GUI.log(chrome.i18n.getMessage('noConfigurationReceived'));
|
||||
|
||||
$('div#port-picker a.connect').click(); // disconnect
|
||||
|
@ -145,13 +143,13 @@ function onOpen(openInfo) {
|
|||
|
||||
GUI.log(chrome.i18n.getMessage('firmwareVersion', [CONFIG.version]));
|
||||
|
||||
if (CONFIG.version >= firmware_version_accepted) {
|
||||
configuration_received = true;
|
||||
if (CONFIG.version >= CONFIGURATOR.firmwareVersionAccepted) {
|
||||
CONFIGURATOR.connectionValid = true;
|
||||
|
||||
$('div#port-picker a.connect').text(chrome.i18n.getMessage('disconnect')).addClass('active');
|
||||
$('#tabs li a:first').click();
|
||||
} else {
|
||||
GUI.log(chrome.i18n.getMessage('firmwareVersionNotSupported', [firmware_version_accepted]));
|
||||
GUI.log(chrome.i18n.getMessage('firmwareVersionNotSupported', [CONFIGURATOR.firmwareVersionAccepted]));
|
||||
$('div#port-picker a.connect').click(); // disconnect
|
||||
}
|
||||
});
|
||||
|
@ -184,18 +182,18 @@ function onClosed(result) {
|
|||
}
|
||||
|
||||
function read_serial(info) {
|
||||
if (!CLI_active && !MSP_pass_through) {
|
||||
if (!CONFIGURATOR.cliActive && !CONFIGURATOR.mspPassThrough) {
|
||||
MSP.read(info);
|
||||
} else if (CLI_active) {
|
||||
handle_CLI(info);
|
||||
} else if (MSP_pass_through) { // needs to be verified, might be removed after pass_through is 100% deployed
|
||||
} else if (CONFIGURATOR.cliActive) {
|
||||
tabs.cli.read(info);
|
||||
} else if (CONFIGURATOR.mspPassThrough) {
|
||||
MSP.read(info);
|
||||
}
|
||||
}
|
||||
|
||||
function sensor_status(sensors_detected) {
|
||||
// initialize variable (if it wasn't)
|
||||
if (typeof sensor_status.previous_sensors_detected == 'undefined') {
|
||||
if (sensor_status.previous_sensors_detected === 'undefined') {
|
||||
sensor_status.previous_sensors_detected = 0;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue