Adafruit_LSM9DS1.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /***************************************************************************
  2. This is a library for the LSM9DS1 Accelerometer and magnentometer/compass
  3. Designed specifically to work with the Adafruit LSM9DS1 Breakouts
  4. These sensors use I2C to communicate, 2 pins are required to interface.
  5. Adafruit invests time and resources providing this open source code,
  6. please support Adafruit andopen-source hardware by purchasing products
  7. from Adafruit!
  8. Written by Kevin Townsend for Adafruit Industries.
  9. BSD license, all text above must be included in any redistribution
  10. ***************************************************************************/
  11. #include <Adafruit_LSM9DS1.h>
  12. /***************************************************************************
  13. CONSTRUCTOR
  14. ***************************************************************************/
  15. /**************************************************************************/
  16. /*!
  17. @brief Instantiate with hardware I2C interface
  18. @param wireBus The I2C wire interface you'd like to use
  19. @param sensorID Unique identifier you'd like for the sensors
  20. */
  21. /**************************************************************************/
  22. Adafruit_LSM9DS1::Adafruit_LSM9DS1(TwoWire *wireBus, int32_t sensorID) {
  23. _wire = wireBus;
  24. initSensor(sensorID);
  25. }
  26. /**************************************************************************/
  27. /*!
  28. @brief Instantiate with default hardware I2C interface
  29. @param sensorID Unique identifier you'd like for the sensors
  30. */
  31. /**************************************************************************/
  32. Adafruit_LSM9DS1::Adafruit_LSM9DS1(int32_t sensorID) {
  33. _wire = &Wire;
  34. initSensor(sensorID);
  35. }
  36. /**************************************************************************/
  37. /*!
  38. @brief Instantiate with hardware SPI interface
  39. @param xgcs SPI CS pin for accelerometer/gyro subchip
  40. @param mcs SPI CS pin for magnetometer subchip
  41. @param sensorID Unique identifier you'd like for the sensors
  42. */
  43. /**************************************************************************/
  44. Adafruit_LSM9DS1::Adafruit_LSM9DS1(int8_t xgcs, int8_t mcs, int32_t sensorID) {
  45. // hardware SPI!
  46. _csm = mcs;
  47. _csxg = xgcs;
  48. _mosi = _miso = _clk = -1;
  49. initSensor(sensorID);
  50. }
  51. /**************************************************************************/
  52. /*!
  53. @brief Instantiate with software SPI interface
  54. @param sclk SPI clock pin
  55. @param smiso SPI MISO pin
  56. @param smosi SPI MOSI pin
  57. @param xgcs SPI CS pin for accelerometer/gyro subchip
  58. @param mcs SPI CS pin for magnetometer subchip
  59. @param sensorID Unique identifier you'd like for the sensors
  60. */
  61. /**************************************************************************/
  62. Adafruit_LSM9DS1::Adafruit_LSM9DS1(int8_t sclk, int8_t smiso, int8_t smosi,
  63. int8_t xgcs, int8_t mcs, int32_t sensorID) {
  64. // software SPI!
  65. _csm = mcs;
  66. _csxg = xgcs;
  67. _mosi = smosi;
  68. _miso = smiso;
  69. _clk = sclk;
  70. initSensor(sensorID);
  71. }
  72. /**************************************************************************/
  73. /*!
  74. @brief Initialize I2C or SPI and detect/initialize subsensors
  75. @returns True if both subsensors were detected on the desired interface
  76. */
  77. /**************************************************************************/
  78. bool Adafruit_LSM9DS1::begin() {
  79. if (_wire) {
  80. // I2C
  81. if (i2c_dev)
  82. delete i2c_dev;
  83. i2c_dev = new Adafruit_I2CDevice(LSM9DS1_ADDRESS_ACCELGYRO, _wire);
  84. if (!i2c_dev->begin())
  85. return false;
  86. if (!_magSensor.begin_I2C(LSM9DS1_ADDRESS_MAG, _wire))
  87. return false;
  88. } else {
  89. // SPI
  90. if (spi_dev)
  91. delete spi_dev;
  92. if (_clk == -1) {
  93. // Hardware SPI
  94. spi_dev = new Adafruit_SPIDevice(_csxg);
  95. if (!_magSensor.begin_SPI(_csm))
  96. return false;
  97. } else {
  98. // Software SPI
  99. spi_dev = new Adafruit_SPIDevice(_csxg, _clk, _miso, _mosi);
  100. if (!_magSensor.begin_SPI(_csm, _clk, _miso, _mosi))
  101. return false;
  102. }
  103. if (!spi_dev->begin())
  104. return false;
  105. }
  106. // soft reset & reboot accel/gyro
  107. write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG8, 0x05);
  108. delay(10);
  109. uint8_t id = read8(XGTYPE, LSM9DS1_REGISTER_WHO_AM_I_XG);
  110. if (id != LSM9DS1_XG_ID)
  111. return false;
  112. // enable gyro continuous
  113. write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG1_G, 0xC0); // on XYZ
  114. // Enable the accelerometer continous
  115. write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG5_XL, 0x38); // enable X Y and Z axis
  116. write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG6_XL,
  117. 0xC0); // 1 KHz out data rate, BW set by ODR, 408Hz anti-aliasing
  118. // enable mag continuous
  119. _magSensor.setOperationMode(LIS3MDL_CONTINUOUSMODE);
  120. // Set default ranges for the various sensors
  121. setupAccel(LSM9DS1_ACCELRANGE_2G, LSM9DS1_ACCELDATARATE_10HZ);
  122. setupMag(LSM9DS1_MAGGAIN_4GAUSS);
  123. setupGyro(LSM9DS1_GYROSCALE_245DPS);
  124. return true;
  125. }
  126. /***************************************************************************
  127. PUBLIC FUNCTIONS
  128. ***************************************************************************/
  129. /**************************************************************************/
  130. /*!
  131. @brief Read all four sensor subcomponents
  132. */
  133. /**************************************************************************/
  134. void Adafruit_LSM9DS1::read() {
  135. /* Read all the sensors. */
  136. readAccel();
  137. readGyro();
  138. readTemp();
  139. readMag();
  140. }
  141. /**************************************************************************/
  142. /*!
  143. @brief Read the sensor magnetometer sensor component
  144. */
  145. /**************************************************************************/
  146. void Adafruit_LSM9DS1::readMag() {
  147. _magSensor.read();
  148. magData.x = _magSensor.x;
  149. magData.y = _magSensor.y;
  150. magData.z = _magSensor.z;
  151. }
  152. /**************************************************************************/
  153. /*!
  154. @brief Read the sensor accelerometer sensor component
  155. */
  156. /**************************************************************************/
  157. void Adafruit_LSM9DS1::readAccel() {
  158. // Read the accelerometer
  159. byte buffer[6];
  160. readBuffer(XGTYPE, 0x80 | LSM9DS1_REGISTER_OUT_X_L_XL, 6, buffer);
  161. uint8_t xlo = buffer[0];
  162. int16_t xhi = buffer[1];
  163. uint8_t ylo = buffer[2];
  164. int16_t yhi = buffer[3];
  165. uint8_t zlo = buffer[4];
  166. int16_t zhi = buffer[5];
  167. // Shift values to create properly formed integer (low byte first)
  168. xhi <<= 8;
  169. xhi |= xlo;
  170. yhi <<= 8;
  171. yhi |= ylo;
  172. zhi <<= 8;
  173. zhi |= zlo;
  174. accelData.x = xhi;
  175. accelData.y = yhi;
  176. accelData.z = zhi;
  177. }
  178. /**************************************************************************/
  179. /*!
  180. @brief Read the sensor gyroscope sensor component
  181. */
  182. /**************************************************************************/
  183. void Adafruit_LSM9DS1::readGyro() {
  184. // Read gyro
  185. byte buffer[6];
  186. readBuffer(XGTYPE, 0x80 | LSM9DS1_REGISTER_OUT_X_L_G, 6, buffer);
  187. uint8_t xlo = buffer[0];
  188. int16_t xhi = buffer[1];
  189. uint8_t ylo = buffer[2];
  190. int16_t yhi = buffer[3];
  191. uint8_t zlo = buffer[4];
  192. int16_t zhi = buffer[5];
  193. // Shift values to create properly formed integer (low byte first)
  194. xhi <<= 8;
  195. xhi |= xlo;
  196. yhi <<= 8;
  197. yhi |= ylo;
  198. zhi <<= 8;
  199. zhi |= zlo;
  200. gyroData.x = xhi;
  201. gyroData.y = yhi;
  202. gyroData.z = zhi;
  203. }
  204. /**************************************************************************/
  205. /*!
  206. @brief Read the sensor temperature sensor component
  207. */
  208. /**************************************************************************/
  209. void Adafruit_LSM9DS1::readTemp() {
  210. // Read temp sensor
  211. byte buffer[2];
  212. readBuffer(XGTYPE, 0x80 | LSM9DS1_REGISTER_TEMP_OUT_L, 2, buffer);
  213. uint8_t xlo = buffer[0];
  214. int16_t xhi = buffer[1];
  215. xhi <<= 8;
  216. xhi |= xlo;
  217. // Shift values to create properly formed integer (low byte first)
  218. temperature = xhi;
  219. }
  220. /**************************************************************************/
  221. /*!
  222. @brief Configure the accelerometer ranging
  223. @param range Can be LSM9DS1_ACCELRANGE_2G, LSM9DS1_ACCELRANGE_4G,
  224. LSM9DS1_ACCELRANGE_8G, LSM9DS1_ACCELRANGE_16G
  225. @param rate Can be LSM9DS1_ACCELDATARATE_10HZ, LSM9DS1_ACCELDATARATE_50HZ,
  226. LSM9DS1_ACCELDATARATE_119HZ, LSM9DS1_ACCELDATARATE_238HZ,
  227. LSM9DS1_ACCELDATARATE_476HZ, LSM9DS1_ACCELDATARATE_952HZ
  228. */
  229. /**************************************************************************/
  230. void Adafruit_LSM9DS1::setupAccel(lsm9ds1AccelRange_t range,
  231. lsm9ds1AccelDataRate_t rate) {
  232. uint8_t reg = read8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG6_XL);
  233. reg &= ~(0b11111000);
  234. reg |= range | rate;
  235. // Serial.println("set range: ");
  236. write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG6_XL, reg);
  237. switch (range) {
  238. case LSM9DS1_ACCELRANGE_2G:
  239. _accel_mg_lsb = LSM9DS1_ACCEL_MG_LSB_2G;
  240. break;
  241. case LSM9DS1_ACCELRANGE_4G:
  242. _accel_mg_lsb = LSM9DS1_ACCEL_MG_LSB_4G;
  243. break;
  244. case LSM9DS1_ACCELRANGE_8G:
  245. _accel_mg_lsb = LSM9DS1_ACCEL_MG_LSB_8G;
  246. break;
  247. case LSM9DS1_ACCELRANGE_16G:
  248. _accel_mg_lsb = LSM9DS1_ACCEL_MG_LSB_16G;
  249. break;
  250. }
  251. }
  252. /**************************************************************************/
  253. /*!
  254. @brief Configure the magnetometer gain
  255. @param gain Can be LSM9DS1_MAGGAIN_4GAUSS, LSM9DS1_MAGGAIN_8GAUSS,
  256. LSM9DS1_MAGGAIN_12GAUSS, LSM9DS1_MAGGAIN_16GAUSS
  257. */
  258. /**************************************************************************/
  259. void Adafruit_LSM9DS1::setupMag(lsm9ds1MagGain_t gain) {
  260. switch (gain) {
  261. case LSM9DS1_MAGGAIN_4GAUSS:
  262. _magSensor.setRange(LIS3MDL_RANGE_4_GAUSS);
  263. break;
  264. case LSM9DS1_MAGGAIN_8GAUSS:
  265. _magSensor.setRange(LIS3MDL_RANGE_8_GAUSS);
  266. break;
  267. case LSM9DS1_MAGGAIN_12GAUSS:
  268. _magSensor.setRange(LIS3MDL_RANGE_12_GAUSS);
  269. break;
  270. case LSM9DS1_MAGGAIN_16GAUSS:
  271. _magSensor.setRange(LIS3MDL_RANGE_16_GAUSS);
  272. break;
  273. }
  274. }
  275. /**************************************************************************/
  276. /*!
  277. @brief Configure the gyroscope scaling
  278. @param scale Can be LSM9DS1_GYROSCALE_245DPS, LSM9DS1_GYROSCALE_500DPS
  279. or LSM9DS1_GYROSCALE_2000DPS
  280. */
  281. /**************************************************************************/
  282. void Adafruit_LSM9DS1::setupGyro(lsm9ds1GyroScale_t scale) {
  283. uint8_t reg = read8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG1_G);
  284. reg &= ~(0b00011000);
  285. reg |= scale;
  286. write8(XGTYPE, LSM9DS1_REGISTER_CTRL_REG1_G, reg);
  287. switch (scale) {
  288. case LSM9DS1_GYROSCALE_245DPS:
  289. _gyro_dps_digit = LSM9DS1_GYRO_DPS_DIGIT_245DPS;
  290. break;
  291. case LSM9DS1_GYROSCALE_500DPS:
  292. _gyro_dps_digit = LSM9DS1_GYRO_DPS_DIGIT_500DPS;
  293. break;
  294. case LSM9DS1_GYROSCALE_2000DPS:
  295. _gyro_dps_digit = LSM9DS1_GYRO_DPS_DIGIT_2000DPS;
  296. break;
  297. }
  298. }
  299. /***************************************************************************
  300. UNIFIED SENSOR FUNCTIONS
  301. ***************************************************************************/
  302. /**************************************************************************/
  303. /*!
  304. @brief Gets the most recent accel sensor events for all 4 sensors
  305. @param accelEvent The accelerometer event object we will fill, pass NULL to
  306. skip
  307. @param magEvent The magnetometer event object we will fill, pass NULL to
  308. skip
  309. @param gyroEvent The gyroscope event object we will fill, pass NULL to skip
  310. @param tempEvent The temperature event object we will fill, pass NULL to
  311. skip
  312. @returns True on successful reads
  313. */
  314. /**************************************************************************/
  315. bool Adafruit_LSM9DS1::getEvent(sensors_event_t *accelEvent,
  316. sensors_event_t *magEvent,
  317. sensors_event_t *gyroEvent,
  318. sensors_event_t *tempEvent) {
  319. /* Grab new sensor reading and timestamp. */
  320. read();
  321. uint32_t timestamp = millis();
  322. /* Update appropriate sensor events. */
  323. if (accelEvent)
  324. getAccelEvent(accelEvent, timestamp);
  325. if (magEvent)
  326. _magSensor.getEvent(magEvent);
  327. if (gyroEvent)
  328. getGyroEvent(gyroEvent, timestamp);
  329. if (tempEvent)
  330. getTempEvent(tempEvent, timestamp);
  331. return true;
  332. }
  333. /**************************************************************************/
  334. /*!
  335. @brief Gets the sensor_t data for all 4 sub-sensors at once call
  336. @param accel The accelerometer sensor_t object we will fill, pass NULL to
  337. skip
  338. @param mag The magnetometer sensor_t object we will fill, pass NULL to skip
  339. @param gyro The gyroscope sensor_t object we will fill, pass NULL to skip
  340. @param temp The temperature sensor_t object we will fill, pass NULL to skip
  341. */
  342. /**************************************************************************/
  343. void Adafruit_LSM9DS1::getSensor(sensor_t *accel, sensor_t *mag, sensor_t *gyro,
  344. sensor_t *temp) {
  345. /* Update appropriate sensor metadata. */
  346. if (accel)
  347. getAccelSensor(accel);
  348. if (mag)
  349. _magSensor.getSensor(mag);
  350. if (gyro)
  351. getGyroSensor(gyro);
  352. if (temp)
  353. getTempSensor(temp);
  354. }
  355. /**************************************************************************/
  356. /*!
  357. @brief Fill in the details about the most recent accelerometer data read
  358. @param event The sensor_event_t object we will fill!
  359. @param timestamp Unused
  360. */
  361. /**************************************************************************/
  362. void Adafruit_LSM9DS1::getAccelEvent(sensors_event_t *event,
  363. uint32_t timestamp) {
  364. memset(event, 0, sizeof(sensors_event_t));
  365. event->version = sizeof(sensors_event_t);
  366. event->sensor_id = _lsm9dso_sensorid_accel;
  367. event->type = SENSOR_TYPE_ACCELEROMETER;
  368. event->timestamp = timestamp;
  369. event->acceleration.x = accelData.x * _accel_mg_lsb;
  370. event->acceleration.x /= 1000;
  371. event->acceleration.x *= SENSORS_GRAVITY_STANDARD;
  372. event->acceleration.y = accelData.y * _accel_mg_lsb;
  373. event->acceleration.y /= 1000;
  374. event->acceleration.y *= SENSORS_GRAVITY_STANDARD;
  375. event->acceleration.z = accelData.z * _accel_mg_lsb;
  376. event->acceleration.z /= 1000;
  377. event->acceleration.z *= SENSORS_GRAVITY_STANDARD;
  378. }
  379. /**************************************************************************/
  380. /*!
  381. @brief Fill in the details about the most recent magnetometer data read
  382. @param event The sensor_event_t object we will fill!
  383. @param timestamp Unused
  384. */
  385. /**************************************************************************/
  386. void Adafruit_LSM9DS1::getMagEvent(sensors_event_t *event, uint32_t timestamp) {
  387. _magSensor.getEvent(event);
  388. }
  389. /**************************************************************************/
  390. /*!
  391. @brief Fill in the details about the most recent gyroscope data read
  392. @param event The sensor_event_t object we will fill!
  393. @param timestamp The millis timestamp when the read occured
  394. */
  395. /**************************************************************************/
  396. void Adafruit_LSM9DS1::getGyroEvent(sensors_event_t *event,
  397. uint32_t timestamp) {
  398. memset(event, 0, sizeof(sensors_event_t));
  399. event->version = sizeof(sensors_event_t);
  400. event->sensor_id = _lsm9dso_sensorid_accel;
  401. event->type = SENSOR_TYPE_GYROSCOPE;
  402. event->timestamp = timestamp;
  403. event->gyro.x = gyroData.x * _gyro_dps_digit * SENSORS_DPS_TO_RADS;
  404. event->gyro.y = gyroData.y * _gyro_dps_digit * SENSORS_DPS_TO_RADS;
  405. event->gyro.z = gyroData.z * _gyro_dps_digit * SENSORS_DPS_TO_RADS;
  406. }
  407. /**************************************************************************/
  408. /*!
  409. @brief Fill in the details about the most recent temperature data read
  410. @param event The sensor_event_t object we will fill!
  411. @param timestamp The millis timestamp when the read occured
  412. */
  413. /**************************************************************************/
  414. void Adafruit_LSM9DS1::getTempEvent(sensors_event_t *event,
  415. uint32_t timestamp) {
  416. memset(event, 0, sizeof(sensors_event_t));
  417. event->version = sizeof(sensors_event_t);
  418. event->sensor_id = _lsm9dso_sensorid_temp;
  419. event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  420. event->timestamp = timestamp;
  421. // This is just a guess since the staring point (21C here) isn't documented :(
  422. event->temperature = 21.0 + (float)temperature / 8;
  423. // event->temperature /= LSM9DS1_TEMP_LSB_DEGREE_CELSIUS;
  424. }
  425. /**************************************************************************/
  426. /*!
  427. @brief Fill in the details about the accelerometer sensor component
  428. @param sensor The sensor_t object we will fill!
  429. */
  430. /**************************************************************************/
  431. void Adafruit_LSM9DS1::getAccelSensor(sensor_t *sensor) {
  432. memset(sensor, 0, sizeof(sensor_t));
  433. strncpy(sensor->name, "LSM9DS1_A", sizeof(sensor->name) - 1);
  434. sensor->name[sizeof(sensor->name) - 1] = 0;
  435. sensor->version = 1;
  436. sensor->sensor_id = _lsm9dso_sensorid_accel;
  437. sensor->type = SENSOR_TYPE_ACCELEROMETER;
  438. sensor->min_delay = 0;
  439. sensor->max_value = 156.8; // +16 g = 156.8 m/s^s
  440. sensor->min_value = -156.8; // -16 g = 156.8 m/s^s
  441. sensor->resolution = 0.0005978; // 0.061 mg = 0.0005978 m/s^2
  442. }
  443. void Adafruit_LSM9DS1::getMagSensor(sensor_t *sensor) {
  444. _magSensor.getSensor(sensor);
  445. }
  446. /**************************************************************************/
  447. /*!
  448. @brief Fill in the details about the gyroscope sensor component
  449. @param sensor The sensor_t object we will fill!
  450. */
  451. /**************************************************************************/
  452. void Adafruit_LSM9DS1::getGyroSensor(sensor_t *sensor) {
  453. memset(sensor, 0, sizeof(sensor_t));
  454. strncpy(sensor->name, "LSM9DS1_G", sizeof(sensor->name) - 1);
  455. sensor->name[sizeof(sensor->name) - 1] = 0;
  456. sensor->version = 1;
  457. sensor->sensor_id = _lsm9dso_sensorid_gyro;
  458. sensor->type = SENSOR_TYPE_GYROSCOPE;
  459. sensor->min_delay = 0;
  460. sensor->max_value = 34.91; // +2000 dps = 34.906586 rad/s
  461. sensor->min_value = -34.91; // "
  462. sensor->resolution = 0.00015271631375; // 8.75 mdps = 0.00015271631375 rad/s
  463. }
  464. /**************************************************************************/
  465. /*!
  466. @brief Fill in the details about the temperature sensor component
  467. @param sensor The sensor_t object we will fill!
  468. */
  469. /**************************************************************************/
  470. void Adafruit_LSM9DS1::getTempSensor(sensor_t *sensor) {
  471. memset(sensor, 0, sizeof(sensor_t));
  472. strncpy(sensor->name, "LSM9DS1_T", sizeof(sensor->name) - 1);
  473. sensor->name[sizeof(sensor->name) - 1] = 0;
  474. sensor->version = 1;
  475. sensor->sensor_id = _lsm9dso_sensorid_temp;
  476. sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  477. sensor->min_delay = 0;
  478. sensor->max_value = 0.0; // ToDo
  479. sensor->min_value = 0.0; // ToDo
  480. sensor->resolution = 0.0; // ToDo
  481. }
  482. /***************************************************************************
  483. PRIVATE FUNCTIONS
  484. ***************************************************************************/
  485. void Adafruit_LSM9DS1::initSensor(int32_t sensorID) {
  486. _lsm9dso_sensorid_accel = sensorID + 1;
  487. //_lsm9dso_sensorid_mag = sensorID + 2;
  488. _lsm9dso_sensorid_gyro = sensorID + 3;
  489. _lsm9dso_sensorid_temp = sensorID + 4;
  490. _accelSensor = Sensor(this, &Adafruit_LSM9DS1::readAccel,
  491. &Adafruit_LSM9DS1::getAccelEvent,
  492. &Adafruit_LSM9DS1::getAccelSensor);
  493. _gyroSensor =
  494. Sensor(this, &Adafruit_LSM9DS1::readGyro, &Adafruit_LSM9DS1::getGyroEvent,
  495. &Adafruit_LSM9DS1::getGyroSensor);
  496. _tempSensor =
  497. Sensor(this, &Adafruit_LSM9DS1::readTemp, &Adafruit_LSM9DS1::getTempEvent,
  498. &Adafruit_LSM9DS1::getTempSensor);
  499. }
  500. void Adafruit_LSM9DS1::write8(boolean type, byte reg, byte value) {
  501. // support for writing directly to magnetometer registers removed
  502. // should access via _magSensor methods
  503. if (type == MAGTYPE)
  504. return;
  505. uint8_t buffer[2] = {i2c_dev ? uint8_t(reg) : uint8_t(reg & 0x7F), value};
  506. if (i2c_dev) {
  507. i2c_dev->write(buffer, 2);
  508. } else {
  509. spi_dev->write(buffer, 2);
  510. }
  511. }
  512. byte Adafruit_LSM9DS1::read8(boolean type, byte reg) {
  513. uint8_t value;
  514. readBuffer(type, reg, 1, &value);
  515. return value;
  516. }
  517. byte Adafruit_LSM9DS1::readBuffer(boolean type, byte reg, byte len,
  518. uint8_t *buffer) {
  519. // support for writing directly to magnetometer registers removed
  520. // should access via _magSensor methods
  521. if (type == MAGTYPE)
  522. return 0;
  523. uint8_t regbuf[1] = {i2c_dev ? uint8_t(reg) : uint8_t(reg | 0x80)};
  524. if (i2c_dev) {
  525. i2c_dev->write_then_read(regbuf, 1, buffer, len);
  526. } else {
  527. spi_dev->write_then_read(regbuf, 1, buffer, len);
  528. }
  529. return len;
  530. }