Beta

Dependencies:   ST_INTERFACES X_NUCLEO_COMMON

Fork of X_NUCLEO_IKS01A2 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTS221Sensor.cpp Source File

HTS221Sensor.cpp

Go to the documentation of this file.
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>&copy; 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 #include "mbed.h"
00042 #include "DevI2C.h"
00043 #include "HTS221Sensor.h"
00044 #include "HTS221_driver.h"
00045 
00046 
00047 /* Class Implementation ------------------------------------------------------*/
00048 
00049 /** Constructor
00050  * @param i2c object of an helper class which handles the I2C peripheral
00051  * @param address the address of the component's instance
00052  */
00053 HTS221Sensor::HTS221Sensor(DevI2C &i2c) : _dev_i2c(i2c)
00054 {
00055   _address = HTS221_I2C_ADDRESS;
00056 };
00057 
00058 
00059 /** Constructor
00060  * @param i2c object of an helper class which handles the I2C peripheral
00061  * @param address the address of the component's instance
00062  */
00063 HTS221Sensor::HTS221Sensor(DevI2C &i2c, uint8_t address) : _dev_i2c(i2c), _address(address)
00064 {
00065 
00066 };
00067 
00068 /**
00069  * @brief     Initializing the component.
00070  * @param[in] init pointer to device specific initalization structure.
00071  * @retval    "0" in case of success, an error code otherwise.
00072  */
00073 int HTS221Sensor::init(void *init)
00074 {
00075   /* Power down the device */
00076   if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
00077   {
00078     return 1;
00079   }
00080 
00081   /* Enable BDU */
00082   if ( HTS221_Set_BduMode( (void *)this, HTS221_ENABLE ) == HTS221_ERROR )
00083   {
00084     return 1;
00085   }
00086   
00087   if(set_odr(1.0f) == 1)
00088   {
00089     return 1;
00090   }
00091   
00092   return 0;
00093 }
00094 
00095 /**
00096  * @brief  Enable HTS221
00097  * @retval 0 in case of success, an error code otherwise
00098  */
00099 int HTS221Sensor::enable(void)
00100 {
00101   /* Power up the device */
00102   if ( HTS221_Activate( (void *)this ) == HTS221_ERROR )
00103   {
00104     return 1;
00105   }
00106 
00107   return 0;
00108 }
00109 
00110 /**
00111  * @brief  Disable HTS221
00112  * @retval 0 in case of success, an error code otherwise
00113  */
00114 int HTS221Sensor::disable(void)
00115 {
00116   /* Power up the device */
00117   if ( HTS221_DeActivate( (void *)this ) == HTS221_ERROR )
00118   {
00119     return 1;
00120   }
00121 
00122   return 0;
00123 }
00124 
00125 /**
00126  * @brief  Read ID address of HTS221
00127  * @param  id the pointer where the ID of the device is stored
00128  * @retval 0 in case of success, an error code otherwise
00129  */
00130 int HTS221Sensor::read_id(uint8_t *id)
00131 {
00132   if(!id)
00133   { 
00134     return 1;
00135   }
00136   
00137   /* Read WHO AM I register */
00138   if ( HTS221_Get_DeviceID( (void *)this, id ) == HTS221_ERROR )
00139   {
00140     return 1;
00141   }
00142 
00143   return 0;
00144 }
00145 
00146 /**
00147  * @brief  Reboot memory content of HTS221
00148  * @param  None
00149  * @retval 0 in case of success, an error code otherwise
00150  */
00151 int HTS221Sensor::reset(void)
00152 {
00153     uint8_t tmpreg;
00154 
00155     /* Read CTRL_REG2 register */
00156     if (read_reg(HTS221_CTRL_REG2, &tmpreg) != 0)
00157     {
00158       return 1;
00159     }
00160 
00161     /* Enable or Disable the reboot memory */
00162     tmpreg |= (0x01 << HTS221_BOOT_BIT);
00163 
00164     /* Write value to MEMS CTRL_REG2 regsister */
00165     if (write_reg(HTS221_CTRL_REG2, tmpreg) != 0)
00166     {
00167       return 1;
00168     }
00169     
00170     return 0;
00171 }
00172 
00173 /**
00174  * @brief  Read HTS221 output register, and calculate the humidity
00175  * @param  pfData the pointer to data output
00176  * @retval 0 in case of success, an error code otherwise
00177  */
00178 int HTS221Sensor::get_humidity(float* pfData)
00179 {
00180   uint16_t uint16data = 0;
00181 
00182   /* Read data from HTS221. */
00183   if ( HTS221_Get_Humidity( (void *)this, &uint16data ) == HTS221_ERROR )
00184   {
00185     return 1;
00186   }
00187 
00188   *pfData = ( float )uint16data / 10.0f;
00189 
00190   return 0;
00191 }
00192 
00193 /**
00194  * @brief  Read HTS221 output register, and calculate the temperature
00195  * @param  pfData the pointer to data output
00196  * @retval 0 in case of success, an error code otherwise
00197  */
00198 int HTS221Sensor::get_temperature(float* pfData)
00199 {
00200   int16_t int16data = 0;
00201 
00202   /* Read data from HTS221. */
00203   if ( HTS221_Get_Temperature( (void *)this, &int16data ) == HTS221_ERROR )
00204   {
00205     return 1;
00206   }
00207 
00208   *pfData = ( float )int16data / 10.0f;
00209 
00210   return 0;
00211 }
00212 
00213 /**
00214  * @brief  Read HTS221 output register, and calculate the humidity
00215  * @param  odr the pointer to the output data rate
00216  * @retval 0 in case of success, an error code otherwise
00217  */
00218 int HTS221Sensor::get_odr(float* odr)
00219 {
00220   HTS221_Odr_et odr_low_level;
00221 
00222   if ( HTS221_Get_Odr( (void *)this, &odr_low_level ) == HTS221_ERROR )
00223   {
00224     return 1;
00225   }
00226 
00227   switch( odr_low_level )
00228   {
00229     case HTS221_ODR_ONE_SHOT :
00230       *odr =  0.0f;
00231       break;
00232     case HTS221_ODR_1HZ      :
00233       *odr =  1.0f;
00234       break;
00235     case HTS221_ODR_7HZ      :
00236       *odr =  7.0f;
00237       break;
00238     case HTS221_ODR_12_5HZ   :
00239       *odr = 12.5f;
00240       break;
00241     default                 :
00242       *odr = -1.0f;
00243       return 1;
00244   }
00245 
00246   return 0;
00247 }
00248 
00249 /**
00250  * @brief  Set ODR
00251  * @param  odr the output data rate to be set
00252  * @retval 0 in case of success, an error code otherwise
00253  */
00254 int HTS221Sensor::set_odr(float odr)
00255 {
00256   HTS221_Odr_et new_odr;
00257 
00258   new_odr = ( odr <= 1.0f ) ? HTS221_ODR_1HZ 
00259           : ( odr <= 7.0f ) ? HTS221_ODR_7HZ 
00260           :                   HTS221_ODR_12_5HZ ;
00261 
00262   if ( HTS221_Set_Odr( (void *)this, new_odr ) == HTS221_ERROR )
00263   {
00264     return 1;
00265   }
00266 
00267   return 0;
00268 }
00269 
00270 
00271 /**
00272  * @brief Read the data from register
00273  * @param reg register address
00274  * @param data register data
00275  * @retval 0 in case of success
00276  * @retval 1 in case of failure
00277  */
00278 int HTS221Sensor::read_reg( uint8_t reg, uint8_t *data )
00279 {
00280 
00281   if ( HTS221_read_reg( (void *)this, reg, 1, data ) == HTS221_ERROR )
00282   {
00283     return 1;
00284   }
00285 
00286   return 0;
00287 }
00288 
00289 /**
00290  * @brief Write the data to register
00291  * @param reg register address
00292  * @param data register data
00293  * @retval 0 in case of success
00294  * @retval 1 in case of failure
00295  */
00296 int HTS221Sensor::write_reg( uint8_t reg, uint8_t data )
00297 {
00298 
00299   if ( HTS221_write_reg( (void *)this, reg, 1, &data ) == HTS221_ERROR )
00300   {
00301     return 1;
00302   }
00303 
00304   return 0;
00305 }
00306 
00307 uint8_t HTS221_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite )
00308 {
00309   return ((HTS221Sensor *)handle)->io_write(pBuffer, WriteAddr, nBytesToWrite);
00310 }
00311 
00312 uint8_t HTS221_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead )
00313 {
00314   return ((HTS221Sensor *)handle)->io_read(pBuffer, ReadAddr, nBytesToRead);
00315 }