-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixLaserSensor.cpp
More file actions
73 lines (58 loc) · 1.37 KB
/
MatrixLaserSensor.cpp
File metadata and controls
73 lines (58 loc) · 1.37 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
#include "MatrixLaserSensor.h"
bool MatrixLaser::begin(){
Wire.begin();
i2cMUXSelect();
delay(50);
if(i2cReadData(Device_ID) == 0x47){
i2cWriteData(Device_CONFIG, 0x04); // reset
delay(500);
i2cWriteData(Device_CONFIG, 0x02); // enable
return true;
}
else{
return false;
}
}
int MatrixLaser::getDistance() {
i2cMUXSelect();
if (((i2cReadData(Device_CONFIG) & 0x01) == 0)) {
int data = (int)(i2cReadData(Distance_H) << 8 | i2cReadData(Distance_L)); // 改為 int 型別
return data;
} else {
return 8191;
}
}
uint8_t MatrixLaser::i2cReadData(LaserRegType reg){
Wire.beginTransmission(MatrixLaser_ADDR);
Wire.write(reg);
Wire.endTransmission(1);
delay(1);
Wire.requestFrom(MatrixLaser_ADDR, 1);
delay(1);
return Wire.read();
}
void MatrixLaser::i2cMUXSelect(){
switch (_ver)
{
case 2:
Wire.beginTransmission(ADDR_PCA954X);
Wire.write(_ch + 4);
Wire.endTransmission(1);
delayMicroseconds(300);
break;
case 3:
Wire.beginTransmission(ADDR_PCA954X);
Wire.write((1 << _ch));
Wire.endTransmission(1);
delayMicroseconds(300);
break;
default:
break;
}
}
void MatrixLaser::i2cWriteData(LaserRegType reg, uint8_t data){
Wire.beginTransmission(MatrixLaser_ADDR);
Wire.write(reg);
Wire.write(data);
Wire.endTransmission(1);
}