1 #ifndef H_CRSF_CONTROLLER
2 #define H_CRSF_CONTROLLER
5 #include "crsf_protocol.h"
7 #include "HardwareSerial.h"
12 #include "driver/uart.h"
15 class CRSFHandset final
: public Handset
20 void Begin() override
;
24 bool IsArmed() override
{ return armCmd
; } // AUX1/CH5 (CH5 mode) or extra byte in RC channels message (Switch mode)
25 void handleInput() override
;
26 void handleOutput(int receivedBytes
);
28 static HardwareSerial Port
;
29 static Stream
*PortSecondary
; // A second UART used to mirror telemetry out on the TX, not read from
31 static uint8_t modelId
; // The model ID as received from the Transmitter
32 static bool ForwardDevicePings
; // true if device pings should be forwarded OTA
33 static bool elrsLUAmode
;
35 static uint32_t GoodPktsCountResult
; // need to latch the results
36 static uint32_t BadPktsCountResult
; // need to latch the results
38 static void makeLinkStatisticsPacket(uint8_t *buffer
);
40 static void packetQueueExtended(uint8_t type
, void *data
, uint8_t len
);
42 void setPacketInterval(int32_t PacketInterval
) override
;
43 void JustSentRFpacket() override
;
44 void sendTelemetryToTX(uint8_t *data
) override
;
46 static uint8_t getModelID() { return modelId
; }
48 uint8_t GetMaxPacketBytes() const override
{ return maxPacketBytes
; }
49 static uint32_t GetCurrentBaudRate() { return UARTrequestedBaud
; }
50 static bool isHalfDuplex() { return halfDuplex
; }
51 int getMinPacketInterval() const override
;
54 inBuffer_U inBuffer
= {};
56 /// OpenTX mixer sync ///
57 volatile uint32_t dataLastRecv
= 0;
58 volatile int32_t OpenTXsyncOffset
= 0;
59 volatile int32_t OpenTXsyncWindow
= 0;
60 volatile int32_t OpenTXsyncWindowSize
= 0;
61 uint32_t OpenTXsyncLastSent
= 0;
64 uint8_t SerialInPacketPtr
= 0; // index where we are reading/writing
65 static bool halfDuplex
;
66 bool transmitting
= false;
67 uint32_t GoodPktsCount
= 0;
68 uint32_t BadPktsCount
= 0;
69 uint32_t UARTwdtLastChecked
= 0;
70 uint8_t maxPacketBytes
= CRSF_MAX_PACKET_LEN
;
71 uint8_t maxPeriodBytes
= CRSF_MAX_PACKET_LEN
;
73 static uint8_t UARTcurrentBaudIdx
;
74 static uint32_t UARTrequestedBaud
;
76 bool armCmd
= false; // Arm command from handset either via ch5 or arm message
77 bool lastArmCmd
= false;
79 #if defined(PLATFORM_ESP32)
80 bool UARTinverted
= false;
83 void sendSyncPacketToTX();
84 void adjustMaxPacketSize();
85 void duplex_set_RX() const;
86 void duplex_set_TX() const;
87 void RcPacketToChannelsData();
88 bool processInternalCrsfPackage(uint8_t *package
);
89 void alignBufferToSync(uint8_t startIdx
);
93 void flush_port_input();