-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ultrasonic.cpp
92 lines (71 loc) · 1.66 KB
/
Ultrasonic.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
#include "Ultrasonic.h"
// constructor
Ultrasonic::Ultrasonic(const byte rxPin, const byte txPin, uint16_t baudrate) : serial(rxPin,txPin) {
serial.begin(baudrate);
}
// send to serial
void Ultrasonic::send(uint8_t hex) {
serial.write(hex);
}
// convert type to hex (listen, short or long
uint8_t Ultrasonic::hexFromReadingType(ReadingType type) {
switch (type) {
case LISTEN: return 0x00; break;
case SHORT: return 0x01; break;
case LONG: return 0x02; break;
}
return 0x00;
}
// should send back 0x12 0x34 0xB9
void Ultrasonic::testUart() {
send(0x00);
send(0x55);
send(0x01);
send(0x00);
}
void Ultrasonic::burstAndThreshold(enum ReadingType type) {
send(0x00);
send(0x55);
send(0x11);
send(hexFromReadingType(type));
send(0x00);
}
uint16_t Ultrasonic::readFirstThreshold() {
send(0x00);
send(0x55);
send(0x21);
send(0x00);
uint16_t highbyte = serial.read();
uint16_t lowbyte = serial.read();
highbyte <<= 8;
serial.read(); // dump checksum
return highbyte+lowbyte;
}
void Ultrasonic::updateEEPROM(uint8_t address, uint8_t data) {
send(0x00);
send(0x55);
send(0x31);
send(address);
send(data);
send(0x00);
}
void Ultrasonic::readFIFO() {
send(0x00);
send(0x55);
send(0x41);
send(0x00);
}
void Ultrasonic::burstAndCapture(enum ReadingType type) {
send(0x00);
send(0x55);
send(0x51);
send(hexFromReadingType(type));
send(0x00);
}
void Ultrasonic::thresholdForMode(enum ReadingType type) {
send(0x00);
send(0x55);
send(0x61);
send(hexFromReadingType(type));
send(0x00);
}