MEMS nano pressure sensor: 260-1260 hPa absolute digital output barometer.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client LPS22HB_JS ... more
LPS22HBSensor.cpp@0:3de9cb936fd1, 2017-09-04 (annotated)
- Committer:
- nikapov
- Date:
- Mon Sep 04 16:06:45 2017 +0000
- Revision:
- 0:3de9cb936fd1
- Child:
- 1:33ddb62b31fd
First version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikapov | 0:3de9cb936fd1 | 1 | /** |
nikapov | 0:3de9cb936fd1 | 2 | ****************************************************************************** |
nikapov | 0:3de9cb936fd1 | 3 | * @file LPS22HBSensor.cpp |
nikapov | 0:3de9cb936fd1 | 4 | * @author CLab |
nikapov | 0:3de9cb936fd1 | 5 | * @version V1.0.0 |
nikapov | 0:3de9cb936fd1 | 6 | * @date 5 August 2016 |
nikapov | 0:3de9cb936fd1 | 7 | * @brief Implementation of an LPS22HB Pressure sensor. |
nikapov | 0:3de9cb936fd1 | 8 | ****************************************************************************** |
nikapov | 0:3de9cb936fd1 | 9 | * @attention |
nikapov | 0:3de9cb936fd1 | 10 | * |
nikapov | 0:3de9cb936fd1 | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
nikapov | 0:3de9cb936fd1 | 12 | * |
nikapov | 0:3de9cb936fd1 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
nikapov | 0:3de9cb936fd1 | 14 | * are permitted provided that the following conditions are met: |
nikapov | 0:3de9cb936fd1 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
nikapov | 0:3de9cb936fd1 | 16 | * this list of conditions and the following disclaimer. |
nikapov | 0:3de9cb936fd1 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nikapov | 0:3de9cb936fd1 | 18 | * this list of conditions and the following disclaimer in the documentation |
nikapov | 0:3de9cb936fd1 | 19 | * and/or other materials provided with the distribution. |
nikapov | 0:3de9cb936fd1 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nikapov | 0:3de9cb936fd1 | 21 | * may be used to endorse or promote products derived from this software |
nikapov | 0:3de9cb936fd1 | 22 | * without specific prior written permission. |
nikapov | 0:3de9cb936fd1 | 23 | * |
nikapov | 0:3de9cb936fd1 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nikapov | 0:3de9cb936fd1 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nikapov | 0:3de9cb936fd1 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nikapov | 0:3de9cb936fd1 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nikapov | 0:3de9cb936fd1 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nikapov | 0:3de9cb936fd1 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nikapov | 0:3de9cb936fd1 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nikapov | 0:3de9cb936fd1 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nikapov | 0:3de9cb936fd1 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nikapov | 0:3de9cb936fd1 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nikapov | 0:3de9cb936fd1 | 34 | * |
nikapov | 0:3de9cb936fd1 | 35 | ****************************************************************************** |
nikapov | 0:3de9cb936fd1 | 36 | */ |
nikapov | 0:3de9cb936fd1 | 37 | |
nikapov | 0:3de9cb936fd1 | 38 | |
nikapov | 0:3de9cb936fd1 | 39 | /* Includes ------------------------------------------------------------------*/ |
nikapov | 0:3de9cb936fd1 | 40 | |
nikapov | 0:3de9cb936fd1 | 41 | |
nikapov | 0:3de9cb936fd1 | 42 | #include "LPS22HBSensor.h" |
nikapov | 0:3de9cb936fd1 | 43 | |
nikapov | 0:3de9cb936fd1 | 44 | |
nikapov | 0:3de9cb936fd1 | 45 | /* Class Implementation ------------------------------------------------------*/ |
nikapov | 0:3de9cb936fd1 | 46 | |
nikapov | 0:3de9cb936fd1 | 47 | /** Constructor |
nikapov | 0:3de9cb936fd1 | 48 | * @param i2c object of an helper class which handles the I2C peripheral |
nikapov | 0:3de9cb936fd1 | 49 | * @param address the address of the component's instance |
nikapov | 0:3de9cb936fd1 | 50 | */ |
nikapov | 0:3de9cb936fd1 | 51 | LPS22HBSensor::LPS22HBSensor(DevI2C &i2c) : _dev_i2c(i2c) |
nikapov | 0:3de9cb936fd1 | 52 | { |
nikapov | 0:3de9cb936fd1 | 53 | _address = LPS22HB_ADDRESS_HIGH; |
nikapov | 0:3de9cb936fd1 | 54 | }; |
nikapov | 0:3de9cb936fd1 | 55 | |
nikapov | 0:3de9cb936fd1 | 56 | |
nikapov | 0:3de9cb936fd1 | 57 | /** Constructor |
nikapov | 0:3de9cb936fd1 | 58 | * @param i2c object of an helper class which handles the I2C peripheral |
nikapov | 0:3de9cb936fd1 | 59 | * @param address the address of the component's instance |
nikapov | 0:3de9cb936fd1 | 60 | */ |
nikapov | 0:3de9cb936fd1 | 61 | LPS22HBSensor::LPS22HBSensor(DevI2C &i2c, uint8_t address) : _dev_i2c(i2c), _address(address) |
nikapov | 0:3de9cb936fd1 | 62 | { |
nikapov | 0:3de9cb936fd1 | 63 | |
nikapov | 0:3de9cb936fd1 | 64 | }; |
nikapov | 0:3de9cb936fd1 | 65 | |
nikapov | 0:3de9cb936fd1 | 66 | /** |
nikapov | 0:3de9cb936fd1 | 67 | * @brief Initializing the component. |
nikapov | 0:3de9cb936fd1 | 68 | * @param[in] init pointer to device specific initalization structure. |
nikapov | 0:3de9cb936fd1 | 69 | * @retval "0" in case of success, an error code otherwise. |
nikapov | 0:3de9cb936fd1 | 70 | */ |
nikapov | 0:3de9cb936fd1 | 71 | int LPS22HBSensor::init(void *init) |
nikapov | 0:3de9cb936fd1 | 72 | { |
nikapov | 0:3de9cb936fd1 | 73 | if ( LPS22HB_Set_PowerMode( (void *)this, LPS22HB_LowPower) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 74 | { |
nikapov | 0:3de9cb936fd1 | 75 | return 1; |
nikapov | 0:3de9cb936fd1 | 76 | } |
nikapov | 0:3de9cb936fd1 | 77 | |
nikapov | 0:3de9cb936fd1 | 78 | /* Power down the device */ |
nikapov | 0:3de9cb936fd1 | 79 | if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_ONE_SHOT ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 80 | { |
nikapov | 0:3de9cb936fd1 | 81 | return 1; |
nikapov | 0:3de9cb936fd1 | 82 | } |
nikapov | 0:3de9cb936fd1 | 83 | |
nikapov | 0:3de9cb936fd1 | 84 | /* Disable low-pass filter on LPS22HB pressure data */ |
nikapov | 0:3de9cb936fd1 | 85 | if( LPS22HB_Set_LowPassFilter( (void *)this, LPS22HB_DISABLE) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 86 | { |
nikapov | 0:3de9cb936fd1 | 87 | return 1; |
nikapov | 0:3de9cb936fd1 | 88 | } |
nikapov | 0:3de9cb936fd1 | 89 | |
nikapov | 0:3de9cb936fd1 | 90 | /* Set low-pass filter cutoff configuration*/ |
nikapov | 0:3de9cb936fd1 | 91 | if( LPS22HB_Set_LowPassFilterCutoff( (void *)this, LPS22HB_ODR_9) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 92 | { |
nikapov | 0:3de9cb936fd1 | 93 | return 1; |
nikapov | 0:3de9cb936fd1 | 94 | } |
nikapov | 0:3de9cb936fd1 | 95 | |
nikapov | 0:3de9cb936fd1 | 96 | /* Set block data update mode */ |
nikapov | 0:3de9cb936fd1 | 97 | if ( LPS22HB_Set_Bdu( (void *)this, LPS22HB_BDU_NO_UPDATE ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 98 | { |
nikapov | 0:3de9cb936fd1 | 99 | return 1; |
nikapov | 0:3de9cb936fd1 | 100 | } |
nikapov | 0:3de9cb936fd1 | 101 | |
nikapov | 0:3de9cb936fd1 | 102 | /* Set automatic increment for multi-byte read/write */ |
nikapov | 0:3de9cb936fd1 | 103 | if( LPS22HB_Set_AutomaticIncrementRegAddress( (void *)this, LPS22HB_ENABLE) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 104 | { |
nikapov | 0:3de9cb936fd1 | 105 | return 1; |
nikapov | 0:3de9cb936fd1 | 106 | } |
nikapov | 0:3de9cb936fd1 | 107 | |
nikapov | 0:3de9cb936fd1 | 108 | _is_enabled = 0; |
nikapov | 0:3de9cb936fd1 | 109 | _last_odr = 25.0f; |
nikapov | 0:3de9cb936fd1 | 110 | |
nikapov | 0:3de9cb936fd1 | 111 | return 0; |
nikapov | 0:3de9cb936fd1 | 112 | } |
nikapov | 0:3de9cb936fd1 | 113 | |
nikapov | 0:3de9cb936fd1 | 114 | |
nikapov | 0:3de9cb936fd1 | 115 | /** |
nikapov | 0:3de9cb936fd1 | 116 | * @brief Enable LPS22HB |
nikapov | 0:3de9cb936fd1 | 117 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:3de9cb936fd1 | 118 | */ |
nikapov | 0:3de9cb936fd1 | 119 | int LPS22HBSensor::enable(void) |
nikapov | 0:3de9cb936fd1 | 120 | { |
nikapov | 0:3de9cb936fd1 | 121 | /* Check if the component is already enabled */ |
nikapov | 0:3de9cb936fd1 | 122 | if ( _is_enabled == 1 ) |
nikapov | 0:3de9cb936fd1 | 123 | { |
nikapov | 0:3de9cb936fd1 | 124 | return 0; |
nikapov | 0:3de9cb936fd1 | 125 | } |
nikapov | 0:3de9cb936fd1 | 126 | |
nikapov | 0:3de9cb936fd1 | 127 | if(Set_ODR_When_Enabled(_last_odr) == 1) |
nikapov | 0:3de9cb936fd1 | 128 | { |
nikapov | 0:3de9cb936fd1 | 129 | return 1; |
nikapov | 0:3de9cb936fd1 | 130 | } |
nikapov | 0:3de9cb936fd1 | 131 | |
nikapov | 0:3de9cb936fd1 | 132 | _is_enabled = 1; |
nikapov | 0:3de9cb936fd1 | 133 | |
nikapov | 0:3de9cb936fd1 | 134 | return 0; |
nikapov | 0:3de9cb936fd1 | 135 | } |
nikapov | 0:3de9cb936fd1 | 136 | |
nikapov | 0:3de9cb936fd1 | 137 | /** |
nikapov | 0:3de9cb936fd1 | 138 | * @brief Disable LPS22HB |
nikapov | 0:3de9cb936fd1 | 139 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:3de9cb936fd1 | 140 | */ |
nikapov | 0:3de9cb936fd1 | 141 | int LPS22HBSensor::disable(void) |
nikapov | 0:3de9cb936fd1 | 142 | { |
nikapov | 0:3de9cb936fd1 | 143 | /* Check if the component is already disabled */ |
nikapov | 0:3de9cb936fd1 | 144 | if ( _is_enabled == 0 ) |
nikapov | 0:3de9cb936fd1 | 145 | { |
nikapov | 0:3de9cb936fd1 | 146 | return 0; |
nikapov | 0:3de9cb936fd1 | 147 | } |
nikapov | 0:3de9cb936fd1 | 148 | |
nikapov | 0:3de9cb936fd1 | 149 | /* Power down the device */ |
nikapov | 0:3de9cb936fd1 | 150 | if ( LPS22HB_Set_Odr( (void *)this, LPS22HB_ODR_ONE_SHOT ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 151 | { |
nikapov | 0:3de9cb936fd1 | 152 | return 1; |
nikapov | 0:3de9cb936fd1 | 153 | } |
nikapov | 0:3de9cb936fd1 | 154 | |
nikapov | 0:3de9cb936fd1 | 155 | _is_enabled = 0; |
nikapov | 0:3de9cb936fd1 | 156 | |
nikapov | 0:3de9cb936fd1 | 157 | return 0; |
nikapov | 0:3de9cb936fd1 | 158 | } |
nikapov | 0:3de9cb936fd1 | 159 | |
nikapov | 0:3de9cb936fd1 | 160 | /** |
nikapov | 0:3de9cb936fd1 | 161 | * @brief Read ID address of LPS22HB |
nikapov | 0:3de9cb936fd1 | 162 | * @param id the pointer where the ID of the device is stored |
nikapov | 0:3de9cb936fd1 | 163 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:3de9cb936fd1 | 164 | */ |
nikapov | 0:3de9cb936fd1 | 165 | int LPS22HBSensor::read_id(uint8_t *id) |
nikapov | 0:3de9cb936fd1 | 166 | { |
nikapov | 0:3de9cb936fd1 | 167 | if(!id) |
nikapov | 0:3de9cb936fd1 | 168 | { |
nikapov | 0:3de9cb936fd1 | 169 | return 1; |
nikapov | 0:3de9cb936fd1 | 170 | } |
nikapov | 0:3de9cb936fd1 | 171 | |
nikapov | 0:3de9cb936fd1 | 172 | /* Read WHO AM I register */ |
nikapov | 0:3de9cb936fd1 | 173 | if ( LPS22HB_Get_DeviceID( (void *)this, id ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 174 | { |
nikapov | 0:3de9cb936fd1 | 175 | return 1; |
nikapov | 0:3de9cb936fd1 | 176 | } |
nikapov | 0:3de9cb936fd1 | 177 | |
nikapov | 0:3de9cb936fd1 | 178 | return 0; |
nikapov | 0:3de9cb936fd1 | 179 | } |
nikapov | 0:3de9cb936fd1 | 180 | |
nikapov | 0:3de9cb936fd1 | 181 | /** |
nikapov | 0:3de9cb936fd1 | 182 | * @brief Reboot memory content of LPS22HB |
nikapov | 0:3de9cb936fd1 | 183 | * @param None |
nikapov | 0:3de9cb936fd1 | 184 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:3de9cb936fd1 | 185 | */ |
nikapov | 0:3de9cb936fd1 | 186 | int LPS22HBSensor::reset(void) |
nikapov | 0:3de9cb936fd1 | 187 | { |
nikapov | 0:3de9cb936fd1 | 188 | /* Read WHO AM I register */ |
nikapov | 0:3de9cb936fd1 | 189 | if ( LPS22HB_MemoryBoot((void *)this) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 190 | { |
nikapov | 0:3de9cb936fd1 | 191 | return 1; |
nikapov | 0:3de9cb936fd1 | 192 | } |
nikapov | 0:3de9cb936fd1 | 193 | |
nikapov | 0:3de9cb936fd1 | 194 | return 0; |
nikapov | 0:3de9cb936fd1 | 195 | } |
nikapov | 0:3de9cb936fd1 | 196 | |
nikapov | 0:3de9cb936fd1 | 197 | /** |
nikapov | 0:3de9cb936fd1 | 198 | * @brief Read LPS22HB output register, and calculate the pressure in mbar |
nikapov | 0:3de9cb936fd1 | 199 | * @param pfData the pressure value in mbar |
nikapov | 0:3de9cb936fd1 | 200 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:3de9cb936fd1 | 201 | */ |
nikapov | 0:3de9cb936fd1 | 202 | int LPS22HBSensor::get_pressure(float* pfData) |
nikapov | 0:3de9cb936fd1 | 203 | { |
nikapov | 0:3de9cb936fd1 | 204 | int32_t int32data = 0; |
nikapov | 0:3de9cb936fd1 | 205 | |
nikapov | 0:3de9cb936fd1 | 206 | /* Read data from LPS22HB. */ |
nikapov | 0:3de9cb936fd1 | 207 | if ( LPS22HB_Get_Pressure( (void *)this, &int32data ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 208 | { |
nikapov | 0:3de9cb936fd1 | 209 | return 1; |
nikapov | 0:3de9cb936fd1 | 210 | } |
nikapov | 0:3de9cb936fd1 | 211 | |
nikapov | 0:3de9cb936fd1 | 212 | *pfData = ( float )int32data / 100.0f; |
nikapov | 0:3de9cb936fd1 | 213 | |
nikapov | 0:3de9cb936fd1 | 214 | return 0; |
nikapov | 0:3de9cb936fd1 | 215 | } |
nikapov | 0:3de9cb936fd1 | 216 | |
nikapov | 0:3de9cb936fd1 | 217 | /** |
nikapov | 0:3de9cb936fd1 | 218 | * @brief Read LPS22HB output register, and calculate the temperature |
nikapov | 0:3de9cb936fd1 | 219 | * @param pfData the temperature value |
nikapov | 0:3de9cb936fd1 | 220 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:3de9cb936fd1 | 221 | */ |
nikapov | 0:3de9cb936fd1 | 222 | int LPS22HBSensor::get_temperature(float *pfData) |
nikapov | 0:3de9cb936fd1 | 223 | { |
nikapov | 0:3de9cb936fd1 | 224 | int16_t int16data = 0; |
nikapov | 0:3de9cb936fd1 | 225 | |
nikapov | 0:3de9cb936fd1 | 226 | /* Read data from LPS22HB. */ |
nikapov | 0:3de9cb936fd1 | 227 | if ( LPS22HB_Get_Temperature( (void *)this, &int16data ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 228 | { |
nikapov | 0:3de9cb936fd1 | 229 | return 1; |
nikapov | 0:3de9cb936fd1 | 230 | } |
nikapov | 0:3de9cb936fd1 | 231 | |
nikapov | 0:3de9cb936fd1 | 232 | *pfData = ( float )int16data / 10.0f; |
nikapov | 0:3de9cb936fd1 | 233 | |
nikapov | 0:3de9cb936fd1 | 234 | return 0; |
nikapov | 0:3de9cb936fd1 | 235 | } |
nikapov | 0:3de9cb936fd1 | 236 | |
nikapov | 0:3de9cb936fd1 | 237 | /** |
nikapov | 0:3de9cb936fd1 | 238 | * @brief Read LPS22HB output data rate |
nikapov | 0:3de9cb936fd1 | 239 | * @param odr the pointer to the output data rate |
nikapov | 0:3de9cb936fd1 | 240 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:3de9cb936fd1 | 241 | */ |
nikapov | 0:3de9cb936fd1 | 242 | int LPS22HBSensor::get_odr(float* odr) |
nikapov | 0:3de9cb936fd1 | 243 | { |
nikapov | 0:3de9cb936fd1 | 244 | LPS22HB_Odr_et odr_low_level; |
nikapov | 0:3de9cb936fd1 | 245 | |
nikapov | 0:3de9cb936fd1 | 246 | if ( LPS22HB_Get_Odr( (void *)this, &odr_low_level ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 247 | { |
nikapov | 0:3de9cb936fd1 | 248 | return 1; |
nikapov | 0:3de9cb936fd1 | 249 | } |
nikapov | 0:3de9cb936fd1 | 250 | |
nikapov | 0:3de9cb936fd1 | 251 | switch( odr_low_level ) |
nikapov | 0:3de9cb936fd1 | 252 | { |
nikapov | 0:3de9cb936fd1 | 253 | case LPS22HB_ODR_ONE_SHOT: |
nikapov | 0:3de9cb936fd1 | 254 | *odr = 0.0f; |
nikapov | 0:3de9cb936fd1 | 255 | break; |
nikapov | 0:3de9cb936fd1 | 256 | case LPS22HB_ODR_1HZ: |
nikapov | 0:3de9cb936fd1 | 257 | *odr = 1.0f; |
nikapov | 0:3de9cb936fd1 | 258 | break; |
nikapov | 0:3de9cb936fd1 | 259 | case LPS22HB_ODR_10HZ: |
nikapov | 0:3de9cb936fd1 | 260 | *odr = 10.0f; |
nikapov | 0:3de9cb936fd1 | 261 | break; |
nikapov | 0:3de9cb936fd1 | 262 | case LPS22HB_ODR_25HZ: |
nikapov | 0:3de9cb936fd1 | 263 | *odr = 25.0f; |
nikapov | 0:3de9cb936fd1 | 264 | break; |
nikapov | 0:3de9cb936fd1 | 265 | case LPS22HB_ODR_50HZ: |
nikapov | 0:3de9cb936fd1 | 266 | *odr = 50.0f; |
nikapov | 0:3de9cb936fd1 | 267 | break; |
nikapov | 0:3de9cb936fd1 | 268 | case LPS22HB_ODR_75HZ: |
nikapov | 0:3de9cb936fd1 | 269 | *odr = 75.0f; |
nikapov | 0:3de9cb936fd1 | 270 | break; |
nikapov | 0:3de9cb936fd1 | 271 | default: |
nikapov | 0:3de9cb936fd1 | 272 | *odr = -1.0f; |
nikapov | 0:3de9cb936fd1 | 273 | return 1; |
nikapov | 0:3de9cb936fd1 | 274 | } |
nikapov | 0:3de9cb936fd1 | 275 | |
nikapov | 0:3de9cb936fd1 | 276 | return 0; |
nikapov | 0:3de9cb936fd1 | 277 | } |
nikapov | 0:3de9cb936fd1 | 278 | |
nikapov | 0:3de9cb936fd1 | 279 | /** |
nikapov | 0:3de9cb936fd1 | 280 | * @brief Set ODR |
nikapov | 0:3de9cb936fd1 | 281 | * @param odr the output data rate to be set |
nikapov | 0:3de9cb936fd1 | 282 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:3de9cb936fd1 | 283 | */ |
nikapov | 0:3de9cb936fd1 | 284 | int LPS22HBSensor::set_odr(float odr) |
nikapov | 0:3de9cb936fd1 | 285 | { |
nikapov | 0:3de9cb936fd1 | 286 | if(_is_enabled == 1) |
nikapov | 0:3de9cb936fd1 | 287 | { |
nikapov | 0:3de9cb936fd1 | 288 | if(Set_ODR_When_Enabled(odr) == 1) |
nikapov | 0:3de9cb936fd1 | 289 | { |
nikapov | 0:3de9cb936fd1 | 290 | return 1; |
nikapov | 0:3de9cb936fd1 | 291 | } |
nikapov | 0:3de9cb936fd1 | 292 | } |
nikapov | 0:3de9cb936fd1 | 293 | else |
nikapov | 0:3de9cb936fd1 | 294 | { |
nikapov | 0:3de9cb936fd1 | 295 | if(Set_ODR_When_Disabled(odr) == 1) |
nikapov | 0:3de9cb936fd1 | 296 | { |
nikapov | 0:3de9cb936fd1 | 297 | return 1; |
nikapov | 0:3de9cb936fd1 | 298 | } |
nikapov | 0:3de9cb936fd1 | 299 | } |
nikapov | 0:3de9cb936fd1 | 300 | |
nikapov | 0:3de9cb936fd1 | 301 | return 0; |
nikapov | 0:3de9cb936fd1 | 302 | } |
nikapov | 0:3de9cb936fd1 | 303 | |
nikapov | 0:3de9cb936fd1 | 304 | |
nikapov | 0:3de9cb936fd1 | 305 | /** |
nikapov | 0:3de9cb936fd1 | 306 | * @brief Set the LPS22HB sensor output data rate when enabled |
nikapov | 0:3de9cb936fd1 | 307 | * @param odr the functional output data rate to be set |
nikapov | 0:3de9cb936fd1 | 308 | * @retval 0 in case of success |
nikapov | 0:3de9cb936fd1 | 309 | * @retval 1 in case of failure |
nikapov | 0:3de9cb936fd1 | 310 | */ |
nikapov | 0:3de9cb936fd1 | 311 | int LPS22HBSensor::Set_ODR_When_Enabled( float odr ) |
nikapov | 0:3de9cb936fd1 | 312 | { |
nikapov | 0:3de9cb936fd1 | 313 | LPS22HB_Odr_et new_odr; |
nikapov | 0:3de9cb936fd1 | 314 | |
nikapov | 0:3de9cb936fd1 | 315 | new_odr = ( odr <= 1.0f ) ? LPS22HB_ODR_1HZ |
nikapov | 0:3de9cb936fd1 | 316 | : ( odr <= 10.0f ) ? LPS22HB_ODR_10HZ |
nikapov | 0:3de9cb936fd1 | 317 | : ( odr <= 25.0f ) ? LPS22HB_ODR_25HZ |
nikapov | 0:3de9cb936fd1 | 318 | : ( odr <= 50.0f ) ? LPS22HB_ODR_50HZ |
nikapov | 0:3de9cb936fd1 | 319 | : LPS22HB_ODR_75HZ; |
nikapov | 0:3de9cb936fd1 | 320 | |
nikapov | 0:3de9cb936fd1 | 321 | if ( LPS22HB_Set_Odr( (void *)this, new_odr ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 322 | { |
nikapov | 0:3de9cb936fd1 | 323 | return 1; |
nikapov | 0:3de9cb936fd1 | 324 | } |
nikapov | 0:3de9cb936fd1 | 325 | |
nikapov | 0:3de9cb936fd1 | 326 | if ( get_odr( &_last_odr ) == 1 ) |
nikapov | 0:3de9cb936fd1 | 327 | { |
nikapov | 0:3de9cb936fd1 | 328 | return 1; |
nikapov | 0:3de9cb936fd1 | 329 | } |
nikapov | 0:3de9cb936fd1 | 330 | |
nikapov | 0:3de9cb936fd1 | 331 | return 0; |
nikapov | 0:3de9cb936fd1 | 332 | } |
nikapov | 0:3de9cb936fd1 | 333 | |
nikapov | 0:3de9cb936fd1 | 334 | /** |
nikapov | 0:3de9cb936fd1 | 335 | * @brief Set the LPS22HB sensor output data rate when disabled |
nikapov | 0:3de9cb936fd1 | 336 | * @param odr the functional output data rate to be set |
nikapov | 0:3de9cb936fd1 | 337 | * @retval 0 in case of success |
nikapov | 0:3de9cb936fd1 | 338 | * @retval 1 in case of failure |
nikapov | 0:3de9cb936fd1 | 339 | */ |
nikapov | 0:3de9cb936fd1 | 340 | int LPS22HBSensor::Set_ODR_When_Disabled( float odr ) |
nikapov | 0:3de9cb936fd1 | 341 | { |
nikapov | 0:3de9cb936fd1 | 342 | _last_odr = ( odr <= 1.0f ) ? 1.0f |
nikapov | 0:3de9cb936fd1 | 343 | : ( odr <= 10.0f ) ? 10.0f |
nikapov | 0:3de9cb936fd1 | 344 | : ( odr <= 25.0f ) ? 25.0f |
nikapov | 0:3de9cb936fd1 | 345 | : ( odr <= 50.0f ) ? 50.0f |
nikapov | 0:3de9cb936fd1 | 346 | : 75.0f; |
nikapov | 0:3de9cb936fd1 | 347 | |
nikapov | 0:3de9cb936fd1 | 348 | return 0; |
nikapov | 0:3de9cb936fd1 | 349 | } |
nikapov | 0:3de9cb936fd1 | 350 | |
nikapov | 0:3de9cb936fd1 | 351 | |
nikapov | 0:3de9cb936fd1 | 352 | /** |
nikapov | 0:3de9cb936fd1 | 353 | * @brief Read the data from register |
nikapov | 0:3de9cb936fd1 | 354 | * @param reg register address |
nikapov | 0:3de9cb936fd1 | 355 | * @param data register data |
nikapov | 0:3de9cb936fd1 | 356 | * @retval 0 in case of success |
nikapov | 0:3de9cb936fd1 | 357 | * @retval 1 in case of failure |
nikapov | 0:3de9cb936fd1 | 358 | */ |
nikapov | 0:3de9cb936fd1 | 359 | int LPS22HBSensor::read_reg( uint8_t reg, uint8_t *data ) |
nikapov | 0:3de9cb936fd1 | 360 | { |
nikapov | 0:3de9cb936fd1 | 361 | |
nikapov | 0:3de9cb936fd1 | 362 | if ( LPS22HB_read_reg( (void *)this, reg, 1, data ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 363 | { |
nikapov | 0:3de9cb936fd1 | 364 | return 1; |
nikapov | 0:3de9cb936fd1 | 365 | } |
nikapov | 0:3de9cb936fd1 | 366 | |
nikapov | 0:3de9cb936fd1 | 367 | return 0; |
nikapov | 0:3de9cb936fd1 | 368 | } |
nikapov | 0:3de9cb936fd1 | 369 | |
nikapov | 0:3de9cb936fd1 | 370 | /** |
nikapov | 0:3de9cb936fd1 | 371 | * @brief Write the data to register |
nikapov | 0:3de9cb936fd1 | 372 | * @param reg register address |
nikapov | 0:3de9cb936fd1 | 373 | * @param data register data |
nikapov | 0:3de9cb936fd1 | 374 | * @retval 0 in case of success |
nikapov | 0:3de9cb936fd1 | 375 | * @retval 1 in case of failure |
nikapov | 0:3de9cb936fd1 | 376 | */ |
nikapov | 0:3de9cb936fd1 | 377 | int LPS22HBSensor::write_reg( uint8_t reg, uint8_t data ) |
nikapov | 0:3de9cb936fd1 | 378 | { |
nikapov | 0:3de9cb936fd1 | 379 | |
nikapov | 0:3de9cb936fd1 | 380 | if ( LPS22HB_write_reg( (void *)this, reg, 1, &data ) == LPS22HB_ERROR ) |
nikapov | 0:3de9cb936fd1 | 381 | { |
nikapov | 0:3de9cb936fd1 | 382 | return 1; |
nikapov | 0:3de9cb936fd1 | 383 | } |
nikapov | 0:3de9cb936fd1 | 384 | |
nikapov | 0:3de9cb936fd1 | 385 | return 0; |
nikapov | 0:3de9cb936fd1 | 386 | } |
nikapov | 0:3de9cb936fd1 | 387 | |
nikapov | 0:3de9cb936fd1 | 388 | |
nikapov | 0:3de9cb936fd1 | 389 | uint8_t LPS22HB_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ) |
nikapov | 0:3de9cb936fd1 | 390 | { |
nikapov | 0:3de9cb936fd1 | 391 | return ((LPS22HBSensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); |
nikapov | 0:3de9cb936fd1 | 392 | } |
nikapov | 0:3de9cb936fd1 | 393 | |
nikapov | 0:3de9cb936fd1 | 394 | uint8_t LPS22HB_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ) |
nikapov | 0:3de9cb936fd1 | 395 | { |
nikapov | 0:3de9cb936fd1 | 396 | return ((LPS22HBSensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); |
nikapov | 0:3de9cb936fd1 | 397 | } |