-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDistanceSensor_A02YYUW.cpp
79 lines (55 loc) · 1.78 KB
/
DistanceSensor_A02YYUW.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
#include "DistanceSensor_A02YYUW.h"
DistanceSensor_A02YYUW::DistanceSensor_A02YYUW(Stream * stream): DistanceSensor_A02YYUW(stream, MIN_DISTANCE, MAX_DISTANCE) {
}
DistanceSensor_A02YYUW::DistanceSensor_A02YYUW(Stream * stream, unsigned int minDistance, unsigned int maxDistance) {
_stream = stream;
_minDistance = minDistance;
_maxDistance = maxDistance;
}
DistanceSensor_A02YYUW_MEASSUREMENT_STATUS DistanceSensor_A02YYUW::meassure() {
unsigned long startTime = millis();
unsigned char data[4] = {};
int i = 0;
unsigned int meassuredDistance;
_flushSerialInput();
_distance = _minDistance;
while (!_stream->available() && i < SERIAL_AVAILABLE_CHECK_CICLES) {
i++;
delay(SERIAL_AVAILABLE_CHECK_DELAY);
}
i = 0;
while(_stream->available() && i < 4) {
data[i] = _stream->read();
i++;
if (data[0] != SERIAL_HEAD_DATA) {
i = 0;
}
}
if (i != 4) {
return DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_SERIAL;
}
if (!_checkSum(data)) {
return DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_CHECK_SUM;
}
meassuredDistance = ((data[1] << 8) + data[2]);
if (meassuredDistance < _minDistance) {
return DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_MIN_LIMIT;
}
if (meassuredDistance > _maxDistance) {
return DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_ERROR_MAX_LIMIT;
}
_distance = meassuredDistance;
return DistanceSensor_A02YYUW_MEASSUREMENT_STATUS_OK;
}
unsigned int DistanceSensor_A02YYUW::getDistance() {
return _distance;
}
bool DistanceSensor_A02YYUW::_checkSum(unsigned char data[]) {
return ((data[0] + data[1] + data[2])& 0x00FF) == data[3];
}
void DistanceSensor_A02YYUW::_flushSerialInput() {
unsigned long startTime = millis();
while (_stream->available()) {
_stream->read();
}
}