-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDFRobot_GestureFaceDetection.cpp
More file actions
300 lines (279 loc) · 7.85 KB
/
DFRobot_GestureFaceDetection.cpp
File metadata and controls
300 lines (279 loc) · 7.85 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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*!
*@file DFRobot_GestureFaceDetection.cpp
*@brief Define the basic structure of class DFRobot_GestureFaceDetection, the implementation of basic methods.
*@details this module is used to identify the information in the QR code
*@copyright Copyright (c) 2025 DFRobot Co.Ltd (http://www.dfrobot.com)
*@License The MIT License (MIT)
*@author [thdyyl](yuanlong.yu@dfrobot.com)
*@version V1.0
*@date 2025-04-02
*@https://github.com/DFRobot/DFRobot_GestureFaceDetection
*/
#include "DFRobot_GestureFaceDetection.h"
DFRobot_GestureFaceDetection::DFRobot_GestureFaceDetection()
{
}
bool DFRobot_GestureFaceDetection::begin()
{
if (reaInputdReg(REG_GFD_PID) == GFD_PID)
{
return true;
}
return false;
}
uint16_t DFRobot_GestureFaceDetection::getPid()
{
return reaInputdReg(REG_GFD_PID);
}
uint16_t DFRobot_GestureFaceDetection::getVid()
{
return reaInputdReg(REG_GFD_VID);
}
uint16_t DFRobot_GestureFaceDetection::getFaceNumber()
{
return reaInputdReg(REG_GFD_FACE_NUMBER);
}
uint16_t DFRobot_GestureFaceDetection::configUart(eBaudConfig_t baud, eParityConfig_t parity, eStopbits_t stopBit)
{
uint16_t ret = 0;
if(baud < eBaud_1200 || baud >= eBaud_MAX){
ret |= ERR_INVALID_BAUD;
}
if(static_cast<uint8_t>(parity) >= UART_CFG_PARITY_MAX){
ret |= ERR_INVALID_PARITY;
}
if(static_cast<uint8_t>(stopBit) >= UART_CFG_STOP_MAX){
ret |= ERR_INVALID_STOPBIT;
}
if(ret != 0){
return ret;
}
uint16_t baudRate = baud;
uint16_t verifyAndStop = ((uint16_t)parity << 8) | ((uint16_t)stopBit & 0xff);
ret |= writeIHoldingReg(REG_GFD_BAUDRATE, baudRate)? SUCCESS : ERR_CONFIG_BUAD;
ret |= writeIHoldingReg(REG_GFD_VERIFY_AND_STOP, verifyAndStop)? SUCCESS: ERR_CONFIG_PARITY_STOPBIT;
return ret;
}
uint16_t DFRobot_GestureFaceDetection::getFaceLocationX()
{
return reaInputdReg(REG_GFD_FACE_LOCATION_X);
}
uint16_t DFRobot_GestureFaceDetection::getFaceLocationY()
{
return reaInputdReg(REG_GFD_FACE_LOCATION_Y);
}
uint16_t DFRobot_GestureFaceDetection::getFaceScore()
{
return reaInputdReg(REG_GFD_FACE_SCORE);
}
uint16_t DFRobot_GestureFaceDetection::getGestureType()
{
return reaInputdReg(REG_GFD_GESTURE_TYPE);
}
uint16_t DFRobot_GestureFaceDetection::getGestureScore()
{
return reaInputdReg(REG_GFD_GESTURE_SCORE);
}
bool DFRobot_GestureFaceDetection::setFaceDetectThres(uint16_t score)
{
if (score > 100)
{
return false;
}
return writeIHoldingReg(REG_GFD_FACE_SCORE_THRESHOLD, score);
}
uint16_t DFRobot_GestureFaceDetection::getFaceDetectThres()
{
return readHoldingReg(REG_GFD_FACE_SCORE_THRESHOLD);
}
bool DFRobot_GestureFaceDetection::setDetectThres(uint16_t x)
{
if (x > 100)
{
return false;
}
return writeIHoldingReg(REG_GFD_FACE_THRESHOLD, x);
}
uint16_t DFRobot_GestureFaceDetection::getDetectThres(){
return readHoldingReg(REG_GFD_FACE_THRESHOLD);
}
bool DFRobot_GestureFaceDetection::setGestureDetectThres(uint16_t score)
{
if (score > 100)
{
return false;
}
return writeIHoldingReg(REG_GFD_GESTURE_SCORE_THRESHOLD, score);
}
uint16_t DFRobot_GestureFaceDetection::getGestureDetectThres(){
return readHoldingReg(REG_GFD_GESTURE_SCORE_THRESHOLD);
}
bool DFRobot_GestureFaceDetection::setDeviceAddr(uint16_t addr)
{
if ((addr == 0) || (addr > 0xF7))
{
return false;
}
return writeIHoldingReg(REG_GFD_ADDR, addr);
}
DFRobot_GestureFaceDetection_UART::DFRobot_GestureFaceDetection_UART(Stream *s_, uint8_t addr)
: DFRobot_RTU(s_)
{
_addr = addr;
}
bool DFRobot_GestureFaceDetection_UART::begin()
{
return DFRobot_GestureFaceDetection::begin();
}
uint16_t DFRobot_GestureFaceDetection_UART::reaInputdReg(uint16_t reg)
{
delay(20);
return readInputRegister(_addr, reg) ;
}
uint16_t DFRobot_GestureFaceDetection_UART::readHoldingReg(uint16_t reg)
{
delay(20);
return readHoldingRegister(_addr, reg);
}
bool DFRobot_GestureFaceDetection_UART::writeIHoldingReg(uint16_t reg, uint16_t data)
{
delay(20);
uint16_t ret = writeHoldingRegister(_addr, reg, data);
LDBG(ret);
return ret == 0;
}
DFRobot_GestureFaceDetection_I2C::DFRobot_GestureFaceDetection_I2C(uint8_t addr)
{
_addr = addr;
}
bool DFRobot_GestureFaceDetection_I2C::begin(TwoWire *pWire)
{
_pWire = pWire;
pWire->setClock(100000);
pWire->begin();
pWire->setClock(100000);
return DFRobot_GestureFaceDetection::begin();
}
uint8_t DFRobot_GestureFaceDetection_I2C::calculate_crc(const uint8_t *data, size_t length)
{
uint8_t crc = 0xFF;
for (size_t i = 0; i < length; i++)
{
crc ^= data[i];
for (uint8_t j = 0; j < 8; j++)
{
if (crc & 0x80)
{
crc = (crc << 1) ^ 0x07;
}
else
{
crc <<= 1;
}
}
crc &= 0xFF;
}
return crc;
}
bool DFRobot_GestureFaceDetection_I2C::writeReg(uint16_t reg, uint16_t data)
{
uint8_t retry = 0;
const uint8_t max_retry = 3;
bool success = false;
uint8_t crc_datas[] = {(uint8_t)(reg >> 8),
(uint8_t)(reg & 0xFF),
(uint8_t)(data >> 8),
(uint8_t)(data & 0xFF)};
uint8_t crc = calculate_crc(crc_datas, 4);
do
{
_pWire->beginTransmission(_addr);
_pWire->write((uint8_t)(reg >> 8));
_pWire->write((uint8_t)(reg & 0xFF));
_pWire->write((uint8_t)(data >> 8));
_pWire->write((uint8_t)(data & 0xFF));
_pWire->write(crc);
uint8_t i2c_error = _pWire->endTransmission();
if (i2c_error != 0)
{
retry++;
delay(10);
continue;
}
#if defined(ESP32)
delay(70);
#else
delay(5);
#endif
uint8_t bytes_read = _pWire->requestFrom(_addr, (uint8_t)3);
if (bytes_read != 3)
{
retry++;
continue;
}
uint8_t redatas[] = {(uint8_t)_pWire->read(), (uint8_t)_pWire->read(), (uint8_t)_pWire->read()};
uint8_t re_crc = calculate_crc(redatas, 2);
if (re_crc != redatas[2] || ((redatas[0] << 8) | redatas[1]) != crc)
{
retry++;
}
else
{
success = true;
break;
}
} while (retry < max_retry);
return success;
}
uint16_t DFRobot_GestureFaceDetection_I2C::readReg(uint16_t reg)
{
const uint8_t max_retry = 3;
uint8_t retry = 0;
uint16_t value = 0xFFFF;
uint8_t crc_datas[] = {(uint8_t)(reg >> 8),
(uint8_t)(reg & 0xFF)};
uint8_t crc = calculate_crc(crc_datas, 2);
do
{
_pWire->beginTransmission(_addr);
_pWire->write((uint8_t)(reg >> 8));
_pWire->write((uint8_t)(reg & 0xff));
_pWire->write(crc);
uint8_t i2c_error = _pWire->endTransmission();
if (i2c_error != 0)
{
retry++;
continue;
}
delay(5);
uint8_t bytes_read = _pWire->requestFrom(_addr, (uint8_t)3);
if (bytes_read != 3)
{
retry++;
continue;
}
uint8_t redatas[] = {(uint8_t)_pWire->read(), (uint8_t)_pWire->read(), (uint8_t)_pWire->read()};
uint8_t re_crc = calculate_crc(redatas, 2);
uint16_t data = (redatas[0] << 8) | redatas[1];
if (data == 0xFFFF || re_crc != redatas[2])
{
retry++;
continue;
}
value = data;
break;
} while (retry < max_retry);
return value;
}
bool DFRobot_GestureFaceDetection_I2C::writeIHoldingReg(uint16_t reg, uint16_t data)
{
return writeReg(reg, data);
}
uint16_t DFRobot_GestureFaceDetection_I2C::reaInputdReg(uint16_t reg)
{
return readReg(INPUT_REG_OFFSET + reg);
}
uint16_t DFRobot_GestureFaceDetection_I2C::readHoldingReg(uint16_t reg)
{
return readReg(reg);
}