From 28880ecc3ea703d4e9734b27a4835a38b5609f5e Mon Sep 17 00:00:00 2001 From: Darren Lines Date: Wed, 18 Jan 2023 12:59:28 +0000 Subject: [PATCH] Add AUTO detect to modes page This adds the very useful AUTO detection of the switches to the range. This is pulled from BetaFlight, where it has proven very useful. To select the switch for the range, set the channel to AUTO, then move the switch. --- _locales/en/messages.json | 5 +++- tabs/auxiliary.js | 49 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 77876b36..a4e7551d 100755 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1486,7 +1486,7 @@ "auxiliaryHelp": { "message": "Use ranges to define the switches on your transmitter and corresponding mode assignments. A receiver channel that gives a reading between a range min/max will activate the mode. Remember to save your settings using the Save button." }, - "auxiliaryToggleUnused": { + "auxiliaryToggleUnused": { "message": "Hide unused modes" }, "auxiliaryMin": { @@ -1498,6 +1498,9 @@ "auxiliaryAddRange": { "message": "Add Range" }, + "auxiliaryAutoChannelSelect" : { + "message": "AUTO" + }, "auxiliaryButtonSave": { "message": "Save" }, diff --git a/tabs/auxiliary.js b/tabs/auxiliary.js index 7d38f164..ab612765 100644 --- a/tabs/auxiliary.js +++ b/tabs/auxiliary.js @@ -137,8 +137,15 @@ TABS.auxiliary.initialize = function (callback) { var channelList = $(rangeTemplate).find('.channel'); var channelOptionTemplate = $(channelList).find('option'); channelOptionTemplate.remove(); + + //add value to autodetect channel + let channelOption = channelOptionTemplate.clone(); + channelOption.text(chrome.i18n.getMessage('auxiliaryAutoChannelSelect')); + channelOption.val(-1); + channelList.append(channelOption); + for (var channelIndex = 0; channelIndex < auxChannelCount; channelIndex++) { - var channelOption = channelOptionTemplate.clone(); + channelOption = channelOptionTemplate.clone(); channelOption.text('CH ' + (channelIndex + 5)); channelOption.val(channelIndex); channelList.append(channelOption); @@ -444,11 +451,51 @@ TABS.auxiliary.initialize = function (callback) { } } + auto_select_channel(RC.channels, RC.active_channels, MISC.rssi_channel); + $(".modeSection").each(function() { $(this).toggle(!hideUnused); }); } + /** + * Autodetect channel based on maximum deference with previous value + * minimum value to autodetect is 100 + */ + function auto_select_channel(RC_channels, activeChannels, RSSI_channel) { + const auto_option = $('.tab-auxiliary select.channel option[value="-1"]:selected'); + if (auto_option.length === 0) { + prevChannelsValues = null; + return; + } + + const fillPrevChannelsValues = function () { + prevChannelsValues = RC_channels.slice(0); //clone array + }; + + if (!prevChannelsValues || RC_channels.length === 0) return fillPrevChannelsValues(); + + let diff_array = RC_channels.map(function(currentValue, index) { + return Math.abs(prevChannelsValues[index] - currentValue); + }); + + diff_array = diff_array.slice(0, activeChannels); + + const largest = diff_array.reduce(function(x,y){ + return (x > y) ? x : y; + }, 0); + + //minimum change to autoselect is 100 + if (largest < 100) return fillPrevChannelsValues(); + + const indexOfMaxValue = diff_array.indexOf(largest); + if (indexOfMaxValue >= 4 && indexOfMaxValue != RSSI_channel - 1){ //set channel + auto_option.parent().val(indexOfMaxValue - 4); + } + + return fillPrevChannelsValues(); + } + let hideUnusedModes = false; chrome.storage.local.get('hideUnusedModes', function (result) { $("input#switch-toggle-unused")