makes GPIO_PIN_RST optional for the sx1276
[ExpressLRS.git] / src / lib / Telemetry / telemetry.h
blob7db01820d2a0cb0ca71aa2190fe3c2d37f54778e
1 #pragma once
3 #include <cstdint>
4 #include "crsf_protocol.h"
6 enum CustomTelemSubTypeID : uint8_t {
7 CRSF_AP_CUSTOM_TELEM_SINGLE_PACKET_PASSTHROUGH = 0xF0,
8 CRSF_AP_CUSTOM_TELEM_STATUS_TEXT = 0xF1,
9 CRSF_AP_CUSTOM_TELEM_MULTI_PACKET_PASSTHROUGH = 0xF2,
12 typedef enum {
13 TELEMETRY_IDLE = 0,
14 RECEIVING_LENGTH,
15 RECEIVING_DATA
16 } telemetry_state_s;
18 typedef struct crsf_telemetry_package_t {
19 const uint8_t type;
20 const uint8_t size;
21 volatile bool locked;
22 volatile bool updated;
23 uint8_t *data;
24 } crsf_telemetry_package_t;
26 #define PAYLOAD_DATA(type0, type1, type2, type3, type4, type5)\
27 uint8_t PayloadData[\
28 CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type0##_PAYLOAD_SIZE) + \
29 CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type1##_PAYLOAD_SIZE) + \
30 CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type2##_PAYLOAD_SIZE) + \
31 CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type3##_PAYLOAD_SIZE) + \
32 CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type4##_PAYLOAD_SIZE) + \
33 CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type5##_PAYLOAD_SIZE) + \
34 CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_GENERAL_RESP_PAYLOAD_SIZE) + \
35 CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_GENERAL_RESP_PAYLOAD_SIZE)]; \
36 crsf_telemetry_package_t payloadTypes[] = {\
37 {CRSF_FRAMETYPE_##type0, CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type0##_PAYLOAD_SIZE), false, false, 0},\
38 {CRSF_FRAMETYPE_##type1, CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type1##_PAYLOAD_SIZE), false, false, 0},\
39 {CRSF_FRAMETYPE_##type2, CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type2##_PAYLOAD_SIZE), false, false, 0},\
40 {CRSF_FRAMETYPE_##type3, CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type3##_PAYLOAD_SIZE), false, false, 0},\
41 {CRSF_FRAMETYPE_##type4, CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type4##_PAYLOAD_SIZE), false, false, 0},\
42 {CRSF_FRAMETYPE_##type5, CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_##type5##_PAYLOAD_SIZE), false, false, 0},\
43 {0, CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_GENERAL_RESP_PAYLOAD_SIZE), false, false, 0},\
44 {0, CRSF_TELEMETRY_TOTAL_SIZE(CRSF_FRAME_GENERAL_RESP_PAYLOAD_SIZE), false, false, 0}};\
45 const uint8_t payloadTypesCount = (sizeof(payloadTypes)/sizeof(crsf_telemetry_package_t))
47 class Telemetry
49 public:
50 Telemetry();
51 bool RXhandleUARTin(uint8_t data);
52 void ResetState();
53 bool ShouldCallBootloader();
54 bool ShouldCallEnterBind();
55 bool ShouldCallUpdateModelMatch();
56 bool ShouldSendDeviceFrame();
57 uint8_t GetUpdatedModelMatch() { return modelMatchId; }
58 bool GetNextPayload(uint8_t* nextPayloadSize, uint8_t **payloadData);
59 uint8_t UpdatedPayloadCount();
60 uint8_t ReceivedPackagesCount();
61 bool AppendTelemetryPackage(uint8_t *package);
62 private:
63 void AppendToPackage(volatile crsf_telemetry_package_t *current);
64 uint8_t CRSFinBuffer[CRSF_MAX_PACKET_LEN];
65 telemetry_state_s telemetry_state;
66 uint8_t currentTelemetryByte;
67 uint8_t currentPayloadIndex;
68 volatile crsf_telemetry_package_t *telemetryPackageHead;
69 uint8_t receivedPackages;
70 bool callBootloader;
71 bool callEnterBind;
72 bool callUpdateModelMatch;
73 bool sendDeviceFrame;
74 uint8_t modelMatchId;