-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSHT21.hpp
38 lines (32 loc) · 1.03 KB
/
SHT21.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#include "I2C.hpp"
class SHT21 : private I2C
{
public:
SHT21(int i2c_instance) : I2C(i2c_instance, m_i2c_address)
{
}
virtual ~SHT21()
{
}
double read_temperature()
{
this->write<1>({{m_trig_temp_measurement}});
auto raw_data_arr{this->read<2>()};
uint16_t raw_data{static_cast<uint16_t>((raw_data_arr[0] << 8) | (raw_data_arr[1] & 0xFC))};
double temperature{-46.85 + 175.72 / 65536.0 * static_cast<double>(raw_data)};
return temperature;
}
double read_humidity()
{
this->write<1>({{m_trig_humid_measurement}});
auto raw_data_arr{this->read<2>()};
uint16_t raw_data{static_cast<uint16_t>((raw_data_arr[0] << 8) | (raw_data_arr[1] & 0xFC))};
double humidity{-6.0 + 125.0 / 65536.0 * static_cast<double>(raw_data)};
return humidity;
}
private:
static constexpr int m_i2c_address{0x40};
static constexpr int m_trig_temp_measurement{0xE3};
static constexpr int m_trig_humid_measurement{0xE5};
};