summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--drivers/lsm303dlm.h52
1 files changed, 52 insertions, 0 deletions
diff --git a/drivers/lsm303dlm.h b/drivers/lsm303dlm.h
new file mode 100644
index 0000000..d58b31b
--- /dev/null
+++ b/drivers/lsm303dlm.h
@@ -0,0 +1,52 @@
+#ifndef LSM303DLM_H
+#define LSM303DLM_H
+
+#include "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(0x18, 0x20, 0x27); // Normal mode, 50 Hz.
+ }
+
+ void update() {
+ uint8_t buf[6];
+ i2c.read_reg(0x18, 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