HTS221 relative humidity and temperature sensor library

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_IKS01A3

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?

UserRevisionLine numberNew contents of line
nikapov 0:7917d6d00a6e 1 /**
nikapov 0:7917d6d00a6e 2 ******************************************************************************
nikapov 0:7917d6d00a6e 3 * @file HTS221Sensor.h
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 Abstract class 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>&copy; 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 /* Prevent recursive inclusion -----------------------------------------------*/
nikapov 0:7917d6d00a6e 40
nikapov 0:7917d6d00a6e 41 #ifndef __HTS221Sensor_H__
nikapov 0:7917d6d00a6e 42 #define __HTS221Sensor_H__
nikapov 0:7917d6d00a6e 43
nikapov 0:7917d6d00a6e 44
nikapov 0:7917d6d00a6e 45 /* Includes ------------------------------------------------------------------*/
nikapov 0:7917d6d00a6e 46
nikapov 0:7917d6d00a6e 47 #include "DevI2C.h"
cparata 4:9f317607860e 48 #include <SPI.h>
nikapov 0:7917d6d00a6e 49 #include "HTS221_driver.h"
nikapov 0:7917d6d00a6e 50 #include "HumiditySensor.h"
nikapov 0:7917d6d00a6e 51 #include "TempSensor.h"
mapellil 1:c4391f20553e 52 #include <assert.h>
nikapov 0:7917d6d00a6e 53
nikapov 0:7917d6d00a6e 54 /* Class Declaration ---------------------------------------------------------*/
nikapov 0:7917d6d00a6e 55
nikapov 0:7917d6d00a6e 56 /**
nikapov 0:7917d6d00a6e 57 * Abstract class of an HTS221 Humidity and Temperature sensor.
nikapov 0:7917d6d00a6e 58 */
cparata 5:ccf7f36492ae 59 class HTS221Sensor : public HumiditySensor, public TempSensor {
cparata 5:ccf7f36492ae 60 public:
cparata 5:ccf7f36492ae 61 HTS221Sensor(SPI *spi, PinName cs_pin = NC, PinName drdy_pin = NC); // SPI3W ONLY
cparata 5:ccf7f36492ae 62 HTS221Sensor(DevI2C *i2c, uint8_t address = HTS221_I2C_ADDRESS, PinName drdy_pin = NC);
nikapov 0:7917d6d00a6e 63 virtual int init(void *init);
nikapov 0:7917d6d00a6e 64 virtual int read_id(uint8_t *id);
nikapov 0:7917d6d00a6e 65 virtual int get_humidity(float *pfData);
nikapov 0:7917d6d00a6e 66 virtual int get_temperature(float *pfData);
nikapov 0:7917d6d00a6e 67 int enable(void);
nikapov 0:7917d6d00a6e 68 int disable(void);
nikapov 0:7917d6d00a6e 69 int reset(void);
nikapov 0:7917d6d00a6e 70 int get_odr(float *odr);
nikapov 0:7917d6d00a6e 71 int set_odr(float odr);
martlefebvre94 6:7021a2a1ac72 72 int get_heater(uint8_t *heater);
martlefebvre94 6:7021a2a1ac72 73 int set_heater(uint8_t heater);
martlefebvre94 6:7021a2a1ac72 74 int get_avg(float *avgh, float *avgt);
martlefebvre94 6:7021a2a1ac72 75 int set_avg(float avgh, float avgt);
nikapov 0:7917d6d00a6e 76 int read_reg(uint8_t reg, uint8_t *data);
nikapov 0:7917d6d00a6e 77 int write_reg(uint8_t reg, uint8_t data);
nikapov 0:7917d6d00a6e 78 /**
nikapov 0:7917d6d00a6e 79 * @brief Utility function to read data.
nikapov 0:7917d6d00a6e 80 * @param pBuffer: pointer to data to be read.
nikapov 0:7917d6d00a6e 81 * @param RegisterAddr: specifies internal address register to be read.
nikapov 0:7917d6d00a6e 82 * @param NumByteToRead: number of bytes to be read.
nikapov 0:7917d6d00a6e 83 * @retval 0 if ok, an error code otherwise.
nikapov 0:7917d6d00a6e 84 */
cparata 5:ccf7f36492ae 85 uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead)
nikapov 0:7917d6d00a6e 86 {
mapellil 2:312ee2694a77 87 if (_dev_spi) {
cparata 5:ccf7f36492ae 88 /* Write Reg Address */
mapellil 2:312ee2694a77 89 _dev_spi->lock();
cparata 4:9f317607860e 90 _cs_pin = 0;
mapellil 2:312ee2694a77 91 /* Write RD Reg Address with RD bit*/
cparata 4:9f317607860e 92 uint8_t TxByte = RegisterAddr | 0x80;
mapellil 2:312ee2694a77 93 _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead);
mapellil 2:312ee2694a77 94 _cs_pin = 1;
cparata 4:9f317607860e 95 _dev_spi->unlock();
mapellil 2:312ee2694a77 96 return 0;
cparata 5:ccf7f36492ae 97 }
cparata 5:ccf7f36492ae 98 if (_dev_i2c) {
cparata 5:ccf7f36492ae 99 return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead);
cparata 5:ccf7f36492ae 100 }
mapellil 2:312ee2694a77 101 return 1;
nikapov 0:7917d6d00a6e 102 }
cparata 5:ccf7f36492ae 103
nikapov 0:7917d6d00a6e 104 /**
nikapov 0:7917d6d00a6e 105 * @brief Utility function to write data.
nikapov 0:7917d6d00a6e 106 * @param pBuffer: pointer to data to be written.
nikapov 0:7917d6d00a6e 107 * @param RegisterAddr: specifies internal address register to be written.
nikapov 0:7917d6d00a6e 108 * @param NumByteToWrite: number of bytes to write.
nikapov 0:7917d6d00a6e 109 * @retval 0 if ok, an error code otherwise.
nikapov 0:7917d6d00a6e 110 */
cparata 5:ccf7f36492ae 111 uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite)
nikapov 0:7917d6d00a6e 112 {
cparata 5:ccf7f36492ae 113 if (_dev_spi) {
mapellil 2:312ee2694a77 114 _dev_spi->lock();
mapellil 2:312ee2694a77 115 _cs_pin = 0;
cparata 5:ccf7f36492ae 116 _dev_spi->write(RegisterAddr);
cparata 5:ccf7f36492ae 117 _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0);
cparata 5:ccf7f36492ae 118 _cs_pin = 1;
mapellil 2:312ee2694a77 119 _dev_spi->unlock();
cparata 5:ccf7f36492ae 120 return 0;
cparata 5:ccf7f36492ae 121 }
cparata 5:ccf7f36492ae 122 if (_dev_i2c) {
cparata 5:ccf7f36492ae 123 return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite);
cparata 5:ccf7f36492ae 124 }
mapellil 2:312ee2694a77 125 return 1;
nikapov 0:7917d6d00a6e 126 }
nikapov 0:7917d6d00a6e 127
cparata 5:ccf7f36492ae 128 private:
nikapov 0:7917d6d00a6e 129
nikapov 0:7917d6d00a6e 130 /* Helper classes. */
mapellil 1:c4391f20553e 131 DevI2C *_dev_i2c;
cparata 4:9f317607860e 132 SPI *_dev_spi;
cparata 5:ccf7f36492ae 133
nikapov 0:7917d6d00a6e 134 /* Configuration */
nikapov 0:7917d6d00a6e 135 uint8_t _address;
cparata 5:ccf7f36492ae 136 DigitalOut _cs_pin;
cparata 5:ccf7f36492ae 137 InterruptIn _drdy_pin;
nikapov 0:7917d6d00a6e 138 };
nikapov 0:7917d6d00a6e 139
nikapov 0:7917d6d00a6e 140 #ifdef __cplusplus
cparata 5:ccf7f36492ae 141 extern "C" {
nikapov 0:7917d6d00a6e 142 #endif
cparata 5:ccf7f36492ae 143 uint8_t HTS221_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite);
cparata 5:ccf7f36492ae 144 uint8_t HTS221_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead);
nikapov 0:7917d6d00a6e 145 #ifdef __cplusplus
cparata 5:ccf7f36492ae 146 }
nikapov 0:7917d6d00a6e 147 #endif
nikapov 0:7917d6d00a6e 148
nikapov 0:7917d6d00a6e 149 #endif