LELEC2811 - I&S / HTS221

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTS221Sensor.h Source File

HTS221Sensor.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    HTS221Sensor.h
00004  * @author  CLab
00005  * @version V1.0.0
00006  * @date    5 August 2016
00007  * @brief   Abstract class 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 /* Prevent recursive inclusion -----------------------------------------------*/
00040 
00041 #ifndef __HTS221Sensor_H__
00042 #define __HTS221Sensor_H__
00043 
00044 
00045 /* Includes ------------------------------------------------------------------*/
00046 
00047 #include "DevI2C.h"
00048 #include <SPI.h>
00049 #include "HTS221_driver.h"
00050 #include "HumiditySensor.h"
00051 #include "TempSensor.h"
00052 #include <assert.h>
00053 
00054 /* Class Declaration ---------------------------------------------------------*/
00055 
00056 /**
00057  * Abstract class of an HTS221 Humidity and Temperature sensor.
00058  */
00059 class HTS221Sensor : public HumiditySensor, public TempSensor {
00060 public:
00061     HTS221Sensor(SPI *spi, PinName cs_pin = NC, PinName drdy_pin = NC); // SPI3W ONLY
00062     HTS221Sensor(DevI2C *i2c, uint8_t address = HTS221_I2C_ADDRESS, PinName drdy_pin = NC);
00063     virtual int init(void *init);
00064     virtual int read_id(uint8_t *id);
00065     virtual int get_humidity(float *pfData);
00066     virtual int get_temperature(float *pfData);
00067     int enable(void);
00068     int disable(void);
00069     int reset(void);
00070     int get_odr(float *odr);
00071     int set_odr(float odr);
00072     int get_heater(uint8_t *heater);
00073     int set_heater(uint8_t heater);
00074     int get_avg(float *avgh, float *avgt);
00075     int set_avg(float avgh, float avgt);
00076     int read_reg(uint8_t reg, uint8_t *data);
00077     int write_reg(uint8_t reg, uint8_t data);
00078     /**
00079      * @brief Utility function to read data.
00080      * @param  pBuffer: pointer to data to be read.
00081      * @param  RegisterAddr: specifies internal address register to be read.
00082      * @param  NumByteToRead: number of bytes to be read.
00083      * @retval 0 if ok, an error code otherwise.
00084      */
00085     uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
00086     {
00087         if (_dev_spi) {
00088             /* Write Reg Address */
00089             _dev_spi->lock();
00090             _cs_pin = 0;
00091             /* Write RD Reg Address with RD bit*/
00092             uint8_t TxByte = RegisterAddr | 0x80;
00093             _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
00094             _cs_pin = 1;
00095             _dev_spi->unlock();
00096             return 0;
00097         }
00098         if (_dev_i2c) {
00099             return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
00100         }
00101         return 1;
00102     }
00103 
00104     /**
00105      * @brief Utility function to write data.
00106      * @param  pBuffer: pointer to data to be written.
00107      * @param  RegisterAddr: specifies internal address register to be written.
00108      * @param  NumByteToWrite: number of bytes to write.
00109      * @retval 0 if ok, an error code otherwise.
00110      */
00111     uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
00112     {
00113         if (_dev_spi) {
00114             _dev_spi->lock();
00115             _cs_pin = 0;
00116             _dev_spi->write(RegisterAddr);
00117             _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
00118             _cs_pin = 1;
00119             _dev_spi->unlock();
00120             return 0;
00121         }
00122         if (_dev_i2c) {
00123             return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
00124         }
00125         return 1;
00126     }
00127 
00128 private:
00129 
00130     /* Helper classes. */
00131     DevI2C *_dev_i2c;
00132     SPI    *_dev_spi;
00133 
00134     /* Configuration */
00135     uint8_t _address;
00136     DigitalOut  _cs_pin;
00137     InterruptIn _drdy_pin;
00138 };
00139 
00140 #ifdef __cplusplus
00141 extern "C" {
00142 #endif
00143 uint8_t HTS221_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);
00144 uint8_t HTS221_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);
00145 #ifdef __cplusplus
00146 }
00147 #endif
00148 
00149 #endif