From 5cfc464eb5da978b0e839327d655f94df26983f5 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sun, 22 Apr 2012 23:11:45 +0200 Subject: Added LSM303DLM driver. --- drivers/lsm303dlm.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 drivers/lsm303dlm.h (limited to 'drivers') 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 -- cgit v1.2.3