Fix WS2812 led definition
[inav.git] / src / main / io / vtx_control.c
blob02fc8010d03be42396ec10f8b592e27a294fa9f9
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
19 // Get target build configuration
20 #include "platform.h"
22 #include "common/maths.h"
24 #include "config/config_eeprom.h"
25 #include "config/parameter_group.h"
26 #include "config/parameter_group_ids.h"
28 #include "drivers/vtx_common.h"
29 #include "drivers/light_led.h"
30 #include "drivers/system.h"
32 #include "fc/config.h"
33 #include "fc/runtime_config.h"
34 #include "fc/settings.h"
36 #include "io/beeper.h"
37 #include "io/osd.h"
38 #include "io/vtx_control.h"
41 #if defined(USE_VTX_CONTROL)
43 PG_REGISTER_WITH_RESET_TEMPLATE(vtxConfig_t, vtxConfig, PG_VTX_CONFIG, 4);
45 PG_RESET_TEMPLATE(vtxConfig_t, vtxConfig,
46 .halfDuplex = SETTING_VTX_HALFDUPLEX_DEFAULT,
47 .smartAudioEarlyAkkWorkaroundEnable = SETTING_VTX_SMARTAUDIO_EARLY_AKK_WORKAROUND_DEFAULT,
48 .smartAudioAltSoftSerialMethod = SETTING_VTX_SMARTAUDIO_ALTERNATE_SOFTSERIAL_METHOD_DEFAULT,
49 .softSerialShortStop = SETTING_VTX_SOFTSERIAL_SHORTSTOP_DEFAULT,
50 .smartAudioStopBits = SETTING_VTX_SMARTAUDIO_STOPBITS_DEFAULT,
53 static uint8_t locked = 0;
55 void vtxControlInit(void)
57 // NOTHING TO DO
60 void vtxControlInputPoll(void)
62 // Check variuos input sources for VTX config updates
64 // XXX: None supported in INAV
67 static void vtxUpdateBandAndChannel(uint8_t bandStep, uint8_t channelStep)
69 if (ARMING_FLAG(ARMED)) {
70 locked = 1;
73 if (!locked) {
74 vtxDevice_t *vtxDevice = vtxCommonDevice();
75 if (vtxDevice) {
76 uint8_t band = 0, channel = 0;
77 vtxCommonGetBandAndChannel(vtxDevice, &band, &channel);
78 vtxCommonSetBandAndChannel(vtxDevice, band + bandStep, channel + channelStep);
83 void vtxIncrementBand(void)
85 vtxUpdateBandAndChannel(+1, 0);
88 void vtxDecrementBand(void)
90 vtxUpdateBandAndChannel(-1, 0);
93 void vtxIncrementChannel(void)
95 vtxUpdateBandAndChannel(0, +1);
98 void vtxDecrementChannel(void)
100 vtxUpdateBandAndChannel(0, -1);
103 void vtxUpdateActivatedChannel(void)
105 if (ARMING_FLAG(ARMED)) {
106 locked = 1;
109 if (!locked) {
110 vtxDevice_t *vtxDevice = vtxCommonDevice();
111 if (vtxDevice) {
112 static uint8_t lastIndex = -1;
114 for (uint8_t index = 0; index < MAX_CHANNEL_ACTIVATION_CONDITION_COUNT; index++) {
115 const vtxChannelActivationCondition_t *vtxChannelActivationCondition = &vtxConfig()->vtxChannelActivationConditions[index];
117 if (isRangeActive(vtxChannelActivationCondition->auxChannelIndex, &vtxChannelActivationCondition->range)
118 && index != lastIndex) {
119 lastIndex = index;
121 vtxCommonSetBandAndChannel(vtxDevice, vtxChannelActivationCondition->band, vtxChannelActivationCondition->channel);
122 break;
129 void vtxCycleBandOrChannel(const uint8_t bandStep, const uint8_t channelStep)
131 vtxDevice_t *vtxDevice = vtxCommonDevice();
133 if (!vtxDevice) {
134 return;
137 uint8_t band = 0, channel = 0;
138 vtxDeviceCapability_t capability;
140 bool haveAllNeededInfo = vtxCommonGetBandAndChannel(vtxDevice, &band, &channel) && vtxCommonGetDeviceCapability(vtxDevice, &capability);
141 if (!haveAllNeededInfo) {
142 return;
145 int newChannel = channel + channelStep;
146 if (newChannel > capability.channelCount) {
147 newChannel = 1;
148 } else if (newChannel < 1) {
149 newChannel = capability.channelCount;
152 int newBand = band + bandStep;
153 if (newBand > capability.bandCount) {
154 newBand = 1;
155 } else if (newBand < 1) {
156 newBand = capability.bandCount;
159 vtxCommonSetBandAndChannel(vtxDevice, newBand, newChannel);
162 void vtxCyclePower(const uint8_t powerStep)
164 vtxDevice_t *vtxDevice = vtxCommonDevice();
166 if (!vtxDevice) {
167 return;
170 uint8_t power = 0;
171 vtxDeviceCapability_t capability;
173 bool haveAllNeededInfo = vtxCommonGetPowerIndex(vtxDevice, &power) && vtxCommonGetDeviceCapability(vtxDevice, &capability);
174 if (!haveAllNeededInfo) {
175 return;
178 int newPower = power + powerStep;
179 if (newPower >= capability.powerCount) {
180 newPower = 0;
181 } else if (newPower < 0) {
182 newPower = capability.powerCount;
185 vtxCommonSetPowerByIndex(vtxDevice, newPower);
188 #endif