generated from pimoroni/pico-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFRobot_mmWave_Radar.cpp
157 lines (136 loc) · 3.75 KB
/
DFRobot_mmWave_Radar.cpp
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
/*!
@file DFRobot_mmWave_Radar.cpp
@brief Implement the basic structure of class mmwave rader sensor-human presence detection
@copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
@licence The MIT License (MIT)
@author huyujie([email protected])
@version V1.0
@date 2020-3-25
@get from https://www.dfrobot.com
@url https://github.com/DFRobot
*/
// Modified to suit Raspberry Pi Pico stdlib
#include "DFRobot_mmWave_Radar.h"
#include "pico/stdlib.h"
#include <cstdio>
#include <cstring>
DFRobot_mmWave_Radar::DFRobot_mmWave_Radar(uart_inst_t *s)
{
_s = s;
}
size_t DFRobot_mmWave_Radar::readN(uint8_t *buf, size_t len)
{
size_t offset = 0, left = len;
const int32_t timeout = 1500;
uint8_t *buffer = buf;
uint32_t curr = to_ms_since_boot(get_absolute_time());
while (left) {
if (uart_is_readable(_s)) {
buffer[offset] = uart_getc(_s);
offset++;
left--;
}
if (to_ms_since_boot(get_absolute_time()) - curr > timeout) {
break;
}
}
return offset;
}
bool DFRobot_mmWave_Radar::recdData(uint8_t *buf)
{
uint32_t timeout = 50000;
long curr = to_ms_since_boot(get_absolute_time());
uint8_t ch;
bool ret = false;
while (!ret) {
if (to_ms_since_boot(get_absolute_time()) - curr > timeout) {
break;
}
if (readN(&ch, 1) == 1) {
if (ch == '$') {
buf[0] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'J') {
buf[1] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'Y') {
buf[2] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'B') {
buf[3] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'S') {
buf[4] = ch;
if (readN(&ch, 1) == 1) {
if (ch == 'S') {
buf[5] = ch;
if (readN(&buf[6], 9) == 9) {
ret = true;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return ret;
}
bool DFRobot_mmWave_Radar::readPresenceDetection(bool *result)
{
uint8_t dat[15] = {0};
if (!recdData(dat)) {
return false;
}
switch (dat[7]) {
case '1':
*result = true;
return true;
case '0':
*result = false;
return true;
default:
return false;
}
}
void DFRobot_mmWave_Radar::stop() {
uart_write_blocking(_s, (uint8_t *) comStop, strlen(comStop));
sleep_ms(comDelay);
}
void DFRobot_mmWave_Radar::start() {
uart_write_blocking(_s, (uint8_t*)comStart, strlen(comStart));
sleep_ms(comDelay);
}
void DFRobot_mmWave_Radar::save() {
uart_write_blocking(_s, (uint8_t*)comSaveCfg, strlen(comSaveCfg));
sleep_ms(comDelay);
}
void DFRobot_mmWave_Radar::DetRangeCfg(float parA_s, float parA_e)
{
char comDetRangeCfg[22] = {0};
int16_t parA_S = parA_s / 0.15;
int16_t parA_E = parA_e / 0.15;
sprintf(comDetRangeCfg, "detRangeCfg -1 %d %d", parA_S, parA_E);
uart_write_blocking(_s, (uint8_t*)comDetRangeCfg, strlen(comDetRangeCfg));
sleep_ms(comDelay);
}
void DFRobot_mmWave_Radar::OutputLatency(float par1, float par2)
{
char comOutputLatency[28] = {0};
int16_t Par1 = par1 * 1000 / 25;
int16_t Par2 = par2 * 1000 / 25;
sprintf(comOutputLatency, "outputLatency -1 %d %d", Par1 , Par2);
uart_write_blocking(_s, (uint8_t*)comOutputLatency, strlen(comOutputLatency));
sleep_ms(comDelay);
}
void DFRobot_mmWave_Radar::factoryReset(void)
{
uart_write_blocking(_s, (uint8_t*)comFactoryReset, strlen(comFactoryReset));
sleep_ms(comDelay);
}