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 CRSF_to_BIT(ChannelData
[4]); } // AUX1
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
[LinkStatisticsFrameLength
+ 4]);
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 bool CRSFframeActive
= false; // since we get a copy of the serial data use this flag to know when to ignore it
66 static bool halfDuplex
;
67 bool transmitting
= false;
68 uint32_t GoodPktsCount
= 0;
69 uint32_t BadPktsCount
= 0;
70 uint32_t UARTwdtLastChecked
= 0;
71 uint8_t maxPacketBytes
= CRSF_MAX_PACKET_LEN
;
72 uint8_t maxPeriodBytes
= CRSF_MAX_PACKET_LEN
;
74 static uint8_t UARTcurrentBaudIdx
;
75 static uint32_t UARTrequestedBaud
;
77 #if defined(PLATFORM_ESP32)
78 bool UARTinverted
= false;
81 void sendSyncPacketToTX();
82 void adjustMaxPacketSize();
83 void duplex_set_RX() const;
84 void duplex_set_TX() const;
85 void RcPacketToChannelsData();
86 bool processInternalCrsfPackage(uint8_t *package
);
90 void flush_port_input();