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: X_NUCLEO_COMMON ST_INTERFACES
HTS221Sensor.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file HTS221Sensor.cpp 00004 * @author CLab 00005 * @version V1.0.0 00006 * @date 5 August 2016 00007 * @brief Implementation of an HTS221 Humidity and Temperature sensor. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 00039 /* Includes ------------------------------------------------------------------*/ 00040 00041 00042 #include "HTS221Sensor.h" 00043 00044 00045 /* Class Implementation ------------------------------------------------------*/ 00046 00047 HTS221Sensor::HTS221Sensor(SPI *spi, PinName cs_pin, PinName drdy_pin) : 00048 _dev_spi(spi), _cs_pin(cs_pin), _drdy_pin(drdy_pin) // SPI3W ONLY 00049 { 00050 assert(spi); 00051 _dev_i2c = NULL; 00052 }; 00053 00054 /** Constructor 00055 * @param i2c object of an helper class which handles the I2C peripheral 00056 * @param address the address of the component's instance 00057 */ 00058 HTS221Sensor::HTS221Sensor(DevI2C *i2c, uint8_t address, PinName drdy_pin) : 00059 _dev_i2c(i2c), _address(address), _cs_pin(NC), _drdy_pin(drdy_pin) 00060 { 00061 assert(i2c); 00062 _dev_spi = NULL; 00063 }; 00064 00065 /** 00066 * @brief Initializing the component. 00067 * @param[in] init pointer to device specific initalization structure. 00068 * @retval "0" in case of success, an error code otherwise. 00069 */ 00070 int HTS221Sensor::init(void *init) 00071 { 00072 /* Power down the device */ 00073 if (HTS221_DeActivate((void *)this) == HTS221_ERROR) { 00074 return 1; 00075 } 00076 00077 /* Enable BDU */ 00078 if (HTS221_Set_BduMode((void *)this, HTS221_ENABLE) == HTS221_ERROR) { 00079 return 1; 00080 } 00081 00082 if (set_odr(1.0f) == 1) { 00083 return 1; 00084 } 00085 00086 return 0; 00087 } 00088 00089 /** 00090 * @brief Enable HTS221 00091 * @retval 0 in case of success, an error code otherwise 00092 */ 00093 int HTS221Sensor::enable(void) 00094 { 00095 /* Power up the device */ 00096 if (HTS221_Activate((void *)this) == HTS221_ERROR) { 00097 return 1; 00098 } 00099 00100 return 0; 00101 } 00102 00103 /** 00104 * @brief Disable HTS221 00105 * @retval 0 in case of success, an error code otherwise 00106 */ 00107 int HTS221Sensor::disable(void) 00108 { 00109 /* Power up the device */ 00110 if (HTS221_DeActivate((void *)this) == HTS221_ERROR) { 00111 return 1; 00112 } 00113 00114 return 0; 00115 } 00116 00117 /** 00118 * @brief Read ID address of HTS221 00119 * @param id the pointer where the ID of the device is stored 00120 * @retval 0 in case of success, an error code otherwise 00121 */ 00122 int HTS221Sensor::read_id(uint8_t *id) 00123 { 00124 if (!id) { 00125 return 1; 00126 } 00127 00128 /* Read WHO AM I register */ 00129 if (HTS221_Get_DeviceID((void *)this, id) == HTS221_ERROR) { 00130 return 1; 00131 } 00132 00133 return 0; 00134 } 00135 00136 /** 00137 * @brief Reboot memory content of HTS221 00138 * @param None 00139 * @retval 0 in case of success, an error code otherwise 00140 */ 00141 int HTS221Sensor::reset(void) 00142 { 00143 uint8_t tmpreg; 00144 00145 /* Read CTRL_REG2 register */ 00146 if (read_reg(HTS221_CTRL_REG2, &tmpreg) != 0) { 00147 return 1; 00148 } 00149 00150 /* Enable or Disable the reboot memory */ 00151 tmpreg |= (0x01 << HTS221_BOOT_BIT); 00152 00153 /* Write value to MEMS CTRL_REG2 regsister */ 00154 if (write_reg(HTS221_CTRL_REG2, tmpreg) != 0) { 00155 return 1; 00156 } 00157 00158 return 0; 00159 } 00160 00161 /** 00162 * @brief Read HTS221 output register, and calculate the humidity 00163 * @param pfData the pointer to data output 00164 * @retval 0 in case of success, an error code otherwise 00165 */ 00166 int HTS221Sensor::get_humidity(float *pfData) 00167 { 00168 uint16_t uint16data = 0; 00169 00170 /* Read data from HTS221. */ 00171 if (HTS221_Get_Humidity((void *)this, &uint16data) == HTS221_ERROR) { 00172 return 1; 00173 } 00174 00175 *pfData = (float)uint16data / 10.0f; 00176 00177 return 0; 00178 } 00179 00180 /** 00181 * @brief Read HTS221 output register, and calculate the temperature 00182 * @param pfData the pointer to data output 00183 * @retval 0 in case of success, an error code otherwise 00184 */ 00185 int HTS221Sensor::get_temperature(float *pfData) 00186 { 00187 int16_t int16data = 0; 00188 00189 /* Read data from HTS221. */ 00190 if (HTS221_Get_Temperature((void *)this, &int16data) == HTS221_ERROR) { 00191 return 1; 00192 } 00193 00194 *pfData = (float)int16data / 10.0f; 00195 00196 return 0; 00197 } 00198 00199 /** 00200 * @brief Read HTS221 output register, and calculate the humidity 00201 * @param odr the pointer to the output data rate 00202 * @retval 0 in case of success, an error code otherwise 00203 */ 00204 int HTS221Sensor::get_odr(float *odr) 00205 { 00206 HTS221_Odr_et odr_low_level; 00207 00208 if (HTS221_Get_Odr((void *)this, &odr_low_level) == HTS221_ERROR) { 00209 return 1; 00210 } 00211 00212 switch (odr_low_level) { 00213 case HTS221_ODR_ONE_SHOT : 00214 *odr = 0.0f; 00215 break; 00216 case HTS221_ODR_1HZ : 00217 *odr = 1.0f; 00218 break; 00219 case HTS221_ODR_7HZ : 00220 *odr = 7.0f; 00221 break; 00222 case HTS221_ODR_12_5HZ : 00223 *odr = 12.5f; 00224 break; 00225 default : 00226 *odr = -1.0f; 00227 return 1; 00228 } 00229 00230 return 0; 00231 } 00232 00233 /** 00234 * @brief Set ODR 00235 * @param odr the output data rate to be set 00236 * @retval 0 in case of success, an error code otherwise 00237 */ 00238 int HTS221Sensor::set_odr(float odr) 00239 { 00240 HTS221_Odr_et new_odr; 00241 00242 new_odr = (odr <= 1.0f) ? HTS221_ODR_1HZ 00243 : (odr <= 7.0f) ? HTS221_ODR_7HZ 00244 : HTS221_ODR_12_5HZ ; 00245 00246 if (HTS221_Set_Odr((void *)this, new_odr) == HTS221_ERROR) { 00247 return 1; 00248 } 00249 00250 return 0; 00251 } 00252 00253 /** 00254 * @brief Get HTS221 heater configuration 00255 * @param heater the pointer to the heater configuration 00256 * @retval 0 in case of success, an error code otherwise 00257 */ 00258 int HTS221Sensor::get_heater(uint8_t *heater) 00259 { 00260 HTS221_State_et heater_low_level; 00261 00262 if (HTS221_Get_HeaterState((void *)this, &heater_low_level) == HTS221_ERROR) { 00263 return 1; 00264 } 00265 00266 switch (heater_low_level) { 00267 case HTS221_DISABLE: 00268 *heater = 0; 00269 break; 00270 case HTS221_ENABLE: 00271 *heater = 1; 00272 break; 00273 default: 00274 return 1; 00275 } 00276 00277 return 0; 00278 } 00279 00280 /** 00281 * @brief Set HTS221 heater configuration 00282 * @param heater the heater configuration to be set 00283 * @retval 0 in case of success, an error code otherwise 00284 */ 00285 int HTS221Sensor::set_heater(uint8_t heater) 00286 { 00287 HTS221_State_et new_heater; 00288 00289 new_heater = (heater == 0) ? HTS221_DISABLE 00290 : HTS221_ENABLE; 00291 00292 if (HTS221_Set_HeaterState((void *)this, new_heater) == HTS221_ERROR) { 00293 return 1; 00294 } 00295 00296 return 0; 00297 } 00298 00299 /** 00300 * @brief Get HTS221 averaging configuration 00301 * @param avgh the pointer to the averaging for humidity, avgt the pointer to the averaging for temperature 00302 * @retval 0 in case of success, an error code otherwise 00303 */ 00304 int HTS221Sensor::get_avg(float *avgh, float *avgt) 00305 { 00306 HTS221_Avgh_et avgh_low_level; 00307 HTS221_Avgt_et avgt_low_level; 00308 00309 if (HTS221_Get_AvgHT((void *)this, &avgh_low_level, &avgt_low_level) == HTS221_ERROR) { 00310 return 1; 00311 } 00312 00313 switch (avgh_low_level) { 00314 case HTS221_AVGH_4 : 00315 *avgh = 4; 00316 break; 00317 case HTS221_AVGH_8 : 00318 *avgh = 8; 00319 break; 00320 case HTS221_AVGH_16 : 00321 *avgh = 16; 00322 break; 00323 case HTS221_AVGH_32 : 00324 *avgh = 32; 00325 break; 00326 case HTS221_AVGH_64 : 00327 *avgh = 64; 00328 break; 00329 case HTS221_AVGH_128 : 00330 *avgh = 128; 00331 break; 00332 case HTS221_AVGH_256 : 00333 *avgh = 256; 00334 break; 00335 case HTS221_AVGH_512 : 00336 *avgh = 512; 00337 break; 00338 default: 00339 return 1; 00340 } 00341 00342 switch (avgt_low_level) { 00343 case HTS221_AVGT_2 : 00344 *avgt = 2; 00345 break; 00346 case HTS221_AVGT_4 : 00347 *avgt = 4; 00348 break; 00349 case HTS221_AVGT_8 : 00350 *avgt = 8; 00351 break; 00352 case HTS221_AVGT_16 : 00353 *avgt = 16; 00354 break; 00355 case HTS221_AVGT_32 : 00356 *avgt = 32; 00357 break; 00358 case HTS221_AVGT_64 : 00359 *avgt = 64; 00360 break; 00361 case HTS221_AVGT_128 : 00362 *avgt = 128; 00363 break; 00364 case HTS221_AVGT_256 : 00365 *avgt = 256; 00366 break; 00367 default: 00368 return 1; 00369 } 00370 00371 return 0; 00372 } 00373 00374 /** 00375 * @brief Set HTS221 averaging configuration 00376 * @param avgh the averagingfor humidity to be set, avgt the averaging for temperature to be set 00377 * @retval 0 in case of success, an error code otherwise 00378 */ 00379 int HTS221Sensor::set_avg(float avgh, float avgt) 00380 { 00381 HTS221_Avgh_et new_avgh; 00382 HTS221_Avgt_et new_avgt; 00383 00384 new_avgh = (avgh == 4) ? HTS221_AVGH_4 00385 : (avgh == 8) ? HTS221_AVGH_8 00386 : (avgh == 16) ? HTS221_AVGH_16 00387 : (avgh == 32) ? HTS221_AVGH_32 00388 : (avgh == 64) ? HTS221_AVGH_64 00389 : (avgh == 128) ? HTS221_AVGH_128 00390 : (avgh == 256) ? HTS221_AVGH_256 00391 : (avgh == 512) ? HTS221_AVGH_512 00392 : HTS221_AVGH_4 ; 00393 00394 new_avgt = (avgt == 2) ? HTS221_AVGT_2 00395 : (avgt == 4) ? HTS221_AVGT_4 00396 : (avgt == 8) ? HTS221_AVGT_8 00397 : (avgt == 16) ? HTS221_AVGT_16 00398 : (avgt == 32) ? HTS221_AVGT_32 00399 : (avgt == 64) ? HTS221_AVGT_64 00400 : (avgt == 128) ? HTS221_AVGT_128 00401 : (avgt == 256) ? HTS221_AVGT_256 00402 : HTS221_AVGT_2 ; 00403 00404 if (HTS221_Set_AvgHT((void *)this, new_avgh, new_avgt) == HTS221_ERROR) { 00405 return 1; 00406 } 00407 00408 return 0; 00409 } 00410 00411 /** 00412 * @brief Read the data from register 00413 * @param reg register address 00414 * @param data register data 00415 * @retval 0 in case of success 00416 * @retval 1 in case of failure 00417 */ 00418 int HTS221Sensor::read_reg(uint8_t reg, uint8_t *data) 00419 { 00420 00421 if (HTS221_read_reg((void *)this, reg, 1, data) == HTS221_ERROR) { 00422 return 1; 00423 } 00424 00425 return 0; 00426 } 00427 00428 /** 00429 * @brief Write the data to register 00430 * @param reg register address 00431 * @param data register data 00432 * @retval 0 in case of success 00433 * @retval 1 in case of failure 00434 */ 00435 int HTS221Sensor::write_reg(uint8_t reg, uint8_t data) 00436 { 00437 00438 if (HTS221_write_reg((void *)this, reg, 1, &data) == HTS221_ERROR) { 00439 return 1; 00440 } 00441 00442 return 0; 00443 } 00444 00445 uint8_t HTS221_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite) 00446 { 00447 return ((HTS221Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite); 00448 } 00449 00450 uint8_t HTS221_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead) 00451 { 00452 return ((HTS221Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead); 00453 }
Generated on Thu Jul 14 2022 22:19:05 by
1.7.2