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)
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/>.
26 #if defined(USE_VTX_COMMON)
30 #include "common/maths.h"
31 #include "common/time.h"
33 #include "drivers/vtx_common.h"
34 #include "drivers/vtx_table.h"
36 #include "config/config.h"
37 #include "fc/rc_modes.h"
38 #include "fc/runtime_config.h"
40 #include "flight/failsafe.h"
42 #include "io/vtx_control.h"
45 #include "pg/pg_ids.h"
49 PG_REGISTER_WITH_RESET_FN(vtxSettingsConfig_t
, vtxSettingsConfig
, PG_VTX_SETTINGS_CONFIG
, 1);
51 void pgResetFn_vtxSettingsConfig(vtxSettingsConfig_t
*vtxSettingsConfig
)
54 vtxSettingsConfig
->band
= 0;
55 vtxSettingsConfig
->channel
= 0;
56 vtxSettingsConfig
->power
= 0;
57 vtxSettingsConfig
->freq
= 0;
59 vtxSettingsConfig
->freq
= VTX_TABLE_DEFAULT_FREQ
;
60 vtxSettingsConfig
->band
= VTX_TABLE_DEFAULT_BAND
;
61 vtxSettingsConfig
->channel
= VTX_TABLE_DEFAULT_CHANNEL
;
62 vtxSettingsConfig
->power
= VTX_TABLE_DEFAULT_POWER
;
64 vtxSettingsConfig
->pitModeFreq
= VTX_TABLE_DEFAULT_PITMODE_FREQ
;
65 vtxSettingsConfig
->lowPowerDisarm
= VTX_LOW_POWER_DISARM_OFF
;
66 vtxSettingsConfig
->softserialAlt
= 0;
75 } vtxScheduleParams_e
;
79 bool settingsUpdated
= false;
81 vtxDevice_t
*vtxDevice
= vtxCommonDevice();
84 // If a device is not registered, we don't have any table to refer.
85 // Don't manipulate settings and just return in this case.
89 // sync frequency in parameter group when band/channel are specified
90 const uint16_t freq
= vtxCommonLookupFrequency(vtxDevice
, vtxSettingsConfig()->band
, vtxSettingsConfig()->channel
);
91 if (vtxSettingsConfig()->band
&& freq
!= vtxSettingsConfig()->freq
) {
92 vtxSettingsConfigMutable()->freq
= freq
;
93 settingsUpdated
= true;
96 #if defined(VTX_SETTINGS_FREQCMD)
97 // constrain pit mode frequency
98 if (vtxSettingsConfig()->pitModeFreq
) {
99 const uint16_t constrainedPitModeFreq
= MAX(vtxSettingsConfig()->pitModeFreq
, VTX_TABLE_MIN_USER_FREQ
);
100 if (constrainedPitModeFreq
!= vtxSettingsConfig()->pitModeFreq
) {
101 vtxSettingsConfigMutable()->pitModeFreq
= constrainedPitModeFreq
;
102 settingsUpdated
= true;
107 if (settingsUpdated
) {
108 saveConfigAndNotify();
112 STATIC_UNIT_TESTED vtxSettingsConfig_t
vtxGetSettings(void)
114 vtxSettingsConfig_t settings
= {
115 .band
= vtxSettingsConfig()->band
,
116 .channel
= vtxSettingsConfig()->channel
,
117 .power
= vtxSettingsConfig()->power
,
118 .freq
= vtxSettingsConfig()->freq
,
119 .pitModeFreq
= vtxSettingsConfig()->pitModeFreq
,
120 .lowPowerDisarm
= vtxSettingsConfig()->lowPowerDisarm
,
123 #if defined(VTX_SETTINGS_FREQCMD)
124 if (IS_RC_MODE_ACTIVE(BOXVTXPITMODE
) && settings
.pitModeFreq
) {
126 settings
.freq
= settings
.pitModeFreq
;
127 settings
.power
= VTX_TABLE_DEFAULT_POWER
;
131 if (!ARMING_FLAG(ARMED
) && !failsafeIsActive() &&
132 (settings
.lowPowerDisarm
== VTX_LOW_POWER_DISARM_ALWAYS
||
133 (settings
.lowPowerDisarm
== VTX_LOW_POWER_DISARM_UNTIL_FIRST_ARM
&& !ARMING_FLAG(WAS_EVER_ARMED
)))) {
134 settings
.power
= VTX_TABLE_DEFAULT_POWER
;
140 static bool vtxProcessBandAndChannel(vtxDevice_t
*vtxDevice
)
142 if (!ARMING_FLAG(ARMED
)) {
145 if (vtxCommonGetBandAndChannel(vtxDevice
, &vtxBand
, &vtxChan
)) {
146 const vtxSettingsConfig_t settings
= vtxGetSettings();
147 if (vtxBand
!= settings
.band
|| vtxChan
!= settings
.channel
) {
148 vtxCommonSetBandAndChannel(vtxDevice
, settings
.band
, settings
.channel
);
156 #if defined(VTX_SETTINGS_FREQCMD)
157 static bool vtxProcessFrequency(vtxDevice_t
*vtxDevice
)
159 if (!ARMING_FLAG(ARMED
)) {
161 if (vtxCommonGetFrequency(vtxDevice
, &vtxFreq
)) {
162 const vtxSettingsConfig_t settings
= vtxGetSettings();
163 if (vtxFreq
!= settings
.freq
) {
164 vtxCommonSetFrequency(vtxDevice
, settings
.freq
);
173 static bool vtxProcessPower(vtxDevice_t
*vtxDevice
)
176 if (vtxCommonGetPowerIndex(vtxDevice
, &vtxPower
)) {
177 const vtxSettingsConfig_t settings
= vtxGetSettings();
178 if (vtxPower
!= settings
.power
) {
179 vtxCommonSetPowerByIndex(vtxDevice
, settings
.power
);
186 static bool vtxProcessPitMode(vtxDevice_t
*vtxDevice
)
188 static bool prevPmSwitchState
= false;
191 if (!ARMING_FLAG(ARMED
) && vtxCommonGetStatus(vtxDevice
, &vtxStatus
)) {
192 bool currPmSwitchState
= IS_RC_MODE_ACTIVE(BOXVTXPITMODE
);
194 if (currPmSwitchState
!= prevPmSwitchState
) {
195 prevPmSwitchState
= currPmSwitchState
;
197 if (currPmSwitchState
) {
198 #if defined(VTX_SETTINGS_FREQCMD)
199 if (vtxSettingsConfig()->pitModeFreq
) {
203 if (!(vtxStatus
& VTX_STATUS_PIT_MODE
)) {
204 vtxCommonSetPitMode(vtxDevice
, true);
209 if (vtxStatus
& VTX_STATUS_PIT_MODE
) {
210 vtxCommonSetPitMode(vtxDevice
, false);
221 static bool vtxProcessStateUpdate(vtxDevice_t
*vtxDevice
)
223 const vtxSettingsConfig_t vtxSettingsState
= vtxGetSettings();
224 vtxSettingsConfig_t vtxState
= vtxSettingsState
;
226 if (vtxSettingsState
.band
) {
227 vtxCommonGetBandAndChannel(vtxDevice
, &vtxState
.band
, &vtxState
.channel
);
228 #if defined(VTX_SETTINGS_FREQCMD)
230 vtxCommonGetFrequency(vtxDevice
, &vtxState
.freq
);
234 vtxCommonGetPowerIndex(vtxDevice
, &vtxState
.power
);
236 return (bool)memcmp(&vtxSettingsState
, &vtxState
, sizeof(vtxSettingsConfig_t
));
239 void vtxUpdate(timeUs_t currentTimeUs
)
241 static uint8_t currentSchedule
= 0;
247 vtxDevice_t
*vtxDevice
= vtxCommonDevice();
249 // Check input sources for config updates
250 vtxControlInputPoll();
252 const uint8_t startingSchedule
= currentSchedule
;
253 bool vtxUpdatePending
= false;
255 switch (currentSchedule
) {
256 case VTX_PARAM_POWER
:
257 vtxUpdatePending
= vtxProcessPower(vtxDevice
);
259 case VTX_PARAM_BANDCHAN
:
260 if (vtxGetSettings().band
) {
261 vtxUpdatePending
= vtxProcessBandAndChannel(vtxDevice
);
262 #if defined(VTX_SETTINGS_FREQCMD)
264 vtxUpdatePending
= vtxProcessFrequency(vtxDevice
);
268 case VTX_PARAM_PITMODE
:
269 vtxUpdatePending
= vtxProcessPitMode(vtxDevice
);
271 case VTX_PARAM_CONFIRM
:
272 vtxUpdatePending
= vtxProcessStateUpdate(vtxDevice
);
277 currentSchedule
= (currentSchedule
+ 1) % VTX_PARAM_COUNT
;
278 } while (!vtxUpdatePending
&& currentSchedule
!= startingSchedule
);
280 if (!ARMING_FLAG(ARMED
) || vtxUpdatePending
) {
281 vtxCommonProcess(vtxDevice
, currentTimeUs
);