Update cloud build defines (#14080)
[betaflight.git] / src / main / cms / cms_menu_vtx_msp.c
blob6f1cd42b63cd93379c6949f44fbc16f53fd05a56
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>
26 #include "platform.h"
28 #if defined(USE_CMS) && defined(USE_VTX_MSP)
30 #include "common/printf.h"
31 #include "common/utils.h"
33 #include "cms/cms.h"
34 #include "cms/cms_types.h"
36 #include "drivers/vtx_common.h"
37 #include "drivers/vtx_table.h"
39 #include "config/config.h"
41 #include "io/vtx_msp.h"
42 #include "io/vtx.h"
44 char mspCmsStatusString[31] = "- -- ---- ----";
45 // m bc ffff tppp
46 // 01234567890123
48 void mspCmsUpdateStatusString(void)
50 vtxDevice_t *vtxDevice = vtxCommonDevice();
52 if (vtxTableBandCount == 0 || vtxTablePowerLevels == 0) {
53 strncpy(mspCmsStatusString, "PLEASE CONFIGURE VTXTABLE", sizeof(mspCmsStatusString));
54 return;
57 mspCmsStatusString[0] = '*';
58 mspCmsStatusString[1] = ' ';
59 uint8_t band;
60 uint8_t chan;
61 if (!vtxCommonGetBandAndChannel(vtxDevice, &band, &chan) || (band == 0 && chan == 0)) {
62 mspCmsStatusString[2] = 'U';//user freq
63 mspCmsStatusString[3] = 'F';
64 } else {
65 mspCmsStatusString[2] = vtxCommonLookupBandLetter(vtxDevice, band);
66 mspCmsStatusString[3] = vtxCommonLookupChannelName(vtxDevice, chan)[0];
68 mspCmsStatusString[4] = ' ';
70 uint16_t freq;
71 if (!vtxCommonGetFrequency(vtxDevice, &freq) || (freq == 0)) {
72 tfp_sprintf(&mspCmsStatusString[5], "----");
73 } else {
74 tfp_sprintf(&mspCmsStatusString[5], "%4d", freq);
77 uint8_t powerIndex;
78 uint16_t powerValue;
79 if (vtxCommonGetPowerIndex(vtxDevice, &powerIndex) && vtxCommonLookupPowerValue(vtxDevice, powerIndex, &powerValue)) {
80 tfp_sprintf(&mspCmsStatusString[9], " *%3d", powerValue);
81 } else {
82 tfp_sprintf(&mspCmsStatusString[9], " ----");
86 uint8_t mspCmsPitMode = 0;
87 uint8_t mspCmsBand = 1;
88 uint8_t mspCmsChan = 1;
89 uint16_t mspCmsFreqRef;
91 static OSD_TAB_t mspCmsEntBand;
92 static OSD_TAB_t mspCmsEntChan;
94 static OSD_UINT16_t mspCmsEntFreqRef = { &mspCmsFreqRef, 5600, 5900, 0 };
96 static uint8_t mspCmsPower = 1;
98 static OSD_TAB_t mspCmsEntPower;
100 static void mspCmsUpdateFreqRef(void)
102 if (mspCmsBand > 0 && mspCmsChan > 0) {
103 mspCmsFreqRef = vtxCommonLookupFrequency(vtxCommonDevice(), mspCmsBand, mspCmsChan);
107 static const void *mspCmsConfigBand(displayPort_t *pDisp, const void *self)
109 UNUSED(pDisp);
110 UNUSED(self);
112 if (mspCmsBand == 0) {
113 // Bounce back
114 mspCmsBand = 1;
115 } else {
116 mspCmsUpdateFreqRef();
119 return NULL;
122 static const void *mspCmsConfigChan(displayPort_t *pDisp, const void *self)
124 UNUSED(pDisp);
125 UNUSED(self);
127 if (mspCmsChan == 0) {
128 // Bounce back
129 mspCmsChan = 1;
130 } else {
131 mspCmsUpdateFreqRef();
134 return NULL;
137 static const void *mspCmsConfigPower(displayPort_t *pDisp, const void *self)
139 UNUSED(pDisp);
140 UNUSED(self);
142 if (mspCmsPower == 0) {
143 // Bounce back
144 mspCmsPower = 1;
147 return NULL;
150 #define MSP_PIT_STATUS_NA (0)
151 #define MSP_PIT_STATUS_OFF (1)
152 #define MSP_PIT_STATUS_ON (2)
154 static const char * const mspCmsPitModeNames[] = {
155 "---", "OFF", "ON "
158 static OSD_TAB_t mspCmsEntPitMode = { &mspCmsPitMode, 2, mspCmsPitModeNames };
160 static const void *mspCmsSetPitMode(displayPort_t *pDisp, const void *self)
162 UNUSED(pDisp);
163 UNUSED(self);
165 if (mspCmsPitMode == MSP_PIT_STATUS_NA) {
166 // Bouce back
167 mspCmsPitMode = MSP_PIT_STATUS_OFF;
168 } else {
169 vtxCommonSetPitMode(vtxCommonDevice(), (mspCmsPitMode == MSP_PIT_STATUS_OFF) ? 0 : 1);
172 return NULL;
175 static const void *mspCmsCommence(displayPort_t *pDisp, const void *self)
177 UNUSED(pDisp);
178 UNUSED(self);
180 vtxDevice_t *device = vtxCommonDevice();
181 vtxCommonSetBandAndChannel(device, mspCmsBand, mspCmsChan);
182 vtxCommonSetPowerByIndex(device, mspCmsPower);
184 // update'vtx_' settings
185 vtxSettingsConfigMutable()->band = mspCmsBand;
186 vtxSettingsConfigMutable()->channel = mspCmsChan;
187 vtxSettingsConfigMutable()->power = mspCmsPower;
188 vtxSettingsConfigMutable()->freq = vtxCommonLookupFrequency(vtxCommonDevice(), mspCmsBand, mspCmsChan);
190 saveConfigAndNotify();
192 return MENU_CHAIN_BACK;
195 static bool mspCmsInitSettings(void)
197 vtxDevice_t *device = vtxCommonDevice();
198 unsigned vtxStatus;
200 if (!device) {
201 return false;
204 vtxCommonGetBandAndChannel(device, &mspCmsBand, &mspCmsChan);
206 mspCmsUpdateFreqRef();
207 if (vtxCommonGetStatus(device, &vtxStatus)) {
208 mspCmsPitMode = (vtxStatus & VTX_STATUS_PIT_MODE) ? MSP_PIT_STATUS_ON : MSP_PIT_STATUS_OFF;
209 } else {
210 mspCmsPitMode = MSP_PIT_STATUS_NA;
213 if (!vtxCommonGetPowerIndex(vtxCommonDevice(), &mspCmsPower)) {
214 mspCmsPower = 1;
217 mspCmsEntBand.val = &mspCmsBand;
218 mspCmsEntBand.max = vtxTableBandCount;
219 mspCmsEntBand.names = vtxTableBandNames;
221 mspCmsEntChan.val = &mspCmsChan;
222 mspCmsEntChan.max = vtxTableChannelCount;
223 mspCmsEntChan.names = vtxTableChannelNames;
225 mspCmsEntPower.val = &mspCmsPower;
226 mspCmsEntPower.max = vtxTablePowerLevels;
227 mspCmsEntPower.names = vtxTablePowerLabels;
229 return true;
232 static const void *mspCmsOnEnter(displayPort_t *pDisp)
234 UNUSED(pDisp);
236 if (!mspCmsInitSettings()) {
237 return MENU_CHAIN_BACK;
240 return NULL;
243 static const OSD_Entry mspCmsMenuCommenceEntries[] = {
244 { "CONFIRM", OME_Label, NULL, NULL },
245 { "YES", OME_Funcall, mspCmsCommence, NULL },
246 { "NO", OME_Back, NULL, NULL },
247 { NULL, OME_END, NULL, NULL }
250 static CMS_Menu mspCmsMenuCommence = {
251 #ifdef CMS_MENU_DEBUG
252 .GUARD_text = "XVTXMSPC",
253 .GUARD_type = OME_MENU,
254 #endif
255 .onEnter = NULL,
256 .onExit = NULL,
257 .onDisplayUpdate = NULL,
258 .entries = mspCmsMenuCommenceEntries,
261 static const OSD_Entry mspMenuEntries[] =
263 { "- MSP -", OME_Label, NULL, NULL },
264 { "", OME_Label | DYNAMIC, NULL, mspCmsStatusString },
265 { "PIT", OME_TAB, mspCmsSetPitMode, &mspCmsEntPitMode },
266 { "BAND", OME_TAB, mspCmsConfigBand, &mspCmsEntBand },
267 { "CHAN", OME_TAB, mspCmsConfigChan, &mspCmsEntChan },
268 { "(FREQ)", OME_UINT16 | DYNAMIC, NULL, &mspCmsEntFreqRef },
269 { "POWER", OME_TAB, mspCmsConfigPower, &mspCmsEntPower },
270 { "SAVE", OME_Submenu, cmsMenuChange, &mspCmsMenuCommence },
271 { "BACK", OME_Back, NULL, NULL },
272 { NULL, OME_END, NULL, NULL }
275 CMS_Menu cmsx_menuVtxMsp = {
276 #ifdef CMS_MENU_DEBUG
277 .GUARD_text = "XVTXMSP",
278 .GUARD_type = OME_MENU,
279 #endif
280 .onEnter = mspCmsOnEnter,
281 .onExit = NULL,
282 .onDisplayUpdate = NULL,
283 .entries = mspMenuEntries,
285 #endif