-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaithinker-rd03.ino
More file actions
170 lines (146 loc) · 3.72 KB
/
aithinker-rd03.ino
File metadata and controls
170 lines (146 loc) · 3.72 KB
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
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <MiniShell.h>
#include "rd03-protocol.h"
#define RADAR_BAUD_RATE 115200
#define PIN_RX D2
#define PIN_TX D3
#define printf Serial.printf
static MiniShell shell(&Serial);
static SoftwareSerial radar(PIN_RX, PIN_TX);
static RD03Protocol proto_ack = RD03Protocol(0xFAFBFCFD, 0x01020304);
static RD03Protocol proto_rsp = RD03Protocol(0xF1F2F3F4, 0xF5F6F7F8);
static bool debug = false;
static void printhex(const char *prefix, const uint8_t *buf, size_t len)
{
printf(prefix);
for (size_t i = 0; i < len; i++) {
printf(" %02X", buf[i]);
}
printf("\n");
}
static int do_reboot(int argc, char *argv[])
{
ESP.restart();
return 0;
}
static int do_debug(int argc, char *argv[])
{
debug = !debug;
printf("debug: %s\n", debug ? "ON" : "OFF");
return 0;
}
static bool radar_command_raw(uint16_t cmd, size_t cmd_len, const uint8_t *cmd_data)
{
uint8_t buf[32];
// send
size_t len = proto_ack.build_command(buf, cmd, cmd_len, cmd_data);
printhex("CMD <", buf, len);
radar.write(buf, len);
// wait
unsigned long begin = millis();
while ((millis() - begin) < 1000) {
if (radar.available()) {
char c = radar.read();
if (proto_ack.process_rx(c)) {
int len = proto_ack.get_data(buf);
printhex("ACK <", buf, len);
return true;
}
}
}
return false;
}
static bool radar_open(void)
{
uint8_t data[2];
data[0] = 1;
data[1] = 0;
return radar_command_raw(0x00FF, 2, data);
}
static bool radar_close(void)
{
return radar_command_raw(0x00FE, 0, NULL);
}
static bool radar_command(uint16_t cmd, size_t cmd_len, const uint8_t *cmd_data)
{
// open
if (!radar_open()) {
printf("radar_open failed!\n");
return -1;
}
delay(100);
// command
if (!radar_command_raw(cmd, cmd_len, cmd_data)) {
printf("radar_command_raw() failed!\n");
}
delay(100);
// close
radar_close();
return 0;
}
static int do_mode(int argc, char *argv[])
{
if (argc < 2) {
return -1;
}
int mode = atoi(argv[1]);
uint8_t data[6];
data[0] = 0;
data[1] = 0;
data[2] = mode;
data[3] = 0;
data[4] = 0;
data[5] = 0;
if (!radar_command(0x0012, sizeof(data), data)) {
printf("radar_command() failed!\n");
}
return 0;
}
// example data 01 78 00 AA 0C 51 02 25 00 34 00 22 00 12 00 11 00 50 00 3A 00 22 00 1D 00 1D 00 28 00 25 00 28 00 11 00
static void decode_report(size_t len, const uint8_t *buf)
{
if (len != 0x23) {
return;
}
int index = 0;
bool present = buf[index++];
uint16_t range = buf[index++];
range += buf[index++] << 8;
printf("%3s %5ucm", present ? "ON" : "OFF", range);
for (int i = 0; i < 16; i++) {
uint16_t energy = buf[index++];
energy += buf[index++] << 8;
printf(" %5u", energy);
}
printf("\n");
}
static const cmd_t commands[] = {
{ "reboot", do_reboot, "Reboot" },
{ "debug", do_debug, "Toggle debug mode" },
{ "mode", do_mode, "Set report mode (0=debug,4=binary,100=ascii)" },
{ NULL, NULL, NULL }
};
void setup(void)
{
Serial.begin(115200);
radar.begin(RADAR_BAUD_RATE);
}
void loop(void)
{
uint8_t buf[256];
shell.process(">", commands);
// process incoming data from radar
while (radar.available()) {
uint8_t c = radar.read();
if (debug) {
printf(" %02X", c);
}
// run receive state machine
if (proto_rsp.process_rx(c)) {
size_t len = proto_rsp.get_data(buf);
printhex("RSP <", buf, len);
decode_report(len, buf);
}
}
}