/ Adafruit_LIS331HH.cpp
Adafruit_LIS331HH.cpp
1 /*! 2 * @file Adafruit_LIS331HH.cpp 3 */ 4 5 #include "Arduino.h" 6 7 #include <Adafruit_LIS331HH.h> 8 #include <Wire.h> 9 10 /*! 11 * @brief Instantiates a new H3LIS331 class in I2C 12 */ 13 Adafruit_LIS331HH::Adafruit_LIS331HH(){}; 14 15 /*! 16 * @brief Sets up the hardware and initializes I2C 17 * @param i2c_address 18 * The I2C address to be used. 19 * @param wire 20 * The Wire object to be used for I2C connections. 21 * @param sensor_id 22 * The user-defined ID to differentiate different sensors 23 * @return True if initialization was successful, otherwise false. 24 */ 25 bool Adafruit_LIS331HH::begin_I2C(uint8_t i2c_address, TwoWire *wire, 26 int32_t sensor_id) { 27 if (i2c_dev) { 28 delete i2c_dev; // remove old interface 29 } 30 31 i2c_dev = new Adafruit_I2CDevice(i2c_address, wire); 32 33 if (!i2c_dev->begin()) { 34 return false; 35 } 36 37 return _init(sensor_id); 38 } 39 40 /*! 41 * @brief Sets up the hardware and initializes hardware SPI 42 * @param cs_pin The arduino pin # connected to chip select 43 * @param theSPI The SPI object to be used for SPI connections. 44 * @param sensor_id 45 * The user-defined ID to differentiate different sensors 46 * @return True if initialization was successful, otherwise false. 47 */ 48 bool Adafruit_LIS331HH::begin_SPI(uint8_t cs_pin, SPIClass *theSPI, 49 int32_t sensor_id) { 50 i2c_dev = NULL; 51 52 if (spi_dev) { 53 delete spi_dev; // remove old interface 54 } 55 spi_dev = new Adafruit_SPIDevice(cs_pin, 56 1000000, // frequency 57 SPI_BITORDER_MSBFIRST, // bit order 58 SPI_MODE0, // data mode 59 theSPI); 60 if (!spi_dev->begin()) { 61 return false; 62 } 63 64 return _init(sensor_id); 65 } 66 67 /*! 68 * @brief Sets up the hardware and initializes software SPI 69 * @param cs_pin The arduino pin # connected to chip select 70 * @param sck_pin The arduino pin # connected to SPI clock 71 * @param miso_pin The arduino pin # connected to SPI MISO 72 * @param mosi_pin The arduino pin # connected to SPI MOSI 73 * @param sensor_id 74 * The user-defined ID to differentiate different sensors 75 * @return True if initialization was successful, otherwise false. 76 */ 77 bool Adafruit_LIS331HH::begin_SPI(int8_t cs_pin, int8_t sck_pin, 78 int8_t miso_pin, int8_t mosi_pin, 79 int32_t sensor_id) { 80 i2c_dev = NULL; 81 82 if (spi_dev) { 83 delete spi_dev; // remove old interface 84 } 85 spi_dev = new Adafruit_SPIDevice(cs_pin, sck_pin, miso_pin, mosi_pin, 86 1000000, // frequency 87 SPI_BITORDER_MSBFIRST, // bit order 88 SPI_MODE0); // data mode 89 if (!spi_dev->begin()) { 90 return false; 91 } 92 93 return _init(sensor_id); 94 } 95 96 /*! @brief Initializer for post i2c/spi init 97 * @param sensor_id Optional unique ID for the sensor set 98 * @returns True if chip identified and initialized 99 */ 100 bool Adafruit_LIS331HH::_init(int32_t sensor_id) { 101 102 uint8_t device_id = getDeviceID(); 103 /* Check connection */ 104 if (device_id != LIS331_CHIP_ID) { 105 /* No H3LIS331 detected ... return false */ 106 // Serial.print("Chip ID: 0x");Serial.println(device_id, HEX); 107 return false; 108 } 109 Adafruit_BusIO_Register _ctrl1 = Adafruit_BusIO_Register( 110 i2c_dev, spi_dev, ADDRBIT8_HIGH_TOREAD, LIS331_REG_CTRL1, 1); 111 _ctrl1.write(0x07); // enable all axes, normal mode 112 113 enableHighPassFilter(false); 114 setDataRate(LIS331_DATARATE_1000_HZ); 115 setRange(LIS331HH_RANGE_24_G); 116 117 return true; 118 } 119 120 void Adafruit_LIS331HH::_scaleValues(void) { 121 122 // actually 12 bit but left justified 123 x >>= 4; 124 y >>= 4; 125 z >>= 4; 126 uint8_t range = getRange(); 127 uint16_t scale_max = 1; 128 129 if (range == LIS331HH_RANGE_6_G) 130 scale_max = 6; 131 if (range == LIS331HH_RANGE_12_G) 132 scale_max = 12; 133 if (range == LIS331HH_RANGE_24_G) 134 scale_max = 24; 135 136 float lsb_value = 2 * scale_max * (float)1 / 4096; 137 138 x_g = ((float)x * lsb_value); 139 y_g = ((float)y * lsb_value); 140 z_g = ((float)z * lsb_value); 141 } 142 143 /** 144 * @brief Sets the measurement range for the H3LIS331 145 * @param range The range to set 146 */ 147 148 void Adafruit_LIS331HH::setRange(lis331hh_range_t range) { 149 writeRange((uint8_t)range); 150 } 151 152 /** 153 * @brief Gets the measurement range for the H3LIS331 154 * @return The range value 155 */ 156 lis331hh_range_t Adafruit_LIS331HH::getRange(void) { 157 158 return (lis331hh_range_t)readRange(); 159 }