-
Notifications
You must be signed in to change notification settings - Fork 3
/
LD2411s.h
130 lines (86 loc) · 3.27 KB
/
LD2411s.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
#include "esphome.h"
using namespace esphome;
class UARTSensor : public Component, public UARTDevice {
public:
UARTSensor(UARTComponent *parent) : UARTDevice(parent) {}
Sensor* presence_sensor = new Sensor();
Sensor* motion_sensor = new Sensor();
Sensor* distance_sensor = new Sensor();
Sensor* max_motion_sensor = new Sensor();
Sensor* min_motion_sensor = new Sensor();
Sensor* max_presence_sensor = new Sensor();
Sensor* min_presence_sensor = new Sensor();
Sensor* unocc_time_sensor = new Sensor();
void setup() {
}
std::vector<int> bytes;
void loop() override
{
while (available())
{
bytes.push_back(read());
//End of Frame is 0x55 0x55
if( (bytes[bytes.size()-2] == 0x55 && bytes[bytes.size()-1] == 0x55) || ( bytes[bytes.size()-4] == 0x04 && bytes[bytes.size()-3] == 0x03 && bytes[bytes.size()-2] == 0x02 && bytes[bytes.size()-1] == 0x01 ) )
{
processPacket();
bytes.clear();
}
}
}
void processPacket() {
std::string str = "";
if ((bytes[0] == 0xAA) && (bytes[1] == 0xAA) && (bytes[2] == 0x00)) {
presence_sensor->publish_state(0);
motion_sensor->publish_state(0);
return;
}
if ( (bytes[0] == 0xAA) && (bytes[1] == 0xAA) && (bytes[2] == 0x01) ) {
presence_sensor->publish_state(1);
motion_sensor->publish_state(1);
unsigned char byte3 = bytes[3];
unsigned char byte4 = bytes[4];
unsigned int distanceHex = (byte4 << 8) | byte3;
int distanceCm = static_cast<int>(distanceHex);
distance_sensor->publish_state(distanceCm);
return;
}
if ( (bytes[0] == 0xAA) && (bytes[1] == 0xAA) && (bytes[2] == 0x02) ) {
presence_sensor->publish_state(1);
motion_sensor->publish_state(0);
unsigned char byte3 = bytes[3];
unsigned char byte4 = bytes[4];
unsigned int distanceHex = (byte4 << 8) | byte3;
int distanceCm = static_cast<int>(distanceHex);
distance_sensor->publish_state(distanceCm);
return;
}
if ( (bytes[0] == 0xFD) && (bytes[1] == 0xFC) && (bytes[2] == 0xFB) && (bytes[3] == 0xFA) && (bytes[6] == 0x73) && (bytes[7] == 0x01) ) {
unsigned char maxmlow = bytes[10];
unsigned char maxmhigh = bytes[11];
unsigned int maxmhex = (maxmhigh << 8) | maxmlow;
int max_m = static_cast<int>(maxmhex);
unsigned char minmlow = bytes[12];
unsigned char minmhigh = bytes[13];
unsigned int minmhex = (minmhigh << 8) | minmlow;
int min_m = static_cast<int>(minmhex);
unsigned char maxplow = bytes[14];
unsigned char maxphigh = bytes[15];
unsigned int maxphex = (maxphigh << 8) | maxplow;
int max_p = static_cast<int>(maxphex);
unsigned char minplow = bytes[16];
unsigned char minphigh = bytes[17];
unsigned int minphex = (minphigh << 8) | minplow;
int min_p = static_cast<int>(minphex);
unsigned char unocclow = bytes[18];
unsigned char unocchigh = bytes[19];
unsigned int unocchex = (unocchigh << 8) | unocclow;
int unocc_time = static_cast<int>(unocchex);
max_motion_sensor->publish_state(max_m);
min_motion_sensor->publish_state(min_m);
max_presence_sensor->publish_state(max_p);
min_presence_sensor->publish_state(min_p);
unocc_time_sensor->publish_state(unocc_time / 10);
return;
}
}
};