Merge branch 'master' into change-to-sending-expresslrs_RFrates_e-in-sync-packet
[ExpressLRS.git] / src / lib / LUA / tx_devLUA.cpp
blob321d1b61eab132df7c7ebbb4e49b3e89ef645094
1 #ifdef TARGET_TX
3 #include "rxtx_devLua.h"
4 #include "CRSF.h"
5 #include "CRSFHandset.h"
6 #include "OTA.h"
7 #include "FHSS.h"
8 #include "helpers.h"
10 #define STR_LUA_ALLAUX "AUX1;AUX2;AUX3;AUX4;AUX5;AUX6;AUX7;AUX8;AUX9;AUX10"
12 #define STR_LUA_ALLAUX_UPDOWN "AUX1" LUASYM_ARROW_UP ";AUX1" LUASYM_ARROW_DN ";AUX2" LUASYM_ARROW_UP ";AUX2" LUASYM_ARROW_DN \
13 ";AUX3" LUASYM_ARROW_UP ";AUX3" LUASYM_ARROW_DN ";AUX4" LUASYM_ARROW_UP ";AUX4" LUASYM_ARROW_DN \
14 ";AUX5" LUASYM_ARROW_UP ";AUX5" LUASYM_ARROW_DN ";AUX6" LUASYM_ARROW_UP ";AUX6" LUASYM_ARROW_DN \
15 ";AUX7" LUASYM_ARROW_UP ";AUX7" LUASYM_ARROW_DN ";AUX8" LUASYM_ARROW_UP ";AUX8" LUASYM_ARROW_DN \
16 ";AUX9" LUASYM_ARROW_UP ";AUX9" LUASYM_ARROW_DN ";AUX10" LUASYM_ARROW_UP ";AUX10" LUASYM_ARROW_DN
18 #if defined(RADIO_SX127X)
19 #define STR_LUA_PACKETRATES \
20 "D50Hz(-112dBm);25Hz(-123dBm);50Hz(-120dBm);100Hz(-117dBm);100Hz Full(-112dBm);200Hz(-112dBm)"
21 #elif defined(RADIO_LR1121)
22 #define STR_LUA_PACKETRATES \
23 "X100F;X150;" \
24 "50 2.4G;100F 2.4G;150 2.4G;250 2.4G;333F 2.4G;500 2.4G;DK250 2.4G;DK500 2.4G;K1000 2.4G;" \
25 "D50 Low;25 Low;50 Low;100 Low;100F Low;200 Low;200F Low;250 Low;K1000F Low"
26 #elif defined(RADIO_SX128X)
27 #define STR_LUA_PACKETRATES \
28 "50Hz(-115dBm);100Hz Full(-112dBm);150Hz(-112dBm);250Hz(-108dBm);333Hz Full(-105dBm);500Hz(-105dBm);" \
29 "D250(-104dBm);D500(-104dBm);F500(-104dBm);F1000(-104dBm)"
30 #else
31 #error Invalid radio configuration!
32 #endif
34 extern char backpackVersion[];
36 static char version_domain[20+1+6+1];
37 char pwrFolderDynamicName[] = "TX Power (1000 Dynamic)";
38 char vtxFolderDynamicName[] = "VTX Admin (OFF:C:1 Aux11 )";
39 static char modelMatchUnit[] = " (ID: 00)";
40 static char tlmBandwidth[] = " (xxxxxbps)";
41 static const char folderNameSeparator[2] = {' ',':'};
42 static const char tlmRatios[] = "Std;Off;1:128;1:64;1:32;1:16;1:8;1:4;1:2;Race";
43 static const char tlmRatiosMav[] = ";;;;;;;;1:2;";
44 static const char switchmodeOpts4ch[] = "Wide;Hybrid";
45 static const char switchmodeOpts4chMav[] = ";Hybrid";
46 static const char switchmodeOpts8ch[] = "8ch;16ch Rate/2;12ch Mixed";
47 static const char switchmodeOpts8chMav[] = ";16ch Rate/2;";
48 static const char antennamodeOpts[] = "Gemini;Ant 1;Ant 2;Switch";
49 static const char linkModeOpts[] = "Normal;MAVLink";
50 static const char luastrDvrAux[] = "Off;" STR_LUA_ALLAUX_UPDOWN;
51 static const char luastrDvrDelay[] = "0s;5s;15s;30s;45s;1min;2min";
52 static const char luastrHeadTrackingEnable[] = "Off;On;" STR_LUA_ALLAUX_UPDOWN;
53 static const char luastrHeadTrackingStart[] = STR_LUA_ALLAUX;
54 static const char luastrOffOn[] = "Off;On";
55 static char luastrPacketRates[] = STR_LUA_PACKETRATES;
57 #define HAS_RADIO (GPIO_PIN_SCK != UNDEF_PIN)
59 static struct luaItem_selection luaAirRate = {
60 {"Packet Rate", CRSF_TEXT_SELECTION},
61 0, // value
62 luastrPacketRates,
63 STR_EMPTYSPACE
66 static struct luaItem_selection luaTlmRate = {
67 {"Telem Ratio", CRSF_TEXT_SELECTION},
68 0, // value
69 tlmRatios,
70 tlmBandwidth
73 //----------------------------POWER------------------
74 static struct luaItem_folder luaPowerFolder = {
75 {"TX Power", CRSF_FOLDER},pwrFolderDynamicName
78 static struct luaItem_selection luaPower = {
79 {"Max Power", CRSF_TEXT_SELECTION},
80 0, // value
81 strPowerLevels,
82 "mW"
85 static struct luaItem_selection luaDynamicPower = {
86 {"Dynamic", CRSF_TEXT_SELECTION},
87 0, // value
88 "Off;Dyn;AUX9;AUX10;AUX11;AUX12",
89 STR_EMPTYSPACE
92 #if defined(GPIO_PIN_FAN_EN) || defined(GPIO_PIN_FAN_PWM)
93 static struct luaItem_selection luaFanThreshold = {
94 {"Fan Thresh", CRSF_TEXT_SELECTION},
95 0, // value
96 "10mW;25mW;50mW;100mW;250mW;500mW;1000mW;2000mW;Never",
97 STR_EMPTYSPACE // units embedded so it won't display "NevermW"
99 #endif
101 #if defined(Regulatory_Domain_EU_CE_2400)
102 static struct luaItem_string luaCELimit = {
103 {"100mW CE LIMIT", CRSF_INFO},
104 STR_EMPTYSPACE
106 #endif
108 //----------------------------POWER------------------
110 static struct luaItem_selection luaSwitch = {
111 {"Switch Mode", CRSF_TEXT_SELECTION},
112 0, // value
113 switchmodeOpts4ch,
114 STR_EMPTYSPACE
117 #if defined(GPIO_PIN_NSS_2)
118 static struct luaItem_selection luaAntenna = {
119 {"Antenna Mode", CRSF_TEXT_SELECTION},
120 0, // value
121 antennamodeOpts,
122 STR_EMPTYSPACE
124 #endif
126 static struct luaItem_selection luaLinkMode = {
127 {"Link Mode", CRSF_TEXT_SELECTION},
128 0, // value
129 linkModeOpts,
130 STR_EMPTYSPACE
133 static struct luaItem_selection luaModelMatch = {
134 {"Model Match", CRSF_TEXT_SELECTION},
135 0, // value
136 luastrOffOn,
137 modelMatchUnit
140 static struct luaItem_command luaBind = {
141 {"Bind", CRSF_COMMAND},
142 lcsIdle, // step
143 STR_EMPTYSPACE
146 static struct luaItem_string luaInfo = {
147 {"Bad/Good", (crsf_value_type_e)(CRSF_INFO | CRSF_FIELD_ELRS_HIDDEN)},
148 STR_EMPTYSPACE
151 static struct luaItem_string luaELRSversion = {
152 {version_domain, CRSF_INFO},
153 commit
156 //---------------------------- WiFi -----------------------------
157 static struct luaItem_folder luaWiFiFolder = {
158 {"WiFi Connectivity", CRSF_FOLDER}
161 #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)
162 static struct luaItem_command luaWebUpdate = {
163 {"Enable WiFi", CRSF_COMMAND},
164 lcsIdle, // step
165 STR_EMPTYSPACE
167 #endif
169 static struct luaItem_command luaRxWebUpdate = {
170 {"Enable Rx WiFi", CRSF_COMMAND},
171 lcsIdle, // step
172 STR_EMPTYSPACE
175 static struct luaItem_command luaTxBackpackUpdate = {
176 {"Enable Backpack WiFi", CRSF_COMMAND},
177 lcsIdle, // step
178 STR_EMPTYSPACE
181 static struct luaItem_command luaVRxBackpackUpdate = {
182 {"Enable VRx WiFi", CRSF_COMMAND},
183 lcsIdle, // step
184 STR_EMPTYSPACE
186 //---------------------------- WiFi -----------------------------
188 #if defined(PLATFORM_ESP32)
189 static struct luaItem_command luaBLEJoystick = {
190 {"BLE Joystick", CRSF_COMMAND},
191 lcsIdle, // step
192 STR_EMPTYSPACE
194 #endif
196 //----------------------------VTX ADMINISTRATOR------------------
197 static struct luaItem_folder luaVtxFolder = {
198 {"VTX Administrator", CRSF_FOLDER},vtxFolderDynamicName
201 static struct luaItem_selection luaVtxBand = {
202 {"Band", CRSF_TEXT_SELECTION},
203 0, // value
204 "Off;A;B;E;F;R;L",
205 STR_EMPTYSPACE
208 static struct luaItem_int8 luaVtxChannel = {
209 {"Channel", CRSF_UINT8},
210 0, // value
211 1, // min
212 8, // max
213 STR_EMPTYSPACE
216 static struct luaItem_selection luaVtxPwr = {
217 {"Pwr Lvl", CRSF_TEXT_SELECTION},
218 0, // value
219 "-;1;2;3;4;5;6;7;8",
220 STR_EMPTYSPACE
223 static struct luaItem_selection luaVtxPit = {
224 {"Pitmode", CRSF_TEXT_SELECTION},
225 0, // value
226 "Off;On;" STR_LUA_ALLAUX_UPDOWN,
227 STR_EMPTYSPACE
230 static struct luaItem_command luaVtxSend = {
231 {"Send VTx", CRSF_COMMAND},
232 lcsIdle, // step
233 STR_EMPTYSPACE
235 //----------------------------VTX ADMINISTRATOR------------------
237 //---------------------------- BACKPACK ------------------
238 static struct luaItem_folder luaBackpackFolder = {
239 {"Backpack", CRSF_FOLDER},
242 #if defined(GPIO_PIN_BACKPACK_EN)
243 static struct luaItem_selection luaBackpackEnable = {
244 {"Backpack", CRSF_TEXT_SELECTION},
245 0, // value
246 luastrOffOn,
247 STR_EMPTYSPACE};
248 #endif
250 static struct luaItem_selection luaDvrAux = {
251 {"DVR Rec", CRSF_TEXT_SELECTION},
252 0, // value
253 luastrDvrAux,
254 STR_EMPTYSPACE};
256 static struct luaItem_selection luaDvrStartDelay = {
257 {"DVR Srt Dly", CRSF_TEXT_SELECTION},
258 0, // value
259 luastrDvrDelay,
260 STR_EMPTYSPACE};
262 static struct luaItem_selection luaDvrStopDelay = {
263 {"DVR Stp Dly", CRSF_TEXT_SELECTION},
264 0, // value
265 luastrDvrDelay,
266 STR_EMPTYSPACE};
268 static struct luaItem_selection luaHeadTrackingEnableChannel = {
269 {"HT Enable", CRSF_TEXT_SELECTION},
270 0, // value
271 luastrHeadTrackingEnable,
272 STR_EMPTYSPACE};
274 static struct luaItem_selection luaHeadTrackingStartChannel = {
275 {"HT Start Channel", CRSF_TEXT_SELECTION},
276 0, // value
277 luastrHeadTrackingStart,
278 STR_EMPTYSPACE};
280 static struct luaItem_selection luaBackpackTelemetry = {
281 {"Telemetry", CRSF_TEXT_SELECTION},
282 0, // value
283 "Off;ESPNOW;WiFi",
284 STR_EMPTYSPACE};
286 static struct luaItem_string luaBackpackVersion = {
287 {"Version", CRSF_INFO},
288 backpackVersion};
290 //---------------------------- BACKPACK ------------------
292 static char luaBadGoodString[10];
293 static int event();
295 extern TxConfig config;
296 extern void VtxTriggerSend();
297 extern void ResetPower();
298 extern uint8_t adjustPacketRateForBaud(uint8_t rate);
299 extern void SetSyncSpam();
300 extern bool RxWiFiReadyToSend;
301 extern bool BackpackTelemReadyToSend;
302 #if defined(USE_TX_BACKPACK)
303 extern bool TxBackpackWiFiReadyToSend;
304 extern bool VRxBackpackWiFiReadyToSend;
305 #endif
306 #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)
307 extern unsigned long rebootTime;
308 extern void setWifiUpdateMode();
309 #endif
311 static void luadevUpdateModelID() {
312 itoa(CRSFHandset::getModelID(), modelMatchUnit+6, 10);
313 strcat(modelMatchUnit, ")");
316 static void luadevUpdateTlmBandwidth()
318 expresslrs_tlm_ratio_e eRatio = (expresslrs_tlm_ratio_e)config.GetTlm();
319 // TLM_RATIO_STD / TLM_RATIO_DISARMED
320 if (eRatio == TLM_RATIO_STD || eRatio == TLM_RATIO_DISARMED)
322 // For Standard ratio, display the ratio instead of bps
323 strcpy(tlmBandwidth, " (1:");
324 uint8_t ratioDiv = TLMratioEnumToValue(ExpressLRS_currAirRate_Modparams->TLMinterval);
325 itoa(ratioDiv, &tlmBandwidth[4], 10);
326 strcat(tlmBandwidth, ")");
329 // TLM_RATIO_NO_TLM
330 else if (eRatio == TLM_RATIO_NO_TLM)
332 tlmBandwidth[0] = '\0';
335 // All normal ratios
336 else
338 tlmBandwidth[0] = ' ';
340 uint16_t hz = 1000000 / ExpressLRS_currAirRate_Modparams->interval;
341 uint8_t ratiodiv = TLMratioEnumToValue(eRatio);
342 uint8_t burst = TLMBurstMaxForRateRatio(hz, ratiodiv);
343 uint8_t bytesPerCall = OtaIsFullRes ? ELRS8_TELEMETRY_BYTES_PER_CALL : ELRS4_TELEMETRY_BYTES_PER_CALL;
344 uint32_t bandwidthValue = bytesPerCall * 8U * burst * hz / ratiodiv / (burst + 1);
345 if (OtaIsFullRes)
347 // Due to fullres also packing telemetry into the LinkStats packet, there is at least
348 // N bytes more data for every rate except 100Hz 1:128, and 2*N bytes more for many
349 // rates. The calculation is a more complex though, so just approximate some of the
350 // extra bandwidth
351 bandwidthValue += 8U * (ELRS8_TELEMETRY_BYTES_PER_CALL - sizeof(OTA_LinkStats_s));
354 itoa(bandwidthValue, &tlmBandwidth[2], 10);
355 strcat(tlmBandwidth, "bps)");
359 static void luadevUpdateBackpackOpts()
361 if (config.GetBackpackDisable())
363 // If backpack is disabled, set all the Backpack select options to "Disabled"
364 LUA_FIELD_HIDE(luaDvrAux);
365 LUA_FIELD_HIDE(luaDvrStartDelay);
366 LUA_FIELD_HIDE(luaDvrStopDelay);
367 LUA_FIELD_HIDE(luaHeadTrackingEnableChannel);
368 LUA_FIELD_HIDE(luaHeadTrackingStartChannel);
369 LUA_FIELD_HIDE(luaBackpackTelemetry);
370 LUA_FIELD_HIDE(luaBackpackVersion);
372 else
374 LUA_FIELD_SHOW(luaDvrAux);
375 LUA_FIELD_SHOW(luaDvrStartDelay);
376 LUA_FIELD_SHOW(luaDvrStopDelay);
377 LUA_FIELD_SHOW(luaHeadTrackingEnableChannel);
378 LUA_FIELD_SHOW(luaHeadTrackingStartChannel);
379 LUA_FIELD_SHOW(luaBackpackTelemetry);
380 LUA_FIELD_SHOW(luaBackpackVersion);
384 #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)
385 static void setBleJoystickMode()
387 connectionState = bleJoystick;
390 static void luahandWifiBle(struct luaPropertiesCommon *item, uint8_t arg)
392 struct luaItem_command *cmd = (struct luaItem_command *)item;
393 void (*setTargetState)();
394 connectionState_e targetState;
395 const char *textConfirm;
396 const char *textRunning;
397 if ((void *)item == (void *)&luaWebUpdate)
399 setTargetState = &setWifiUpdateMode;
400 textConfirm = "Enter WiFi Update?";
401 textRunning = "WiFi Running...";
402 targetState = wifiUpdate;
404 else
406 setTargetState = &setBleJoystickMode;
407 textConfirm = "Start BLE Joystick?";
408 textRunning = "Joystick Running...";
409 targetState = bleJoystick;
412 switch ((luaCmdStep_e)arg)
414 case lcsClick:
415 if (connectionState == connected)
417 sendLuaCommandResponse(cmd, lcsAskConfirm, textConfirm);
418 return;
420 // fallthrough (clicking while not connected goes right to exectute)
422 case lcsConfirmed:
423 sendLuaCommandResponse(cmd, lcsExecuting, textRunning);
424 setTargetState();
425 break;
427 case lcsCancel:
428 sendLuaCommandResponse(cmd, lcsIdle, STR_EMPTYSPACE);
429 if (connectionState == targetState)
431 rebootTime = millis() + 400;
433 break;
435 default: // LUACMDSTEP_NONE on load, LUACMDSTEP_EXECUTING (our lua) or LUACMDSTEP_QUERY (Crossfire Config)
436 sendLuaCommandResponse(cmd, cmd->step, cmd->info);
437 break;
440 #endif
442 static void luahandSimpleSendCmd(struct luaPropertiesCommon *item, uint8_t arg)
444 const char *msg = "Sending...";
445 static uint32_t lastLcsPoll;
446 if (arg < lcsCancel)
448 lastLcsPoll = millis();
449 if ((void *)item == (void *)&luaBind)
451 msg = "Binding...";
452 EnterBindingModeSafely();
454 else if ((void *)item == (void *)&luaVtxSend)
456 VtxTriggerSend();
458 else if ((void *)item == (void *)&luaRxWebUpdate)
460 RxWiFiReadyToSend = true;
462 #if defined(USE_TX_BACKPACK)
463 else if ((void *)item == (void *)&luaTxBackpackUpdate && OPT_USE_TX_BACKPACK)
465 TxBackpackWiFiReadyToSend = true;
467 else if ((void *)item == (void *)&luaVRxBackpackUpdate && OPT_USE_TX_BACKPACK)
469 VRxBackpackWiFiReadyToSend = true;
471 #endif
472 sendLuaCommandResponse((struct luaItem_command *)item, lcsExecuting, msg);
473 } /* if doExecute */
474 else if(arg == lcsCancel || ((millis() - lastLcsPoll)> 2000))
476 sendLuaCommandResponse((struct luaItem_command *)item, lcsIdle, STR_EMPTYSPACE);
480 static void updateFolderName_TxPower()
482 uint8_t txPwrDyn = config.GetDynamicPower() ? config.GetBoostChannel() + 1 : 0;
483 uint8_t pwrFolderLabelOffset = 10; // start writing after "TX Power ("
485 // Power Level
486 pwrFolderLabelOffset += findLuaSelectionLabel(&luaPower, &pwrFolderDynamicName[pwrFolderLabelOffset], config.GetPower() - MinPower);
488 // Dynamic Power
489 if (txPwrDyn)
491 pwrFolderDynamicName[pwrFolderLabelOffset++] = folderNameSeparator[0];
492 pwrFolderLabelOffset += findLuaSelectionLabel(&luaDynamicPower, &pwrFolderDynamicName[pwrFolderLabelOffset], txPwrDyn);
495 pwrFolderDynamicName[pwrFolderLabelOffset++] = ')';
496 pwrFolderDynamicName[pwrFolderLabelOffset] = '\0';
499 static void updateFolderName_VtxAdmin()
501 uint8_t vtxBand = config.GetVtxBand();
502 if (vtxBand)
504 luaVtxFolder.dyn_name = vtxFolderDynamicName;
505 uint8_t vtxFolderLabelOffset = 11; // start writing after "VTX Admin ("
507 // Band
508 vtxFolderLabelOffset += findLuaSelectionLabel(&luaVtxBand, &vtxFolderDynamicName[vtxFolderLabelOffset], vtxBand);
509 vtxFolderDynamicName[vtxFolderLabelOffset++] = folderNameSeparator[1];
511 // Channel
512 vtxFolderDynamicName[vtxFolderLabelOffset++] = '1' + config.GetVtxChannel();
514 // VTX Power
515 uint8_t vtxPwr = config.GetVtxPower();
516 //if power is no-change (-), don't show, also hide pitmode
517 if (vtxPwr)
519 vtxFolderDynamicName[vtxFolderLabelOffset++] = folderNameSeparator[1];
520 vtxFolderLabelOffset += findLuaSelectionLabel(&luaVtxPwr, &vtxFolderDynamicName[vtxFolderLabelOffset], vtxPwr);
522 // Pit Mode
523 uint8_t vtxPit = config.GetVtxPitmode();
524 //if pitmode is off, don't show
525 //show pitmode AuxSwitch or show P if not OFF
526 if (vtxPit != 0)
528 if (vtxPit != 1)
530 vtxFolderDynamicName[vtxFolderLabelOffset++] = folderNameSeparator[1];
531 vtxFolderLabelOffset += findLuaSelectionLabel(&luaVtxPit, &vtxFolderDynamicName[vtxFolderLabelOffset], vtxPit);
533 else
535 vtxFolderDynamicName[vtxFolderLabelOffset++] = folderNameSeparator[1];
536 vtxFolderDynamicName[vtxFolderLabelOffset++] = 'P';
540 vtxFolderDynamicName[vtxFolderLabelOffset++] = ')';
541 vtxFolderDynamicName[vtxFolderLabelOffset] = '\0';
543 else
545 //don't show vtx settings if band is OFF
546 luaVtxFolder.dyn_name = NULL;
550 /***
551 * @brief: Update the luaBadGoodString with the current bad/good count
552 * This item is hidden on our Lua and only displayed in other systems that don't poll our status
553 * Called from luaRegisterDevicePingCallback
554 ****/
555 static void luadevUpdateBadGood()
557 itoa(CRSFHandset::BadPktsCountResult, luaBadGoodString, 10);
558 strcat(luaBadGoodString, "/");
559 itoa(CRSFHandset::GoodPktsCountResult, luaBadGoodString + strlen(luaBadGoodString), 10);
562 /***
563 * @brief: Update the dynamic strings used for folder names and labels
564 ***/
565 void luadevUpdateFolderNames()
567 updateFolderName_TxPower();
568 updateFolderName_VtxAdmin();
570 // These aren't folder names, just string labels slapped in the units field generally
571 luadevUpdateTlmBandwidth();
572 luadevUpdateBackpackOpts();
575 static void recalculatePacketRateOptions(int minInterval)
577 const char *allRates = STR_LUA_PACKETRATES;
578 const char *pos = allRates;
579 luastrPacketRates[0] = 0;
580 for (int i=0 ; i < RATE_MAX ; i++)
582 uint8_t rate = i;
583 rate = RATE_MAX - 1 - rate;
584 bool rateAllowed = (get_elrs_airRateConfig(rate)->interval * get_elrs_airRateConfig(rate)->numOfSends) >= minInterval;
586 // Skip unsupported modes for hardware with only a single LR1121 or with a single RF path
587 rateAllowed &= isSupportedRFRate(rate);
589 const char *semi = strchrnul(pos, ';');
590 if (rateAllowed)
592 strncat(luastrPacketRates, pos, semi - pos);
594 pos = semi;
595 if (*semi == ';')
597 strcat(luastrPacketRates, ";");
598 pos = semi+1;
602 // trim off trailing semicolons (assumes luastrPacketRates has at least 1 non-semicolon)
603 for (auto lastPos = strlen(luastrPacketRates)-1; luastrPacketRates[lastPos] == ';'; lastPos--)
605 luastrPacketRates[lastPos] = '\0';
609 uint8_t adjustSwitchModeForAirRate(OtaSwitchMode_e eSwitchMode, uint8_t packetSize)
611 // Only the fullres modes have 3 switch modes, so reset the switch mode if outside the
612 // range for 4ch mode
613 if (packetSize == OTA4_PACKET_SIZE)
615 if (eSwitchMode > smHybridOr16ch)
616 return smWideOr8ch;
619 return eSwitchMode;
622 static void registerLuaParameters()
624 if (HAS_RADIO) {
625 registerLUAParameter(&luaAirRate, [](struct luaPropertiesCommon *item, uint8_t arg) {
626 if (arg < RATE_MAX)
628 uint8_t selectedRate = RATE_MAX - 1 - arg;
629 uint8_t actualRate = adjustPacketRateForBaud(selectedRate);
630 uint8_t newSwitchMode = adjustSwitchModeForAirRate(
631 (OtaSwitchMode_e)config.GetSwitchMode(), get_elrs_airRateConfig(actualRate)->PayloadLength);
632 // If the switch mode is going to change, block the change while connected
633 bool isDisconnected = connectionState == disconnected;
634 // Don't allow the switch mode to change if the TX is in mavlink mode
635 // Wide switchmode is not compatible with mavlink, and the switchmode is
636 // auto configuredwhen entering mavlink mode
637 bool isMavlinkMode = config.GetLinkMode() == TX_MAVLINK_MODE;
638 if (newSwitchMode == OtaSwitchModeCurrent || (isDisconnected && !isMavlinkMode))
640 config.SetRate(actualRate);
641 config.SetSwitchMode(newSwitchMode);
642 if (actualRate != selectedRate)
644 setLuaWarningFlag(LUA_FLAG_ERROR_BAUDRATE, true);
647 else
648 setLuaWarningFlag(LUA_FLAG_ERROR_CONNECTED, true);
651 registerLUAParameter(&luaTlmRate, [](struct luaPropertiesCommon *item, uint8_t arg) {
652 expresslrs_tlm_ratio_e eRatio = (expresslrs_tlm_ratio_e)arg;
653 if (eRatio <= TLM_RATIO_DISARMED)
655 bool isMavlinkMode = config.GetLinkMode() == TX_MAVLINK_MODE;
656 // Don't allow TLM ratio changes if using AIRPORT or Mavlink
657 if (!firmwareOptions.is_airport && !isMavlinkMode)
659 config.SetTlm(eRatio);
663 if (!firmwareOptions.is_airport)
665 registerLUAParameter(&luaSwitch, [](struct luaPropertiesCommon *item, uint8_t arg) {
666 // Only allow changing switch mode when disconnected since we need to guarantee
667 // the pack and unpack functions are matched
668 bool isDisconnected = connectionState == disconnected;
669 // Don't allow the switch mode to change if the TX is in mavlink mode
670 // Wide switchmode is not compatible with mavlink, and the switchmode is
671 // auto configuredwhen entering mavlink mode
672 bool isMavlinkMode = config.GetLinkMode() == TX_MAVLINK_MODE;
673 if (isDisconnected && !isMavlinkMode)
675 config.SetSwitchMode(arg);
676 OtaUpdateSerializers((OtaSwitchMode_e)arg, ExpressLRS_currAirRate_Modparams->PayloadLength);
678 else if (!isMavlinkMode) // No need to display warning as no switch change can be made while in Mavlink mode.
680 setLuaWarningFlag(LUA_FLAG_ERROR_CONNECTED, true);
684 if (isDualRadio())
686 registerLUAParameter(&luaAntenna, [](struct luaPropertiesCommon *item, uint8_t arg) {
687 config.SetAntennaMode(arg);
690 registerLUAParameter(&luaLinkMode, [](struct luaPropertiesCommon *item, uint8_t arg) {
691 // Only allow changing when disconnected since we need to guarantee
692 // the switch pack and unpack functions are matched on the tx and rx.
693 bool isDisconnected = connectionState == disconnected;
694 if (isDisconnected)
696 config.SetLinkMode(arg);
698 else
700 setLuaWarningFlag(LUA_FLAG_ERROR_CONNECTED, true);
703 if (!firmwareOptions.is_airport)
705 registerLUAParameter(&luaModelMatch, [](struct luaPropertiesCommon *item, uint8_t arg) {
706 bool newModelMatch = arg;
707 config.SetModelMatch(newModelMatch);
708 if (connectionState == connected)
710 mspPacket_t msp;
711 msp.reset();
712 msp.makeCommand();
713 msp.function = MSP_SET_RX_CONFIG;
714 msp.addByte(MSP_ELRS_MODEL_ID);
715 msp.addByte(newModelMatch ? CRSFHandset::getModelID() : 0xff);
716 CRSF::AddMspMessage(&msp, CRSF_ADDRESS_CRSF_RECEIVER);
718 luadevUpdateModelID();
722 // POWER folder
723 registerLUAParameter(&luaPowerFolder);
724 luadevGeneratePowerOpts(&luaPower);
725 registerLUAParameter(&luaPower, [](struct luaPropertiesCommon *item, uint8_t arg) {
726 config.SetPower((PowerLevels_e)constrain(arg + POWERMGNT::getMinPower(), POWERMGNT::getMinPower(), POWERMGNT::getMaxPower()));
727 if (!config.IsModified())
729 ResetPower();
731 }, luaPowerFolder.common.id);
732 registerLUAParameter(&luaDynamicPower, [](struct luaPropertiesCommon *item, uint8_t arg) {
733 config.SetDynamicPower(arg > 0);
734 config.SetBoostChannel((arg - 1) > 0 ? arg - 1 : 0);
735 }, luaPowerFolder.common.id);
737 if (GPIO_PIN_FAN_EN != UNDEF_PIN || GPIO_PIN_FAN_PWM != UNDEF_PIN) {
738 registerLUAParameter(&luaFanThreshold, [](struct luaPropertiesCommon *item, uint8_t arg){
739 config.SetPowerFanThreshold(arg);
740 }, luaPowerFolder.common.id);
742 #if defined(Regulatory_Domain_EU_CE_2400)
743 if (HAS_RADIO) {
744 registerLUAParameter(&luaCELimit, NULL, luaPowerFolder.common.id);
746 #endif
747 if ((HAS_RADIO || OPT_USE_TX_BACKPACK) && !firmwareOptions.is_airport) {
748 // VTX folder
749 registerLUAParameter(&luaVtxFolder);
750 registerLUAParameter(&luaVtxBand, [](struct luaPropertiesCommon *item, uint8_t arg) {
751 config.SetVtxBand(arg);
752 }, luaVtxFolder.common.id);
753 registerLUAParameter(&luaVtxChannel, [](struct luaPropertiesCommon *item, uint8_t arg) {
754 config.SetVtxChannel(arg - 1);
755 }, luaVtxFolder.common.id);
756 registerLUAParameter(&luaVtxPwr, [](struct luaPropertiesCommon *item, uint8_t arg) {
757 config.SetVtxPower(arg);
758 }, luaVtxFolder.common.id);
759 registerLUAParameter(&luaVtxPit, [](struct luaPropertiesCommon *item, uint8_t arg) {
760 config.SetVtxPitmode(arg);
761 }, luaVtxFolder.common.id);
762 registerLUAParameter(&luaVtxSend, &luahandSimpleSendCmd, luaVtxFolder.common.id);
765 // WIFI folder
766 #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266)
767 registerLUAParameter(&luaWiFiFolder);
768 registerLUAParameter(&luaWebUpdate, &luahandWifiBle, luaWiFiFolder.common.id);
769 #else
770 if (HAS_RADIO || OPT_USE_TX_BACKPACK) {
771 registerLUAParameter(&luaWiFiFolder);
773 #endif
774 if (HAS_RADIO) {
775 registerLUAParameter(&luaRxWebUpdate, &luahandSimpleSendCmd, luaWiFiFolder.common.id);
777 if (OPT_USE_TX_BACKPACK) {
778 registerLUAParameter(&luaTxBackpackUpdate, &luahandSimpleSendCmd, luaWiFiFolder.common.id);
779 registerLUAParameter(&luaVRxBackpackUpdate, &luahandSimpleSendCmd, luaWiFiFolder.common.id);
780 // Backpack folder
781 registerLUAParameter(&luaBackpackFolder);
782 #if defined(GPIO_PIN_BACKPACK_EN)
783 if (GPIO_PIN_BACKPACK_EN != UNDEF_PIN)
785 registerLUAParameter(
786 &luaBackpackEnable, [](luaPropertiesCommon *item, uint8_t arg) {
787 // option is Off/On (enable) and config storage is On/Off (disable)
788 config.SetBackpackDisable(arg == 0);
789 }, luaBackpackFolder.common.id);
791 #endif
792 registerLUAParameter(
793 &luaDvrAux, [](luaPropertiesCommon *item, uint8_t arg) {
794 if (config.GetBackpackDisable() == false)
795 config.SetDvrAux(arg);
797 luaBackpackFolder.common.id);
798 registerLUAParameter(
799 &luaDvrStartDelay, [](luaPropertiesCommon *item, uint8_t arg) {
800 if (config.GetBackpackDisable() == false)
801 config.SetDvrStartDelay(arg);
803 luaBackpackFolder.common.id);
804 registerLUAParameter(
805 &luaDvrStopDelay, [](luaPropertiesCommon *item, uint8_t arg) {
806 if (config.GetBackpackDisable() == false)
807 config.SetDvrStopDelay(arg);
809 luaBackpackFolder.common.id);
810 registerLUAParameter(
811 &luaHeadTrackingEnableChannel, [](luaPropertiesCommon *item, uint8_t arg) {
812 config.SetPTREnableChannel(arg);
814 luaBackpackFolder.common.id);
815 registerLUAParameter(
816 &luaHeadTrackingStartChannel, [](luaPropertiesCommon *item, uint8_t arg) {
817 config.SetPTRStartChannel(arg);
819 luaBackpackFolder.common.id);
820 registerLUAParameter(
821 &luaBackpackTelemetry, [](luaPropertiesCommon *item, uint8_t arg) {
822 config.SetBackpackTlmMode(arg);
823 BackpackTelemReadyToSend = true;
824 }, luaBackpackFolder.common.id);
826 registerLUAParameter(&luaBackpackVersion, nullptr, luaBackpackFolder.common.id);
830 #if defined(PLATFORM_ESP32)
831 registerLUAParameter(&luaBLEJoystick, &luahandWifiBle);
832 #endif
834 if (HAS_RADIO) {
835 registerLUAParameter(&luaBind, &luahandSimpleSendCmd);
838 registerLUAParameter(&luaInfo);
839 if (strlen(version) < 21) {
840 strlcpy(version_domain, version, 21);
841 strlcat(version_domain, " ", sizeof(version_domain));
842 } else {
843 strlcpy(version_domain, version, 18);
844 strlcat(version_domain, "... ", sizeof(version_domain));
846 strlcat(version_domain, FHSSconfig->domain, sizeof(version_domain));
847 registerLUAParameter(&luaELRSversion);
848 registerLUAParameter(NULL);
851 static int event()
853 if (connectionState > FAILURE_STATES)
855 return DURATION_NEVER;
858 bool isMavlinkMode = config.GetLinkMode() == TX_MAVLINK_MODE;
859 uint8_t currentRate = adjustPacketRateForBaud(config.GetRate());
860 recalculatePacketRateOptions(handset->getMinPacketInterval());
861 setLuaTextSelectionValue(&luaAirRate, RATE_MAX - 1 - currentRate);
863 setLuaTextSelectionValue(&luaTlmRate, config.GetTlm());
864 luaTlmRate.options = isMavlinkMode ? tlmRatiosMav : tlmRatios;
866 setLuaTextSelectionValue(&luaSwitch, config.GetSwitchMode());
867 if (isMavlinkMode)
869 luaSwitch.options = OtaIsFullRes ? switchmodeOpts8chMav : switchmodeOpts4chMav;
871 else
873 luaSwitch.options = OtaIsFullRes ? switchmodeOpts8ch : switchmodeOpts4ch;
876 if (isDualRadio())
878 setLuaTextSelectionValue(&luaAntenna, config.GetAntennaMode());
880 setLuaTextSelectionValue(&luaLinkMode, config.GetLinkMode());
881 luadevUpdateModelID();
882 setLuaTextSelectionValue(&luaModelMatch, (uint8_t)config.GetModelMatch());
883 setLuaTextSelectionValue(&luaPower, config.GetPower() - MinPower);
884 if (GPIO_PIN_FAN_EN != UNDEF_PIN || GPIO_PIN_FAN_PWM != UNDEF_PIN)
886 setLuaTextSelectionValue(&luaFanThreshold, config.GetPowerFanThreshold());
889 uint8_t dynamic = config.GetDynamicPower() ? config.GetBoostChannel() + 1 : 0;
890 setLuaTextSelectionValue(&luaDynamicPower, dynamic);
892 setLuaTextSelectionValue(&luaVtxBand, config.GetVtxBand());
893 setLuaUint8Value(&luaVtxChannel, config.GetVtxChannel() + 1);
894 setLuaTextSelectionValue(&luaVtxPwr, config.GetVtxPower());
895 setLuaTextSelectionValue(&luaVtxPit, config.GetVtxPitmode());
896 if (OPT_USE_TX_BACKPACK)
898 #if defined(GPIO_PIN_BACKPACK_EN)
899 setLuaTextSelectionValue(&luaBackpackEnable, config.GetBackpackDisable() ? 0 : 1);
900 #endif
901 setLuaTextSelectionValue(&luaDvrAux, config.GetBackpackDisable() ? 0 : config.GetDvrAux());
902 setLuaTextSelectionValue(&luaDvrStartDelay, config.GetBackpackDisable() ? 0 : config.GetDvrStartDelay());
903 setLuaTextSelectionValue(&luaDvrStopDelay, config.GetBackpackDisable() ? 0 : config.GetDvrStopDelay());
904 setLuaTextSelectionValue(&luaHeadTrackingEnableChannel, config.GetBackpackDisable() ? 0 : config.GetPTREnableChannel());
905 setLuaTextSelectionValue(&luaHeadTrackingStartChannel, config.GetBackpackDisable() ? 0 : config.GetPTRStartChannel());
906 setLuaTextSelectionValue(&luaBackpackTelemetry, config.GetBackpackDisable() ? 0 : config.GetBackpackTlmMode());
907 setLuaStringValue(&luaBackpackVersion, backpackVersion);
909 luadevUpdateFolderNames();
910 return DURATION_IMMEDIATELY;
913 static int timeout()
915 if (luaHandleUpdateParameter())
917 SetSyncSpam();
919 return DURATION_IMMEDIATELY;
922 static int start()
924 if (connectionState > FAILURE_STATES)
926 return DURATION_NEVER;
928 handset->registerParameterUpdateCallback(luaParamUpdateReq);
929 registerLuaParameters();
931 setLuaStringValue(&luaInfo, luaBadGoodString);
932 luaRegisterDevicePingCallback(&luadevUpdateBadGood);
934 event();
935 return DURATION_IMMEDIATELY;
938 device_t LUA_device = {
939 .initialize = NULL,
940 .start = start,
941 .event = event,
942 .timeout = timeout
945 #endif