From 3adbf0e579498c4caa23ed8562e09d2cc89d7fde Mon Sep 17 00:00:00 2001 From: microbuilder Date: Tue, 11 Oct 2016 09:37:38 +0200 Subject: [PATCH] Merged original and unified libs --- Adafruit_LSM303.h | 11 ++++ Adafruit_LSM303_U.cpp | 102 ++++++++++++++++++++++++++++++ Adafruit_LSM303_U.h | 15 +++++ examples/deprecated/Test/Test.ino | 32 ++++++++++ library.properties | 2 +- 5 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 Adafruit_LSM303.h create mode 100644 examples/deprecated/Test/Test.ino diff --git a/Adafruit_LSM303.h b/Adafruit_LSM303.h new file mode 100644 index 0000000..3e6f2f6 --- /dev/null +++ b/Adafruit_LSM303.h @@ -0,0 +1,11 @@ +// This file is provided for compatibility reasons with the deprecated +// (non-Unified) Adafruit_LSM303 library. Any new development should use +// the Adafruit_LSM303_U.h header, this file is provided to try to minimize +// issues with projects that depend on the older driver. + +#ifndef __ADAFRUIT_LSM303_H__ +#define __ADAFRUIT_LSM303_H__ + +#include "Adafruit_LSM303_U.h" + +#endif // __ADAFRUIT_LSM303_H__ diff --git a/Adafruit_LSM303_U.cpp b/Adafruit_LSM303_U.cpp index 65cc5bd..26c9202 100644 --- a/Adafruit_LSM303_U.cpp +++ b/Adafruit_LSM303_U.cpp @@ -555,3 +555,105 @@ void Adafruit_LSM303_Mag_Unified::getSensor(sensor_t *sensor) { sensor->min_value = 0.0F; // TBD sensor->resolution = 0.0F; // TBD } + +/* --- The code below is no longer maintained and provided solely for */ +/* --- compatibility reasons! */ + +/*************************************************************************** + DEPRECATED (NON UNIFIED) DRIVER (Adafruit_LSM303.c/h) + ***************************************************************************/ + +/*************************************************************************** + CONSTRUCTOR + ***************************************************************************/ +bool Adafruit_LSM303::begin() +{ + Wire.begin(); + + // Enable the accelerometer + write8(LSM303_ADDRESS_ACCEL, LSM303_REGISTER_ACCEL_CTRL_REG1_A, 0x27); + + // Enable the magnetometer + write8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_MR_REG_M, 0x00); + + return true; +} + +/*************************************************************************** + PUBLIC FUNCTIONS + ***************************************************************************/ +void Adafruit_LSM303::read() +{ + // Read the accelerometer + Wire.beginTransmission((byte)LSM303_ADDRESS_ACCEL); + Wire.write(LSM303_REGISTER_ACCEL_OUT_X_L_A | 0x80); + Wire.endTransmission(); + Wire.requestFrom((byte)LSM303_ADDRESS_ACCEL, (byte)6); + + // Wait around until enough data is available + while (Wire.available() < 6); + + uint8_t xlo = Wire.read(); + uint8_t xhi = Wire.read(); + uint8_t ylo = Wire.read(); + uint8_t yhi = Wire.read(); + uint8_t zlo = Wire.read(); + uint8_t zhi = Wire.read(); + + // Shift values to create properly formed integer (low byte first) + accelData.x = (int16_t)((uint16_t)xlo | ((uint16_t)xhi << 8)) >> 4; + accelData.y = (int16_t)((uint16_t)ylo | ((uint16_t)yhi << 8)) >> 4; + accelData.z = (int16_t)((uint16_t)zlo | ((uint16_t)zhi << 8)) >> 4; + + // Read the magnetometer + Wire.beginTransmission((byte)LSM303_ADDRESS_MAG); + Wire.write(LSM303_REGISTER_MAG_OUT_X_H_M); + Wire.endTransmission(); + Wire.requestFrom((byte)LSM303_ADDRESS_MAG, (byte)6); + + // Wait around until enough data is available + while (Wire.available() < 6); + + // Note high before low (different than accel) + xhi = Wire.read(); + xlo = Wire.read(); + zhi = Wire.read(); + zlo = Wire.read(); + yhi = Wire.read(); + ylo = Wire.read(); + + // Shift values to create properly formed integer (low byte first) + magData.x = (int16_t)((uint16_t)xlo | ((uint16_t)xhi << 8)); + magData.y = (int16_t)((uint16_t)ylo | ((uint16_t)yhi << 8)); + magData.z = (int16_t)((uint16_t)zlo | ((uint16_t)zhi << 8)); +} + +void Adafruit_LSM303::setMagGain(lsm303MagGain gain) +{ + write8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_CRB_REG_M, (byte)gain); +} + +/*************************************************************************** + PRIVATE FUNCTIONS + ***************************************************************************/ +void Adafruit_LSM303::write8(byte address, byte reg, byte value) +{ + Wire.beginTransmission(address); + Wire.write(reg); + Wire.write(value); + Wire.endTransmission(); +} + +byte Adafruit_LSM303::read8(byte address, byte reg) +{ + byte value; + + Wire.beginTransmission(address); + Wire.write(reg); + Wire.endTransmission(); + Wire.requestFrom(address, (byte)1); + value = Wire.read(); + Wire.endTransmission(); + + return value; +} diff --git a/Adafruit_LSM303_U.h b/Adafruit_LSM303_U.h index 036a029..512671a 100644 --- a/Adafruit_LSM303_U.h +++ b/Adafruit_LSM303_U.h @@ -193,4 +193,19 @@ class Adafruit_LSM303_Mag_Unified : public Adafruit_Sensor void read(void); }; +/* Non Unified (old) driver for compatibility reasons */ +class Adafruit_LSM303 +{ + public: + bool begin(void); + void read(void); + void setMagGain(lsm303MagGain gain); + + lsm303AccelData accelData; // Last read accelerometer data will be available here + lsm303MagData magData; // Last read magnetometer data will be available here + + void write8(byte address, byte reg, byte value); + byte read8(byte address, byte reg); +}; + #endif diff --git a/examples/deprecated/Test/Test.ino b/examples/deprecated/Test/Test.ino new file mode 100644 index 0000000..da51ead --- /dev/null +++ b/examples/deprecated/Test/Test.ino @@ -0,0 +1,32 @@ +#include +#include +#include + +Adafruit_LSM303 lsm; + +void setup() +{ +#ifndef ESP8266 + while (!Serial); // will pause Zero, Leonardo, etc until serial console opens +#endif + Serial.begin(9600); + + // Try to initialise and warn if we couldn't detect the chip + if (!lsm.begin()) + { + Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); + while (1); + } +} + +void loop() +{ + lsm.read(); + Serial.print("AX: "); Serial.print((int)lsm.accelData.x); Serial.print(" "); + Serial.print("AY: "); Serial.print((int)lsm.accelData.y); Serial.print(" "); + Serial.print("AZ: "); Serial.print((int)lsm.accelData.z); Serial.print(" "); + Serial.print("MX: "); Serial.print((int)lsm.magData.x); Serial.print(" "); + Serial.print("MY: "); Serial.print((int)lsm.magData.y); Serial.print(" "); + Serial.print("MZ: "); Serial.println((int)lsm.magData.z); Serial.print(" "); + delay(100); +} diff --git a/library.properties b/library.properties index ae09d5d..627161b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit LSM303DLHC -version=1.0.2 +version=1.0.3 author=Adafruit maintainer=Adafruit sentence=Unified sensor driver for Adafruit's LSM303 Breakout (Accelerometer + Magnetometer)