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