Capacitive digital sensor for relative humidity and temperature.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: HelloWorld_ST_Sensors MOTENV_Mbed mbed-os-mqtt-client HTS221_JS ... more
Revision 2:312ee2694a77, committed 2017-10-02
- Comitter:
- mapellil
- Date:
- Mon Oct 02 09:28:41 2017 +0200
- Parent:
- 1:c4391f20553e
- Child:
- 3:aadcccf527ef
- Commit message:
- Added SPI3/3Wires sensor support, some API modifcations
Changed in this revision
HTS221Sensor.cpp | Show annotated file Show diff for this revision Revisions of this file |
HTS221Sensor.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/HTS221Sensor.cpp Wed Sep 27 16:36:58 2017 +0200 +++ b/HTS221Sensor.cpp Mon Oct 02 09:28:41 2017 +0200 @@ -44,14 +44,22 @@ /* Class Implementation ------------------------------------------------------*/ +HTS221Sensor::HTS221Sensor(SPI *spi, PinName cs_pin, PinName drdy_pin) : + _dev_spi(spi), _cs_pin(cs_pin), _drdy_pin(drdy_pin) // SPI3W ONLY +{ + assert(spi); + _dev_i2c = NULL; +}; + /** Constructor * @param i2c object of an helper class which handles the I2C peripheral * @param address the address of the component's instance */ -HTS221Sensor::HTS221Sensor(DevI2C *i2c, uint8_t address, PinName drdy_pin) : - _dev_i2c(i2c), _address(address), _cs_pin(NC), _drdy_pin(drdy_pin) +HTS221Sensor::HTS221Sensor(DevI2C *i2c, uint8_t address, PinName drdy_pin) : + _dev_i2c(i2c), _address(address), _cs_pin(NC), _drdy_pin(drdy_pin) { - assert (i2c); + assert(i2c); + _dev_spi = NULL; }; /**
--- a/HTS221Sensor.h Wed Sep 27 16:36:58 2017 +0200 +++ b/HTS221Sensor.h Mon Oct 02 09:28:41 2017 +0200 @@ -58,6 +58,7 @@ 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); @@ -79,7 +80,19 @@ */ uint8_t io_read(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToRead) { - return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); + if (_dev_spi) { + /* Write Reg Address */ + _dev_spi->lock(); + _cs_pin = 0; + /* Write RD Reg Address with RD bit*/ + uint8_t TxByte = RegisterAddr | 0x80; + _dev_spi->write((char *)&TxByte, 1, (char *)pBuffer, (int) NumByteToRead); + _cs_pin = 1; + _dev_spi->unlock(); + return 0; + } + if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_read(pBuffer, _address, RegisterAddr, NumByteToRead); + return 1; } /** @@ -91,13 +104,24 @@ */ uint8_t io_write(uint8_t* pBuffer, uint8_t RegisterAddr, uint16_t NumByteToWrite) { - return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); + if (_dev_spi) { + _dev_spi->lock(); + _cs_pin = 0; + int data = _dev_spi->write(RegisterAddr); + _dev_spi->write((char *)pBuffer, (int) NumByteToWrite, NULL, 0); + _cs_pin = 1; + _dev_spi->unlock(); + return data; + } + if (_dev_i2c) return (uint8_t) _dev_i2c->i2c_write(pBuffer, _address, RegisterAddr, NumByteToWrite); + return 1; } private: /* Helper classes. */ DevI2C *_dev_i2c; + SPI * _dev_spi; /* Configuration */ uint8_t _address;