makes GPIO_PIN_RST optional for the sx1276
[ExpressLRS.git] / src / lib / VTX / devVTX.cpp
blobceffbf814427041e072798a6f889c76892bdf3bf
1 #include "targets.h"
2 #include "common.h"
3 #include "device.h"
5 #include "config.h"
6 #include "CRSF.h"
7 #include "msp.h"
8 #include "logging.h"
10 extern bool ICACHE_RAM_ATTR IsArmed();
11 extern CRSF crsf;
12 extern MSP msp;
14 static enum VtxSendState_e
16 VTXSS_UNKNOWN, // Status of the remote side is unknown, so we should send immediately if connected
17 VTXSS_MODIFIED, // Config is editied, should always be sent regardless of connect state
18 VTXSS_SENDING1, VTXSS_SENDING2, VTXSS_SENDING3, VTXSS_SENDINGDONE, // Send the config 3x
19 VTXSS_CONFIRMED // Status of remote side is consistent with our config
20 } VtxSendState;
22 void VtxTriggerSend()
24 VtxSendState = VTXSS_MODIFIED;
25 devicesTriggerEvent();
28 static void eepromWriteToMSPOut()
30 mspPacket_t packet;
31 packet.reset();
32 packet.function = MSP_EEPROM_WRITE;
33 packet.addByte(0);
34 packet.addByte(0);
35 packet.addByte(0);
36 packet.addByte(0);
38 crsf.AddMspMessage(&packet);
41 static void VtxConfigToMSPOut()
43 DBGLN("Sending VtxConfig");
44 uint8_t vtxIdx = (config.GetVtxBand()-1) * 8 + config.GetVtxChannel();
46 mspPacket_t packet;
47 packet.reset();
48 packet.makeCommand();
49 packet.function = MSP_SET_VTX_CONFIG;
50 packet.addByte(vtxIdx);
51 packet.addByte(0);
52 packet.addByte(config.GetVtxPower());
53 packet.addByte(config.GetVtxPitmode());
55 crsf.AddMspMessage(&packet);
56 msp.sendPacket(&packet, &Serial); // send to tx-backpack as MSP
59 static int event()
61 if (VtxSendState == VTXSS_MODIFIED ||
62 (VtxSendState == VTXSS_UNKNOWN && connectionState == connected))
64 VtxSendState = VTXSS_SENDING1;
65 return 1000;
68 if (connectionState == disconnected)
69 VtxSendState = VTXSS_UNKNOWN;
71 return DURATION_NEVER;
74 static int timeout()
76 // 0 = off in the lua Band field
77 // Do not send while armed
78 if (config.GetVtxBand() == 0 || IsArmed())
80 VtxSendState = VTXSS_CONFIRMED;
81 return DURATION_NEVER;
84 VtxConfigToMSPOut();
86 VtxSendState = (VtxSendState_e)((int)VtxSendState + 1);
87 if (VtxSendState < VTXSS_SENDINGDONE)
88 return 500; // repeat send in 500ms
90 if (connectionState == connected)
92 // Connected while sending, assume the MSP got to the RX
93 VtxSendState = VTXSS_CONFIRMED;
94 eepromWriteToMSPOut();
96 else
98 VtxSendState = VTXSS_UNKNOWN;
99 // Never received a connection, clear the queue which now
100 // has multiple VTX config packets in it
101 crsf.ResetMspQueue();
104 return DURATION_NEVER;
107 device_t VTX_device = {
108 .initialize = NULL,
109 .start = NULL,
110 .event = event,
111 .timeout = timeout