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/>.
28 #if defined(USE_CMS) && defined(USE_VTX_MSP)
30 #include "common/printf.h"
31 #include "common/utils.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"
44 char mspCmsStatusString
[31] = "- -- ---- ----";
48 void mspCmsUpdateStatusString(void)
50 vtxDevice_t
*vtxDevice
= vtxCommonDevice();
52 if (vtxTableBandCount
== 0 || vtxTablePowerLevels
== 0) {
53 strncpy(mspCmsStatusString
, "PLEASE CONFIGURE VTXTABLE", sizeof(mspCmsStatusString
));
57 mspCmsStatusString
[0] = '*';
58 mspCmsStatusString
[1] = ' ';
61 if (!vtxCommonGetBandAndChannel(vtxDevice
, &band
, &chan
) || (band
== 0 && chan
== 0)) {
62 mspCmsStatusString
[2] = 'U';//user freq
63 mspCmsStatusString
[3] = 'F';
65 mspCmsStatusString
[2] = vtxCommonLookupBandLetter(vtxDevice
, band
);
66 mspCmsStatusString
[3] = vtxCommonLookupChannelName(vtxDevice
, chan
)[0];
68 mspCmsStatusString
[4] = ' ';
71 if (!vtxCommonGetFrequency(vtxDevice
, &freq
) || (freq
== 0)) {
72 tfp_sprintf(&mspCmsStatusString
[5], "----");
74 tfp_sprintf(&mspCmsStatusString
[5], "%4d", freq
);
79 if (vtxCommonGetPowerIndex(vtxDevice
, &powerIndex
) && vtxCommonLookupPowerValue(vtxDevice
, powerIndex
, &powerValue
)) {
80 tfp_sprintf(&mspCmsStatusString
[9], " *%3d", powerValue
);
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
)
112 if (mspCmsBand
== 0) {
116 mspCmsUpdateFreqRef();
122 static const void *mspCmsConfigChan(displayPort_t
*pDisp
, const void *self
)
127 if (mspCmsChan
== 0) {
131 mspCmsUpdateFreqRef();
137 static const void *mspCmsConfigPower(displayPort_t
*pDisp
, const void *self
)
142 if (mspCmsPower
== 0) {
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
[] = {
158 static OSD_TAB_t mspCmsEntPitMode
= { &mspCmsPitMode
, 2, mspCmsPitModeNames
};
160 static const void *mspCmsSetPitMode(displayPort_t
*pDisp
, const void *self
)
165 if (mspCmsPitMode
== MSP_PIT_STATUS_NA
) {
167 mspCmsPitMode
= MSP_PIT_STATUS_OFF
;
169 vtxCommonSetPitMode(vtxCommonDevice(), (mspCmsPitMode
== MSP_PIT_STATUS_OFF
) ? 0 : 1);
175 static const void *mspCmsCommence(displayPort_t
*pDisp
, const void *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();
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
;
210 mspCmsPitMode
= MSP_PIT_STATUS_NA
;
213 if (!vtxCommonGetPowerIndex(vtxCommonDevice(), &mspCmsPower
)) {
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
;
232 static const void *mspCmsOnEnter(displayPort_t
*pDisp
)
236 if (!mspCmsInitSettings()) {
237 return MENU_CHAIN_BACK
;
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
,
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
,
280 .onEnter
= mspCmsOnEnter
,
282 .onDisplayUpdate
= NULL
,
283 .entries
= mspMenuEntries
,