Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
Merged original and unified libs
Browse files Browse the repository at this point in the history
  • Loading branch information
microbuilder committed Oct 11, 2016
1 parent 421141d commit 3adbf0e
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Adafruit_LSM303.h
Original file line number Diff line number Diff line change
@@ -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__
102 changes: 102 additions & 0 deletions Adafruit_LSM303_U.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions Adafruit_LSM303_U.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 32 additions & 0 deletions examples/deprecated/Test/Test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_LSM303.h>

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);
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit LSM303DLHC
version=1.0.2
version=1.0.3
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Unified sensor driver for Adafruit's LSM303 Breakout (Accelerometer + Magnetometer)
Expand Down

0 comments on commit 3adbf0e

Please sign in to comment.