mirror of
https://github.com/iNavFlight/inav-configurator.git
synced 2025-07-25 01:05:12 +03:00
timing methods extracted to separate classes
This commit is contained in:
parent
fc11eca5c0
commit
d1fcb81017
26 changed files with 262 additions and 219 deletions
|
@ -802,7 +802,7 @@ function configuration_restore(callback) {
|
|||
function reinitialize() {
|
||||
GUI.log(chrome.i18n.getMessage('deviceRebooting'));
|
||||
|
||||
GUI.timeout_add('waiting_for_bootup', function waiting_for_bootup() {
|
||||
helper.timeout.add('waiting_for_bootup', function waiting_for_bootup() {
|
||||
MSP.send_message(MSPCodes.MSP_IDENT, false, false, function () {
|
||||
GUI.log(chrome.i18n.getMessage('deviceReady'));
|
||||
|
||||
|
|
161
js/gui.js
161
js/gui.js
|
@ -11,8 +11,6 @@ var GUI_control = function () {
|
|||
this.active_tab;
|
||||
this.tab_switch_in_progress = false;
|
||||
this.operating_system;
|
||||
this.interval_array = [];
|
||||
this.timeout_array = [];
|
||||
this.defaultAllowedTabsWhenDisconnected = [
|
||||
'landing',
|
||||
'firmware_flasher',
|
||||
|
@ -51,162 +49,6 @@ var GUI_control = function () {
|
|||
else this.operating_system = "Unknown";
|
||||
};
|
||||
|
||||
// Timer managing methods
|
||||
|
||||
// name = string
|
||||
// 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) {
|
||||
var data = {'name': name, 'timer': null, 'code': code, 'interval': interval, 'fired': 0, 'paused': false};
|
||||
|
||||
if (first == true) {
|
||||
code(); // execute code
|
||||
|
||||
data.fired++; // increment counter
|
||||
}
|
||||
|
||||
data.timer = setInterval(function() {
|
||||
code(); // execute code
|
||||
|
||||
data.fired++; // increment counter
|
||||
}, interval);
|
||||
|
||||
this.interval_array.push(data); // push to primary interval array
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
// name = string
|
||||
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
|
||||
|
||||
this.interval_array.splice(i, 1); // remove element/object from array
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
// name = string
|
||||
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);
|
||||
this.interval_array[i].paused = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
// name = string
|
||||
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];
|
||||
|
||||
obj.timer = setInterval(function() {
|
||||
obj.code(); // execute code
|
||||
|
||||
obj.fired++; // increment counter
|
||||
}, obj.interval);
|
||||
|
||||
obj.paused = false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
// 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) {
|
||||
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) {
|
||||
if (self.interval_array[i].name == name) {
|
||||
keep = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!keep) {
|
||||
clearInterval(this.interval_array[i].timer); // stop timer
|
||||
|
||||
this.interval_array.splice(i, 1); // remove element/object from array
|
||||
|
||||
timers_killed++;
|
||||
}
|
||||
}
|
||||
|
||||
return timers_killed;
|
||||
};
|
||||
|
||||
// name = string
|
||||
// code = function reference (code to be executed)
|
||||
// timeout = timeout in miliseconds
|
||||
GUI_control.prototype.timeout_add = function (name, code, timeout) {
|
||||
var self = this;
|
||||
var data = {'name': name, 'timer': null, 'timeout': timeout};
|
||||
|
||||
// start timer with "cleaning" callback
|
||||
data.timer = setTimeout(function() {
|
||||
code(); // execute code
|
||||
|
||||
// remove object from array
|
||||
var index = self.timeout_array.indexOf(data);
|
||||
if (index > -1) self.timeout_array.splice(index, 1);
|
||||
}, timeout);
|
||||
|
||||
this.timeout_array.push(data); // push to primary timeout array
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
// name = string
|
||||
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
|
||||
|
||||
this.timeout_array.splice(i, 1); // remove element/object from array
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
// no input paremeters
|
||||
// return = returns timers killed in last call
|
||||
GUI_control.prototype.timeout_kill_all = function () {
|
||||
var timers_killed = 0;
|
||||
|
||||
for (var i = 0; i < this.timeout_array.length; i++) {
|
||||
clearTimeout(this.timeout_array[i].timer); // stop timer
|
||||
|
||||
timers_killed++;
|
||||
}
|
||||
|
||||
this.timeout_array = []; // drop objects
|
||||
|
||||
return timers_killed;
|
||||
};
|
||||
|
||||
// message = string
|
||||
GUI_control.prototype.log = function (message) {
|
||||
var command_log = $('div#log');
|
||||
|
@ -233,7 +75,8 @@ GUI_control.prototype.log = function (message) {
|
|||
// default switch doesn't require callback to be set
|
||||
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
|
||||
|
||||
helper.interval.killAll();
|
||||
|
||||
if (this.active_tab) {
|
||||
TABS[this.active_tab].cleanup(callback);
|
||||
|
|
131
js/intervals.js
Normal file
131
js/intervals.js
Normal file
|
@ -0,0 +1,131 @@
|
|||
'use strict';
|
||||
|
||||
var helper = helper || {};
|
||||
|
||||
helper.interval = (function () {
|
||||
|
||||
var privateScope = {},
|
||||
publicScope = {};
|
||||
|
||||
privateScope.intervals = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} name
|
||||
* @param {Function} code function reference (code to be executed)
|
||||
* @param {int} interval time interval in milliseconds
|
||||
* @param {boolean} first true/false if code should be ran initially before next timer interval hits
|
||||
* @returns {{name: *, timer: null, code: *, interval: *, fired: number, paused: boolean}}
|
||||
*/
|
||||
publicScope.add = function (name, code, interval, first) {
|
||||
var data = {
|
||||
'name': name,
|
||||
'timer': null,
|
||||
'code': code,
|
||||
'interval': interval,
|
||||
'fired': 0,
|
||||
'paused': false
|
||||
};
|
||||
|
||||
if (first == true) {
|
||||
code(); // execute code
|
||||
data.fired++; // increment counter
|
||||
}
|
||||
|
||||
data.timer = setInterval(function() {
|
||||
code();
|
||||
data.fired++;
|
||||
}, interval);
|
||||
|
||||
privateScope.intervals.push(data);
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Method removes and stop execution of interval callback
|
||||
* @param {string} name
|
||||
* @returns {boolean}
|
||||
*/
|
||||
publicScope.remove = function (name) {
|
||||
for (var i = 0; i < privateScope.intervals.length; i++) {
|
||||
if (privateScope.intervals[i].name == name) {
|
||||
clearInterval(privateScope.intervals[i].timer); // stop timer
|
||||
privateScope.intervals.splice(i, 1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name
|
||||
* @returns {boolean}
|
||||
*/
|
||||
publicScope.pause = function (name) {
|
||||
for (var i = 0; i < privateScope.intervals.length; i++) {
|
||||
if (privateScope.intervals[i].name == name) {
|
||||
clearInterval(privateScope.intervals[i].timer);
|
||||
privateScope.intervals[i].paused = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name
|
||||
* @returns {boolean}
|
||||
*/
|
||||
publicScope.resume = function (name) {
|
||||
for (var i = 0; i < privateScope.intervals.length; i++) {
|
||||
if (privateScope.intervals[i].name == name && privateScope.intervals[i].paused) {
|
||||
var obj = privateScope.intervals[i];
|
||||
|
||||
obj.timer = setInterval(function() {
|
||||
obj.code(); // execute code
|
||||
obj.fired++; // increment counter
|
||||
}, obj.interval);
|
||||
|
||||
obj.paused = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {=} keep_array
|
||||
* @returns {number}
|
||||
*/
|
||||
publicScope.killAll = function (keep_array) {
|
||||
var timers_killed = 0;
|
||||
|
||||
for (var i = (privateScope.intervals.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) {
|
||||
if (privateScope.intervals[i].name == name) {
|
||||
keep = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!keep) {
|
||||
clearInterval(privateScope.intervals[i].timer); // stop timer
|
||||
privateScope.intervals.splice(i, 1); // remove element/object from array
|
||||
timers_killed++;
|
||||
}
|
||||
}
|
||||
|
||||
return timers_killed;
|
||||
};
|
||||
|
||||
return publicScope;
|
||||
})();
|
|
@ -112,7 +112,7 @@ PortHandler.check = function () {
|
|||
if (GUI.auto_connect && !GUI.connecting_to && !GUI.connected_to) {
|
||||
// we need firmware flasher protection over here
|
||||
if (GUI.active_tab != 'firmware_flasher') {
|
||||
GUI.timeout_add('auto-connect_timeout', function () {
|
||||
helper.timeout.add('auto-connect_timeout', function () {
|
||||
$('div#port-picker a.connect').click();
|
||||
}, 100); // timeout so bus have time to initialize after being detected by the system
|
||||
}
|
||||
|
|
|
@ -154,7 +154,7 @@ STM32_protocol.prototype.initialize = function () {
|
|||
self.read(info);
|
||||
});
|
||||
|
||||
GUI.interval_add('STM32_timeout', function () {
|
||||
helper.interval.add('STM32_timeout', function () {
|
||||
if (self.upload_process_alive) { // process is running
|
||||
self.upload_process_alive = false;
|
||||
} else {
|
||||
|
@ -166,7 +166,7 @@ STM32_protocol.prototype.initialize = function () {
|
|||
googleAnalytics.sendEvent('Flashing', 'Programming', 'timeout');
|
||||
|
||||
// protocol got stuck, clear timer and disconnect
|
||||
GUI.interval_remove('STM32_timeout');
|
||||
helper.interval.remove('STM32_timeout');
|
||||
|
||||
// exit
|
||||
self.upload_procedure(99);
|
||||
|
@ -361,10 +361,10 @@ STM32_protocol.prototype.upload_procedure = function (step) {
|
|||
$('span.progressLabel').text('Contacting bootloader ...');
|
||||
|
||||
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
|
||||
helper.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
|
||||
self.send([0x7F], 1, function (reply) {
|
||||
if (reply[0] == 0x7F || reply[0] == self.status.ACK || reply[0] == self.status.NACK) {
|
||||
GUI.interval_remove('stm32_initialize_mcu');
|
||||
helper.interval.remove('stm32_initialize_mcu');
|
||||
console.log('STM32 - Serial interface initialized on the MCU side');
|
||||
|
||||
// proceed to next step
|
||||
|
@ -373,7 +373,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
|
|||
$('span.progressLabel').text('Communication with bootloader failed');
|
||||
self.progress_bar_e.addClass('invalid');
|
||||
|
||||
GUI.interval_remove('stm32_initialize_mcu');
|
||||
helper.interval.remove('stm32_initialize_mcu');
|
||||
|
||||
// disconnect
|
||||
self.upload_procedure(99);
|
||||
|
@ -387,8 +387,8 @@ STM32_protocol.prototype.upload_procedure = function (step) {
|
|||
$('span.progressLabel').text('No response from the bootloader, programming: FAILED');
|
||||
self.progress_bar_e.addClass('invalid');
|
||||
|
||||
GUI.interval_remove('stm32_initialize_mcu');
|
||||
GUI.interval_remove('STM32_timeout');
|
||||
helper.interval.remove('stm32_initialize_mcu');
|
||||
helper.interval.remove('STM32_timeout');
|
||||
|
||||
// exit
|
||||
self.upload_procedure(99);
|
||||
|
@ -749,7 +749,7 @@ STM32_protocol.prototype.upload_procedure = function (step) {
|
|||
break;
|
||||
case 99:
|
||||
// disconnect
|
||||
GUI.interval_remove('STM32_timeout'); // stop STM32 timeout timer (everything is finished now)
|
||||
helper.interval.remove('STM32_timeout'); // stop STM32 timeout timer (everything is finished now)
|
||||
|
||||
// close connection
|
||||
serial.disconnect(function (result) {
|
||||
|
|
|
@ -35,7 +35,7 @@ $(document).ready(function () {
|
|||
}, 5000);
|
||||
} else {
|
||||
|
||||
GUI.timeout_add('waiting_for_bootup', function waiting_for_bootup() {
|
||||
helper.timeout.add('waiting_for_bootup', function waiting_for_bootup() {
|
||||
MSP.send_message(MSPCodes.MSP_IDENT, false, false, function () {
|
||||
//noinspection JSUnresolvedVariable
|
||||
GUI.log(chrome.i18n.getMessage('deviceReady'));
|
||||
|
@ -98,8 +98,8 @@ $(document).ready(function () {
|
|||
|
||||
serial.connect(selected_port, {bitrate: selected_baud}, onOpen);
|
||||
} else {
|
||||
GUI.timeout_kill_all();
|
||||
GUI.interval_kill_all();
|
||||
helper.timeout.killAll();
|
||||
helper.interval.killAll();
|
||||
GUI.tab_switch_cleanup();
|
||||
GUI.tab_switch_in_progress = false;
|
||||
|
||||
|
@ -213,7 +213,7 @@ function onOpen(openInfo) {
|
|||
serial.onReceive.addListener(read_serial);
|
||||
|
||||
// disconnect after 10 seconds with error if we don't get IDENT data
|
||||
GUI.timeout_add('connecting', function () {
|
||||
helper.timeout.add('connecting', function () {
|
||||
if (!CONFIGURATOR.connectionValid) {
|
||||
GUI.log(chrome.i18n.getMessage('noConfigurationReceived'));
|
||||
|
||||
|
@ -306,7 +306,7 @@ function onOpen(openInfo) {
|
|||
}
|
||||
|
||||
function onConnect() {
|
||||
GUI.timeout_remove('connecting'); // kill connecting timer
|
||||
helper.timeout.remove('connecting'); // kill connecting timer
|
||||
$('div#connectbutton a.connect_state').text(chrome.i18n.getMessage('disconnect')).addClass('active');
|
||||
$('div#connectbutton a.connect').addClass('active');
|
||||
$('#tabs ul.mode-disconnected').hide();
|
||||
|
@ -488,7 +488,7 @@ function update_dataflash_global() {
|
|||
|
||||
function startLiveDataRefreshTimer() {
|
||||
// live data refresh
|
||||
GUI.timeout_add('data_refresh', function () { update_live_status(); }, 100);
|
||||
helper.timeout.add('data_refresh', function () { update_live_status(); }, 100);
|
||||
}
|
||||
|
||||
function update_live_status() {
|
||||
|
@ -566,7 +566,7 @@ function update_live_status() {
|
|||
}
|
||||
|
||||
statuswrapper.show();
|
||||
GUI.timeout_remove('data_refresh');
|
||||
helper.timeout.remove('data_refresh');
|
||||
startLiveDataRefreshTimer();
|
||||
}
|
||||
|
||||
|
|
66
js/timeouts.js
Normal file
66
js/timeouts.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
'use strict';
|
||||
|
||||
var helper = helper || {};
|
||||
|
||||
helper.timeout = (function () {
|
||||
|
||||
var privateScope = {},
|
||||
publicScope = {};
|
||||
|
||||
privateScope.timeouts = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {function } code
|
||||
* @param {number} timeout
|
||||
* @returns {{name: *, timer: null, timeout: *}}
|
||||
*/
|
||||
publicScope.add = function (name, code, timeout) {
|
||||
var data = {'name': name, 'timer': null, 'timeout': timeout};
|
||||
|
||||
// start timer with "cleaning" callback
|
||||
data.timer = setTimeout(function() {
|
||||
code(); // execute code
|
||||
|
||||
// remove object from array
|
||||
var index = privateScope.timeouts.indexOf(data);
|
||||
if (index > -1) privateScope.timeouts.splice(index, 1);
|
||||
}, timeout);
|
||||
|
||||
privateScope.timeouts.push(data); // push to primary timeout array
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
publicScope.remove = function (name) {
|
||||
for (var i = 0; i < privateScope.timeouts.length; i++) {
|
||||
if (privateScope.timeouts[i].name == name) {
|
||||
clearTimeout(privateScope.timeouts[i].timer); // stop timer
|
||||
privateScope.timeouts.splice(i, 1); // remove element/object from array
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {number} number of killed timeouts
|
||||
*/
|
||||
publicScope.killAll = function () {
|
||||
var timers_killed = 0;
|
||||
|
||||
for (var i = 0; i < privateScope.timeouts.length; i++) {
|
||||
clearTimeout(privateScope.timeouts[i].timer); // stop timer
|
||||
timers_killed++;
|
||||
}
|
||||
|
||||
privateScope.timeouts = []; // drop objects
|
||||
|
||||
return timers_killed;
|
||||
};
|
||||
|
||||
return publicScope;
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue