-
Notifications
You must be signed in to change notification settings - Fork 37
/
ld2410_uart.h
280 lines (256 loc) · 7.88 KB
/
ld2410_uart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "esphome.h"
#define CHECK_BIT(var, pos) (((var) >> (pos)) & 1)
class LD2410 : public PollingComponent, public UARTDevice
{
public:
LD2410(UARTComponent *parent) : UARTDevice(parent) {}
BinarySensor *hasTarget = new BinarySensor();
BinarySensor *hasMovingTarget = new BinarySensor();
BinarySensor *hasStillTarget = new BinarySensor();
BinarySensor *lastCommandSuccess = new BinarySensor();
Sensor *movingTargetDistance = new Sensor();
Sensor *movingTargetEnergy = new Sensor();
Sensor *stillTargetDistance = new Sensor();
Sensor *stillTargetEnergy = new Sensor();
Sensor *detectDistance = new Sensor();
Number *maxMovingDistanceRange;
Number *maxStillDistanceRange;
int movingSensitivities[9] = {0};
int stillSensitivities[9] = {0};
Number *noneDuration;
long lastPeriodicMillis = millis();
void setNumbers(Number *maxMovingDistanceRange_, Number *maxStillDistanceRange_, Number *noneDuration_){
maxMovingDistanceRange = maxMovingDistanceRange_;
maxStillDistanceRange = maxStillDistanceRange_;
noneDuration = noneDuration_;
}
void sendCommand(char *commandStr, char *commandValue, int commandValueLen)
{
lastCommandSuccess->publish_state(false);
// frame start bytes
write_byte(0xFD);
write_byte(0xFC);
write_byte(0xFB);
write_byte(0xFA);
// length bytes
int len = 2;
if (commandValue != nullptr)
len += commandValueLen;
write_byte(lowByte(len));
write_byte(highByte(len));
// command string bytes
write_byte(commandStr[0]);
write_byte(commandStr[1]);
// command value bytes
if (commandValue != nullptr)
{
for (int i = 0; i < commandValueLen; i++)
{
write_byte(commandValue[i]);
}
}
// frame end bytes
write_byte(0x04);
write_byte(0x03);
write_byte(0x02);
write_byte(0x01);
delay(50);
}
int twoByteToInt(char firstByte, char secondByte)
{
return (int16_t)(secondByte << 8) + firstByte;
}
void handlePeriodicData(char *buffer, int len)
{
if (len < 12)
return; // 4 frame start bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame end bytes
if (buffer[0] != 0xF4 || buffer[1] != 0xF3 || buffer[2] != 0xF2 || buffer[3] != 0xF1)
return; // check 4 frame start bytes
if (buffer[7] != 0xAA || buffer[len - 6] != 0x55 || buffer[len - 5] != 0x00)
return; // data head=0xAA, data end=0x55, crc=0x00
/*
Data Type: 6th byte
0x01: Engineering mode
0x02: Normal mode
*/
char dataType = buffer[5];
/*
Target states: 9th byte
0x00 = No target
0x01 = Moving targets
0x02 = Still targets
0x03 = Moving+Still targets
*/
char stateByte = buffer[8];
hasTarget->publish_state(stateByte != 0x00);
/*
Reduce data update rate to prevent home assistant database size glow fast
*/
long currentMillis = millis();
if (currentMillis - lastPeriodicMillis < 1000)
return;
lastPeriodicMillis = currentMillis;
hasMovingTarget->publish_state(CHECK_BIT(stateByte, 0));
hasStillTarget->publish_state(CHECK_BIT(stateByte, 1));
/*
Moving target distance: 10~11th bytes
Moving target energy: 12th byte
Still target distance: 13~14th bytes
Still target energy: 15th byte
Detect distance: 16~17th bytes
*/
int newMovingTargetDistance = twoByteToInt(buffer[9], buffer[10]);
if (movingTargetDistance->get_state() != newMovingTargetDistance)
movingTargetDistance->publish_state(newMovingTargetDistance);
int newMovingTargetEnergy = buffer[11];
if (movingTargetEnergy->get_state() != newMovingTargetEnergy)
movingTargetEnergy->publish_state(newMovingTargetEnergy);
int newStillTargetDistance = twoByteToInt(buffer[12], buffer[13]);
if (stillTargetDistance->get_state() != newStillTargetDistance)
stillTargetDistance->publish_state(newStillTargetDistance);
int newStillTargetEnergy = buffer[14];
if (stillTargetEnergy->get_state() != newStillTargetEnergy)
stillTargetEnergy->publish_state(buffer[14]);
int newDetectDistance = twoByteToInt(buffer[15], buffer[16]);
if (detectDistance->get_state() != newDetectDistance)
detectDistance->publish_state(newDetectDistance);
if (dataType == 0x01)
{ // engineering mode
// todo: support engineering mode data
}
}
void handleACKData(char *buffer, int len)
{
if (len < 10)
return;
if (buffer[0] != 0xFD || buffer[1] != 0xFC || buffer[2] != 0xFB || buffer[3] != 0xFA)
return; // check 4 frame start bytes
if (buffer[7] != 0x01)
return;
if (twoByteToInt(buffer[8], buffer[9]) != 0x00)
{
lastCommandSuccess->publish_state(false);
return;
}
lastCommandSuccess->publish_state(true);
switch (buffer[6])
{
case 0x61: // Query parameters response
{
if (buffer[10] != 0xAA)
return; // value head=0xAA
/*
Moving distance range: 13th byte
Still distance range: 14th byte
*/
maxMovingDistanceRange->publish_state(buffer[12]);
maxStillDistanceRange->publish_state(buffer[13]);
/*
Moving Sensitivities: 15~23th bytes
Still Sensitivities: 24~32th bytes
*/
for (int i = 0; i < 9; i++)
{
movingSensitivities[i] = buffer[14 + i];
}
for (int i = 0; i < 9; i++)
{
stillSensitivities[i] = buffer[23 + i];
}
/*
None Duration: 33~34th bytes
*/
noneDuration->publish_state(twoByteToInt(buffer[32], buffer[33]));
}
break;
default:
break;
}
}
void readline(int readch, char *buffer, int len)
{
static int pos = 0;
if (readch >= 0)
{
if (pos < len - 1)
{
buffer[pos++] = readch;
buffer[pos] = 0;
}
else
{
pos = 0;
}
if (pos >= 4)
{
if (buffer[pos - 4] == 0xF8 && buffer[pos - 3] == 0xF7 && buffer[pos - 2] == 0xF6 && buffer[pos - 1] == 0xF5)
{
handlePeriodicData(buffer, pos);
pos = 0; // Reset position index ready for next time
}
else if (buffer[pos - 4] == 0x04 && buffer[pos - 3] == 0x03 && buffer[pos - 2] == 0x02 && buffer[pos - 1] == 0x01)
{
handleACKData(buffer, pos);
pos = 0; // Reset position index ready for next time
}
}
}
return;
}
void setConfigMode(bool enable)
{
char cmd[2] = {enable ? 0xFF : 0xFE, 0x00};
char value[2] = {0x01, 0x00};
sendCommand(cmd, enable ? value : nullptr, 2);
}
void queryParameters()
{
char cmd_query[2] = {0x61, 0x00};
sendCommand(cmd_query, nullptr, 0);
}
void setup() override
{
set_update_interval(15000);
}
void loop() override
{
const int max_line_length = 80;
static char buffer[max_line_length];
while (available())
{
readline(read(), buffer, max_line_length);
}
}
void setEngineeringMode(bool enable)
{
char cmd[2] = {enable ? 0x62 : 0x63, 0x00};
sendCommand(cmd, nullptr, 0);
}
void setMaxDistancesAndNoneDuration(int maxMovingDistanceRange, int maxStillDistanceRange, int noneDuration)
{
char cmd[2] = {0x60, 0x00};
char value[18] = {0x00, 0x00, lowByte(maxMovingDistanceRange), highByte(maxMovingDistanceRange), 0x00, 0x00, 0x01, 0x00, lowByte(maxStillDistanceRange), highByte(maxStillDistanceRange), 0x00, 0x00, 0x02, 0x00, lowByte(noneDuration), highByte(noneDuration), 0x00, 0x00};
sendCommand(cmd, value, 18);
queryParameters();
}
void factoryReset()
{
char cmd[2] = {0xA2, 0x00};
sendCommand(cmd, nullptr, 0);
}
void reboot()
{
char cmd[2] = {0xA3, 0x00};
sendCommand(cmd, nullptr, 0);
// not need to exit config mode because the ld2410 will reboot automatically
}
void setBaudrate(int index)
{
char cmd[2] = {0xA1, 0x00};
char value[2] = {index, 0x00};
sendCommand(cmd, value, 2);
}
void update()
{
}
};