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