From e43d297e752f8836ac44564e8f6947179de1da2c Mon Sep 17 00:00:00 2001 From: MuffinsaursRex Date: Sat, 16 May 2015 19:04:06 +0200 Subject: [PATCH 1/5] Added 32bit LSM303 library to work with Teensy3.0/3.1 and possibly other 32bit processors. Fixed scope conflict causing a crashing bug when calling the breath() function. --- .../Adafruit_LSM303_Old_32bit.cpp | 114 ++++++++++++++++ .../Adafruit_LSM303_Old_32bit.h | 124 ++++++++++++++++++ Adafruit_LSM303_Old_32bit/README.md | 21 +++ .../examples/Test/Test.ino | 28 ++++ 4 files changed, 287 insertions(+) create mode 100644 Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.cpp create mode 100644 Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.h create mode 100644 Adafruit_LSM303_Old_32bit/README.md create mode 100644 Adafruit_LSM303_Old_32bit/examples/Test/Test.ino diff --git a/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.cpp b/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.cpp new file mode 100644 index 0000000..7dd7d17 --- /dev/null +++ b/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.cpp @@ -0,0 +1,114 @@ +/*************************************************************************** + This is a library for the LSM303 Accelerometer and magnentometer/compass + + Designed specifically to work with the Adafruit LSM303DLHC Breakout + + These displays use I2C to communicate, 2 pins are required to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ +#include + +/*************************************************************************** + CONSTRUCTOR + ***************************************************************************/ +bool Adafruit_LSM303_Old_32bit::begin() +{ + Wire.begin(); +Serial.println("Wire"); + + // 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_Old_32bit::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)(xlo | (xhi << 8)) >> 4; + accelData.y = (int16_t)(ylo | (yhi << 8)) >> 4; + accelData.z = (int16_t)(zlo | (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)(xlo | (xhi << 8)); + magData.y = (int16_t)(ylo | (yhi << 8)); + magData.z = (int16_t)(zlo | (zhi << 8)); + + // ToDo: Calculate orientation + magData.orientation = 0.0; +} + +void Adafruit_LSM303_Old_32bit::setMagGain(lsm303MagGain gain) +{ + write8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_CRB_REG_M, (byte)gain); +} + +/*************************************************************************** + PRIVATE FUNCTIONS + ***************************************************************************/ +void Adafruit_LSM303_Old_32bit::write8(byte address, byte reg, byte value) +{ + Wire.beginTransmission(address); + Wire.write(reg); + Wire.write(value); + Wire.endTransmission(); +} + +byte Adafruit_LSM303_Old_32bit::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_Old_32bit/Adafruit_LSM303_Old_32bit.h b/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.h new file mode 100644 index 0000000..bc893cd --- /dev/null +++ b/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.h @@ -0,0 +1,124 @@ +/*************************************************************************** + This is a library for the LSM303 Accelerometer and magnentometer/compass + + Designed specifically to work with the Adafruit LSM303DLHC Breakout + + These displays use I2C to communicate, 2 pins are required to interface. + + Adafruit invests time and resources providing this open source code, + please support Adafruit andopen-source hardware by purchasing products + from Adafruit! + + Written by Kevin Townsend for Adafruit Industries. + BSD license, all text above must be included in any redistribution + ***************************************************************************/ +#ifndef __LSM303_H__ +#define __LSM303_H__ + +#if (ARDUINO >= 100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif +#include "Wire.h" + +#define LSM303_ADDRESS_ACCEL (0x32 >> 1) // 0011001x +#define LSM303_ADDRESS_MAG (0x3C >> 1) // 0011110x +#define LSM303_ID (0b11010100) + +class Adafruit_LSM303_Old_32bit +{ + public: + typedef enum + { // DEFAULT TYPE + LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20, // 00000111 rw + LSM303_REGISTER_ACCEL_CTRL_REG2_A = 0x21, // 00000000 rw + LSM303_REGISTER_ACCEL_CTRL_REG3_A = 0x22, // 00000000 rw + LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23, // 00000000 rw + LSM303_REGISTER_ACCEL_CTRL_REG5_A = 0x24, // 00000000 rw + LSM303_REGISTER_ACCEL_CTRL_REG6_A = 0x25, // 00000000 rw + LSM303_REGISTER_ACCEL_REFERENCE_A = 0x26, // 00000000 r + LSM303_REGISTER_ACCEL_STATUS_REG_A = 0x27, // 00000000 r + LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28, + LSM303_REGISTER_ACCEL_OUT_X_H_A = 0x29, + LSM303_REGISTER_ACCEL_OUT_Y_L_A = 0x2A, + LSM303_REGISTER_ACCEL_OUT_Y_H_A = 0x2B, + LSM303_REGISTER_ACCEL_OUT_Z_L_A = 0x2C, + LSM303_REGISTER_ACCEL_OUT_Z_H_A = 0x2D, + LSM303_REGISTER_ACCEL_FIFO_CTRL_REG_A = 0x2E, + LSM303_REGISTER_ACCEL_FIFO_SRC_REG_A = 0x2F, + LSM303_REGISTER_ACCEL_INT1_CFG_A = 0x30, + LSM303_REGISTER_ACCEL_INT1_SOURCE_A = 0x31, + LSM303_REGISTER_ACCEL_INT1_THS_A = 0x32, + LSM303_REGISTER_ACCEL_INT1_DURATION_A = 0x33, + LSM303_REGISTER_ACCEL_INT2_CFG_A = 0x34, + LSM303_REGISTER_ACCEL_INT2_SOURCE_A = 0x35, + LSM303_REGISTER_ACCEL_INT2_THS_A = 0x36, + LSM303_REGISTER_ACCEL_INT2_DURATION_A = 0x37, + LSM303_REGISTER_ACCEL_CLICK_CFG_A = 0x38, + LSM303_REGISTER_ACCEL_CLICK_SRC_A = 0x39, + LSM303_REGISTER_ACCEL_CLICK_THS_A = 0x3A, + LSM303_REGISTER_ACCEL_TIME_LIMIT_A = 0x3B, + LSM303_REGISTER_ACCEL_TIME_LATENCY_A = 0x3C, + LSM303_REGISTER_ACCEL_TIME_WINDOW_A = 0x3D + } lsm303AccelRegisters_t; + + typedef enum + { + LSM303_REGISTER_MAG_CRA_REG_M = 0x00, + LSM303_REGISTER_MAG_CRB_REG_M = 0x01, + LSM303_REGISTER_MAG_MR_REG_M = 0x02, + LSM303_REGISTER_MAG_OUT_X_H_M = 0x03, + LSM303_REGISTER_MAG_OUT_X_L_M = 0x04, + LSM303_REGISTER_MAG_OUT_Z_H_M = 0x05, + LSM303_REGISTER_MAG_OUT_Z_L_M = 0x06, + LSM303_REGISTER_MAG_OUT_Y_H_M = 0x07, + LSM303_REGISTER_MAG_OUT_Y_L_M = 0x08, + LSM303_REGISTER_MAG_SR_REG_Mg = 0x09, + LSM303_REGISTER_MAG_IRA_REG_M = 0x0A, + LSM303_REGISTER_MAG_IRB_REG_M = 0x0B, + LSM303_REGISTER_MAG_IRC_REG_M = 0x0C, + LSM303_REGISTER_MAG_TEMP_OUT_H_M = 0x31, + LSM303_REGISTER_MAG_TEMP_OUT_L_M = 0x32 + } lsm303MagRegisters_t; + + typedef enum + { + LSM303_MAGGAIN_1_3 = 0x20, // +/- 1.3 + LSM303_MAGGAIN_1_9 = 0x40, // +/- 1.9 + LSM303_MAGGAIN_2_5 = 0x60, // +/- 2.5 + LSM303_MAGGAIN_4_0 = 0x80, // +/- 4.0 + LSM303_MAGGAIN_4_7 = 0xA0, // +/- 4.7 + LSM303_MAGGAIN_5_6 = 0xC0, // +/- 5.6 + LSM303_MAGGAIN_8_1 = 0xE0 // +/- 8.1 + } lsm303MagGain; + + typedef struct lsm303AccelData_s + { + float x; + float y; + float z; + } lsm303AccelData; + + typedef struct lsm303MagData_s + { + float x; + float y; + float z; + float orientation; + } lsm303MagData; + + 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); + + private: +}; + +#endif diff --git a/Adafruit_LSM303_Old_32bit/README.md b/Adafruit_LSM303_Old_32bit/README.md new file mode 100644 index 0000000..e07bbd5 --- /dev/null +++ b/Adafruit_LSM303_Old_32bit/README.md @@ -0,0 +1,21 @@ +Adafruit_LSM303 +=============== + +This is a library for the Adafruit triple-axis accelerometer/magnetometer LSM303DLHC breakout + +Designed specifically to work with the Adafruit LSM303 Breakout + ----> https://www.adafruit.com/products/1120 + +These displays use I2C to communicate, 2 pins are required to interface +Adafruit invests time and resources providing this open source code, +please support Adafruit and open-source hardware by purchasing +products from Adafruit! + +Check out the links above for our tutorials and wiring diagrams + +Written by Kevin Townsend for Adafruit Industries. +BSD license, all text above must be included in any redistribution + +To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_LSM303. Check that the Adafruit_LSM303 folder contains Adafruit_LSM303.cpp and Adafruit_LSM303.h + +Place the Adafruit_LSM303 library folder your *arduinosketchfolder*/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. diff --git a/Adafruit_LSM303_Old_32bit/examples/Test/Test.ino b/Adafruit_LSM303_Old_32bit/examples/Test/Test.ino new file mode 100644 index 0000000..2fe0914 --- /dev/null +++ b/Adafruit_LSM303_Old_32bit/examples/Test/Test.ino @@ -0,0 +1,28 @@ +#include +#include + +Adafruit_LSM303 lsm; + +void setup() +{ + 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("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" "); + Serial.print("Y: "); Serial.print((int)lsm.accelData.y); Serial.print(" "); + Serial.print("Z: "); Serial.println((int)lsm.accelData.z); Serial.print(" "); + Serial.print("Mag X: "); Serial.print((int)lsm.magData.x); Serial.print(" "); + Serial.print("Y: "); Serial.print((int)lsm.magData.y); Serial.print(" "); + Serial.print("Z: "); Serial.println((int)lsm.magData.z); Serial.print(" "); + delay(1000); +} From 12033e62a92ef8c7295f9383f90dd39aad8e59cd Mon Sep 17 00:00:00 2001 From: Jason Poindexter Date: Wed, 11 Oct 2017 12:13:54 -0700 Subject: [PATCH 2/5] Updated LSM303 Library Included new libraries that support 32bit processors and the LSM303. Verified working on ESP8266 --- Adafruit_LSM303DLHC | 1 + Adafruit_LSM303_Old/Adafruit_LSM303_Old.cpp | 114 ---------------- Adafruit_LSM303_Old/Adafruit_LSM303_Old.h | 124 ------------------ Adafruit_LSM303_Old/README.md | 21 --- Adafruit_LSM303_Old/examples/Test/Test.ino | 28 ---- .../Adafruit_LSM303_Old_32bit.cpp | 114 ---------------- .../Adafruit_LSM303_Old_32bit.h | 124 ------------------ Adafruit_LSM303_Old_32bit/README.md | 21 --- .../examples/Test/Test.ino | 28 ---- Adafruit_Sensor | 1 + hermes/hermes.ino | 16 ++- 11 files changed, 15 insertions(+), 577 deletions(-) create mode 160000 Adafruit_LSM303DLHC delete mode 100644 Adafruit_LSM303_Old/Adafruit_LSM303_Old.cpp delete mode 100644 Adafruit_LSM303_Old/Adafruit_LSM303_Old.h delete mode 100644 Adafruit_LSM303_Old/README.md delete mode 100644 Adafruit_LSM303_Old/examples/Test/Test.ino delete mode 100644 Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.cpp delete mode 100644 Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.h delete mode 100644 Adafruit_LSM303_Old_32bit/README.md delete mode 100644 Adafruit_LSM303_Old_32bit/examples/Test/Test.ino create mode 160000 Adafruit_Sensor diff --git a/Adafruit_LSM303DLHC b/Adafruit_LSM303DLHC new file mode 160000 index 0000000..3adbf0e --- /dev/null +++ b/Adafruit_LSM303DLHC @@ -0,0 +1 @@ +Subproject commit 3adbf0e579498c4caa23ed8562e09d2cc89d7fde diff --git a/Adafruit_LSM303_Old/Adafruit_LSM303_Old.cpp b/Adafruit_LSM303_Old/Adafruit_LSM303_Old.cpp deleted file mode 100644 index 40da296..0000000 --- a/Adafruit_LSM303_Old/Adafruit_LSM303_Old.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/*************************************************************************** - This is a library for the LSM303 Accelerometer and magnentometer/compass - - Designed specifically to work with the Adafruit LSM303DLHC Breakout - - These displays use I2C to communicate, 2 pins are required to interface. - - Adafruit invests time and resources providing this open source code, - please support Adafruit andopen-source hardware by purchasing products - from Adafruit! - - Written by Kevin Townsend for Adafruit Industries. - BSD license, all text above must be included in any redistribution - ***************************************************************************/ -#include - -/*************************************************************************** - CONSTRUCTOR - ***************************************************************************/ -bool Adafruit_LSM303_Old::begin() -{ - Wire.begin(); -Serial.println("Wire"); - - // 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_Old::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 = (xlo | (xhi << 8)) >> 4; - accelData.y = (ylo | (yhi << 8)) >> 4; - accelData.z = (zlo | (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 = (xlo | (xhi << 8)); - magData.y = (ylo | (yhi << 8)); - magData.z = (zlo | (zhi << 8)); - - // ToDo: Calculate orientation - magData.orientation = 0.0; -} - -void Adafruit_LSM303_Old::setMagGain(lsm303MagGain gain) -{ - write8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_CRB_REG_M, (byte)gain); -} - -/*************************************************************************** - PRIVATE FUNCTIONS - ***************************************************************************/ -void Adafruit_LSM303_Old::write8(byte address, byte reg, byte value) -{ - Wire.beginTransmission(address); - Wire.write(reg); - Wire.write(value); - Wire.endTransmission(); -} - -byte Adafruit_LSM303_Old::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_Old/Adafruit_LSM303_Old.h b/Adafruit_LSM303_Old/Adafruit_LSM303_Old.h deleted file mode 100644 index 3f5b111..0000000 --- a/Adafruit_LSM303_Old/Adafruit_LSM303_Old.h +++ /dev/null @@ -1,124 +0,0 @@ -/*************************************************************************** - This is a library for the LSM303 Accelerometer and magnentometer/compass - - Designed specifically to work with the Adafruit LSM303DLHC Breakout - - These displays use I2C to communicate, 2 pins are required to interface. - - Adafruit invests time and resources providing this open source code, - please support Adafruit andopen-source hardware by purchasing products - from Adafruit! - - Written by Kevin Townsend for Adafruit Industries. - BSD license, all text above must be included in any redistribution - ***************************************************************************/ -#ifndef __LSM303_H__ -#define __LSM303_H__ - -#if (ARDUINO >= 100) - #include "Arduino.h" -#else - #include "WProgram.h" -#endif -#include "Wire.h" - -#define LSM303_ADDRESS_ACCEL (0x32 >> 1) // 0011001x -#define LSM303_ADDRESS_MAG (0x3C >> 1) // 0011110x -#define LSM303_ID (0b11010100) - -class Adafruit_LSM303_Old -{ - public: - typedef enum - { // DEFAULT TYPE - LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20, // 00000111 rw - LSM303_REGISTER_ACCEL_CTRL_REG2_A = 0x21, // 00000000 rw - LSM303_REGISTER_ACCEL_CTRL_REG3_A = 0x22, // 00000000 rw - LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23, // 00000000 rw - LSM303_REGISTER_ACCEL_CTRL_REG5_A = 0x24, // 00000000 rw - LSM303_REGISTER_ACCEL_CTRL_REG6_A = 0x25, // 00000000 rw - LSM303_REGISTER_ACCEL_REFERENCE_A = 0x26, // 00000000 r - LSM303_REGISTER_ACCEL_STATUS_REG_A = 0x27, // 00000000 r - LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28, - LSM303_REGISTER_ACCEL_OUT_X_H_A = 0x29, - LSM303_REGISTER_ACCEL_OUT_Y_L_A = 0x2A, - LSM303_REGISTER_ACCEL_OUT_Y_H_A = 0x2B, - LSM303_REGISTER_ACCEL_OUT_Z_L_A = 0x2C, - LSM303_REGISTER_ACCEL_OUT_Z_H_A = 0x2D, - LSM303_REGISTER_ACCEL_FIFO_CTRL_REG_A = 0x2E, - LSM303_REGISTER_ACCEL_FIFO_SRC_REG_A = 0x2F, - LSM303_REGISTER_ACCEL_INT1_CFG_A = 0x30, - LSM303_REGISTER_ACCEL_INT1_SOURCE_A = 0x31, - LSM303_REGISTER_ACCEL_INT1_THS_A = 0x32, - LSM303_REGISTER_ACCEL_INT1_DURATION_A = 0x33, - LSM303_REGISTER_ACCEL_INT2_CFG_A = 0x34, - LSM303_REGISTER_ACCEL_INT2_SOURCE_A = 0x35, - LSM303_REGISTER_ACCEL_INT2_THS_A = 0x36, - LSM303_REGISTER_ACCEL_INT2_DURATION_A = 0x37, - LSM303_REGISTER_ACCEL_CLICK_CFG_A = 0x38, - LSM303_REGISTER_ACCEL_CLICK_SRC_A = 0x39, - LSM303_REGISTER_ACCEL_CLICK_THS_A = 0x3A, - LSM303_REGISTER_ACCEL_TIME_LIMIT_A = 0x3B, - LSM303_REGISTER_ACCEL_TIME_LATENCY_A = 0x3C, - LSM303_REGISTER_ACCEL_TIME_WINDOW_A = 0x3D - } lsm303AccelRegisters_t; - - typedef enum - { - LSM303_REGISTER_MAG_CRA_REG_M = 0x00, - LSM303_REGISTER_MAG_CRB_REG_M = 0x01, - LSM303_REGISTER_MAG_MR_REG_M = 0x02, - LSM303_REGISTER_MAG_OUT_X_H_M = 0x03, - LSM303_REGISTER_MAG_OUT_X_L_M = 0x04, - LSM303_REGISTER_MAG_OUT_Z_H_M = 0x05, - LSM303_REGISTER_MAG_OUT_Z_L_M = 0x06, - LSM303_REGISTER_MAG_OUT_Y_H_M = 0x07, - LSM303_REGISTER_MAG_OUT_Y_L_M = 0x08, - LSM303_REGISTER_MAG_SR_REG_Mg = 0x09, - LSM303_REGISTER_MAG_IRA_REG_M = 0x0A, - LSM303_REGISTER_MAG_IRB_REG_M = 0x0B, - LSM303_REGISTER_MAG_IRC_REG_M = 0x0C, - LSM303_REGISTER_MAG_TEMP_OUT_H_M = 0x31, - LSM303_REGISTER_MAG_TEMP_OUT_L_M = 0x32 - } lsm303MagRegisters_t; - - typedef enum - { - LSM303_MAGGAIN_1_3 = 0x20, // +/- 1.3 - LSM303_MAGGAIN_1_9 = 0x40, // +/- 1.9 - LSM303_MAGGAIN_2_5 = 0x60, // +/- 2.5 - LSM303_MAGGAIN_4_0 = 0x80, // +/- 4.0 - LSM303_MAGGAIN_4_7 = 0xA0, // +/- 4.7 - LSM303_MAGGAIN_5_6 = 0xC0, // +/- 5.6 - LSM303_MAGGAIN_8_1 = 0xE0 // +/- 8.1 - } lsm303MagGain; - - typedef struct lsm303AccelData_s - { - float x; - float y; - float z; - } lsm303AccelData; - - typedef struct lsm303MagData_s - { - float x; - float y; - float z; - float orientation; - } lsm303MagData; - - 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); - - private: -}; - -#endif diff --git a/Adafruit_LSM303_Old/README.md b/Adafruit_LSM303_Old/README.md deleted file mode 100644 index e07bbd5..0000000 --- a/Adafruit_LSM303_Old/README.md +++ /dev/null @@ -1,21 +0,0 @@ -Adafruit_LSM303 -=============== - -This is a library for the Adafruit triple-axis accelerometer/magnetometer LSM303DLHC breakout - -Designed specifically to work with the Adafruit LSM303 Breakout - ----> https://www.adafruit.com/products/1120 - -These displays use I2C to communicate, 2 pins are required to interface -Adafruit invests time and resources providing this open source code, -please support Adafruit and open-source hardware by purchasing -products from Adafruit! - -Check out the links above for our tutorials and wiring diagrams - -Written by Kevin Townsend for Adafruit Industries. -BSD license, all text above must be included in any redistribution - -To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_LSM303. Check that the Adafruit_LSM303 folder contains Adafruit_LSM303.cpp and Adafruit_LSM303.h - -Place the Adafruit_LSM303 library folder your *arduinosketchfolder*/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. diff --git a/Adafruit_LSM303_Old/examples/Test/Test.ino b/Adafruit_LSM303_Old/examples/Test/Test.ino deleted file mode 100644 index 2fe0914..0000000 --- a/Adafruit_LSM303_Old/examples/Test/Test.ino +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -Adafruit_LSM303 lsm; - -void setup() -{ - 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("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" "); - Serial.print("Y: "); Serial.print((int)lsm.accelData.y); Serial.print(" "); - Serial.print("Z: "); Serial.println((int)lsm.accelData.z); Serial.print(" "); - Serial.print("Mag X: "); Serial.print((int)lsm.magData.x); Serial.print(" "); - Serial.print("Y: "); Serial.print((int)lsm.magData.y); Serial.print(" "); - Serial.print("Z: "); Serial.println((int)lsm.magData.z); Serial.print(" "); - delay(1000); -} diff --git a/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.cpp b/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.cpp deleted file mode 100644 index 7dd7d17..0000000 --- a/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/*************************************************************************** - This is a library for the LSM303 Accelerometer and magnentometer/compass - - Designed specifically to work with the Adafruit LSM303DLHC Breakout - - These displays use I2C to communicate, 2 pins are required to interface. - - Adafruit invests time and resources providing this open source code, - please support Adafruit andopen-source hardware by purchasing products - from Adafruit! - - Written by Kevin Townsend for Adafruit Industries. - BSD license, all text above must be included in any redistribution - ***************************************************************************/ -#include - -/*************************************************************************** - CONSTRUCTOR - ***************************************************************************/ -bool Adafruit_LSM303_Old_32bit::begin() -{ - Wire.begin(); -Serial.println("Wire"); - - // 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_Old_32bit::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)(xlo | (xhi << 8)) >> 4; - accelData.y = (int16_t)(ylo | (yhi << 8)) >> 4; - accelData.z = (int16_t)(zlo | (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)(xlo | (xhi << 8)); - magData.y = (int16_t)(ylo | (yhi << 8)); - magData.z = (int16_t)(zlo | (zhi << 8)); - - // ToDo: Calculate orientation - magData.orientation = 0.0; -} - -void Adafruit_LSM303_Old_32bit::setMagGain(lsm303MagGain gain) -{ - write8(LSM303_ADDRESS_MAG, LSM303_REGISTER_MAG_CRB_REG_M, (byte)gain); -} - -/*************************************************************************** - PRIVATE FUNCTIONS - ***************************************************************************/ -void Adafruit_LSM303_Old_32bit::write8(byte address, byte reg, byte value) -{ - Wire.beginTransmission(address); - Wire.write(reg); - Wire.write(value); - Wire.endTransmission(); -} - -byte Adafruit_LSM303_Old_32bit::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_Old_32bit/Adafruit_LSM303_Old_32bit.h b/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.h deleted file mode 100644 index bc893cd..0000000 --- a/Adafruit_LSM303_Old_32bit/Adafruit_LSM303_Old_32bit.h +++ /dev/null @@ -1,124 +0,0 @@ -/*************************************************************************** - This is a library for the LSM303 Accelerometer and magnentometer/compass - - Designed specifically to work with the Adafruit LSM303DLHC Breakout - - These displays use I2C to communicate, 2 pins are required to interface. - - Adafruit invests time and resources providing this open source code, - please support Adafruit andopen-source hardware by purchasing products - from Adafruit! - - Written by Kevin Townsend for Adafruit Industries. - BSD license, all text above must be included in any redistribution - ***************************************************************************/ -#ifndef __LSM303_H__ -#define __LSM303_H__ - -#if (ARDUINO >= 100) - #include "Arduino.h" -#else - #include "WProgram.h" -#endif -#include "Wire.h" - -#define LSM303_ADDRESS_ACCEL (0x32 >> 1) // 0011001x -#define LSM303_ADDRESS_MAG (0x3C >> 1) // 0011110x -#define LSM303_ID (0b11010100) - -class Adafruit_LSM303_Old_32bit -{ - public: - typedef enum - { // DEFAULT TYPE - LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20, // 00000111 rw - LSM303_REGISTER_ACCEL_CTRL_REG2_A = 0x21, // 00000000 rw - LSM303_REGISTER_ACCEL_CTRL_REG3_A = 0x22, // 00000000 rw - LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23, // 00000000 rw - LSM303_REGISTER_ACCEL_CTRL_REG5_A = 0x24, // 00000000 rw - LSM303_REGISTER_ACCEL_CTRL_REG6_A = 0x25, // 00000000 rw - LSM303_REGISTER_ACCEL_REFERENCE_A = 0x26, // 00000000 r - LSM303_REGISTER_ACCEL_STATUS_REG_A = 0x27, // 00000000 r - LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28, - LSM303_REGISTER_ACCEL_OUT_X_H_A = 0x29, - LSM303_REGISTER_ACCEL_OUT_Y_L_A = 0x2A, - LSM303_REGISTER_ACCEL_OUT_Y_H_A = 0x2B, - LSM303_REGISTER_ACCEL_OUT_Z_L_A = 0x2C, - LSM303_REGISTER_ACCEL_OUT_Z_H_A = 0x2D, - LSM303_REGISTER_ACCEL_FIFO_CTRL_REG_A = 0x2E, - LSM303_REGISTER_ACCEL_FIFO_SRC_REG_A = 0x2F, - LSM303_REGISTER_ACCEL_INT1_CFG_A = 0x30, - LSM303_REGISTER_ACCEL_INT1_SOURCE_A = 0x31, - LSM303_REGISTER_ACCEL_INT1_THS_A = 0x32, - LSM303_REGISTER_ACCEL_INT1_DURATION_A = 0x33, - LSM303_REGISTER_ACCEL_INT2_CFG_A = 0x34, - LSM303_REGISTER_ACCEL_INT2_SOURCE_A = 0x35, - LSM303_REGISTER_ACCEL_INT2_THS_A = 0x36, - LSM303_REGISTER_ACCEL_INT2_DURATION_A = 0x37, - LSM303_REGISTER_ACCEL_CLICK_CFG_A = 0x38, - LSM303_REGISTER_ACCEL_CLICK_SRC_A = 0x39, - LSM303_REGISTER_ACCEL_CLICK_THS_A = 0x3A, - LSM303_REGISTER_ACCEL_TIME_LIMIT_A = 0x3B, - LSM303_REGISTER_ACCEL_TIME_LATENCY_A = 0x3C, - LSM303_REGISTER_ACCEL_TIME_WINDOW_A = 0x3D - } lsm303AccelRegisters_t; - - typedef enum - { - LSM303_REGISTER_MAG_CRA_REG_M = 0x00, - LSM303_REGISTER_MAG_CRB_REG_M = 0x01, - LSM303_REGISTER_MAG_MR_REG_M = 0x02, - LSM303_REGISTER_MAG_OUT_X_H_M = 0x03, - LSM303_REGISTER_MAG_OUT_X_L_M = 0x04, - LSM303_REGISTER_MAG_OUT_Z_H_M = 0x05, - LSM303_REGISTER_MAG_OUT_Z_L_M = 0x06, - LSM303_REGISTER_MAG_OUT_Y_H_M = 0x07, - LSM303_REGISTER_MAG_OUT_Y_L_M = 0x08, - LSM303_REGISTER_MAG_SR_REG_Mg = 0x09, - LSM303_REGISTER_MAG_IRA_REG_M = 0x0A, - LSM303_REGISTER_MAG_IRB_REG_M = 0x0B, - LSM303_REGISTER_MAG_IRC_REG_M = 0x0C, - LSM303_REGISTER_MAG_TEMP_OUT_H_M = 0x31, - LSM303_REGISTER_MAG_TEMP_OUT_L_M = 0x32 - } lsm303MagRegisters_t; - - typedef enum - { - LSM303_MAGGAIN_1_3 = 0x20, // +/- 1.3 - LSM303_MAGGAIN_1_9 = 0x40, // +/- 1.9 - LSM303_MAGGAIN_2_5 = 0x60, // +/- 2.5 - LSM303_MAGGAIN_4_0 = 0x80, // +/- 4.0 - LSM303_MAGGAIN_4_7 = 0xA0, // +/- 4.7 - LSM303_MAGGAIN_5_6 = 0xC0, // +/- 5.6 - LSM303_MAGGAIN_8_1 = 0xE0 // +/- 8.1 - } lsm303MagGain; - - typedef struct lsm303AccelData_s - { - float x; - float y; - float z; - } lsm303AccelData; - - typedef struct lsm303MagData_s - { - float x; - float y; - float z; - float orientation; - } lsm303MagData; - - 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); - - private: -}; - -#endif diff --git a/Adafruit_LSM303_Old_32bit/README.md b/Adafruit_LSM303_Old_32bit/README.md deleted file mode 100644 index e07bbd5..0000000 --- a/Adafruit_LSM303_Old_32bit/README.md +++ /dev/null @@ -1,21 +0,0 @@ -Adafruit_LSM303 -=============== - -This is a library for the Adafruit triple-axis accelerometer/magnetometer LSM303DLHC breakout - -Designed specifically to work with the Adafruit LSM303 Breakout - ----> https://www.adafruit.com/products/1120 - -These displays use I2C to communicate, 2 pins are required to interface -Adafruit invests time and resources providing this open source code, -please support Adafruit and open-source hardware by purchasing -products from Adafruit! - -Check out the links above for our tutorials and wiring diagrams - -Written by Kevin Townsend for Adafruit Industries. -BSD license, all text above must be included in any redistribution - -To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder Adafruit_LSM303. Check that the Adafruit_LSM303 folder contains Adafruit_LSM303.cpp and Adafruit_LSM303.h - -Place the Adafruit_LSM303 library folder your *arduinosketchfolder*/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. diff --git a/Adafruit_LSM303_Old_32bit/examples/Test/Test.ino b/Adafruit_LSM303_Old_32bit/examples/Test/Test.ino deleted file mode 100644 index 2fe0914..0000000 --- a/Adafruit_LSM303_Old_32bit/examples/Test/Test.ino +++ /dev/null @@ -1,28 +0,0 @@ -#include -#include - -Adafruit_LSM303 lsm; - -void setup() -{ - 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("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" "); - Serial.print("Y: "); Serial.print((int)lsm.accelData.y); Serial.print(" "); - Serial.print("Z: "); Serial.println((int)lsm.accelData.z); Serial.print(" "); - Serial.print("Mag X: "); Serial.print((int)lsm.magData.x); Serial.print(" "); - Serial.print("Y: "); Serial.print((int)lsm.magData.y); Serial.print(" "); - Serial.print("Z: "); Serial.println((int)lsm.magData.z); Serial.print(" "); - delay(1000); -} diff --git a/Adafruit_Sensor b/Adafruit_Sensor new file mode 160000 index 0000000..e985f22 --- /dev/null +++ b/Adafruit_Sensor @@ -0,0 +1 @@ +Subproject commit e985f2253a687ef377cde3dcfb1f788830d1bc09 diff --git a/hermes/hermes.ino b/hermes/hermes.ino index 5476444..51b3e34 100644 --- a/hermes/hermes.ino +++ b/hermes/hermes.ino @@ -47,7 +47,8 @@ // Accel imports. #include -#include +#include +#include // Our custom data type. #include "AccelReading.h" @@ -133,7 +134,7 @@ void pauseOnKeystroke() { // accel // /////////// -Adafruit_LSM303_Old lsm; // Bridge to accelerometer hardware. +Adafruit_LSM303 lsm; // Bridge to accelerometer hardware. AccelReading accelBuffer[10]; // Buffer for storing the last 10 readings. int bufferPosition; // Current read position of the buffer. @@ -147,8 +148,17 @@ unsigned long lastSignificantMovementTime; // Initialization. void accelSetup() { Serial.println("BEGIN"); + + + // Try to initialise and warn if we couldn't detect the chip + digitalWrite(ONBOARD_LED_PIN, HIGH); + if (!lsm.begin()) + { + Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); + while (1); + } + digitalWrite(ONBOARD_LED_PIN, LOW); - lsm.begin(); bufferPosition = 0; From 19d8ad0669db3aac8ca05c5a194b25e9a29a1bc8 Mon Sep 17 00:00:00 2001 From: Jason Poindexter Date: Wed, 11 Oct 2017 12:22:26 -0700 Subject: [PATCH 3/5] Fixed breathe() crashing due to stip scope --- hermes/hermes.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hermes/hermes.ino b/hermes/hermes.ino index 51b3e34..483478d 100644 --- a/hermes/hermes.ino +++ b/hermes/hermes.ino @@ -396,7 +396,7 @@ void updateLED() { //showColor(scale); if (sleep()) { - breathe(strip); + breathe(); } else { crawlColor(pixelColor); } @@ -646,7 +646,7 @@ void resetBreathe() { keyframePointer = 0; } -void breathe(Adafruit_NeoPixel strip) { +void breathe() { int numKeyframes = sizeof(KEYFRAMES) - 1; float period = SLEEP_CYCLE_MS / numKeyframes; unsigned long now = millis(); From 53afcc019338b8ef3e4a7e724a0aef5de5c0a947 Mon Sep 17 00:00:00 2001 From: Jason Poindexter Date: Wed, 11 Oct 2017 13:59:29 -0700 Subject: [PATCH 4/5] Update hermes.ino --- hermes/hermes.ino | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/hermes/hermes.ino b/hermes/hermes.ino index 483478d..961c468 100644 --- a/hermes/hermes.ino +++ b/hermes/hermes.ino @@ -151,15 +151,9 @@ void accelSetup() { // Try to initialise and warn if we couldn't detect the chip - digitalWrite(ONBOARD_LED_PIN, HIGH); - if (!lsm.begin()) - { - Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!"); - while (1); + if (!lsm.begin()) { + Serial.println("Unable to initialize the LSM303. Check your wiring!"); } - digitalWrite(ONBOARD_LED_PIN, LOW); - - bufferPosition = 0; // Initialize the full buffer to zero. From bf7655322a507ffcba193acea0845fb602dc28ae Mon Sep 17 00:00:00 2001 From: Jason Poindexter Date: Sat, 28 Jul 2018 17:52:22 -0700 Subject: [PATCH 5/5] Constrain color scale so we dont get 0 as LED color --- hermes/hermes.ino | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/hermes/hermes.ino b/hermes/hermes.ino index 961c468..5a6789c 100644 --- a/hermes/hermes.ino +++ b/hermes/hermes.ino @@ -21,7 +21,7 @@ // Raising this raises the vector magnitude needed to reach max (purple), // and thus lowers sensitivity. // Eg: 800 = more sensitive, 1600 = less sensitive -#define HERMES_SENSITIVITY 1600.0 +#define HERMES_SENSITIVITY 3200.0 // Emulate two strips by starting the crawl in the // middle of the strip and crawling both ways. #define ENABLE_SPLIT_STRIP 1 @@ -47,10 +47,7 @@ // Accel imports. #include -#include -#include - -// Our custom data type. +#include #include "AccelReading.h" void setup() { @@ -134,7 +131,7 @@ void pauseOnKeystroke() { // accel // /////////// -Adafruit_LSM303 lsm; // Bridge to accelerometer hardware. +Adafruit_LSM303_Old lsm; // Bridge to accelerometer hardware. AccelReading accelBuffer[10]; // Buffer for storing the last 10 readings. int bufferPosition; // Current read position of the buffer. @@ -355,7 +352,7 @@ bool equalReadings(AccelReading a, AccelReading b) { // color // /////////// -int COLOR_RANGE = 384; +int COLOR_RANGE = 383; uint32_t lastColor; unsigned long lastCrawl; uint32_t lightArray[LED_COUNT]; @@ -485,6 +482,7 @@ void showColor(float scale) { // Takes a scale, from 0.0 to 1.0, indicating progression // through the color rainbow. uint32_t pixelColorForScale(double scale) { + scale = max(min(scale, 1), 0); float brightness = MAX_BRIGHTNESS * (scale + MIN_BRIGHTNESS); int c = COLOR_RANGE * scale; // Intentionally round to an int.