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/>.
25 #if defined(USE_VTX_CONTROL)
27 #include "common/maths.h"
28 #include "common/time.h"
29 #include "common/utils.h"
31 #include "config/parameter_group.h"
32 #include "config/parameter_group_ids.h"
34 #include "drivers/vtx_common.h"
37 #include "fc/config.h"
38 #include "fc/rc_modes.h"
39 #include "fc/runtime_config.h"
40 #include "fc/settings.h"
42 #include "flight/failsafe.h"
45 #include "io/vtx_string.h"
46 #include "io/vtx_control.h"
48 PG_REGISTER_WITH_RESET_TEMPLATE(vtxSettingsConfig_t
, vtxSettingsConfig
, PG_VTX_SETTINGS_CONFIG
, 2);
50 PG_RESET_TEMPLATE(vtxSettingsConfig_t
, vtxSettingsConfig
,
51 .band
= SETTING_VTX_BAND_DEFAULT
,
52 .channel
= SETTING_VTX_CHANNEL_DEFAULT
,
53 .power
= SETTING_VTX_POWER_DEFAULT
,
54 .pitModeChan
= SETTING_VTX_PIT_MODE_CHAN_DEFAULT
,
55 .lowPowerDisarm
= SETTING_VTX_LOW_POWER_DISARM_DEFAULT
,
56 .maxPowerOverride
= SETTING_VTX_MAX_POWER_OVERRIDE_DEFAULT
,
57 .frequencyGroup
= SETTING_VTX_FREQUENCY_GROUP_DEFAULT
,
65 } vtxScheduleParams_e
;
71 static vtxSettingsConfig_t
* vtxGetRuntimeSettings(void)
73 static vtxSettingsConfig_t settings
;
75 settings
.band
= vtxSettingsConfig()->band
;
76 settings
.channel
= vtxSettingsConfig()->channel
;
77 settings
.power
= vtxSettingsConfig()->power
;
78 settings
.pitModeChan
= vtxSettingsConfig()->pitModeChan
;
79 settings
.lowPowerDisarm
= vtxSettingsConfig()->lowPowerDisarm
;
81 if (!ARMING_FLAG(ARMED
) && !failsafeIsActive() &&
82 ((settings
.lowPowerDisarm
== VTX_LOW_POWER_DISARM_ALWAYS
) ||
83 (settings
.lowPowerDisarm
== VTX_LOW_POWER_DISARM_UNTIL_FIRST_ARM
&& !ARMING_FLAG(WAS_EVER_ARMED
)))) {
85 settings
.power
= VTX_SETTINGS_DEFAULT_POWER
;
91 static bool vtxProcessBandAndChannel(vtxDevice_t
*vtxDevice
, const vtxSettingsConfig_t
* runtimeSettings
)
96 // Shortcut for undefined band
97 if (!runtimeSettings
->band
) {
101 if (!vtxCommonGetBandAndChannel(vtxDevice
, &vtxBand
, &vtxChan
)) {
105 if (vtxBand
!= runtimeSettings
->band
|| vtxChan
!= runtimeSettings
->channel
) {
106 vtxCommonSetBandAndChannel(vtxDevice
, runtimeSettings
->band
, runtimeSettings
->channel
);
113 static bool vtxProcessPower(vtxDevice_t
*vtxDevice
, const vtxSettingsConfig_t
* runtimeSettings
)
117 if (!vtxCommonGetPowerIndex(vtxDevice
, &vtxPower
)) {
121 if (vtxPower
!= runtimeSettings
->power
) {
122 vtxCommonSetPowerByIndex(vtxDevice
, runtimeSettings
->power
);
129 static bool vtxProcessPitMode(vtxDevice_t
*vtxDevice
, const vtxSettingsConfig_t
* runtimeSettings
)
131 UNUSED(runtimeSettings
);
135 bool currPmSwitchState
= false;
136 static bool prevPmSwitchState
= false;
138 if (!vtxCommonGetPitMode(vtxDevice
, &pitOnOff
)) {
142 if (currPmSwitchState
!= prevPmSwitchState
) {
143 prevPmSwitchState
= currPmSwitchState
;
145 if (currPmSwitchState
) {
148 vtxCommonSetPitMode(vtxDevice
, true);
154 vtxCommonSetPitMode(vtxDevice
, false);
163 void vtxUpdate(timeUs_t currentTimeUs
)
165 static uint8_t currentSchedule
= 0;
171 vtxDevice_t
*vtxDevice
= vtxCommonDevice();
173 // Check input sources for config updates
174 vtxControlInputPoll();
176 // Build runtime settings
177 const vtxSettingsConfig_t
* runtimeSettings
= vtxGetRuntimeSettings();
179 switch (currentSchedule
) {
180 case VTX_PARAM_POWER
:
181 vtxProcessPower(vtxDevice
, runtimeSettings
);
183 case VTX_PARAM_BANDCHAN
:
184 vtxProcessBandAndChannel(vtxDevice
, runtimeSettings
);
186 case VTX_PARAM_PITMODE
:
187 vtxProcessPitMode(vtxDevice
, runtimeSettings
);
193 vtxCommonProcess(vtxDevice
, currentTimeUs
);
195 currentSchedule
= (currentSchedule
+ 1) % VTX_PARAM_COUNT
;