updates
[inav.git] / src / main / io / vtx.c
blob495554e93ae5cee8433555f9646d9c4b017abb36
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"
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"
36 #include "fc/cli.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"
44 #include "io/vtx.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,
60 typedef enum {
61 VTX_PARAM_POWER = 0,
62 VTX_PARAM_BANDCHAN,
63 VTX_PARAM_PITMODE,
64 VTX_PARAM_COUNT
65 } vtxScheduleParams_e;
67 void vtxInit(void)
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;
88 return &settings;
91 static bool vtxProcessBandAndChannel(vtxDevice_t *vtxDevice, const vtxSettingsConfig_t * runtimeSettings)
93 uint8_t vtxBand;
94 uint8_t vtxChan;
96 // Shortcut for undefined band
97 if (!runtimeSettings->band) {
98 return false;
101 if (!vtxCommonGetBandAndChannel(vtxDevice, &vtxBand, &vtxChan)) {
102 return false;
105 if (vtxBand != runtimeSettings->band || vtxChan != runtimeSettings->channel) {
106 vtxCommonSetBandAndChannel(vtxDevice, runtimeSettings->band, runtimeSettings->channel);
107 return true;
110 return false;
113 static bool vtxProcessPower(vtxDevice_t *vtxDevice, const vtxSettingsConfig_t * runtimeSettings)
115 uint8_t vtxPower;
117 if (!vtxCommonGetPowerIndex(vtxDevice, &vtxPower)) {
118 return false;
121 if (vtxPower != runtimeSettings->power) {
122 vtxCommonSetPowerByIndex(vtxDevice, runtimeSettings->power);
123 return true;
126 return false;
129 static bool vtxProcessPitMode(vtxDevice_t *vtxDevice, const vtxSettingsConfig_t * runtimeSettings)
131 UNUSED(runtimeSettings);
133 uint8_t pitOnOff;
135 bool currPmSwitchState = false;
136 static bool prevPmSwitchState = false;
138 if (!vtxCommonGetPitMode(vtxDevice, &pitOnOff)) {
139 return false;
142 if (currPmSwitchState != prevPmSwitchState) {
143 prevPmSwitchState = currPmSwitchState;
145 if (currPmSwitchState) {
146 if (0) {
147 if (!pitOnOff) {
148 vtxCommonSetPitMode(vtxDevice, true);
149 return true;
152 } else {
153 if (pitOnOff) {
154 vtxCommonSetPitMode(vtxDevice, false);
155 return true;
160 return false;
163 void vtxUpdate(timeUs_t currentTimeUs)
165 static uint8_t currentSchedule = 0;
167 if (cliMode) {
168 return;
171 vtxDevice_t *vtxDevice = vtxCommonDevice();
172 if (vtxDevice) {
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);
182 break;
183 case VTX_PARAM_BANDCHAN:
184 vtxProcessBandAndChannel(vtxDevice, runtimeSettings);
185 break;
186 case VTX_PARAM_PITMODE:
187 vtxProcessPitMode(vtxDevice, runtimeSettings);
188 break;
189 default:
190 break;
193 vtxCommonProcess(vtxDevice, currentTimeUs);
195 currentSchedule = (currentSchedule + 1) % VTX_PARAM_COUNT;
199 #endif