HTS221 relative humidity and temperature sensor library
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
HTS221Sensor.cpp@6:7021a2a1ac72, 2019-09-17 (annotated)
- Committer:
- martlefebvre94
- Date:
- Tue Sep 17 08:40:01 2019 +0000
- Revision:
- 6:7021a2a1ac72
- Parent:
- 5:ccf7f36492ae
Addition of functions for the configuration of the relative humidity and temperature sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikapov | 0:7917d6d00a6e | 1 | /** |
nikapov | 0:7917d6d00a6e | 2 | ****************************************************************************** |
nikapov | 0:7917d6d00a6e | 3 | * @file HTS221Sensor.cpp |
nikapov | 0:7917d6d00a6e | 4 | * @author CLab |
nikapov | 0:7917d6d00a6e | 5 | * @version V1.0.0 |
nikapov | 0:7917d6d00a6e | 6 | * @date 5 August 2016 |
nikapov | 0:7917d6d00a6e | 7 | * @brief Implementation of an HTS221 Humidity and Temperature sensor. |
nikapov | 0:7917d6d00a6e | 8 | ****************************************************************************** |
nikapov | 0:7917d6d00a6e | 9 | * @attention |
nikapov | 0:7917d6d00a6e | 10 | * |
nikapov | 0:7917d6d00a6e | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
nikapov | 0:7917d6d00a6e | 12 | * |
nikapov | 0:7917d6d00a6e | 13 | * Redistribution and use in source and binary forms, with or without modification, |
nikapov | 0:7917d6d00a6e | 14 | * are permitted provided that the following conditions are met: |
nikapov | 0:7917d6d00a6e | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
nikapov | 0:7917d6d00a6e | 16 | * this list of conditions and the following disclaimer. |
nikapov | 0:7917d6d00a6e | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
nikapov | 0:7917d6d00a6e | 18 | * this list of conditions and the following disclaimer in the documentation |
nikapov | 0:7917d6d00a6e | 19 | * and/or other materials provided with the distribution. |
nikapov | 0:7917d6d00a6e | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
nikapov | 0:7917d6d00a6e | 21 | * may be used to endorse or promote products derived from this software |
nikapov | 0:7917d6d00a6e | 22 | * without specific prior written permission. |
nikapov | 0:7917d6d00a6e | 23 | * |
nikapov | 0:7917d6d00a6e | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
nikapov | 0:7917d6d00a6e | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
nikapov | 0:7917d6d00a6e | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
nikapov | 0:7917d6d00a6e | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
nikapov | 0:7917d6d00a6e | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
nikapov | 0:7917d6d00a6e | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
nikapov | 0:7917d6d00a6e | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
nikapov | 0:7917d6d00a6e | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
nikapov | 0:7917d6d00a6e | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
nikapov | 0:7917d6d00a6e | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
nikapov | 0:7917d6d00a6e | 34 | * |
nikapov | 0:7917d6d00a6e | 35 | ****************************************************************************** |
nikapov | 0:7917d6d00a6e | 36 | */ |
nikapov | 0:7917d6d00a6e | 37 | |
nikapov | 0:7917d6d00a6e | 38 | |
nikapov | 0:7917d6d00a6e | 39 | /* Includes ------------------------------------------------------------------*/ |
nikapov | 0:7917d6d00a6e | 40 | |
nikapov | 0:7917d6d00a6e | 41 | |
nikapov | 0:7917d6d00a6e | 42 | #include "HTS221Sensor.h" |
nikapov | 0:7917d6d00a6e | 43 | |
nikapov | 0:7917d6d00a6e | 44 | |
nikapov | 0:7917d6d00a6e | 45 | /* Class Implementation ------------------------------------------------------*/ |
nikapov | 0:7917d6d00a6e | 46 | |
cparata | 5:ccf7f36492ae | 47 | HTS221Sensor::HTS221Sensor(SPI *spi, PinName cs_pin, PinName drdy_pin) : |
cparata | 5:ccf7f36492ae | 48 | _dev_spi(spi), _cs_pin(cs_pin), _drdy_pin(drdy_pin) // SPI3W ONLY |
cparata | 5:ccf7f36492ae | 49 | { |
cparata | 5:ccf7f36492ae | 50 | assert(spi); |
mapellil | 2:312ee2694a77 | 51 | _dev_i2c = NULL; |
mapellil | 2:312ee2694a77 | 52 | }; |
mapellil | 2:312ee2694a77 | 53 | |
nikapov | 0:7917d6d00a6e | 54 | /** Constructor |
nikapov | 0:7917d6d00a6e | 55 | * @param i2c object of an helper class which handles the I2C peripheral |
nikapov | 0:7917d6d00a6e | 56 | * @param address the address of the component's instance |
nikapov | 0:7917d6d00a6e | 57 | */ |
mapellil | 2:312ee2694a77 | 58 | HTS221Sensor::HTS221Sensor(DevI2C *i2c, uint8_t address, PinName drdy_pin) : |
cparata | 5:ccf7f36492ae | 59 | _dev_i2c(i2c), _address(address), _cs_pin(NC), _drdy_pin(drdy_pin) |
nikapov | 0:7917d6d00a6e | 60 | { |
mapellil | 2:312ee2694a77 | 61 | assert(i2c); |
mapellil | 2:312ee2694a77 | 62 | _dev_spi = NULL; |
nikapov | 0:7917d6d00a6e | 63 | }; |
nikapov | 0:7917d6d00a6e | 64 | |
nikapov | 0:7917d6d00a6e | 65 | /** |
nikapov | 0:7917d6d00a6e | 66 | * @brief Initializing the component. |
nikapov | 0:7917d6d00a6e | 67 | * @param[in] init pointer to device specific initalization structure. |
nikapov | 0:7917d6d00a6e | 68 | * @retval "0" in case of success, an error code otherwise. |
nikapov | 0:7917d6d00a6e | 69 | */ |
nikapov | 0:7917d6d00a6e | 70 | int HTS221Sensor::init(void *init) |
nikapov | 0:7917d6d00a6e | 71 | { |
cparata | 5:ccf7f36492ae | 72 | /* Power down the device */ |
cparata | 5:ccf7f36492ae | 73 | if (HTS221_DeActivate((void *)this) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 74 | return 1; |
cparata | 5:ccf7f36492ae | 75 | } |
nikapov | 0:7917d6d00a6e | 76 | |
cparata | 5:ccf7f36492ae | 77 | /* Enable BDU */ |
cparata | 5:ccf7f36492ae | 78 | if (HTS221_Set_BduMode((void *)this, HTS221_ENABLE) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 79 | return 1; |
cparata | 5:ccf7f36492ae | 80 | } |
cparata | 5:ccf7f36492ae | 81 | |
cparata | 5:ccf7f36492ae | 82 | if (set_odr(1.0f) == 1) { |
cparata | 5:ccf7f36492ae | 83 | return 1; |
cparata | 5:ccf7f36492ae | 84 | } |
cparata | 5:ccf7f36492ae | 85 | |
cparata | 5:ccf7f36492ae | 86 | return 0; |
nikapov | 0:7917d6d00a6e | 87 | } |
nikapov | 0:7917d6d00a6e | 88 | |
nikapov | 0:7917d6d00a6e | 89 | /** |
nikapov | 0:7917d6d00a6e | 90 | * @brief Enable HTS221 |
nikapov | 0:7917d6d00a6e | 91 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:7917d6d00a6e | 92 | */ |
nikapov | 0:7917d6d00a6e | 93 | int HTS221Sensor::enable(void) |
nikapov | 0:7917d6d00a6e | 94 | { |
cparata | 5:ccf7f36492ae | 95 | /* Power up the device */ |
cparata | 5:ccf7f36492ae | 96 | if (HTS221_Activate((void *)this) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 97 | return 1; |
cparata | 5:ccf7f36492ae | 98 | } |
nikapov | 0:7917d6d00a6e | 99 | |
cparata | 5:ccf7f36492ae | 100 | return 0; |
nikapov | 0:7917d6d00a6e | 101 | } |
nikapov | 0:7917d6d00a6e | 102 | |
nikapov | 0:7917d6d00a6e | 103 | /** |
nikapov | 0:7917d6d00a6e | 104 | * @brief Disable HTS221 |
nikapov | 0:7917d6d00a6e | 105 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:7917d6d00a6e | 106 | */ |
nikapov | 0:7917d6d00a6e | 107 | int HTS221Sensor::disable(void) |
nikapov | 0:7917d6d00a6e | 108 | { |
cparata | 5:ccf7f36492ae | 109 | /* Power up the device */ |
cparata | 5:ccf7f36492ae | 110 | if (HTS221_DeActivate((void *)this) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 111 | return 1; |
cparata | 5:ccf7f36492ae | 112 | } |
nikapov | 0:7917d6d00a6e | 113 | |
cparata | 5:ccf7f36492ae | 114 | return 0; |
nikapov | 0:7917d6d00a6e | 115 | } |
nikapov | 0:7917d6d00a6e | 116 | |
nikapov | 0:7917d6d00a6e | 117 | /** |
nikapov | 0:7917d6d00a6e | 118 | * @brief Read ID address of HTS221 |
nikapov | 0:7917d6d00a6e | 119 | * @param id the pointer where the ID of the device is stored |
nikapov | 0:7917d6d00a6e | 120 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:7917d6d00a6e | 121 | */ |
nikapov | 0:7917d6d00a6e | 122 | int HTS221Sensor::read_id(uint8_t *id) |
nikapov | 0:7917d6d00a6e | 123 | { |
cparata | 5:ccf7f36492ae | 124 | if (!id) { |
cparata | 5:ccf7f36492ae | 125 | return 1; |
cparata | 5:ccf7f36492ae | 126 | } |
nikapov | 0:7917d6d00a6e | 127 | |
cparata | 5:ccf7f36492ae | 128 | /* Read WHO AM I register */ |
cparata | 5:ccf7f36492ae | 129 | if (HTS221_Get_DeviceID((void *)this, id) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 130 | return 1; |
cparata | 5:ccf7f36492ae | 131 | } |
cparata | 5:ccf7f36492ae | 132 | |
cparata | 5:ccf7f36492ae | 133 | return 0; |
nikapov | 0:7917d6d00a6e | 134 | } |
nikapov | 0:7917d6d00a6e | 135 | |
nikapov | 0:7917d6d00a6e | 136 | /** |
nikapov | 0:7917d6d00a6e | 137 | * @brief Reboot memory content of HTS221 |
nikapov | 0:7917d6d00a6e | 138 | * @param None |
nikapov | 0:7917d6d00a6e | 139 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:7917d6d00a6e | 140 | */ |
nikapov | 0:7917d6d00a6e | 141 | int HTS221Sensor::reset(void) |
nikapov | 0:7917d6d00a6e | 142 | { |
nikapov | 0:7917d6d00a6e | 143 | uint8_t tmpreg; |
nikapov | 0:7917d6d00a6e | 144 | |
nikapov | 0:7917d6d00a6e | 145 | /* Read CTRL_REG2 register */ |
cparata | 5:ccf7f36492ae | 146 | if (read_reg(HTS221_CTRL_REG2, &tmpreg) != 0) { |
cparata | 5:ccf7f36492ae | 147 | return 1; |
nikapov | 0:7917d6d00a6e | 148 | } |
nikapov | 0:7917d6d00a6e | 149 | |
nikapov | 0:7917d6d00a6e | 150 | /* Enable or Disable the reboot memory */ |
nikapov | 0:7917d6d00a6e | 151 | tmpreg |= (0x01 << HTS221_BOOT_BIT); |
nikapov | 0:7917d6d00a6e | 152 | |
nikapov | 0:7917d6d00a6e | 153 | /* Write value to MEMS CTRL_REG2 regsister */ |
cparata | 5:ccf7f36492ae | 154 | if (write_reg(HTS221_CTRL_REG2, tmpreg) != 0) { |
cparata | 5:ccf7f36492ae | 155 | return 1; |
nikapov | 0:7917d6d00a6e | 156 | } |
cparata | 5:ccf7f36492ae | 157 | |
nikapov | 0:7917d6d00a6e | 158 | return 0; |
nikapov | 0:7917d6d00a6e | 159 | } |
nikapov | 0:7917d6d00a6e | 160 | |
nikapov | 0:7917d6d00a6e | 161 | /** |
nikapov | 0:7917d6d00a6e | 162 | * @brief Read HTS221 output register, and calculate the humidity |
nikapov | 0:7917d6d00a6e | 163 | * @param pfData the pointer to data output |
nikapov | 0:7917d6d00a6e | 164 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:7917d6d00a6e | 165 | */ |
cparata | 5:ccf7f36492ae | 166 | int HTS221Sensor::get_humidity(float *pfData) |
nikapov | 0:7917d6d00a6e | 167 | { |
cparata | 5:ccf7f36492ae | 168 | uint16_t uint16data = 0; |
nikapov | 0:7917d6d00a6e | 169 | |
cparata | 5:ccf7f36492ae | 170 | /* Read data from HTS221. */ |
cparata | 5:ccf7f36492ae | 171 | if (HTS221_Get_Humidity((void *)this, &uint16data) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 172 | return 1; |
cparata | 5:ccf7f36492ae | 173 | } |
nikapov | 0:7917d6d00a6e | 174 | |
cparata | 5:ccf7f36492ae | 175 | *pfData = (float)uint16data / 10.0f; |
nikapov | 0:7917d6d00a6e | 176 | |
cparata | 5:ccf7f36492ae | 177 | return 0; |
nikapov | 0:7917d6d00a6e | 178 | } |
nikapov | 0:7917d6d00a6e | 179 | |
nikapov | 0:7917d6d00a6e | 180 | /** |
nikapov | 0:7917d6d00a6e | 181 | * @brief Read HTS221 output register, and calculate the temperature |
nikapov | 0:7917d6d00a6e | 182 | * @param pfData the pointer to data output |
nikapov | 0:7917d6d00a6e | 183 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:7917d6d00a6e | 184 | */ |
cparata | 5:ccf7f36492ae | 185 | int HTS221Sensor::get_temperature(float *pfData) |
nikapov | 0:7917d6d00a6e | 186 | { |
cparata | 5:ccf7f36492ae | 187 | int16_t int16data = 0; |
nikapov | 0:7917d6d00a6e | 188 | |
cparata | 5:ccf7f36492ae | 189 | /* Read data from HTS221. */ |
cparata | 5:ccf7f36492ae | 190 | if (HTS221_Get_Temperature((void *)this, &int16data) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 191 | return 1; |
cparata | 5:ccf7f36492ae | 192 | } |
nikapov | 0:7917d6d00a6e | 193 | |
cparata | 5:ccf7f36492ae | 194 | *pfData = (float)int16data / 10.0f; |
nikapov | 0:7917d6d00a6e | 195 | |
cparata | 5:ccf7f36492ae | 196 | return 0; |
nikapov | 0:7917d6d00a6e | 197 | } |
nikapov | 0:7917d6d00a6e | 198 | |
nikapov | 0:7917d6d00a6e | 199 | /** |
nikapov | 0:7917d6d00a6e | 200 | * @brief Read HTS221 output register, and calculate the humidity |
nikapov | 0:7917d6d00a6e | 201 | * @param odr the pointer to the output data rate |
nikapov | 0:7917d6d00a6e | 202 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:7917d6d00a6e | 203 | */ |
cparata | 5:ccf7f36492ae | 204 | int HTS221Sensor::get_odr(float *odr) |
nikapov | 0:7917d6d00a6e | 205 | { |
cparata | 5:ccf7f36492ae | 206 | HTS221_Odr_et odr_low_level; |
nikapov | 0:7917d6d00a6e | 207 | |
cparata | 5:ccf7f36492ae | 208 | if (HTS221_Get_Odr((void *)this, &odr_low_level) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 209 | return 1; |
cparata | 5:ccf7f36492ae | 210 | } |
nikapov | 0:7917d6d00a6e | 211 | |
cparata | 5:ccf7f36492ae | 212 | switch (odr_low_level) { |
cparata | 5:ccf7f36492ae | 213 | case HTS221_ODR_ONE_SHOT: |
cparata | 5:ccf7f36492ae | 214 | *odr = 0.0f; |
cparata | 5:ccf7f36492ae | 215 | break; |
cparata | 5:ccf7f36492ae | 216 | case HTS221_ODR_1HZ : |
cparata | 5:ccf7f36492ae | 217 | *odr = 1.0f; |
cparata | 5:ccf7f36492ae | 218 | break; |
cparata | 5:ccf7f36492ae | 219 | case HTS221_ODR_7HZ : |
cparata | 5:ccf7f36492ae | 220 | *odr = 7.0f; |
cparata | 5:ccf7f36492ae | 221 | break; |
cparata | 5:ccf7f36492ae | 222 | case HTS221_ODR_12_5HZ : |
cparata | 5:ccf7f36492ae | 223 | *odr = 12.5f; |
cparata | 5:ccf7f36492ae | 224 | break; |
cparata | 5:ccf7f36492ae | 225 | default : |
cparata | 5:ccf7f36492ae | 226 | *odr = -1.0f; |
cparata | 5:ccf7f36492ae | 227 | return 1; |
cparata | 5:ccf7f36492ae | 228 | } |
nikapov | 0:7917d6d00a6e | 229 | |
cparata | 5:ccf7f36492ae | 230 | return 0; |
nikapov | 0:7917d6d00a6e | 231 | } |
nikapov | 0:7917d6d00a6e | 232 | |
nikapov | 0:7917d6d00a6e | 233 | /** |
nikapov | 0:7917d6d00a6e | 234 | * @brief Set ODR |
nikapov | 0:7917d6d00a6e | 235 | * @param odr the output data rate to be set |
nikapov | 0:7917d6d00a6e | 236 | * @retval 0 in case of success, an error code otherwise |
nikapov | 0:7917d6d00a6e | 237 | */ |
nikapov | 0:7917d6d00a6e | 238 | int HTS221Sensor::set_odr(float odr) |
nikapov | 0:7917d6d00a6e | 239 | { |
cparata | 5:ccf7f36492ae | 240 | HTS221_Odr_et new_odr; |
nikapov | 0:7917d6d00a6e | 241 | |
cparata | 5:ccf7f36492ae | 242 | new_odr = (odr <= 1.0f) ? HTS221_ODR_1HZ |
cparata | 5:ccf7f36492ae | 243 | : (odr <= 7.0f) ? HTS221_ODR_7HZ |
cparata | 5:ccf7f36492ae | 244 | : HTS221_ODR_12_5HZ; |
nikapov | 0:7917d6d00a6e | 245 | |
cparata | 5:ccf7f36492ae | 246 | if (HTS221_Set_Odr((void *)this, new_odr) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 247 | return 1; |
cparata | 5:ccf7f36492ae | 248 | } |
nikapov | 0:7917d6d00a6e | 249 | |
cparata | 5:ccf7f36492ae | 250 | return 0; |
nikapov | 0:7917d6d00a6e | 251 | } |
nikapov | 0:7917d6d00a6e | 252 | |
martlefebvre94 | 6:7021a2a1ac72 | 253 | /** |
martlefebvre94 | 6:7021a2a1ac72 | 254 | * @brief Get HTS221 heater configuration |
martlefebvre94 | 6:7021a2a1ac72 | 255 | * @param heater the pointer to the heater configuration |
martlefebvre94 | 6:7021a2a1ac72 | 256 | * @retval 0 in case of success, an error code otherwise |
martlefebvre94 | 6:7021a2a1ac72 | 257 | */ |
martlefebvre94 | 6:7021a2a1ac72 | 258 | int HTS221Sensor::get_heater(uint8_t *heater) |
martlefebvre94 | 6:7021a2a1ac72 | 259 | { |
martlefebvre94 | 6:7021a2a1ac72 | 260 | HTS221_State_et heater_low_level; |
martlefebvre94 | 6:7021a2a1ac72 | 261 | |
martlefebvre94 | 6:7021a2a1ac72 | 262 | if (HTS221_Get_HeaterState((void *)this, &heater_low_level) == HTS221_ERROR) { |
martlefebvre94 | 6:7021a2a1ac72 | 263 | return 1; |
martlefebvre94 | 6:7021a2a1ac72 | 264 | } |
martlefebvre94 | 6:7021a2a1ac72 | 265 | |
martlefebvre94 | 6:7021a2a1ac72 | 266 | switch (heater_low_level) { |
martlefebvre94 | 6:7021a2a1ac72 | 267 | case HTS221_DISABLE: |
martlefebvre94 | 6:7021a2a1ac72 | 268 | *heater = 0; |
martlefebvre94 | 6:7021a2a1ac72 | 269 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 270 | case HTS221_ENABLE: |
martlefebvre94 | 6:7021a2a1ac72 | 271 | *heater = 1; |
martlefebvre94 | 6:7021a2a1ac72 | 272 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 273 | default: |
martlefebvre94 | 6:7021a2a1ac72 | 274 | return 1; |
martlefebvre94 | 6:7021a2a1ac72 | 275 | } |
martlefebvre94 | 6:7021a2a1ac72 | 276 | |
martlefebvre94 | 6:7021a2a1ac72 | 277 | return 0; |
martlefebvre94 | 6:7021a2a1ac72 | 278 | } |
martlefebvre94 | 6:7021a2a1ac72 | 279 | |
martlefebvre94 | 6:7021a2a1ac72 | 280 | /** |
martlefebvre94 | 6:7021a2a1ac72 | 281 | * @brief Set HTS221 heater configuration |
martlefebvre94 | 6:7021a2a1ac72 | 282 | * @param heater the heater configuration to be set |
martlefebvre94 | 6:7021a2a1ac72 | 283 | * @retval 0 in case of success, an error code otherwise |
martlefebvre94 | 6:7021a2a1ac72 | 284 | */ |
martlefebvre94 | 6:7021a2a1ac72 | 285 | int HTS221Sensor::set_heater(uint8_t heater) |
martlefebvre94 | 6:7021a2a1ac72 | 286 | { |
martlefebvre94 | 6:7021a2a1ac72 | 287 | HTS221_State_et new_heater; |
martlefebvre94 | 6:7021a2a1ac72 | 288 | |
martlefebvre94 | 6:7021a2a1ac72 | 289 | new_heater = (heater == 0) ? HTS221_DISABLE |
martlefebvre94 | 6:7021a2a1ac72 | 290 | : HTS221_ENABLE; |
martlefebvre94 | 6:7021a2a1ac72 | 291 | |
martlefebvre94 | 6:7021a2a1ac72 | 292 | if (HTS221_Set_HeaterState((void *)this, new_heater) == HTS221_ERROR) { |
martlefebvre94 | 6:7021a2a1ac72 | 293 | return 1; |
martlefebvre94 | 6:7021a2a1ac72 | 294 | } |
martlefebvre94 | 6:7021a2a1ac72 | 295 | |
martlefebvre94 | 6:7021a2a1ac72 | 296 | return 0; |
martlefebvre94 | 6:7021a2a1ac72 | 297 | } |
martlefebvre94 | 6:7021a2a1ac72 | 298 | |
martlefebvre94 | 6:7021a2a1ac72 | 299 | /** |
martlefebvre94 | 6:7021a2a1ac72 | 300 | * @brief Get HTS221 averaging configuration |
martlefebvre94 | 6:7021a2a1ac72 | 301 | * @param avgh the pointer to the averaging for humidity, avgt the pointer to the averaging for temperature |
martlefebvre94 | 6:7021a2a1ac72 | 302 | * @retval 0 in case of success, an error code otherwise |
martlefebvre94 | 6:7021a2a1ac72 | 303 | */ |
martlefebvre94 | 6:7021a2a1ac72 | 304 | int HTS221Sensor::get_avg(float *avgh, float *avgt) |
martlefebvre94 | 6:7021a2a1ac72 | 305 | { |
martlefebvre94 | 6:7021a2a1ac72 | 306 | HTS221_Avgh_et avgh_low_level; |
martlefebvre94 | 6:7021a2a1ac72 | 307 | HTS221_Avgt_et avgt_low_level; |
martlefebvre94 | 6:7021a2a1ac72 | 308 | |
martlefebvre94 | 6:7021a2a1ac72 | 309 | if (HTS221_Get_AvgHT((void *)this, &avgh_low_level, &avgt_low_level) == HTS221_ERROR) { |
martlefebvre94 | 6:7021a2a1ac72 | 310 | return 1; |
martlefebvre94 | 6:7021a2a1ac72 | 311 | } |
martlefebvre94 | 6:7021a2a1ac72 | 312 | |
martlefebvre94 | 6:7021a2a1ac72 | 313 | switch (avgh_low_level) { |
martlefebvre94 | 6:7021a2a1ac72 | 314 | case HTS221_AVGH_4: |
martlefebvre94 | 6:7021a2a1ac72 | 315 | *avgh = 4; |
martlefebvre94 | 6:7021a2a1ac72 | 316 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 317 | case HTS221_AVGH_8: |
martlefebvre94 | 6:7021a2a1ac72 | 318 | *avgh = 8; |
martlefebvre94 | 6:7021a2a1ac72 | 319 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 320 | case HTS221_AVGH_16: |
martlefebvre94 | 6:7021a2a1ac72 | 321 | *avgh = 16; |
martlefebvre94 | 6:7021a2a1ac72 | 322 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 323 | case HTS221_AVGH_32: |
martlefebvre94 | 6:7021a2a1ac72 | 324 | *avgh = 32; |
martlefebvre94 | 6:7021a2a1ac72 | 325 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 326 | case HTS221_AVGH_64: |
martlefebvre94 | 6:7021a2a1ac72 | 327 | *avgh = 64; |
martlefebvre94 | 6:7021a2a1ac72 | 328 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 329 | case HTS221_AVGH_128: |
martlefebvre94 | 6:7021a2a1ac72 | 330 | *avgh = 128; |
martlefebvre94 | 6:7021a2a1ac72 | 331 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 332 | case HTS221_AVGH_256: |
martlefebvre94 | 6:7021a2a1ac72 | 333 | *avgh = 256; |
martlefebvre94 | 6:7021a2a1ac72 | 334 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 335 | case HTS221_AVGH_512: |
martlefebvre94 | 6:7021a2a1ac72 | 336 | *avgh = 512; |
martlefebvre94 | 6:7021a2a1ac72 | 337 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 338 | default: |
martlefebvre94 | 6:7021a2a1ac72 | 339 | return 1; |
martlefebvre94 | 6:7021a2a1ac72 | 340 | } |
martlefebvre94 | 6:7021a2a1ac72 | 341 | |
martlefebvre94 | 6:7021a2a1ac72 | 342 | switch (avgt_low_level) { |
martlefebvre94 | 6:7021a2a1ac72 | 343 | case HTS221_AVGT_2: |
martlefebvre94 | 6:7021a2a1ac72 | 344 | *avgt = 2; |
martlefebvre94 | 6:7021a2a1ac72 | 345 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 346 | case HTS221_AVGT_4: |
martlefebvre94 | 6:7021a2a1ac72 | 347 | *avgt = 4; |
martlefebvre94 | 6:7021a2a1ac72 | 348 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 349 | case HTS221_AVGT_8: |
martlefebvre94 | 6:7021a2a1ac72 | 350 | *avgt = 8; |
martlefebvre94 | 6:7021a2a1ac72 | 351 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 352 | case HTS221_AVGT_16: |
martlefebvre94 | 6:7021a2a1ac72 | 353 | *avgt = 16; |
martlefebvre94 | 6:7021a2a1ac72 | 354 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 355 | case HTS221_AVGT_32: |
martlefebvre94 | 6:7021a2a1ac72 | 356 | *avgt = 32; |
martlefebvre94 | 6:7021a2a1ac72 | 357 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 358 | case HTS221_AVGT_64: |
martlefebvre94 | 6:7021a2a1ac72 | 359 | *avgt = 64; |
martlefebvre94 | 6:7021a2a1ac72 | 360 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 361 | case HTS221_AVGT_128: |
martlefebvre94 | 6:7021a2a1ac72 | 362 | *avgt = 128; |
martlefebvre94 | 6:7021a2a1ac72 | 363 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 364 | case HTS221_AVGT_256: |
martlefebvre94 | 6:7021a2a1ac72 | 365 | *avgt = 256; |
martlefebvre94 | 6:7021a2a1ac72 | 366 | break; |
martlefebvre94 | 6:7021a2a1ac72 | 367 | default: |
martlefebvre94 | 6:7021a2a1ac72 | 368 | return 1; |
martlefebvre94 | 6:7021a2a1ac72 | 369 | } |
martlefebvre94 | 6:7021a2a1ac72 | 370 | |
martlefebvre94 | 6:7021a2a1ac72 | 371 | return 0; |
martlefebvre94 | 6:7021a2a1ac72 | 372 | } |
martlefebvre94 | 6:7021a2a1ac72 | 373 | |
martlefebvre94 | 6:7021a2a1ac72 | 374 | /** |
martlefebvre94 | 6:7021a2a1ac72 | 375 | * @brief Set HTS221 averaging configuration |
martlefebvre94 | 6:7021a2a1ac72 | 376 | * @param avgh the averagingfor humidity to be set, avgt the averaging for temperature to be set |
martlefebvre94 | 6:7021a2a1ac72 | 377 | * @retval 0 in case of success, an error code otherwise |
martlefebvre94 | 6:7021a2a1ac72 | 378 | */ |
martlefebvre94 | 6:7021a2a1ac72 | 379 | int HTS221Sensor::set_avg(float avgh, float avgt) |
martlefebvre94 | 6:7021a2a1ac72 | 380 | { |
martlefebvre94 | 6:7021a2a1ac72 | 381 | HTS221_Avgh_et new_avgh; |
martlefebvre94 | 6:7021a2a1ac72 | 382 | HTS221_Avgt_et new_avgt; |
martlefebvre94 | 6:7021a2a1ac72 | 383 | |
martlefebvre94 | 6:7021a2a1ac72 | 384 | new_avgh = (avgh == 4) ? HTS221_AVGH_4 |
martlefebvre94 | 6:7021a2a1ac72 | 385 | : (avgh == 8) ? HTS221_AVGH_8 |
martlefebvre94 | 6:7021a2a1ac72 | 386 | : (avgh == 16) ? HTS221_AVGH_16 |
martlefebvre94 | 6:7021a2a1ac72 | 387 | : (avgh == 32) ? HTS221_AVGH_32 |
martlefebvre94 | 6:7021a2a1ac72 | 388 | : (avgh == 64) ? HTS221_AVGH_64 |
martlefebvre94 | 6:7021a2a1ac72 | 389 | : (avgh == 128) ? HTS221_AVGH_128 |
martlefebvre94 | 6:7021a2a1ac72 | 390 | : (avgh == 256) ? HTS221_AVGH_256 |
martlefebvre94 | 6:7021a2a1ac72 | 391 | : (avgh == 512) ? HTS221_AVGH_512 |
martlefebvre94 | 6:7021a2a1ac72 | 392 | : HTS221_AVGH_4; |
martlefebvre94 | 6:7021a2a1ac72 | 393 | |
martlefebvre94 | 6:7021a2a1ac72 | 394 | new_avgt = (avgt == 2) ? HTS221_AVGT_2 |
martlefebvre94 | 6:7021a2a1ac72 | 395 | : (avgt == 4) ? HTS221_AVGT_4 |
martlefebvre94 | 6:7021a2a1ac72 | 396 | : (avgt == 8) ? HTS221_AVGT_8 |
martlefebvre94 | 6:7021a2a1ac72 | 397 | : (avgt == 16) ? HTS221_AVGT_16 |
martlefebvre94 | 6:7021a2a1ac72 | 398 | : (avgt == 32) ? HTS221_AVGT_32 |
martlefebvre94 | 6:7021a2a1ac72 | 399 | : (avgt == 64) ? HTS221_AVGT_64 |
martlefebvre94 | 6:7021a2a1ac72 | 400 | : (avgt == 128) ? HTS221_AVGT_128 |
martlefebvre94 | 6:7021a2a1ac72 | 401 | : (avgt == 256) ? HTS221_AVGT_256 |
martlefebvre94 | 6:7021a2a1ac72 | 402 | : HTS221_AVGT_2; |
martlefebvre94 | 6:7021a2a1ac72 | 403 | |
martlefebvre94 | 6:7021a2a1ac72 | 404 | if (HTS221_Set_AvgHT((void *)this, new_avgh, new_avgt) == HTS221_ERROR) { |
martlefebvre94 | 6:7021a2a1ac72 | 405 | return 1; |
martlefebvre94 | 6:7021a2a1ac72 | 406 | } |
martlefebvre94 | 6:7021a2a1ac72 | 407 | |
martlefebvre94 | 6:7021a2a1ac72 | 408 | return 0; |
martlefebvre94 | 6:7021a2a1ac72 | 409 | } |
nikapov | 0:7917d6d00a6e | 410 | |
nikapov | 0:7917d6d00a6e | 411 | /** |
nikapov | 0:7917d6d00a6e | 412 | * @brief Read the data from register |
nikapov | 0:7917d6d00a6e | 413 | * @param reg register address |
nikapov | 0:7917d6d00a6e | 414 | * @param data register data |
nikapov | 0:7917d6d00a6e | 415 | * @retval 0 in case of success |
nikapov | 0:7917d6d00a6e | 416 | * @retval 1 in case of failure |
nikapov | 0:7917d6d00a6e | 417 | */ |
cparata | 5:ccf7f36492ae | 418 | int HTS221Sensor::read_reg(uint8_t reg, uint8_t *data) |
nikapov | 0:7917d6d00a6e | 419 | { |
nikapov | 0:7917d6d00a6e | 420 | |
cparata | 5:ccf7f36492ae | 421 | if (HTS221_read_reg((void *)this, reg, 1, data) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 422 | return 1; |
cparata | 5:ccf7f36492ae | 423 | } |
nikapov | 0:7917d6d00a6e | 424 | |
cparata | 5:ccf7f36492ae | 425 | return 0; |
nikapov | 0:7917d6d00a6e | 426 | } |
nikapov | 0:7917d6d00a6e | 427 | |
nikapov | 0:7917d6d00a6e | 428 | /** |
nikapov | 0:7917d6d00a6e | 429 | * @brief Write the data to register |
nikapov | 0:7917d6d00a6e | 430 | * @param reg register address |
nikapov | 0:7917d6d00a6e | 431 | * @param data register data |
nikapov | 0:7917d6d00a6e | 432 | * @retval 0 in case of success |
nikapov | 0:7917d6d00a6e | 433 | * @retval 1 in case of failure |
nikapov | 0:7917d6d00a6e | 434 | */ |
cparata | 5:ccf7f36492ae | 435 | int HTS221Sensor::write_reg(uint8_t reg, uint8_t data) |
nikapov | 0:7917d6d00a6e | 436 | { |
nikapov | 0:7917d6d00a6e | 437 | |
cparata | 5:ccf7f36492ae | 438 | if (HTS221_write_reg((void *)this, reg, 1, &data) == HTS221_ERROR) { |
cparata | 5:ccf7f36492ae | 439 | return 1; |
cparata | 5:ccf7f36492ae | 440 | } |
nikapov | 0:7917d6d00a6e | 441 | |
cparata | 5:ccf7f36492ae | 442 | return 0; |
nikapov | 0:7917d6d00a6e | 443 | } |
nikapov | 0:7917d6d00a6e | 444 | |
cparata | 5:ccf7f36492ae | 445 | uint8_t HTS221_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite) |
nikapov | 0:7917d6d00a6e | 446 | { |
cparata | 5:ccf7f36492ae | 447 | return ((HTS221Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); |
nikapov | 0:7917d6d00a6e | 448 | } |
nikapov | 0:7917d6d00a6e | 449 | |
cparata | 5:ccf7f36492ae | 450 | uint8_t HTS221_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead) |
nikapov | 0:7917d6d00a6e | 451 | { |
cparata | 5:ccf7f36492ae | 452 | return ((HTS221Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); |
nikapov | 0:7917d6d00a6e | 453 | } |