Convert direct reference to string and freq table to vtxCommonXXX services
[betaflight.git] / src / main / io / vtx.c
blob15c8f004e8273cd00d7ba6fc432c915fc6108ef5
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdint.h>
22 #include <string.h>
24 #include "platform.h"
26 #if defined(USE_VTX_COMMON)
28 #include "common/maths.h"
29 #include "common/time.h"
31 #include "drivers/vtx_common.h"
33 #include "fc/config.h"
34 #include "fc/rc_modes.h"
35 #include "fc/runtime_config.h"
37 #include "flight/failsafe.h"
39 #include "io/vtx.h"
40 #include "io/vtx_string.h"
41 #include "io/vtx_control.h"
43 #include "interface/cli.h"
45 #include "pg/pg.h"
46 #include "pg/pg_ids.h"
49 PG_REGISTER_WITH_RESET_TEMPLATE(vtxSettingsConfig_t, vtxSettingsConfig, PG_VTX_SETTINGS_CONFIG, 0);
51 PG_RESET_TEMPLATE(vtxSettingsConfig_t, vtxSettingsConfig,
52 .band = VTX_SETTINGS_DEFAULT_BAND,
53 .channel = VTX_SETTINGS_DEFAULT_CHANNEL,
54 .power = VTX_SETTINGS_DEFAULT_POWER,
55 .freq = VTX_SETTINGS_DEFAULT_FREQ,
56 .pitModeFreq = VTX_SETTINGS_DEFAULT_PITMODE_FREQ,
57 .lowPowerDisarm = VTX_LOW_POWER_DISARM_OFF,
60 typedef enum {
61 VTX_PARAM_POWER = 0,
62 VTX_PARAM_BANDCHAN,
63 VTX_PARAM_PITMODE,
64 VTX_PARAM_CONFIRM,
65 VTX_PARAM_COUNT
66 } vtxScheduleParams_e;
68 void vtxInit(void)
70 bool settingsUpdated = false;
72 // sync frequency in parameter group when band/channel are specified
73 const uint16_t freq = vtxCommonLookupFrequency(vtxCommonDevice(), vtxSettingsConfig()->band, vtxSettingsConfig()->channel);
74 if (vtxSettingsConfig()->band && freq != vtxSettingsConfig()->freq) {
75 vtxSettingsConfigMutable()->freq = freq;
76 settingsUpdated = true;
79 #if defined(VTX_SETTINGS_FREQCMD)
80 // constrain pit mode frequency
81 if (vtxSettingsConfig()->pitModeFreq) {
82 const uint16_t constrainedPitModeFreq = MAX(vtxSettingsConfig()->pitModeFreq, VTX_SETTINGS_MIN_USER_FREQ);
83 if (constrainedPitModeFreq != vtxSettingsConfig()->pitModeFreq) {
84 vtxSettingsConfigMutable()->pitModeFreq = constrainedPitModeFreq;
85 settingsUpdated = true;
88 #endif
90 if (settingsUpdated) {
91 saveConfigAndNotify();
95 STATIC_UNIT_TESTED vtxSettingsConfig_t vtxGetSettings(void)
97 vtxSettingsConfig_t settings = {
98 .band = vtxSettingsConfig()->band,
99 .channel = vtxSettingsConfig()->channel,
100 .power = vtxSettingsConfig()->power,
101 .freq = vtxSettingsConfig()->freq,
102 .pitModeFreq = vtxSettingsConfig()->pitModeFreq,
103 .lowPowerDisarm = vtxSettingsConfig()->lowPowerDisarm,
106 #if defined(VTX_SETTINGS_FREQCMD)
107 if (IS_RC_MODE_ACTIVE(BOXVTXPITMODE) && settings.pitModeFreq) {
108 settings.band = 0;
109 settings.freq = settings.pitModeFreq;
110 settings.power = VTX_SETTINGS_DEFAULT_POWER;
112 #endif
114 if (!ARMING_FLAG(ARMED) && !failsafeIsActive() &&
115 (settings.lowPowerDisarm == VTX_LOW_POWER_DISARM_ALWAYS ||
116 (settings.lowPowerDisarm == VTX_LOW_POWER_DISARM_UNTIL_FIRST_ARM && !ARMING_FLAG(WAS_EVER_ARMED)))) {
117 settings.power = VTX_SETTINGS_DEFAULT_POWER;
120 return settings;
123 static bool vtxProcessBandAndChannel(vtxDevice_t *vtxDevice)
125 if(!ARMING_FLAG(ARMED)) {
126 uint8_t vtxBand;
127 uint8_t vtxChan;
128 if (vtxCommonGetBandAndChannel(vtxDevice, &vtxBand, &vtxChan)) {
129 const vtxSettingsConfig_t settings = vtxGetSettings();
130 if (vtxBand != settings.band || vtxChan != settings.channel) {
131 vtxCommonSetBandAndChannel(vtxDevice, settings.band, settings.channel);
132 return true;
136 return false;
139 #if defined(VTX_SETTINGS_FREQCMD)
140 static bool vtxProcessFrequency(vtxDevice_t *vtxDevice)
142 if(!ARMING_FLAG(ARMED)) {
143 uint16_t vtxFreq;
144 if (vtxCommonGetFrequency(vtxDevice, &vtxFreq)) {
145 const vtxSettingsConfig_t settings = vtxGetSettings();
146 if (vtxFreq != settings.freq) {
147 vtxCommonSetFrequency(vtxDevice, settings.freq);
148 return true;
152 return false;
154 #endif
156 static bool vtxProcessPower(vtxDevice_t *vtxDevice)
158 uint8_t vtxPower;
159 if (vtxCommonGetPowerIndex(vtxDevice, &vtxPower)) {
160 const vtxSettingsConfig_t settings = vtxGetSettings();
161 if (vtxPower != settings.power) {
162 vtxCommonSetPowerByIndex(vtxDevice, settings.power);
163 return true;
166 return false;
169 static bool vtxProcessPitMode(vtxDevice_t *vtxDevice)
171 static bool prevPmSwitchState = false;
173 uint8_t pitOnOff;
174 if (!ARMING_FLAG(ARMED) && vtxCommonGetPitMode(vtxDevice, &pitOnOff)) {
175 bool currPmSwitchState = IS_RC_MODE_ACTIVE(BOXVTXPITMODE);
177 if (currPmSwitchState != prevPmSwitchState) {
178 prevPmSwitchState = currPmSwitchState;
180 if (currPmSwitchState) {
181 #if defined(VTX_SETTINGS_FREQCMD)
182 if (vtxSettingsConfig()->pitModeFreq) {
183 return false;
185 #endif
186 if (!pitOnOff) {
187 vtxCommonSetPitMode(vtxDevice, true);
189 return true;
191 } else {
192 if (pitOnOff) {
193 vtxCommonSetPitMode(vtxDevice, false);
195 return true;
201 return false;
204 static bool vtxProcessStateUpdate(vtxDevice_t *vtxDevice)
206 const vtxSettingsConfig_t vtxSettingsState = vtxGetSettings();
207 vtxSettingsConfig_t vtxState = vtxSettingsState;
209 if (vtxSettingsState.band) {
210 vtxCommonGetBandAndChannel(vtxDevice, &vtxState.band, &vtxState.channel);
211 #if defined(VTX_SETTINGS_FREQCMD)
212 } else {
213 vtxCommonGetFrequency(vtxDevice, &vtxState.freq);
214 #endif
217 vtxCommonGetPowerIndex(vtxDevice, &vtxState.power);
219 return (bool)memcmp(&vtxSettingsState, &vtxState, sizeof(vtxSettingsConfig_t));
222 void vtxUpdate(timeUs_t currentTimeUs)
224 static uint8_t currentSchedule = 0;
226 if (cliMode) {
227 return;
230 vtxDevice_t *vtxDevice = vtxCommonDevice();
231 if (vtxDevice) {
232 // Check input sources for config updates
233 vtxControlInputPoll();
235 const uint8_t startingSchedule = currentSchedule;
236 bool vtxUpdatePending = false;
237 do {
238 switch (currentSchedule) {
239 case VTX_PARAM_POWER:
240 vtxUpdatePending = vtxProcessPower(vtxDevice);
241 break;
242 case VTX_PARAM_BANDCHAN:
243 if (vtxGetSettings().band) {
244 vtxUpdatePending = vtxProcessBandAndChannel(vtxDevice);
245 #if defined(VTX_SETTINGS_FREQCMD)
246 } else {
247 vtxUpdatePending = vtxProcessFrequency(vtxDevice);
248 #endif
250 break;
251 case VTX_PARAM_PITMODE:
252 vtxUpdatePending = vtxProcessPitMode(vtxDevice);
253 break;
254 case VTX_PARAM_CONFIRM:
255 vtxUpdatePending = vtxProcessStateUpdate(vtxDevice);
256 break;
257 default:
258 break;
260 currentSchedule = (currentSchedule + 1) % VTX_PARAM_COUNT;
261 } while (!vtxUpdatePending && currentSchedule != startingSchedule);
263 if (!ARMING_FLAG(ARMED) || vtxUpdatePending) {
264 vtxCommonProcess(vtxDevice, currentTimeUs);
269 #endif