Adafruit LIS2MDL Magnetometer Library
Adafruit_LIS2MDL.h
1 /***************************************************************************
2  This is a library for the LIS2MDL magnentometer/compass
3 
4  Designed specifically to work with the Adafruit LSM303AGR and LIS2MDL
5  Breakouts
6 
7  These displays use I2C to communicate, 2 pins are required to interface.
8 
9  Adafruit invests time and resources providing this open source code,
10  please support Adafruit and open-source hardware by purchasing products
11  from Adafruit!
12 
13  Written by Bryan Siepert for Adafruit Industries.
14  BSD license, all text above must be included in any redistribution
15  ***************************************************************************/
16 #ifndef LIS2MDL_MAG_H
17 #define LIS2MDL_MAG_H
18 
19 #include <Adafruit_BusIO_Register.h>
20 
21 #include <Adafruit_I2CDevice.h>
22 #include <Adafruit_SPIDevice.h>
23 #include <Adafruit_Sensor.h>
24 #include <Wire.h>
25 
26 /*=========================================================================
27  I2C ADDRESS/BITS
28  -----------------------------------------------------------------------*/
29 #define _ADDRESS_MAG 0x1E //< Default address
30 #define _CHIP_ID 0x40 //< Chip ID from WHO_AM_I register
31 #define LIS2MDL_MAG_LSB 1.5
32 #define LIS2MDL_MILLIGAUSS_TO_MICROTESLA 0.1
33 /*=========================================================================*/
34 
35 typedef enum {
36  LIS2MDL_OFFSET_X_REG_L = 0x45,
37  LIS2MDL_OFFSET_X_REG_H = 0x46,
38  LIS2MDL_OFFSET_Y_REG_L = 0x47,
39  LIS2MDL_OFFSET_Y_REG_H = 0x48,
40  LIS2MDL_OFFSET_Z_REG_L = 0x49,
41  LIS2MDL_OFFSET_Z_REG_H = 0x4A,
42  LIS2MDL_WHO_AM_I = 0x4F,
43  LIS2MDL_CFG_REG_A = 0x60,
44  LIS2MDL_CFG_REG_B = 0x61,
45  LIS2MDL_CFG_REG_C = 0x62,
46  LIS2MDL_INT_CRTL_REG = 0x63,
47  LIS2MDL_INT_SOURCE_REG = 0x64,
48  LIS2MDL_INT_THS_L_REG = 0x65,
49  LIS2MDL_STATUS_REG = 0x67,
50  LIS2MDL_OUTX_L_REG = 0x68,
51  LIS2MDL_OUTX_H_REG = 0x69,
52  LIS2MDL_OUTY_L_REG = 0x6A,
53  LIS2MDL_OUTY_H_REG = 0x6B,
54  LIS2MDL_OUTZ_L_REG = 0x6C,
55  LIS2MDL_OUTZ_H_REG = 0x6D,
56 } lis2mdl_register_t;
57 /*=========================================================================*/
58 
59 /*=========================================================================
60  MAGNETOMETER UPDATE RATE SETTINGS
61  -----------------------------------------------------------------------*/
62 typedef enum {
63  LIS2MDL_RATE_10_HZ, //< 10 Hz
64  LIS2MDL_RATE_20_HZ, //< 20 Hz
65  LIS2MDL_RATE_50_HZ, //< 50 Hz
66  LIS2MDL_RATE_100_HZ, //< 100 Hz
67 } lis2mdl_rate_t;
68 /*=========================================================================*/
69 
70 /**************************************************************************/
74 /**************************************************************************/
75 typedef struct lis2mdl_data {
76  int16_t x;
77  int16_t y;
78  int16_t z;
80 /*=========================================================================*/
81 
82 /**************************************************************************/
86 /**************************************************************************/
87 class Adafruit_LIS2MDL : public Adafruit_Sensor {
88 public:
89  Adafruit_LIS2MDL(int32_t sensorID = -1);
90 
91  bool begin(uint8_t i2c_addr = _ADDRESS_MAG, TwoWire *wire = &Wire);
92  bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI);
93  bool begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
94  int8_t mosi_pin);
95 
96  lis2mdl_rate_t getDataRate();
97  void setDataRate(lis2mdl_rate_t rate);
98  bool getEvent(sensors_event_t *);
99  void getSensor(sensor_t *);
100  void reset(void);
101 
102  void enableInterrupts(bool);
103  void interruptsActiveHigh(bool);
105 
106 private:
107  bool _init(void);
108 
109  int32_t _sensorID;
110  Adafruit_BusIO_Register *config_a;
111 
112  void read(void);
113 
114  Adafruit_I2CDevice *i2c_dev = NULL;
115  Adafruit_SPIDevice *spi_dev = NULL;
116 };
117 
118 #endif
lis2mdl_data_t raw
struct instance to hold raw data
Definition: Adafruit_LIS2MDL.h:104
a data thing
Definition: Adafruit_LIS2MDL.h:75
int16_t y
y-axis raw data
Definition: Adafruit_LIS2MDL.h:77
int16_t z
z-axis raw data
Definition: Adafruit_LIS2MDL.h:78
int16_t x
x-axis raw data
Definition: Adafruit_LIS2MDL.h:76
Unified sensor driver for the magnetometer.
Definition: Adafruit_LIS2MDL.h:87