FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
aht20.hpp
1#pragma once
2
3#include <atomic>
4#include <chrono>
5#include <cstdint>
6#include <iostream>
7#include <stdexcept>
8#include <thread>
9
10#include "bsp_i2c.hpp" // I2cDevice class definition
11
12/* AHT20 temperature and humidity sensor driver */
13/* Supports I2C communication and periodic measurement */
14class Aht20 {
15 public:
16 /* Default I2C address of AHT20 (0x38) */
17 static constexpr uint8_t DEFAULT_I2C_ADDR = 0x38;
18
24 explicit Aht20(I2cDevice& i2c) : i2c_(i2c) {
25 InitSensor();
26 thread_ = std::thread(&Aht20::ThreadFun, this);
27 }
28
33 running_ = false;
34 if (thread_.joinable()) {
35 thread_.join();
36 }
37 }
38
42 void Display() {
43 std::cout << "Temperature: " << temperature_ << " °C" << '\n';
44 std::cout << "Humidity: " << humidity_ << " %RH" << '\n';
45 }
46
47 /* Get the current temperature value (unit: Celsius) */
48 float GetTemperature() const { return temperature_; }
49
50 /* Get the current humidity value (unit: %RH) */
51 float GetHumidity() const { return humidity_; }
52
53 private:
54 I2cDevice& i2c_; /* Reference to I2C device */
55 std::thread thread_; /* Background measurement thread */
56 std::atomic<bool> running_{true};
57
58 float temperature_ = 0.0f; /* Current temperature */
59 float humidity_ = 0.0f; /* Current humidity */
60
64 void InitSensor() {
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 }
70
74 void ThreadFun() {
75 while (running_) {
76 ReadSensor();
77 std::this_thread::sleep_for(std::chrono::milliseconds(500));
78 }
79 }
80
84 void ReadSensor() {
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 }
110};
void InitSensor()
Definition aht20.hpp:64
~Aht20()
Definition aht20.hpp:32
void Display()
Definition aht20.hpp:42
void ThreadFun()
Definition aht20.hpp:74
Aht20(I2cDevice &i2c)
Definition aht20.hpp:24
void ReadSensor()
Definition aht20.hpp:84
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
void WriteRegister(uint8_t reg, uint8_t value)
Definition bsp_i2c.hpp:72