From 271cf418418278f23976e2c009f792500d7fa597 Mon Sep 17 00:00:00 2001 From: Sebastian Reichel Date: Sun, 14 Oct 2012 05:51:55 +0200 Subject: fix ADXL345 driver --- main.vala | 4 ++-- sensors/accelerometer.vala | 2 +- sensors/accelerometer/adxl345.vala | 14 ++++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/main.vala b/main.vala index 79bfdc2..6cfe244 100644 --- a/main.vala +++ b/main.vala @@ -27,8 +27,8 @@ Kinematics kinematics; bool loop() { try { /* get sensor data */ - int16 gx, gy, gz; - uint16 ax, ay, az, mx, my, mz; + int16 gx, gy, gz, ax, ay, az; + uint16 mx, my, mz; gyroscope.get_data(out gx, out gy, out gz); accelerometer.get_data(out ax, out ay, out az); compass.get_data(out mx, out my, out mz); diff --git a/sensors/accelerometer.vala b/sensors/accelerometer.vala index d9563f9..b1f9607 100644 --- a/sensors/accelerometer.vala +++ b/sensors/accelerometer.vala @@ -15,5 +15,5 @@ public interface Accelerometer : Device { public abstract void init(KeyFile cfg) throws Error; - public abstract void get_data(out uint16 x, out uint16 y, out uint16 z) throws Error; + public abstract void get_data(out int16 x, out int16 y, out int16 z) throws Error; } diff --git a/sensors/accelerometer/adxl345.vala b/sensors/accelerometer/adxl345.vala index 199903e..4761178 100644 --- a/sensors/accelerometer/adxl345.vala +++ b/sensors/accelerometer/adxl345.vala @@ -16,6 +16,11 @@ public class ADXL345 : I2CDevice, Accelerometer { public ADXL345(uint8 dev, uint8 addr = 0x53) throws I2CError { setup(dev, addr); + + set_byte(0x2D, 0x00); /* reset */ + set_byte(0x31, 0x01); /* +/- 4g */ + set_byte(0x2C, 0x0b); /* 200hz */ + set_byte(0x2D, 0x18); /* mode: measurement + auto sleep */ } public void init(KeyFile cfg) throws KeyFileError, I2CError { @@ -42,10 +47,11 @@ public class ADXL345 : I2CDevice, Accelerometer { set_byte(0x20, z); } - public void get_data(out uint16 x, out uint16 y, out uint16 z) throws I2CError { - x = get_big_word(0x32); - y = get_big_word(0x34); - z = get_big_word(0x36); + public void get_data(out int16 x, out int16 y, out int16 z) throws I2CError { + var data = get_block(0x32, 6); + x = data[0] | data[1] << 8; + y = data[2] | data[3] << 8; + z = data[4] | data[5] << 8; } } -- cgit v1.2.3