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
|
#ifndef LSM303DLM_H
#define LSM303DLM_H
#include <i2c/i2c.h>
class LSM303DLM_A {
private:
I2C& i2c;
public:
int16_t x, y, z;
LSM303DLM_A(I2C& i2c_bus) : i2c(i2c_bus) {}
void init() {
i2c.write_reg(0x19, 0x20, 0x27); // Normal mode, 50 Hz.
}
void update() {
uint8_t buf[6];
i2c.read_reg(0x19, 0xa8, 6, buf);
x = (buf[1] << 8 | buf[0]) - 0;
y = (buf[3] << 8 | buf[2]) - 0;
z = (buf[5] << 8 | buf[4]) - 0;
}
};
class LSM303DLM_M {
private:
I2C& i2c;
public:
int16_t x, y, z;
LSM303DLM_M(I2C& i2c_bus) : i2c(i2c_bus) {}
void init() {
i2c.write_reg(0x1e, 0x02, 0x00); // Continous operation mode.
}
void update() {
uint8_t buf[6];
i2c.read_reg(0x1e, 0x03, 6, buf);
x = (buf[0] << 8 | buf[1]) - 0;
y = (buf[2] << 8 | buf[3]) - 0;
z = (buf[4] << 8 | buf[5]) - 0;
}
};
#endif
|