HTS221 relative humidity and temperature sensor library
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Diff: HTS221Sensor.h
- Revision:
- 5:ccf7f36492ae
- Parent:
- 4:9f317607860e
- Child:
- 6:7021a2a1ac72
--- a/HTS221Sensor.h Mon Mar 04 13:50:28 2019 +0000 +++ b/HTS221Sensor.h Wed Jul 24 14:17:21 2019 +0000 @@ -56,11 +56,10 @@ /** * Abstract class of an HTS221 Humidity and Temperature sensor. */ -class HTS221Sensor : public HumiditySensor, public TempSensor -{ - public: - HTS221Sensor(SPI *spi, PinName cs_pin=NC, PinName drdy_pin=NC); // SPI3W ONLY - HTS221Sensor(DevI2C *i2c, uint8_t address=HTS221_I2C_ADDRESS, PinName drdy_pin=NC); +class HTS221Sensor : public HumiditySensor, public TempSensor { +public: + HTS221Sensor(SPI *spi, PinName cs_pin = NC, PinName drdy_pin = NC); // SPI3W ONLY + HTS221Sensor(DevI2C *i2c, uint8_t address = HTS221_I2C_ADDRESS, PinName drdy_pin = NC); virtual int init(void *init); virtual int read_id(uint8_t *id); virtual int get_humidity(float *pfData); @@ -79,10 +78,10 @@ * @param NumByteToRead: number of bytes to be read. * @retval 0 if ok, an error code otherwise. */ - uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) + uint8_t io_read(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) { if (_dev_spi) { - /* Write Reg Address */ + /* Write Reg Address */ _dev_spi->lock(); _cs_pin = 0; /* Write RD Reg Address with RD bit*/ @@ -91,11 +90,13 @@ _cs_pin = 1; _dev_spi->unlock(); return 0; - } - if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); + } + if (_dev_i2c) { + return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); + } return 1; } - + /** * @brief Utility function to write data. * @param pBuffer: pointer to data to be written. @@ -103,40 +104,42 @@ * @param NumByteToWrite: number of bytes to write. * @retval 0 if ok, an error code otherwise. */ - uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) + uint8_t io_write(uint8_t *pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) { - if (_dev_spi) { + if (_dev_spi) { _dev_spi->lock(); _cs_pin = 0; - _dev_spi->write(RegisterAddr); - _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); - _cs_pin = 1; + _dev_spi->write(RegisterAddr); + _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); + _cs_pin = 1; _dev_spi->unlock(); - return 0; - } - if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); + return 0; + } + if (_dev_i2c) { + return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); + } return 1; } - private: +private: /* Helper classes. */ DevI2C *_dev_i2c; SPI *_dev_spi; - + /* Configuration */ uint8_t _address; - DigitalOut _cs_pin; - InterruptIn _drdy_pin; + DigitalOut _cs_pin; + InterruptIn _drdy_pin; }; #ifdef __cplusplus - extern "C" { +extern "C" { #endif -uint8_t HTS221_io_write( void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite ); -uint8_t HTS221_io_read( void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead ); +uint8_t HTS221_io_write(void *handle, uint8_t WriteAddr, uint8_t *pBuffer, uint16_t nBytesToWrite); +uint8_t HTS221_io_read(void *handle, uint8_t ReadAddr, uint8_t *pBuffer, uint16_t nBytesToRead); #ifdef __cplusplus - } +} #endif #endif