10 #include "devButton.h"
16 // Delay after disconnect to preserve the VTXSS_CONFIRMED status
17 // Needs to be long enough to reconnect, but short enough to
18 // reset between the user switching equipment
19 #define VTX_DISCONNECT_DEBOUNCE_MS (10 * 1000)
21 extern Stream
*TxBackpack
;
22 static uint8_t pitmodeAuxState
= 0;
23 static bool sendEepromWrite
= true;
25 static enum VtxSendState_e
27 VTXSS_UNKNOWN
, // Status of the remote side is unknown, so we should send immediately if connected
28 VTXSS_MODIFIED
, // Config is editied, should always be sent regardless of connect state
29 VTXSS_SENDING1
, VTXSS_SENDING2
, VTXSS_SENDING3
, VTXSS_SENDINGDONE
, // Send the config 3x
30 VTXSS_CONFIRMED
// Status of remote side is consistent with our config
35 VtxSendState
= VTXSS_MODIFIED
;
36 devicesTriggerEvent();
39 void VtxPitmodeSwitchUpdate()
41 if (config
.GetVtxPitmode() <= PITMODE_ON
)
43 pitmodeAuxState
= config
.GetVtxPitmode();
47 uint8_t auxInverted
= config
.GetVtxPitmode() % 2;
48 uint8_t auxNumber
= (config
.GetVtxPitmode() / 2) + 3;
49 uint8_t newPitmodeAuxState
= CRSF_to_BIT(ChannelData
[auxNumber
]) ^ auxInverted
;
51 if (pitmodeAuxState
!= newPitmodeAuxState
)
53 pitmodeAuxState
= newPitmodeAuxState
;
54 sendEepromWrite
= false;
59 static void eepromWriteToMSPOut()
63 packet
.function
= MSP_EEPROM_WRITE
;
65 CRSF::AddMspMessage(&packet
, CRSF_ADDRESS_FLIGHT_CONTROLLER
);
68 static void VtxConfigToMSPOut()
70 DBGLN("Sending VtxConfig");
71 uint8_t vtxIdx
= (config
.GetVtxBand()-1) * 8 + config
.GetVtxChannel();
76 packet
.function
= MSP_SET_VTX_CONFIG
;
77 packet
.addByte(vtxIdx
); // band/channel or frequency low byte
78 packet
.addByte(0); // frequency high byte, if frequency mode
79 if (config
.GetVtxPower())
81 packet
.addByte(config
.GetVtxPower());
82 packet
.addByte(pitmodeAuxState
);
85 CRSF::AddMspMessage(&packet
, CRSF_ADDRESS_FLIGHT_CONTROLLER
);
87 if (!handset
->IsArmed()) // Do not send while armed. There is no need to change the video frequency while armed. It can also cause VRx modules to flash up their OSD menu e.g. Rapidfire.
89 MSP::sendPacket(&packet
, TxBackpack
); // send to tx-backpack as MSP
93 static void initialize()
95 registerButtonFunction(ACTION_SEND_VTX
, VtxTriggerSend
);
100 if (VtxSendState
== VTXSS_MODIFIED
||
101 (VtxSendState
== VTXSS_UNKNOWN
&& connectionState
== connected
))
103 VtxSendState
= VTXSS_SENDING1
;
107 if (connectionState
== disconnected
)
109 // If the VtxSend has completed, wait before going back to VTXSS_UNKNOWN
110 // to ignore a temporary disconnect after saving EEPROM
111 if (VtxSendState
== VTXSS_CONFIRMED
)
113 VtxSendState
= VTXSS_CONFIRMED
;
114 return VTX_DISCONNECT_DEBOUNCE_MS
;
116 VtxSendState
= VTXSS_UNKNOWN
;
118 else if (VtxSendState
== VTXSS_CONFIRMED
&& connectionState
== connected
)
120 return DURATION_NEVER
;
123 return DURATION_IGNORE
;
128 // 0 = off in the lua Band field
129 if (config
.GetVtxBand() == 0)
131 VtxSendState
= VTXSS_CONFIRMED
;
132 return DURATION_NEVER
;
135 // Can only get here in VTXSS_CONFIRMED state if still disconnected
136 if (VtxSendState
== VTXSS_CONFIRMED
)
138 VtxSendState
= VTXSS_UNKNOWN
;
139 return DURATION_NEVER
;
144 VtxSendState
= (VtxSendState_e
)((int)VtxSendState
+ 1);
145 if (VtxSendState
< VTXSS_SENDINGDONE
)
146 return 500; // repeat send in 500ms
148 if (connectionState
== connected
)
150 // Connected while sending, assume the MSP got to the RX
151 VtxSendState
= VTXSS_CONFIRMED
;
153 eepromWriteToMSPOut();
154 sendEepromWrite
= true;
158 VtxSendState
= VTXSS_UNKNOWN
;
159 // Never received a connection, clear the queue which now
160 // has multiple VTX config packets in it
161 CRSF::ResetMspQueue();
164 return DURATION_NEVER
;
167 device_t VTX_device
= {
168 .initialize
= initialize
,