TR-mbed 1.0
Loading...
Searching...
No Matches
DJIRemote2.h
Go to the documentation of this file.
1#ifndef VTM_RECEIVER_H
2#define VTM_RECEIVER_H
3
4#include "mbed.h"
5#include <cstdint>
6#include <cstddef>
7
8struct VTMInput {
9 uint16_t ch0 = 1024;
10 uint16_t ch1 = 1024;
11 uint16_t ch2 = 1024;
12 uint16_t ch3 = 1024;
13
14 uint8_t mode = 1;
15 uint8_t pause = 0;
16 uint8_t btnL = 0;
17 uint8_t btnR = 0;
18
19 uint16_t dial = 1024;
20 uint8_t trigger = 0;
21
22 int16_t mouseX = 0;
23 int16_t mouseY = 0;
24 int16_t mouseZ = 0;
25
26 uint8_t mouseL = 0;
27 uint8_t mouseR = 0;
28 uint8_t mouseM = 0;
29
30 uint16_t keyboard = 0;
31 uint16_t CRC_in = 0;
32};
33
35public:
36 static constexpr uint8_t HEADER_BYTE_0 = 0xA9;
37 static constexpr uint8_t HEADER_BYTE_1 = 0x53;
38 static constexpr size_t FRAME_SIZE = 21;
39 static constexpr size_t STREAM_BUFFER_SIZE = 64;
40 static constexpr float STICK_MAX_VALUE = 1684.0f;
41
42 DJIRemote2(PinName tx, PinName rx, int baud = 921600);
43
44 bool update(); // returns true when a full new frame is decoded
45 void clear();
46 void zeroInputs();
47
48 uint64_t prevFrameTime = 0;
49
50 const VTMInput& getData() const;
51 bool hasValidFrame() const;
52
53 uint64_t getLastFrameTimeUs() const;
54 uint64_t getFramePeriodUs() const;
55 double getFrameRateHz() const;
56
57 // specifies a particular joystick
65
66 // specifies a particular mode
67 enum class ModeSwitch
68 {
69 MODE_C,
70 MODE_N,
71 MODE_S
72 };
73
74 // A list of the particular keys to interact with, in bit order.
75 enum class Key
76 {
77 W = 0,
78 S,
79 A,
80 D,
81 SHIFT,
82 CTRL,
83 Q,
84 E,
85 R,
86 F,
87 G,
88 Z,
89 X,
90 C,
91 V,
92 B
93 };
94
95 // check if trigger is pressed
96 bool TriggerPressed() const;
97 // check if custom left button is pressed
98 bool CUSTLPressed() const;
99 // check if custom right button is pressed
100 bool CUSTRPressed() const;
101 // check if pause button is pressed
102 bool PAUSEPressed() const;
103
107 int16_t getMouseX() const;
108
112 int16_t getMouseY() const;
113
117 int16_t getMouseZ() const;
118
122 bool getMouseL() const;
123
127 bool getMouseR() const;
128
132 bool keyPressed(Key key) const;
133
137 int16_t getWheel() const;
138
139 bool CUSTLToggled() const;
140 bool CUSTRToggled() const;
141 bool PAUSEToggled() const;
142
143 float getJoystickValue(Joystick joy) const;
144 float apply_deadzone(float value) const;
145 float getDialValue() const;
146 ModeSwitch getMode() const;
147
148private:
149 BufferedSerial serial_;
150
151 uint8_t streamBuffer_[STREAM_BUFFER_SIZE];
152 size_t streamCount_;
153
154 VTMInput data_;
155 bool validFrame_;
156
157 uint64_t lastFrameTimeUs_;
158 uint64_t currentFrameTimeUs_;
159 uint64_t framePeriodUs_;
160
161
162 void readIncomingBytes();
163 bool tryParseFrame();
164 int findHeader() const;
165 void decodeFrame(const uint8_t* frame);
166 void shiftLeft(size_t count);
167};
168
169#endif
Definition DJIRemote2.h:34
bool update()
Definition DJIRemote2.cpp:102
static constexpr uint8_t HEADER_BYTE_1
Definition DJIRemote2.h:37
void clear()
Definition DJIRemote2.cpp:114
static constexpr float STICK_MAX_VALUE
Definition DJIRemote2.h:40
static constexpr size_t FRAME_SIZE
Definition DJIRemote2.h:38
int16_t getWheel() const
Definition DJIRemote2.cpp:421
Joystick
Definition DJIRemote2.h:59
bool keyPressed(Key key) const
Definition DJIRemote2.cpp:419
bool PAUSEToggled() const
Definition DJIRemote2.cpp:398
bool getMouseR() const
Definition DJIRemote2.cpp:417
int16_t getMouseX() const
Definition DJIRemote2.cpp:409
ModeSwitch
Definition DJIRemote2.h:68
bool hasValidFrame() const
Definition DJIRemote2.cpp:157
bool CUSTRToggled() const
Definition DJIRemote2.cpp:385
Key
Definition DJIRemote2.h:76
bool TriggerPressed() const
Definition DJIRemote2.cpp:368
const VTMInput & getData() const
Definition DJIRemote2.cpp:152
void zeroInputs()
Definition DJIRemote2.cpp:125
bool PAUSEPressed() const
Definition DJIRemote2.cpp:396
uint64_t prevFrameTime
Definition DJIRemote2.h:48
double getFrameRateHz() const
Definition DJIRemote2.cpp:172
static constexpr uint8_t HEADER_BYTE_0
Definition DJIRemote2.h:36
ModeSwitch getMode() const
Definition DJIRemote2.cpp:348
float apply_deadzone(float value) const
Definition DJIRemote2.cpp:328
int16_t getMouseZ() const
Definition DJIRemote2.cpp:413
bool CUSTLToggled() const
Definition DJIRemote2.cpp:372
bool getMouseL() const
Definition DJIRemote2.cpp:415
bool CUSTLPressed() const
Definition DJIRemote2.cpp:370
float getJoystickValue(Joystick joy) const
Definition DJIRemote2.cpp:333
uint64_t getLastFrameTimeUs() const
Definition DJIRemote2.cpp:162
int16_t getMouseY() const
Definition DJIRemote2.cpp:411
bool CUSTRPressed() const
Definition DJIRemote2.cpp:383
uint64_t getFramePeriodUs() const
Definition DJIRemote2.cpp:167
float getDialValue() const
Definition DJIRemote2.cpp:363
static constexpr size_t STREAM_BUFFER_SIZE
Definition DJIRemote2.h:39
Definition DJIRemote2.h:8
uint16_t CRC_in
Definition DJIRemote2.h:31
int16_t mouseY
Definition DJIRemote2.h:23
uint8_t mouseL
Definition DJIRemote2.h:26
int16_t mouseX
Definition DJIRemote2.h:22
uint8_t mouseR
Definition DJIRemote2.h:27
uint8_t btnL
Definition DJIRemote2.h:16
uint8_t mouseM
Definition DJIRemote2.h:28
uint16_t ch1
Definition DJIRemote2.h:10
uint16_t dial
Definition DJIRemote2.h:19
uint16_t ch0
Definition DJIRemote2.h:9
uint16_t keyboard
Definition DJIRemote2.h:30
uint8_t trigger
Definition DJIRemote2.h:20
uint8_t pause
Definition DJIRemote2.h:15
int16_t mouseZ
Definition DJIRemote2.h:24
uint16_t ch2
Definition DJIRemote2.h:11
uint16_t ch3
Definition DJIRemote2.h:12
uint8_t mode
Definition DJIRemote2.h:14
uint8_t btnR
Definition DJIRemote2.h:17