Update cloud build defines (#14080)
[betaflight.git] / src / main / cms / cms_menu_vtx_tramp.c
blobc3a1f3a38580d0228b8aaf377a0b23aa0050f85d
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 <stdbool.h>
22 #include <stdint.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <drivers/vtx_table.h>
27 #include "platform.h"
29 #if defined(USE_CMS) && defined(USE_VTX_TRAMP)
31 #include "common/printf.h"
32 #include "common/utils.h"
34 #include "cms/cms.h"
35 #include "cms/cms_types.h"
37 #include "drivers/vtx_common.h"
39 #include "config/config.h"
41 #include "io/vtx_tramp.h"
42 #include "io/vtx.h"
44 char trampCmsStatusString[31] = "- -- ---- ----";
45 // m bc ffff tppp
46 // 01234567890123
48 static int16_t trampCmsTemp;
49 static OSD_INT16_t trampCmsEntTemp = { &trampCmsTemp, -100, 300, 0 };
51 void trampCmsUpdateStatusString(void)
53 vtxDevice_t *vtxDevice = vtxCommonDevice();
55 if (vtxTableBandCount == 0 || vtxTablePowerLevels == 0) {
56 strncpy(trampCmsStatusString, "PLEASE CONFIGURE VTXTABLE", sizeof(trampCmsStatusString));
57 return;
60 trampCmsStatusString[0] = '*';
61 trampCmsStatusString[1] = ' ';
62 uint8_t band;
63 uint8_t chan;
64 if (!vtxCommonGetBandAndChannel(vtxDevice, &band, &chan) || (band == 0 && chan == 0)) {
65 trampCmsStatusString[2] = 'U';//user freq
66 trampCmsStatusString[3] = 'F';
67 } else {
68 trampCmsStatusString[2] = vtxCommonLookupBandLetter(vtxDevice, band);
69 trampCmsStatusString[3] = vtxCommonLookupChannelName(vtxDevice, chan)[0];
71 trampCmsStatusString[4] = ' ';
73 uint16_t freq;
74 if (!vtxCommonGetFrequency(vtxDevice, &freq) || (freq == 0)) {
75 tfp_sprintf(&trampCmsStatusString[5], "----");
76 } else {
77 tfp_sprintf(&trampCmsStatusString[5], "%4d", freq);
80 uint16_t actualPower = vtxTrampGetCurrentActualPower();
81 uint8_t powerIndex;
82 uint16_t powerValue;
83 if (actualPower > 0 && vtxCommonGetPowerIndex(vtxDevice, &powerIndex) && vtxCommonLookupPowerValue(vtxDevice, powerIndex, &powerValue)) {
84 tfp_sprintf(&trampCmsStatusString[9], " %c%3d", (actualPower == powerValue) ? ' ' : '*', actualPower);
85 } else {
86 tfp_sprintf(&trampCmsStatusString[9], " ----");
89 trampCmsTemp = vtxTrampGetCurrentTemp();
92 uint8_t trampCmsPitMode = 0;
93 uint8_t trampCmsBand = 1;
94 uint8_t trampCmsChan = 1;
95 uint16_t trampCmsFreqRef;
97 static OSD_TAB_t trampCmsEntBand;
98 static OSD_TAB_t trampCmsEntChan;
100 static OSD_UINT16_t trampCmsEntFreqRef = { &trampCmsFreqRef, 5600, 5900, 0 };
102 static uint8_t trampCmsPower = 1;
104 static OSD_TAB_t trampCmsEntPower;
106 static void trampCmsUpdateFreqRef(void)
108 if (trampCmsBand > 0 && trampCmsChan > 0) {
109 trampCmsFreqRef = vtxCommonLookupFrequency(vtxCommonDevice(), trampCmsBand, trampCmsChan);
113 static const void *trampCmsConfigBand(displayPort_t *pDisp, const void *self)
115 UNUSED(pDisp);
116 UNUSED(self);
118 if (trampCmsBand == 0) {
119 // Bounce back
120 trampCmsBand = 1;
121 } else {
122 trampCmsUpdateFreqRef();
125 return NULL;
128 static const void *trampCmsConfigChan(displayPort_t *pDisp, const void *self)
130 UNUSED(pDisp);
131 UNUSED(self);
133 if (trampCmsChan == 0) {
134 // Bounce back
135 trampCmsChan = 1;
136 } else {
137 trampCmsUpdateFreqRef();
140 return NULL;
143 static const void *trampCmsConfigPower(displayPort_t *pDisp, const void *self)
145 UNUSED(pDisp);
146 UNUSED(self);
148 if (trampCmsPower == 0) {
149 // Bounce back
150 trampCmsPower = 1;
153 return NULL;
156 #define TRAMP_PIT_STATUS_NA (0)
157 #define TRAMP_PIT_STATUS_OFF (1)
158 #define TRAMP_PIT_STATUS_ON (2)
160 static const char * const trampCmsPitModeNames[] = {
161 "---", "OFF", "ON "
164 static OSD_TAB_t trampCmsEntPitMode = { &trampCmsPitMode, 2, trampCmsPitModeNames };
166 static const void *trampCmsSetPitMode(displayPort_t *pDisp, const void *self)
168 UNUSED(pDisp);
169 UNUSED(self);
171 if (trampCmsPitMode == TRAMP_PIT_STATUS_NA) {
172 // Bouce back
173 trampCmsPitMode = TRAMP_PIT_STATUS_OFF;
174 } else {
175 vtxCommonSetPitMode(vtxCommonDevice(),
176 (trampCmsPitMode == TRAMP_PIT_STATUS_OFF) ? 0 : 1);
179 return NULL;
182 static const void *trampCmsCommence(displayPort_t *pDisp, const void *self)
184 UNUSED(pDisp);
185 UNUSED(self);
187 vtxDevice_t *device = vtxCommonDevice();
188 vtxCommonSetBandAndChannel(device, trampCmsBand, trampCmsChan);
189 vtxCommonSetPowerByIndex(device, trampCmsPower);
191 // update'vtx_' settings
192 vtxSettingsConfigMutable()->band = trampCmsBand;
193 vtxSettingsConfigMutable()->channel = trampCmsChan;
194 vtxSettingsConfigMutable()->power = trampCmsPower;
195 vtxSettingsConfigMutable()->freq = vtxCommonLookupFrequency(vtxCommonDevice(), trampCmsBand, trampCmsChan);
197 saveConfigAndNotify();
198 cmsMenuExit(pDisp, self);
200 return MENU_CHAIN_BACK;
203 static bool trampCmsInitSettings(void)
205 vtxDevice_t *device = vtxCommonDevice();
206 unsigned vtxStatus;
208 if (!device) {
209 return false;
212 vtxCommonGetBandAndChannel(device, &trampCmsBand, &trampCmsChan);
214 trampCmsUpdateFreqRef();
215 if (vtxCommonGetStatus(device, &vtxStatus)) {
216 trampCmsPitMode = (vtxStatus & VTX_STATUS_PIT_MODE) ? TRAMP_PIT_STATUS_ON : TRAMP_PIT_STATUS_OFF;
217 } else {
218 trampCmsPitMode = TRAMP_PIT_STATUS_NA;
221 if (!vtxCommonGetPowerIndex(vtxCommonDevice(), &trampCmsPower)) {
222 trampCmsPower = 1;
225 trampCmsEntBand.val = &trampCmsBand;
226 trampCmsEntBand.max = vtxTableBandCount;
227 trampCmsEntBand.names = vtxTableBandNames;
229 trampCmsEntChan.val = &trampCmsChan;
230 trampCmsEntChan.max = vtxTableChannelCount;
231 trampCmsEntChan.names = vtxTableChannelNames;
233 trampCmsEntPower.val = &trampCmsPower;
234 trampCmsEntPower.max = vtxTablePowerLevels;
235 trampCmsEntPower.names = vtxTablePowerLabels;
237 return true;
240 static const void *trampCmsOnEnter(displayPort_t *pDisp)
242 UNUSED(pDisp);
244 if (!trampCmsInitSettings()) {
245 return MENU_CHAIN_BACK;
248 return NULL;
251 static const OSD_Entry trampCmsMenuCommenceEntries[] = {
252 { "CONFIRM", OME_Label, NULL, NULL },
253 { "YES", OME_OSD_Exit, trampCmsCommence, (void *)CMS_EXIT },
254 { "NO", OME_Back, NULL, NULL },
255 { NULL, OME_END, NULL, NULL }
258 static CMS_Menu trampCmsMenuCommence = {
259 #ifdef CMS_MENU_DEBUG
260 .GUARD_text = "XVTXTRC",
261 .GUARD_type = OME_MENU,
262 #endif
263 .onEnter = NULL,
264 .onExit = NULL,
265 .onDisplayUpdate = NULL,
266 .entries = trampCmsMenuCommenceEntries,
269 static const OSD_Entry trampMenuEntries[] =
271 { "- TRAMP -", OME_Label, NULL, NULL },
273 { "", OME_Label | DYNAMIC, NULL, trampCmsStatusString },
274 { "PIT", OME_TAB, trampCmsSetPitMode, &trampCmsEntPitMode },
275 { "BAND", OME_TAB, trampCmsConfigBand, &trampCmsEntBand },
276 { "CHAN", OME_TAB, trampCmsConfigChan, &trampCmsEntChan },
277 { "(FREQ)", OME_UINT16 | DYNAMIC, NULL, &trampCmsEntFreqRef },
278 { "POWER", OME_TAB, trampCmsConfigPower, &trampCmsEntPower },
279 { "T(C)", OME_INT16 | DYNAMIC, NULL, &trampCmsEntTemp },
280 { "SAVE&EXIT", OME_Submenu, cmsMenuChange, &trampCmsMenuCommence },
282 { "BACK", OME_Back, NULL, NULL },
283 { NULL, OME_END, NULL, NULL }
286 CMS_Menu cmsx_menuVtxTramp = {
287 #ifdef CMS_MENU_DEBUG
288 .GUARD_text = "XVTXTR",
289 .GUARD_type = OME_MENU,
290 #endif
291 .onEnter = trampCmsOnEnter,
292 .onExit = NULL,
293 .onDisplayUpdate = NULL,
294 .entries = trampMenuEntries,
296 #endif