Adafruit LIS3MDL Arduino Library
Adafruit_LIS3MDL.h
Go to the documentation of this file.
1 
6 #ifndef ADAFRUIT_LIS3MDL_H
7 #define ADAFRUIT_LIS3MDL_H
8 
9 #include <Adafruit_BusIO_Register.h>
10 #include <Adafruit_I2CDevice.h>
11 #include <Adafruit_SPIDevice.h>
12 #include <Adafruit_Sensor.h>
13 #include <Wire.h>
14 
15 /*=========================================================================
16 I2C ADDRESS/BITS
17 -----------------------------------------------------------------------*/
18 #define LIS3MDL_I2CADDR_DEFAULT (0x1C)
19 /*=========================================================================*/
20 
21 #define LIS3MDL_REG_WHO_AM_I 0x0F
22 #define LIS3MDL_REG_CTRL_REG1 0x20
23 #define LIS3MDL_REG_CTRL_REG2 0x21
24 #define LIS3MDL_REG_CTRL_REG3 0x22
25 #define LIS3MDL_REG_CTRL_REG4 0x23
26 #define LIS3MDL_REG_STATUS 0x27
27 #define LIS3MDL_REG_OUT_X_L 0x28
28 #define LIS3MDL_REG_INT_CFG 0x30
29 #define LIS3MDL_REG_INT_THS_L 0x32
30 
31 
32 typedef enum {
38 
40 typedef enum {
54 
56 typedef enum {
62 
64 typedef enum {
69 
71 class Adafruit_LIS3MDL : public Adafruit_Sensor {
72 public:
73  Adafruit_LIS3MDL(void);
74  bool begin_I2C(uint8_t i2c_addr = LIS3MDL_I2CADDR_DEFAULT,
75  TwoWire *wire = &Wire);
76  bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI,
77  uint32_t frequency = 1000000);
78  bool begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
79  int8_t mosi_pin, uint32_t frequency = 1000000);
80 
81  void reset(void);
82 
87  void setDataRate(lis3mdl_dataRate_t dataRate);
89  void setRange(lis3mdl_range_t range);
91  void setIntThreshold(uint16_t value);
92  uint16_t getIntThreshold(void);
93  void configInterrupt(bool enableX, bool enableY, bool enableZ, bool polarity,
94  bool latch, bool enableInt);
95  void selfTest(bool flag);
96 
97  void read();
98  bool getEvent(sensors_event_t *event);
99  void getSensor(sensor_t *sensor);
100 
101  // Arduino compatible API
102  int readMagneticField(float &x, float &y, float &z);
103  float magneticFieldSampleRate(void);
104  int magneticFieldAvailable(void);
105 
106  int16_t x,
107  y,
108  z;
109  float x_gauss,
110  y_gauss,
111  z_gauss;
112 
115 
116 private:
117  bool _init(void);
118 
119  Adafruit_I2CDevice *i2c_dev = NULL;
120  Adafruit_SPIDevice *spi_dev = NULL;
121 
122  int32_t _sensorID;
123 };
124 
125 #endif
void reset(void)
Performs a software reset.
Definition: Adafruit_LIS3MDL.cpp:150
5 Hz
Definition: Adafruit_LIS3MDL.h:44
lis3mdl_range_t
Definition: Adafruit_LIS3MDL.h:32
void read()
Read the XYZ data from the magnetometer and store in the internal x, y and z (and x_g...
Definition: Adafruit_LIS3MDL.cpp:169
void selfTest(bool flag)
Enable or disable self-test.
Definition: Adafruit_LIS3MDL.cpp:471
40 Hz
Definition: Adafruit_LIS3MDL.h:47
void setDataRate(lis3mdl_dataRate_t dataRate)
Sets the data rate for the LIS3MDL (controls power consumption) from 0.625 Hz to 80Hz.
Definition: Adafruit_LIS3MDL.cpp:300
int readMagneticField(float &x, float &y, float &z)
Read magnetic data.
Definition: Adafruit_LIS3MDL.cpp:537
bool begin_I2C(uint8_t i2c_addr=LIS3MDL_I2CADDR_DEFAULT, TwoWire *wire=&Wire)
Sets up the hardware and initializes I2C.
Definition: Adafruit_LIS3MDL.cpp:42
Low power mode.
Definition: Adafruit_LIS3MDL.h:57
float y_gauss
The last read Y mag in &#39;gauss&#39;.
Definition: Adafruit_LIS3MDL.h:109
+/- 12g
Definition: Adafruit_LIS3MDL.h:35
300 Hz (FAST_ODR + HP)
Definition: Adafruit_LIS3MDL.h:50
lis3mdl_performancemode_t
Definition: Adafruit_LIS3MDL.h:56
Definition: Adafruit_LIS3MDL.h:71
0.625 Hz
Definition: Adafruit_LIS3MDL.h:41
Medium performance mode.
Definition: Adafruit_LIS3MDL.h:58
#define LIS3MDL_I2CADDR_DEFAULT
Default breakout addres.
Definition: Adafruit_LIS3MDL.h:18
void setOperationMode(lis3mdl_operationmode_t mode)
Set the operation mode, LIS3MDL_CONTINUOUSMODE, LIS3MDL_SINGLEMODE or LIS3MDL_POWERDOWNMODE.
Definition: Adafruit_LIS3MDL.cpp:348
1.25 Hz
Definition: Adafruit_LIS3MDL.h:42
void setRange(lis3mdl_range_t range)
Set the resolution range: +-4 gauss, 8 gauss, 12 gauss, or 16 gauss.
Definition: Adafruit_LIS3MDL.cpp:380
+/- 16g
Definition: Adafruit_LIS3MDL.h:36
float x_gauss
The last read X mag in &#39;gauss&#39;.
Definition: Adafruit_LIS3MDL.h:109
lis3mdl_dataRate_t getDataRate(void)
Gets the data rate for the LIS3MDL (controls power consumption)
Definition: Adafruit_LIS3MDL.cpp:332
Continuous conversion.
Definition: Adafruit_LIS3MDL.h:65
lis3mdl_dataRate_t
Definition: Adafruit_LIS3MDL.h:40
lis3mdl_operationmode_t getOperationMode(void)
Get the operation mode.
Definition: Adafruit_LIS3MDL.cpp:365
Ultra-high performance mode.
Definition: Adafruit_LIS3MDL.h:60
uint16_t getIntThreshold(void)
Get the interrupt threshold value.
Definition: Adafruit_LIS3MDL.cpp:429
void getSensor(sensor_t *sensor)
Gets the sensor_t device data, Adafruit Unified Sensor format.
Definition: Adafruit_LIS3MDL.cpp:236
lis3mdl_range_t getRange(void)
Read the resolution range: +-4 gauss, 8 gauss, 12 gauss, or 16 gauss.
Definition: Adafruit_LIS3MDL.cpp:397
Powered-down mode.
Definition: Adafruit_LIS3MDL.h:67
+/- 8g
Definition: Adafruit_LIS3MDL.h:34
High performance mode.
Definition: Adafruit_LIS3MDL.h:59
lis3mdl_performancemode_t getPerformanceMode(void)
Get the performance mode.
Definition: Adafruit_LIS3MDL.cpp:284
20 Hz
Definition: Adafruit_LIS3MDL.h:46
80 Hz
Definition: Adafruit_LIS3MDL.h:48
bool getEvent(sensors_event_t *event)
Gets the most recent sensor event, Adafruit Unified Sensor format.
Definition: Adafruit_LIS3MDL.cpp:211
10 Hz
Definition: Adafruit_LIS3MDL.h:45
void configInterrupt(bool enableX, bool enableY, bool enableZ, bool polarity, bool latch, bool enableInt)
Configure INT_CFG.
Definition: Adafruit_LIS3MDL.cpp:448
+/- 4g (default value)
Definition: Adafruit_LIS3MDL.h:33
lis3mdl_operationmode_t
Definition: Adafruit_LIS3MDL.h:64
Adafruit_LIS3MDL(void)
Instantiates a new LIS3MDL class.
Definition: Adafruit_LIS3MDL.cpp:32
void setIntThreshold(uint16_t value)
Set the interrupt threshold value.
Definition: Adafruit_LIS3MDL.cpp:415
int magneticFieldAvailable(void)
Check for available data from magnetic.
Definition: Adafruit_LIS3MDL.cpp:523
bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI=&SPI, uint32_t frequency=1000000)
Sets up the hardware and initializes hardware SPI.
Definition: Adafruit_LIS3MDL.cpp:64
int16_t x
The last read X mag in raw units.
Definition: Adafruit_LIS3MDL.h:106
lis3mdl_range_t rangeBuffered
buffer for the magnetometer range
Definition: Adafruit_LIS3MDL.h:114
float z_gauss
The last read Z mag in &#39;gauss&#39;.
Definition: Adafruit_LIS3MDL.h:109
1000 Hz (FAST_ODR + LP)
Definition: Adafruit_LIS3MDL.h:52
float magneticFieldSampleRate(void)
Get the magnetic data rate.
Definition: Adafruit_LIS3MDL.cpp:487
560 Hz (FAST_ODR + MP)
Definition: Adafruit_LIS3MDL.h:51
2.5 Hz
Definition: Adafruit_LIS3MDL.h:43
Single-shot conversion.
Definition: Adafruit_LIS3MDL.h:66
155 Hz (FAST_ODR + UHP)
Definition: Adafruit_LIS3MDL.h:49
void setPerformanceMode(lis3mdl_performancemode_t mode)
Set the performance mode, LIS3MDL_LOWPOWERMODE, LIS3MDL_MEDIUMMODE, LIS3MDL_HIGHMODE or LIS3MDL_ULTRA...
Definition: Adafruit_LIS3MDL.cpp:259
int16_t y
The last read Y mag in raw units.
Definition: Adafruit_LIS3MDL.h:106
int16_t z
The last read Z mag in raw units.
Definition: Adafruit_LIS3MDL.h:106