Skip to content
Snippets Groups Projects
Commit d2b64af1 authored by Karol Krizka's avatar Karol Krizka Committed by Elisabetta Pianori
Browse files

Add support for HTS221 climate sensor

parent 48a4196a
Branches
No related tags found
No related merge requests found
......@@ -25,6 +25,7 @@ target_sources(DevCom
#BME280.cpp
HIH6130.cpp
HIH4000.cpp
HTS221.cpp
SHT85.cpp
Si7021.cpp
NTCSensor.cpp
......
#include "HTS221.h"
#include <unistd.h>
#include <math.h>
#include "NotSupportedException.h"
#include "ChecksumException.h"
HTS221::HTS221(std::shared_ptr<I2CCom> i2c)
: m_i2c(i2c)
{ }
void HTS221::init()
{
// Read temperature calibration
m_T0_degC=m_i2c->read_reg8(0x32);
m_T1_degC=m_i2c->read_reg8(0x33);
uint8_t T1T0msb = m_i2c->read_reg8(0x35);
m_T0_degC|=((T1T0msb&0x3)<<8);
m_T1_degC|=((T1T0msb&0xC)<<6);
m_T0_OUT=(m_i2c->read_reg8(0x3D)<<8)|(m_i2c->read_reg8(0x3C)<<0);
m_T1_OUT=(m_i2c->read_reg8(0x3F)<<8)|(m_i2c->read_reg8(0x3E)<<0);
// Read humidity calibration
m_H0_rH=m_i2c->read_reg8(0x30);
m_H1_rH=m_i2c->read_reg8(0x31);
m_H0_T0_OUT=(m_i2c->read_reg8(0x37)<<8)|(m_i2c->read_reg8(0x36)<<0);
m_H1_T0_OUT=(m_i2c->read_reg8(0x3B)<<8)|(m_i2c->read_reg8(0x3A)<<0);
// Power on the device
powerDown(false);
}
void HTS221::reset()
{ }
void HTS221::powerDown(bool value)
{
uint8_t CTRL_REG1=m_i2c->read_reg8(0x20);
m_i2c->write_reg8(0x20, CTRL_REG1|((!value)<<7));
}
void HTS221::oneShot()
{
uint8_t CTRL_REG2=m_i2c->read_reg8(0x21);
m_i2c->write_reg8(0x21, CTRL_REG2|1);
}
void HTS221::read()
{
oneShot(); // Start a one shot measurement
do
{ m_status = m_i2c->read_reg8(0x27); }
while(((m_status&0x3)!=3));
// Get temperature
int16_t T_OUT=(m_i2c->read_reg8(0x2B)<<8)|(m_i2c->read_reg8(0x2A)<<0);
m_temperature = (m_T1_degC - m_T0_degC)*(T_OUT - m_T0_OUT)/(m_T1_OUT - m_T0_OUT) + m_T0_degC;
m_temperature/=8;
// Get humidity
int16_t H_T_OUT=(m_i2c->read_reg8(0x29)<<8)|(m_i2c->read_reg8(0x28)<<0);
m_humidity = (m_H1_rH - m_H0_rH)*(H_T_OUT - m_H0_T0_OUT)/(m_H1_T0_OUT - m_H0_T0_OUT) + m_H0_rH;
m_humidity/=2;
}
uint HTS221::status() const
{ return m_status; }
float HTS221::temperature() const
{ return m_temperature; }
float HTS221::humidity() const
{ return m_humidity; }
float HTS221::pressure() const
{ throw NotSupportedException("Si7021 does not have a pressure sensor"); }
#ifndef HTS221_H
#define HTS221_H
#include "ClimateSensor.h"
#include "I2CCom.h"
#include <memory>
/**
* The HTS221 climate sensor.
* [Datasheet](https://www.st.com/resource/en/datasheet/hts221.pdf)
*/
class HTS221 : public ClimateSensor
{
public:
HTS221(std::shared_ptr<I2CCom> i2c);
virtual ~HTS221() =default;
virtual void init();
virtual void reset();
virtual void read();
virtual unsigned status() const;
virtual float temperature() const;
virtual float humidity() const;
virtual float pressure() const;
//! Set power down bit
/**
* \param value True(/false) to enable(/disable) power down mode
*/
void powerDown(bool value);
//! Trigger a new measurement when running in one-shot mode.
void oneShot();
private:
std::shared_ptr<I2CCom> m_i2c;
//
// Calibration information
// Temperature
int16_t m_T0_degC;
int16_t m_T1_degC;
int16_t m_T0_OUT;
int16_t m_T1_OUT;
// Humidity
int16_t m_H0_rH;
int16_t m_H1_rH;
int16_t m_H0_T0_OUT;
int16_t m_H1_T0_OUT;
//
// Data
int m_status;
float m_temperature;
float m_humidity;
};
#endif // HTS221_H
......@@ -3,6 +3,7 @@
#include "ClimateSensor.h"
#include "NTCSensor.h"
#include "HTS221.h"
#include "Si7021.h"
#include "SHT85.h"
#include "HIH6130.h"
......@@ -647,13 +648,15 @@ void register_devcom(py::module& m) {
.def("pressure", &ClimateSensor::pressure)
.def("dewPoint", &ClimateSensor::dewPoint);
py::class_<NTCSensor, PySensor<NTCSensor>, ClimateSensor, std::shared_ptr<NTCSensor>>(m, "NTCSensor")
.def(py::init<uint8_t, std::shared_ptr<ADCDevice>, bool, float, float, float, bool, float>())
.def(py::init<uint8_t, std::shared_ptr<ADCDevice>, bool, float, float, float, bool, float, float>());
py::class_<NTCSensor, PySensor<NTCSensor>, ClimateSensor, std::shared_ptr<NTCSensor>>(m, "NTCSensor");
py::class_<HTS221, PySensor_w_status<HTS221>, ClimateSensor, std::shared_ptr<HTS221>>(m, "HTS221")
.def(py::init<std::shared_ptr<I2CCom>>())
.def("status", &HTS221::status);
py::class_<Si7021, PySensor<Si7021>, ClimateSensor, std::shared_ptr<Si7021>>(m, "Si7021")
.def(py::init<std::shared_ptr<I2CCom>>());
py::class_<SHT85, PySensor_w_status<SHT85>, ClimateSensor, std::shared_ptr<SHT85>>(m, "SHT85")
.def(py::init<std::shared_ptr<I2CCom>>())
.def("status", &SHT85::status);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment