FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
Aht20 Class Reference
Collaboration diagram for Aht20:

Public Member Functions

 Aht20 (I2cDevice &i2c)
 
 ~Aht20 ()
 
void Display ()
 
float GetTemperature () const
 
float GetHumidity () const
 

Static Public Attributes

static constexpr uint8_t DEFAULT_I2C_ADDR = 0x38
 

Private Member Functions

void InitSensor ()
 
void ThreadFun ()
 
void ReadSensor ()
 

Private Attributes

I2cDevicei2c_
 
std::thread thread_
 
std::atomic< bool > running_ {true}
 
float temperature_ = 0.0f
 
float humidity_ = 0.0f
 

Detailed Description

Definition at line 14 of file aht20.hpp.

Constructor & Destructor Documentation

◆ Aht20()

Aht20::Aht20 ( I2cDevice i2c)
inlineexplicit

Constructor: initialize the sensor and start the measurement thread

Parameters
i2cReference to an I2C device

Definition at line 24 of file aht20.hpp.

24 : i2c_(i2c) {
25 InitSensor();
26 thread_ = std::thread(&Aht20::ThreadFun, this);
27 }
void InitSensor()
Definition aht20.hpp:64
void ThreadFun()
Definition aht20.hpp:74

◆ ~Aht20()

Aht20::~Aht20 ( )
inline

Destructor: stop the background thread

Definition at line 32 of file aht20.hpp.

32 {
33 running_ = false;
34 if (thread_.joinable()) {
35 thread_.join();
36 }
37 }

Member Function Documentation

◆ Display()

void Aht20::Display ( )
inline

Print current temperature and humidity to stdout

Definition at line 42 of file aht20.hpp.

42 {
43 std::cout << "Temperature: " << temperature_ << " °C" << '\n';
44 std::cout << "Humidity: " << humidity_ << " %RH" << '\n';
45 }

◆ GetHumidity()

float Aht20::GetHumidity ( ) const
inline

Definition at line 51 of file aht20.hpp.

51{ return humidity_; }

◆ GetTemperature()

float Aht20::GetTemperature ( ) const
inline

Definition at line 48 of file aht20.hpp.

48{ return temperature_; }

◆ InitSensor()

void Aht20::InitSensor ( )
inlineprivate

Initialize the AHT20 sensor

Definition at line 64 of file aht20.hpp.

64 {
65 /* Write initialization sequence */
66 i2c_.WriteRegister(0xBE, 0x08);
67 i2c_.WriteRegister(0xBE, 0x00);
68 std::this_thread::sleep_for(std::chrono::milliseconds(10));
69 }
void WriteRegister(uint8_t reg, uint8_t value)
Definition bsp_i2c.hpp:72

◆ ReadSensor()

void Aht20::ReadSensor ( )
inlineprivate

Read and decode one sample of temperature and humidity

Definition at line 84 of file aht20.hpp.

84 {
85 const uint8_t CMD[3] = {0xAC, 0x33, 0x00};
86 i2c_.WriteRaw(CMD, 3); /* Send measurement command */
87
88 std::this_thread::sleep_for(
89 std::chrono::milliseconds(80)); /* Wait for conversion */
90
91 uint8_t buf[6] = {};
92 i2c_.ReadRegisters(0x00, buf, 6); /* Read 6-byte measurement data */
93
94 if ((buf[0] & 0x80) != 0) {
95 return; /* Sensor is busy */
96 }
97
98 /* Parse humidity */
99 uint32_t raw_h = ((buf[1] << 12) | (buf[2] << 4) | (buf[3] >> 4));
100 float humidity = static_cast<float>(raw_h) * 100.0f / 1048576.0f;
101
102 if (humidity != 0.0f) {
103 humidity_ = humidity;
104 }
105
106 /* Parse temperature */
107 uint32_t raw_t = ((buf[3] & 0x0F) << 16) | (buf[4] << 8) | buf[5];
108 temperature_ = static_cast<float>(raw_t) * 200.0f / 1048576.0f - 50.0f;
109 }
void WriteRaw(const uint8_t *data, size_t length)
Definition bsp_i2c.hpp:104
void ReadRegisters(uint8_t reg, uint8_t *buffer, size_t length)
Definition bsp_i2c.hpp:86

◆ ThreadFun()

void Aht20::ThreadFun ( )
inlineprivate

Thread function: read temperature and humidity every 500ms

Definition at line 74 of file aht20.hpp.

74 {
75 while (running_) {
76 ReadSensor();
77 std::this_thread::sleep_for(std::chrono::milliseconds(500));
78 }
79 }
void ReadSensor()
Definition aht20.hpp:84

Field Documentation

◆ DEFAULT_I2C_ADDR

constexpr uint8_t Aht20::DEFAULT_I2C_ADDR = 0x38
staticconstexpr

Definition at line 17 of file aht20.hpp.

◆ humidity_

float Aht20::humidity_ = 0.0f
private

Definition at line 59 of file aht20.hpp.

◆ i2c_

I2cDevice& Aht20::i2c_
private

Definition at line 54 of file aht20.hpp.

◆ running_

std::atomic<bool> Aht20::running_ {true}
private

Definition at line 56 of file aht20.hpp.

56{true};

◆ temperature_

float Aht20::temperature_ = 0.0f
private

Definition at line 58 of file aht20.hpp.

◆ thread_

std::thread Aht20::thread_
private

Definition at line 55 of file aht20.hpp.


The documentation for this class was generated from the following file: