forked from amperka/Troyka-IMU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathl3g4200d.cpp
137 lines (111 loc) · 2.35 KB
/
l3g4200d.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
#include <Arduino.h>
#include <Wire.h>
#include "l3g4200d.h"
#define CTRL_REG1 0x20
#define CTRL_REG1_PD (1 << 3)
#define CTRL_REG4 0x23
#define OUT_X 0x29
#define OUT_Y 0x2B
#define OUT_Z 0x2D
#define ADR_FS_250 0x00
#define ADR_FS_500 0x10
#define ADR_FS_2000 0x20
#define SENS_FS_250 0.00875
#define SENS_FS_500 0.0175
#define SENS_FS_2000 0.07
L3G4200D_TWI::L3G4200D_TWI(uint8_t addr)
{
_addr = addr;
_ctrlReg1 = 0x3; // default according to datasheet
}
void L3G4200D_TWI::setRange(uint8_t range)
{
switch (range) {
case RANGE_250: {
_ctrlReg4 = ADR_FS_250;
_mult = SENS_FS_250;
break;
}
case RANGE_500: {
_ctrlReg4 = ADR_FS_500;
_mult = SENS_FS_500;
break;
}
case RANGE_2000: {
_ctrlReg4 = ADR_FS_2000;
_mult = SENS_FS_2000;
break;
}
default: {
_mult = SENS_FS_250;
}
break;
}
writeCtrlReg4();
}
void L3G4200D_TWI::begin()
{
Wire.begin();
_mult = SENS_FS_250;
sleep(false);
}
void L3G4200D_TWI::sleep(bool enable)
{
if (enable)
_ctrlReg1 &= ~CTRL_REG1_PD;
else
_ctrlReg1 |= CTRL_REG1_PD;
writeCtrlReg1();
}
int16_t L3G4200D_TWI::readX()
{
return readAxis(OUT_X);
}
int16_t L3G4200D_TWI::readY()
{
return readAxis(OUT_Y);
}
int16_t L3G4200D_TWI::readZ()
{
return readAxis(OUT_Z);
}
float L3G4200D_TWI::readX_DegPerSec()
{
return readX()*_mult;
}
float L3G4200D_TWI::readY_DegPerSec()
{
return readY()*_mult;
}
float L3G4200D_TWI::readZ_DegPerSec()
{
return readZ()*_mult;
}
int16_t L3G4200D_TWI::readAxis(uint8_t reg)
{
return (int16_t)readByte(reg) << 8 | readByte(reg - 1);
}
uint8_t L3G4200D_TWI::readByte(uint8_t reg)
{
Wire.beginTransmission(_addr);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(_addr, 1u);
while (!Wire.available())
;
return Wire.read();
}
void L3G4200D_TWI::writeCtrlReg1()
{
Wire.beginTransmission(_addr);
Wire.write(CTRL_REG1);
Wire.write(_ctrlReg1);
Wire.endTransmission();
}
void L3G4200D_TWI::writeCtrlReg4()
{
Wire.beginTransmission(_addr);
Wire.write(CTRL_REG4);
Wire.write(_ctrlReg4);
Wire.endTransmission();
}