Adafruit_LIS3MDL.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*!
  2. * @file Adafruit_LIS3MDL.cpp
  3. *
  4. * @mainpage Adafruit LIS3MDL Breakout
  5. *
  6. * @section intro_sec Introduction
  7. *
  8. * This is a library for the Adafruit LIS3MDL magnetometer breakout board
  9. * ----> https://www.adafruit.com/product/4479
  10. *
  11. * Adafruit invests time and resources providing this open source code,
  12. * please support Adafruit and open-source hardware by purchasing
  13. * products from Adafruit!
  14. *
  15. * @author Limor Fried (Adafruit Industries)
  16. *
  17. * @section license License
  18. *
  19. * BSD license, all text here must be included in any redistribution.
  20. *
  21. */
  22. #include "Arduino.h"
  23. #include <Adafruit_LIS3MDL.h>
  24. #include <Wire.h>
  25. /**************************************************************************/
  26. /*!
  27. @brief Instantiates a new LIS3MDL class
  28. */
  29. /**************************************************************************/
  30. Adafruit_LIS3MDL::Adafruit_LIS3MDL() {}
  31. /*!
  32. * @brief Sets up the hardware and initializes I2C
  33. * @param i2c_address
  34. * The I2C address to be used.
  35. * @param wire
  36. * The Wire object to be used for I2C connections.
  37. * @return True if initialization was successful, otherwise false.
  38. */
  39. bool Adafruit_LIS3MDL::begin_I2C(uint8_t i2c_address, TwoWire *wire) {
  40. if (i2c_dev)
  41. delete i2c_dev;
  42. if (spi_dev)
  43. delete spi_dev;
  44. spi_dev = NULL;
  45. i2c_dev = new Adafruit_I2CDevice(i2c_address, wire);
  46. if (!i2c_dev->begin()) {
  47. return false;
  48. }
  49. return _init();
  50. }
  51. /*!
  52. * @brief Sets up the hardware and initializes hardware SPI
  53. * @param cs_pin The arduino pin # connected to chip select
  54. * @param theSPI The SPI object to be used for SPI connections.
  55. * @param frequency The SPI bus frequency
  56. * @return True if initialization was successful, otherwise false.
  57. */
  58. boolean Adafruit_LIS3MDL::begin_SPI(uint8_t cs_pin, SPIClass *theSPI,
  59. uint32_t frequency) {
  60. if (i2c_dev)
  61. delete i2c_dev;
  62. if (spi_dev)
  63. delete spi_dev;
  64. i2c_dev = NULL;
  65. spi_dev = new Adafruit_SPIDevice(cs_pin,
  66. frequency, // frequency
  67. SPI_BITORDER_MSBFIRST, // bit order
  68. SPI_MODE0, // data mode
  69. theSPI);
  70. if (!spi_dev->begin()) {
  71. return false;
  72. }
  73. return _init();
  74. }
  75. /*!
  76. * @brief Sets up the hardware and initializes software SPI
  77. * @param cs_pin The arduino pin # connected to chip select
  78. * @param sck_pin The arduino pin # connected to SPI clock
  79. * @param miso_pin The arduino pin # connected to SPI MISO
  80. * @param mosi_pin The arduino pin # connected to SPI MOSI
  81. * @param frequency The SPI bus frequency
  82. * @return True if initialization was successful, otherwise false.
  83. */
  84. bool Adafruit_LIS3MDL::begin_SPI(int8_t cs_pin, int8_t sck_pin, int8_t miso_pin,
  85. int8_t mosi_pin, uint32_t frequency) {
  86. if (i2c_dev)
  87. delete i2c_dev;
  88. if (spi_dev)
  89. delete spi_dev;
  90. i2c_dev = NULL;
  91. spi_dev = new Adafruit_SPIDevice(cs_pin, sck_pin, miso_pin, mosi_pin,
  92. frequency, // frequency
  93. SPI_BITORDER_MSBFIRST, // bit order
  94. SPI_MODE0); // data mode
  95. if (!spi_dev->begin()) {
  96. return false;
  97. }
  98. return _init();
  99. }
  100. /*!
  101. * @brief Common initialization code for I2C & SPI
  102. * @return True if initialization was successful, otherwise false.
  103. */
  104. bool Adafruit_LIS3MDL::_init(void) {
  105. // Check connection
  106. Adafruit_BusIO_Register chip_id =
  107. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  108. LIS3MDL_REG_WHO_AM_I, 1);
  109. // make sure we're talking to the right chip
  110. if (chip_id.read() != 0x3D) {
  111. // No LIS3MDL detected ... return false
  112. return false;
  113. }
  114. reset();
  115. // 155Hz default rate
  116. setDataRate(LIS3MDL_DATARATE_155_HZ);
  117. // lowest range
  118. setRange(LIS3MDL_RANGE_4_GAUSS);
  119. setOperationMode(LIS3MDL_CONTINUOUSMODE);
  120. return true;
  121. }
  122. /**************************************************************************/
  123. /*!
  124. @brief Performs a software reset
  125. */
  126. /**************************************************************************/
  127. void Adafruit_LIS3MDL::reset(void) {
  128. Adafruit_BusIO_Register CTRL_REG2 =
  129. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  130. LIS3MDL_REG_CTRL_REG2, 1);
  131. Adafruit_BusIO_RegisterBits resetbits =
  132. Adafruit_BusIO_RegisterBits(&CTRL_REG2, 1, 2);
  133. resetbits.write(0x1);
  134. delay(10);
  135. getRange();
  136. }
  137. /**************************************************************************/
  138. /*!
  139. @brief Read the XYZ data from the magnetometer and store in the internal
  140. x, y and z (and x_g, y_g, z_g) member variables.
  141. */
  142. /**************************************************************************/
  143. void Adafruit_LIS3MDL::read(void) {
  144. uint8_t buffer[6];
  145. Adafruit_BusIO_Register XYZDataReg = Adafruit_BusIO_Register(
  146. i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, LIS3MDL_REG_OUT_X_L, 6);
  147. XYZDataReg.read(buffer, 6);
  148. x = buffer[0];
  149. x |= buffer[1] << 8;
  150. y = buffer[2];
  151. y |= buffer[3] << 8;
  152. z = buffer[4];
  153. z |= buffer[5] << 8;
  154. float scale = 1; // LSB per gauss
  155. switch (rangeBuffered) {
  156. case LIS3MDL_RANGE_16_GAUSS:
  157. scale = 1711;
  158. break;
  159. case LIS3MDL_RANGE_12_GAUSS:
  160. scale = 2281;
  161. break;
  162. case LIS3MDL_RANGE_8_GAUSS:
  163. scale = 3421;
  164. break;
  165. case LIS3MDL_RANGE_4_GAUSS:
  166. scale = 6842;
  167. break;
  168. }
  169. x_gauss = (float)x / scale;
  170. y_gauss = (float)y / scale;
  171. z_gauss = (float)z / scale;
  172. }
  173. /**************************************************************************/
  174. /*!
  175. @brief Gets the most recent sensor event, Adafruit Unified Sensor format
  176. @param event Pointer to an Adafruit Unified sensor_event_t object that
  177. we'll fill in
  178. @returns True on successful read
  179. */
  180. /**************************************************************************/
  181. bool Adafruit_LIS3MDL::getEvent(sensors_event_t *event) {
  182. /* Clear the event */
  183. memset(event, 0, sizeof(sensors_event_t));
  184. event->version = sizeof(sensors_event_t);
  185. event->sensor_id = _sensorID;
  186. event->type = SENSOR_TYPE_MAGNETIC_FIELD;
  187. event->timestamp = millis();
  188. read();
  189. event->magnetic.x = x_gauss * 100; // microTesla per gauss
  190. event->magnetic.y = y_gauss * 100; // microTesla per gauss
  191. event->magnetic.z = z_gauss * 100; // microTesla per gauss
  192. return true;
  193. }
  194. /**************************************************************************/
  195. /*!
  196. @brief Gets the sensor_t device data, Adafruit Unified Sensor format
  197. @param sensor Pointer to an Adafruit Unified sensor_t object that we'll
  198. fill in
  199. */
  200. /**************************************************************************/
  201. void Adafruit_LIS3MDL::getSensor(sensor_t *sensor) {
  202. /* Clear the sensor_t object */
  203. memset(sensor, 0, sizeof(sensor_t));
  204. /* Insert the sensor name in the fixed length char array */
  205. strncpy(sensor->name, "LIS3MDL", sizeof(sensor->name) - 1);
  206. sensor->name[sizeof(sensor->name) - 1] = 0;
  207. sensor->version = 1;
  208. sensor->sensor_id = _sensorID;
  209. sensor->type = SENSOR_TYPE_MAGNETIC_FIELD;
  210. sensor->min_delay = 0;
  211. sensor->min_value = -1600; // -16 gauss in uTesla
  212. sensor->max_value = 1600; // +16 gauss in uTesla
  213. sensor->resolution = 0.015; // 100/6842 uTesla per LSB at +-4 gauss range
  214. }
  215. /**************************************************************************/
  216. /*!
  217. @brief Set the performance mode, LIS3MDL_LOWPOWERMODE, LIS3MDL_MEDIUMMODE,
  218. LIS3MDL_HIGHMODE or LIS3MDL_ULTRAHIGHMODE
  219. @param mode Enumerated lis3mdl_performancemode_t
  220. */
  221. /**************************************************************************/
  222. void Adafruit_LIS3MDL::setPerformanceMode(lis3mdl_performancemode_t mode) {
  223. // write xy
  224. Adafruit_BusIO_Register CTRL_REG1 =
  225. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  226. LIS3MDL_REG_CTRL_REG1, 1);
  227. Adafruit_BusIO_RegisterBits performancemodebits =
  228. Adafruit_BusIO_RegisterBits(&CTRL_REG1, 2, 5);
  229. performancemodebits.write((uint8_t)mode);
  230. // write z
  231. Adafruit_BusIO_Register CTRL_REG4 =
  232. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  233. LIS3MDL_REG_CTRL_REG4, 1);
  234. Adafruit_BusIO_RegisterBits performancemodezbits =
  235. Adafruit_BusIO_RegisterBits(&CTRL_REG4, 2, 2);
  236. performancemodezbits.write((uint8_t)mode);
  237. }
  238. /**************************************************************************/
  239. /*!
  240. @brief Get the performance mode
  241. @returns Enumerated lis3mdl_performancemode_t, LIS3MDL_LOWPOWERMODE,
  242. LIS3MDL_MEDIUMMODE, LIS3MDL_HIGHMODE or LIS3MDL_ULTRAHIGHMODE
  243. */
  244. /**************************************************************************/
  245. lis3mdl_performancemode_t Adafruit_LIS3MDL::getPerformanceMode(void) {
  246. Adafruit_BusIO_Register CTRL_REG1 =
  247. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  248. LIS3MDL_REG_CTRL_REG1, 1);
  249. Adafruit_BusIO_RegisterBits performancemodebits =
  250. Adafruit_BusIO_RegisterBits(&CTRL_REG1, 2, 5);
  251. return (lis3mdl_performancemode_t)performancemodebits.read();
  252. }
  253. /**************************************************************************/
  254. /*!
  255. @brief Sets the data rate for the LIS3MDL (controls power consumption)
  256. from 0.625 Hz to 80Hz
  257. @param dataRate Enumerated lis3mdl_dataRate_t
  258. */
  259. /**************************************************************************/
  260. void Adafruit_LIS3MDL::setDataRate(lis3mdl_dataRate_t dataRate) {
  261. if (dataRate == LIS3MDL_DATARATE_155_HZ) {
  262. // set OP to UHP
  263. setPerformanceMode(LIS3MDL_ULTRAHIGHMODE);
  264. }
  265. if (dataRate == LIS3MDL_DATARATE_300_HZ) {
  266. // set OP to HP
  267. setPerformanceMode(LIS3MDL_HIGHMODE);
  268. }
  269. if (dataRate == LIS3MDL_DATARATE_560_HZ) {
  270. // set OP to MP
  271. setPerformanceMode(LIS3MDL_MEDIUMMODE);
  272. }
  273. if (dataRate == LIS3MDL_DATARATE_1000_HZ) {
  274. // set OP to LP
  275. setPerformanceMode(LIS3MDL_LOWPOWERMODE);
  276. }
  277. delay(10);
  278. Adafruit_BusIO_Register CTRL_REG1 =
  279. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  280. LIS3MDL_REG_CTRL_REG1, 1);
  281. Adafruit_BusIO_RegisterBits dataratebits =
  282. Adafruit_BusIO_RegisterBits(&CTRL_REG1, 4, 1); // includes FAST_ODR
  283. dataratebits.write((uint8_t)dataRate);
  284. }
  285. /**************************************************************************/
  286. /*!
  287. @brief Gets the data rate for the LIS3MDL (controls power consumption)
  288. @return Enumerated lis3mdl_dataRate_t from 0.625 Hz to 80Hz
  289. */
  290. /**************************************************************************/
  291. lis3mdl_dataRate_t Adafruit_LIS3MDL::getDataRate(void) {
  292. Adafruit_BusIO_Register CTRL_REG1 =
  293. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  294. LIS3MDL_REG_CTRL_REG1, 1);
  295. Adafruit_BusIO_RegisterBits dataratebits =
  296. Adafruit_BusIO_RegisterBits(&CTRL_REG1, 4, 1); // includes FAST_ODR
  297. return (lis3mdl_dataRate_t)dataratebits.read();
  298. }
  299. /**************************************************************************/
  300. /*!
  301. @brief Set the operation mode, LIS3MDL_CONTINUOUSMODE,
  302. LIS3MDL_SINGLEMODE or LIS3MDL_POWERDOWNMODE
  303. @param mode Enumerated lis3mdl_operationmode_t
  304. */
  305. /**************************************************************************/
  306. void Adafruit_LIS3MDL::setOperationMode(lis3mdl_operationmode_t mode) {
  307. // write x and y
  308. Adafruit_BusIO_Register CTRL_REG3 =
  309. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  310. LIS3MDL_REG_CTRL_REG3, 1);
  311. Adafruit_BusIO_RegisterBits opmodebits =
  312. Adafruit_BusIO_RegisterBits(&CTRL_REG3, 2, 0);
  313. opmodebits.write((uint8_t)mode);
  314. }
  315. /**************************************************************************/
  316. /*!
  317. @brief Get the operation mode
  318. @returns Enumerated lis3mdl_operationmode_t, LIS3MDL_CONTINUOUSMODE,
  319. LIS3MDL_SINGLEMODE or LIS3MDL_POWERDOWNMODE
  320. */
  321. /**************************************************************************/
  322. lis3mdl_operationmode_t Adafruit_LIS3MDL::getOperationMode(void) {
  323. Adafruit_BusIO_Register CTRL_REG3 =
  324. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  325. LIS3MDL_REG_CTRL_REG3, 1);
  326. Adafruit_BusIO_RegisterBits opmodebits =
  327. Adafruit_BusIO_RegisterBits(&CTRL_REG3, 2, 0);
  328. return (lis3mdl_operationmode_t)opmodebits.read();
  329. }
  330. /**************************************************************************/
  331. /*!
  332. @brief Set the resolution range: +-4 gauss, 8 gauss, 12 gauss, or 16 gauss.
  333. @param range Enumerated lis3mdl_range_t
  334. */
  335. /**************************************************************************/
  336. void Adafruit_LIS3MDL::setRange(lis3mdl_range_t range) {
  337. Adafruit_BusIO_Register CTRL_REG2 =
  338. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  339. LIS3MDL_REG_CTRL_REG2, 1);
  340. Adafruit_BusIO_RegisterBits rangebits =
  341. Adafruit_BusIO_RegisterBits(&CTRL_REG2, 2, 5);
  342. rangebits.write((uint8_t)range);
  343. rangeBuffered = range;
  344. }
  345. /**************************************************************************/
  346. /*!
  347. @brief Read the resolution range: +-4 gauss, 8 gauss, 12 gauss, or 16 gauss.
  348. @returns Enumerated lis3mdl_range_t
  349. */
  350. /**************************************************************************/
  351. lis3mdl_range_t Adafruit_LIS3MDL::getRange(void) {
  352. Adafruit_BusIO_Register CTRL_REG2 =
  353. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  354. LIS3MDL_REG_CTRL_REG2, 1);
  355. Adafruit_BusIO_RegisterBits rangebits =
  356. Adafruit_BusIO_RegisterBits(&CTRL_REG2, 2, 5);
  357. rangeBuffered = (lis3mdl_range_t)rangebits.read();
  358. return rangeBuffered;
  359. }
  360. /**************************************************************************/
  361. /*!
  362. @brief Set the interrupt threshold value
  363. @param value 16-bit unsigned raw value
  364. */
  365. /**************************************************************************/
  366. void Adafruit_LIS3MDL::setIntThreshold(uint16_t value) {
  367. value &= 0x7FFF; // high bit must be 0!
  368. Adafruit_BusIO_Register INT_THS =
  369. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  370. LIS3MDL_REG_INT_THS_L, 2);
  371. INT_THS.write(value);
  372. }
  373. /**************************************************************************/
  374. /*!
  375. @brief Get the interrupt threshold value
  376. @returns 16-bit unsigned raw value
  377. */
  378. /**************************************************************************/
  379. uint16_t Adafruit_LIS3MDL::getIntThreshold(void) {
  380. Adafruit_BusIO_Register INT_THS =
  381. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  382. LIS3MDL_REG_INT_THS_L, 2);
  383. return INT_THS.read();
  384. }
  385. /**************************************************************************/
  386. /*!
  387. @brief Configure INT_CFG
  388. @param enableX Enable interrupt generation on X-axis
  389. @param enableY Enable interrupt generation on Y-axis
  390. @param enableZ Enable interrupt generation on Z-axis
  391. @param polarity Sets the polarity of the INT output logic
  392. @param latch If true (latched) the INT pin remains in the same state
  393. until INT_SRC is read.
  394. @param enableInt Interrupt enable on INT pin
  395. */
  396. /**************************************************************************/
  397. void Adafruit_LIS3MDL::configInterrupt(bool enableX, bool enableY, bool enableZ,
  398. bool polarity, bool latch,
  399. bool enableInt) {
  400. uint8_t value = 0x08; // set default bits, see table 36
  401. value |= enableX << 7;
  402. value |= enableY << 6;
  403. value |= enableZ << 5;
  404. value |= polarity << 2;
  405. value |= latch << 1;
  406. value |= enableInt;
  407. Adafruit_BusIO_Register INT_CFG = Adafruit_BusIO_Register(
  408. i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, LIS3MDL_REG_INT_CFG, 1);
  409. INT_CFG.write(value);
  410. }
  411. /**************************************************************************/
  412. /*!
  413. @brief Enable or disable self-test
  414. @param flag If true, enable self-test
  415. */
  416. /**************************************************************************/
  417. void Adafruit_LIS3MDL::selfTest(bool flag) {
  418. Adafruit_BusIO_Register CTRL_REG1 =
  419. Adafruit_BusIO_Register(i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC,
  420. LIS3MDL_REG_CTRL_REG1, 1);
  421. Adafruit_BusIO_RegisterBits stbit =
  422. Adafruit_BusIO_RegisterBits(&CTRL_REG1, 1, 0);
  423. stbit.write(flag);
  424. }
  425. /**************************************************************************/
  426. /*!
  427. @brief Get the magnetic data rate.
  428. @returns The data rate in float
  429. */
  430. float Adafruit_LIS3MDL::magneticFieldSampleRate(void) {
  431. switch (this->getDataRate()) {
  432. case LIS3MDL_DATARATE_0_625_HZ:
  433. return 0.625f;
  434. case LIS3MDL_DATARATE_1_25_HZ:
  435. return 1.25f;
  436. case LIS3MDL_DATARATE_2_5_HZ:
  437. return 2.5f;
  438. case LIS3MDL_DATARATE_5_HZ:
  439. return 5.0f;
  440. case LIS3MDL_DATARATE_10_HZ:
  441. return 10.0f;
  442. case LIS3MDL_DATARATE_20_HZ:
  443. return 20.0f;
  444. case LIS3MDL_DATARATE_40_HZ:
  445. return 40.0f;
  446. case LIS3MDL_DATARATE_80_HZ:
  447. return 80.0f;
  448. case LIS3MDL_DATARATE_155_HZ:
  449. return 155.0f;
  450. case LIS3MDL_DATARATE_300_HZ:
  451. return 300.0f;
  452. case LIS3MDL_DATARATE_560_HZ:
  453. return 560.0f;
  454. case LIS3MDL_DATARATE_1000_HZ:
  455. return 1000.0f;
  456. }
  457. return 0;
  458. }
  459. /**************************************************************************/
  460. /*!
  461. @brief Check for available data from magnetic
  462. @returns 1 if available, 0 if not
  463. */
  464. int Adafruit_LIS3MDL::magneticFieldAvailable(void) {
  465. Adafruit_BusIO_Register REG_STATUS = Adafruit_BusIO_Register(
  466. i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, LIS3MDL_REG_STATUS, 1);
  467. return (REG_STATUS.read() & 0x08) ? 1 : 0;
  468. }
  469. /**************************************************************************/
  470. /*!
  471. @brief Read magnetic data
  472. @param x reference to x axis
  473. @param y reference to y axis
  474. @param z reference to z axis
  475. @returns 1 if success, 0 if not
  476. */
  477. int Adafruit_LIS3MDL::readMagneticField(float &x, float &y, float &z) {
  478. int16_t data[3];
  479. Adafruit_BusIO_Register XYZDataReg = Adafruit_BusIO_Register(
  480. i2c_dev, spi_dev, AD8_HIGH_TOREAD_AD7_HIGH_TOINC, LIS3MDL_REG_OUT_X_L, 6);
  481. if (!XYZDataReg.read((uint8_t *)data, sizeof(data))) {
  482. x = y = z = NAN;
  483. return 0;
  484. }
  485. x = data[0] * 4.0 * 100.0 / 32768.0;
  486. y = data[1] * 4.0 * 100.0 / 32768.0;
  487. z = data[2] * 4.0 * 100.0 / 32768.0;
  488. return 1;
  489. }