![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Port of Adafruit Arduino code
Fork of Adafruit9-DOf by
Header/Adafruit_LSM303_U.h@0:772bf4786416, 2015-03-21 (annotated)
- Committer:
- bmanga95
- Date:
- Sat Mar 21 12:33:05 2015 +0000
- Revision:
- 0:772bf4786416
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bmanga95 | 0:772bf4786416 | 1 | /*************************************************************************** |
bmanga95 | 0:772bf4786416 | 2 | This is a library for the LSM303 Accelerometer and magnentometer/compass |
bmanga95 | 0:772bf4786416 | 3 | |
bmanga95 | 0:772bf4786416 | 4 | Designed specifically to work with the Adafruit LSM303DLHC Breakout |
bmanga95 | 0:772bf4786416 | 5 | |
bmanga95 | 0:772bf4786416 | 6 | These displays use I2C to communicate, 2 pins are required to interface. |
bmanga95 | 0:772bf4786416 | 7 | |
bmanga95 | 0:772bf4786416 | 8 | Adafruit invests time and resources providing this open source code, |
bmanga95 | 0:772bf4786416 | 9 | please support Adafruit andopen-source hardware by purchasing products |
bmanga95 | 0:772bf4786416 | 10 | from Adafruit! |
bmanga95 | 0:772bf4786416 | 11 | |
bmanga95 | 0:772bf4786416 | 12 | Written by Kevin Townsend for Adafruit Industries. |
bmanga95 | 0:772bf4786416 | 13 | BSD license, all text above must be included in any redistribution |
bmanga95 | 0:772bf4786416 | 14 | ***************************************************************************/ |
bmanga95 | 0:772bf4786416 | 15 | #ifndef __LSM303_H__ |
bmanga95 | 0:772bf4786416 | 16 | #define __LSM303_H__ |
bmanga95 | 0:772bf4786416 | 17 | |
bmanga95 | 0:772bf4786416 | 18 | #include <Adafruit_Sensor.h> |
bmanga95 | 0:772bf4786416 | 19 | #include "I2C_base.h" |
bmanga95 | 0:772bf4786416 | 20 | |
bmanga95 | 0:772bf4786416 | 21 | /*========================================================================= |
bmanga95 | 0:772bf4786416 | 22 | I2C ADDRESS/BITS |
bmanga95 | 0:772bf4786416 | 23 | -----------------------------------------------------------------------*/ |
bmanga95 | 0:772bf4786416 | 24 | #define LSM303_ADDRESS_ACCEL (0x32 >> 1) // 0011001x |
bmanga95 | 0:772bf4786416 | 25 | #define LSM303_ADDRESS_MAG (0x3C >> 1) // 0011110x |
bmanga95 | 0:772bf4786416 | 26 | /*=========================================================================*/ |
bmanga95 | 0:772bf4786416 | 27 | |
bmanga95 | 0:772bf4786416 | 28 | /*========================================================================= |
bmanga95 | 0:772bf4786416 | 29 | REGISTERS |
bmanga95 | 0:772bf4786416 | 30 | -----------------------------------------------------------------------*/ |
bmanga95 | 0:772bf4786416 | 31 | typedef enum |
bmanga95 | 0:772bf4786416 | 32 | { // DEFAULT TYPE |
bmanga95 | 0:772bf4786416 | 33 | LSM303_REGISTER_ACCEL_CTRL_REG1_A = 0x20, // 00000111 rw |
bmanga95 | 0:772bf4786416 | 34 | LSM303_REGISTER_ACCEL_CTRL_REG2_A = 0x21, // 00000000 rw |
bmanga95 | 0:772bf4786416 | 35 | LSM303_REGISTER_ACCEL_CTRL_REG3_A = 0x22, // 00000000 rw |
bmanga95 | 0:772bf4786416 | 36 | LSM303_REGISTER_ACCEL_CTRL_REG4_A = 0x23, // 00000000 rw |
bmanga95 | 0:772bf4786416 | 37 | LSM303_REGISTER_ACCEL_CTRL_REG5_A = 0x24, // 00000000 rw |
bmanga95 | 0:772bf4786416 | 38 | LSM303_REGISTER_ACCEL_CTRL_REG6_A = 0x25, // 00000000 rw |
bmanga95 | 0:772bf4786416 | 39 | LSM303_REGISTER_ACCEL_REFERENCE_A = 0x26, // 00000000 r |
bmanga95 | 0:772bf4786416 | 40 | LSM303_REGISTER_ACCEL_STATUS_REG_A = 0x27, // 00000000 r |
bmanga95 | 0:772bf4786416 | 41 | LSM303_REGISTER_ACCEL_OUT_X_L_A = 0x28, |
bmanga95 | 0:772bf4786416 | 42 | LSM303_REGISTER_ACCEL_OUT_X_H_A = 0x29, |
bmanga95 | 0:772bf4786416 | 43 | LSM303_REGISTER_ACCEL_OUT_Y_L_A = 0x2A, |
bmanga95 | 0:772bf4786416 | 44 | LSM303_REGISTER_ACCEL_OUT_Y_H_A = 0x2B, |
bmanga95 | 0:772bf4786416 | 45 | LSM303_REGISTER_ACCEL_OUT_Z_L_A = 0x2C, |
bmanga95 | 0:772bf4786416 | 46 | LSM303_REGISTER_ACCEL_OUT_Z_H_A = 0x2D, |
bmanga95 | 0:772bf4786416 | 47 | LSM303_REGISTER_ACCEL_FIFO_CTRL_REG_A = 0x2E, |
bmanga95 | 0:772bf4786416 | 48 | LSM303_REGISTER_ACCEL_FIFO_SRC_REG_A = 0x2F, |
bmanga95 | 0:772bf4786416 | 49 | LSM303_REGISTER_ACCEL_INT1_CFG_A = 0x30, |
bmanga95 | 0:772bf4786416 | 50 | LSM303_REGISTER_ACCEL_INT1_SOURCE_A = 0x31, |
bmanga95 | 0:772bf4786416 | 51 | LSM303_REGISTER_ACCEL_INT1_THS_A = 0x32, |
bmanga95 | 0:772bf4786416 | 52 | LSM303_REGISTER_ACCEL_INT1_DURATION_A = 0x33, |
bmanga95 | 0:772bf4786416 | 53 | LSM303_REGISTER_ACCEL_INT2_CFG_A = 0x34, |
bmanga95 | 0:772bf4786416 | 54 | LSM303_REGISTER_ACCEL_INT2_SOURCE_A = 0x35, |
bmanga95 | 0:772bf4786416 | 55 | LSM303_REGISTER_ACCEL_INT2_THS_A = 0x36, |
bmanga95 | 0:772bf4786416 | 56 | LSM303_REGISTER_ACCEL_INT2_DURATION_A = 0x37, |
bmanga95 | 0:772bf4786416 | 57 | LSM303_REGISTER_ACCEL_CLICK_CFG_A = 0x38, |
bmanga95 | 0:772bf4786416 | 58 | LSM303_REGISTER_ACCEL_CLICK_SRC_A = 0x39, |
bmanga95 | 0:772bf4786416 | 59 | LSM303_REGISTER_ACCEL_CLICK_THS_A = 0x3A, |
bmanga95 | 0:772bf4786416 | 60 | LSM303_REGISTER_ACCEL_TIME_LIMIT_A = 0x3B, |
bmanga95 | 0:772bf4786416 | 61 | LSM303_REGISTER_ACCEL_TIME_LATENCY_A = 0x3C, |
bmanga95 | 0:772bf4786416 | 62 | LSM303_REGISTER_ACCEL_TIME_WINDOW_A = 0x3D |
bmanga95 | 0:772bf4786416 | 63 | } lsm303AccelRegisters_t; |
bmanga95 | 0:772bf4786416 | 64 | |
bmanga95 | 0:772bf4786416 | 65 | typedef enum |
bmanga95 | 0:772bf4786416 | 66 | { |
bmanga95 | 0:772bf4786416 | 67 | LSM303_REGISTER_MAG_CRA_REG_M = 0x00, |
bmanga95 | 0:772bf4786416 | 68 | LSM303_REGISTER_MAG_CRB_REG_M = 0x01, |
bmanga95 | 0:772bf4786416 | 69 | LSM303_REGISTER_MAG_MR_REG_M = 0x02, |
bmanga95 | 0:772bf4786416 | 70 | LSM303_REGISTER_MAG_OUT_X_H_M = 0x03, |
bmanga95 | 0:772bf4786416 | 71 | LSM303_REGISTER_MAG_OUT_X_L_M = 0x04, |
bmanga95 | 0:772bf4786416 | 72 | LSM303_REGISTER_MAG_OUT_Z_H_M = 0x05, |
bmanga95 | 0:772bf4786416 | 73 | LSM303_REGISTER_MAG_OUT_Z_L_M = 0x06, |
bmanga95 | 0:772bf4786416 | 74 | LSM303_REGISTER_MAG_OUT_Y_H_M = 0x07, |
bmanga95 | 0:772bf4786416 | 75 | LSM303_REGISTER_MAG_OUT_Y_L_M = 0x08, |
bmanga95 | 0:772bf4786416 | 76 | LSM303_REGISTER_MAG_SR_REG_Mg = 0x09, |
bmanga95 | 0:772bf4786416 | 77 | LSM303_REGISTER_MAG_IRA_REG_M = 0x0A, |
bmanga95 | 0:772bf4786416 | 78 | LSM303_REGISTER_MAG_IRB_REG_M = 0x0B, |
bmanga95 | 0:772bf4786416 | 79 | LSM303_REGISTER_MAG_IRC_REG_M = 0x0C, |
bmanga95 | 0:772bf4786416 | 80 | LSM303_REGISTER_MAG_TEMP_OUT_H_M = 0x31, |
bmanga95 | 0:772bf4786416 | 81 | LSM303_REGISTER_MAG_TEMP_OUT_L_M = 0x32 |
bmanga95 | 0:772bf4786416 | 82 | } lsm303MagRegisters_t; |
bmanga95 | 0:772bf4786416 | 83 | /*=========================================================================*/ |
bmanga95 | 0:772bf4786416 | 84 | |
bmanga95 | 0:772bf4786416 | 85 | /*========================================================================= |
bmanga95 | 0:772bf4786416 | 86 | MAGNETOMETER GAIN SETTINGS |
bmanga95 | 0:772bf4786416 | 87 | -----------------------------------------------------------------------*/ |
bmanga95 | 0:772bf4786416 | 88 | typedef enum |
bmanga95 | 0:772bf4786416 | 89 | { |
bmanga95 | 0:772bf4786416 | 90 | LSM303_MAGGAIN_1_3 = 0x20, // +/- 1.3 |
bmanga95 | 0:772bf4786416 | 91 | LSM303_MAGGAIN_1_9 = 0x40, // +/- 1.9 |
bmanga95 | 0:772bf4786416 | 92 | LSM303_MAGGAIN_2_5 = 0x60, // +/- 2.5 |
bmanga95 | 0:772bf4786416 | 93 | LSM303_MAGGAIN_4_0 = 0x80, // +/- 4.0 |
bmanga95 | 0:772bf4786416 | 94 | LSM303_MAGGAIN_4_7 = 0xA0, // +/- 4.7 |
bmanga95 | 0:772bf4786416 | 95 | LSM303_MAGGAIN_5_6 = 0xC0, // +/- 5.6 |
bmanga95 | 0:772bf4786416 | 96 | LSM303_MAGGAIN_8_1 = 0xE0 // +/- 8.1 |
bmanga95 | 0:772bf4786416 | 97 | } lsm303MagGain; |
bmanga95 | 0:772bf4786416 | 98 | /*=========================================================================*/ |
bmanga95 | 0:772bf4786416 | 99 | |
bmanga95 | 0:772bf4786416 | 100 | /*========================================================================= |
bmanga95 | 0:772bf4786416 | 101 | INTERNAL MAGNETOMETER DATA TYPE |
bmanga95 | 0:772bf4786416 | 102 | -----------------------------------------------------------------------*/ |
bmanga95 | 0:772bf4786416 | 103 | typedef struct lsm303MagData_s |
bmanga95 | 0:772bf4786416 | 104 | { |
bmanga95 | 0:772bf4786416 | 105 | float x; |
bmanga95 | 0:772bf4786416 | 106 | float y; |
bmanga95 | 0:772bf4786416 | 107 | float z; |
bmanga95 | 0:772bf4786416 | 108 | float orientation; |
bmanga95 | 0:772bf4786416 | 109 | } lsm303MagData; |
bmanga95 | 0:772bf4786416 | 110 | /*=========================================================================*/ |
bmanga95 | 0:772bf4786416 | 111 | |
bmanga95 | 0:772bf4786416 | 112 | /*========================================================================= |
bmanga95 | 0:772bf4786416 | 113 | INTERNAL ACCELERATION DATA TYPE |
bmanga95 | 0:772bf4786416 | 114 | -----------------------------------------------------------------------*/ |
bmanga95 | 0:772bf4786416 | 115 | typedef struct lsm303AccelData_s |
bmanga95 | 0:772bf4786416 | 116 | { |
bmanga95 | 0:772bf4786416 | 117 | float x; |
bmanga95 | 0:772bf4786416 | 118 | float y; |
bmanga95 | 0:772bf4786416 | 119 | float z; |
bmanga95 | 0:772bf4786416 | 120 | } lsm303AccelData; |
bmanga95 | 0:772bf4786416 | 121 | /*=========================================================================*/ |
bmanga95 | 0:772bf4786416 | 122 | |
bmanga95 | 0:772bf4786416 | 123 | /*========================================================================= |
bmanga95 | 0:772bf4786416 | 124 | CHIP ID |
bmanga95 | 0:772bf4786416 | 125 | -----------------------------------------------------------------------*/ |
bmanga95 | 0:772bf4786416 | 126 | #define LSM303_ID (0b11010100) |
bmanga95 | 0:772bf4786416 | 127 | /*=========================================================================*/ |
bmanga95 | 0:772bf4786416 | 128 | |
bmanga95 | 0:772bf4786416 | 129 | /* Unified sensor driver for the accelerometer */ |
bmanga95 | 0:772bf4786416 | 130 | class Adafruit_LSM303_Accel_Unified : public Adafruit_Sensor |
bmanga95 | 0:772bf4786416 | 131 | { |
bmanga95 | 0:772bf4786416 | 132 | public: |
bmanga95 | 0:772bf4786416 | 133 | Adafruit_LSM303_Accel_Unified(int32_t sensorID = -1); |
bmanga95 | 0:772bf4786416 | 134 | |
bmanga95 | 0:772bf4786416 | 135 | bool begin(void); |
bmanga95 | 0:772bf4786416 | 136 | void getEvent(sensors_event_t*); |
bmanga95 | 0:772bf4786416 | 137 | void getSensor(sensor_t*); |
bmanga95 | 0:772bf4786416 | 138 | void enableAutoRange(bool enable){} |
bmanga95 | 0:772bf4786416 | 139 | |
bmanga95 | 0:772bf4786416 | 140 | private: |
bmanga95 | 0:772bf4786416 | 141 | lsm303AccelData _accelData; // Last read accelerometer data will be available here |
bmanga95 | 0:772bf4786416 | 142 | int32_t _sensorID; |
bmanga95 | 0:772bf4786416 | 143 | |
bmanga95 | 0:772bf4786416 | 144 | void write8(byte address, byte reg, byte value); |
bmanga95 | 0:772bf4786416 | 145 | byte read8(byte address, byte reg); |
bmanga95 | 0:772bf4786416 | 146 | void read(void); |
bmanga95 | 0:772bf4786416 | 147 | }; |
bmanga95 | 0:772bf4786416 | 148 | |
bmanga95 | 0:772bf4786416 | 149 | /* Unified sensor driver for the magnetometer */ |
bmanga95 | 0:772bf4786416 | 150 | class Adafruit_LSM303_Mag_Unified : public Adafruit_Sensor |
bmanga95 | 0:772bf4786416 | 151 | { |
bmanga95 | 0:772bf4786416 | 152 | public: |
bmanga95 | 0:772bf4786416 | 153 | Adafruit_LSM303_Mag_Unified(int32_t sensorID = -1); |
bmanga95 | 0:772bf4786416 | 154 | |
bmanga95 | 0:772bf4786416 | 155 | bool begin(void); |
bmanga95 | 0:772bf4786416 | 156 | void enableAutoRange(bool enable); |
bmanga95 | 0:772bf4786416 | 157 | void setMagGain(lsm303MagGain gain); |
bmanga95 | 0:772bf4786416 | 158 | void getEvent(sensors_event_t*); |
bmanga95 | 0:772bf4786416 | 159 | void getSensor(sensor_t*); |
bmanga95 | 0:772bf4786416 | 160 | |
bmanga95 | 0:772bf4786416 | 161 | private: |
bmanga95 | 0:772bf4786416 | 162 | lsm303MagGain _magGain; |
bmanga95 | 0:772bf4786416 | 163 | lsm303MagData _magData; // Last read magnetometer data will be available here |
bmanga95 | 0:772bf4786416 | 164 | int32_t _sensorID; |
bmanga95 | 0:772bf4786416 | 165 | bool _autoRangeEnabled; |
bmanga95 | 0:772bf4786416 | 166 | |
bmanga95 | 0:772bf4786416 | 167 | void write8(byte address, byte reg, byte value); |
bmanga95 | 0:772bf4786416 | 168 | byte read8(byte address, byte reg); |
bmanga95 | 0:772bf4786416 | 169 | void read(void); |
bmanga95 | 0:772bf4786416 | 170 | }; |
bmanga95 | 0:772bf4786416 | 171 | |
bmanga95 | 0:772bf4786416 | 172 | #endif |