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

Public Member Functions

void Init (Ads1115< 2 > *ads, Aht20 *aht, Bmp280 *bmp, CompGuiX *gui)
 
float GetTemperature () const
 
float GetLight () const
 
float GetHumidity () const
 
float GetCompensatedTemperature () const
 
float GetPressure () const
 

Private Attributes

Aht20aht_ = nullptr
 
Bmp280bmp_ = nullptr
 
CompGuiXgui_ = nullptr
 
float temperature_ = 0.0f
 
float light_ = 0.0f
 
std::deque< float > light_queue_
 

Detailed Description

Definition at line 15 of file sensor_manager.hpp.

Member Function Documentation

◆ GetCompensatedTemperature()

float SensorManager::GetCompensatedTemperature ( ) const
inline

Definition at line 83 of file sensor_manager.hpp.

83 {
84 return aht_ ? aht_->GetTemperature() : 0.0f;
85 }

◆ GetHumidity()

float SensorManager::GetHumidity ( ) const
inline

Definition at line 80 of file sensor_manager.hpp.

80{ return aht_ ? aht_->GetHumidity() : 0.0f; }

◆ GetLight()

float SensorManager::GetLight ( ) const
inline

Definition at line 77 of file sensor_manager.hpp.

77{ return light_; }

◆ GetPressure()

float SensorManager::GetPressure ( ) const
inline

Definition at line 88 of file sensor_manager.hpp.

88{ return bmp_ ? bmp_->ReadPressure() : 0.0f; }
float ReadPressure()
Definition bmp280.hpp:73

◆ GetTemperature()

float SensorManager::GetTemperature ( ) const
inline

Definition at line 74 of file sensor_manager.hpp.

74{ return temperature_; }

◆ Init()

void SensorManager::Init ( Ads1115< 2 > *  ads,
Aht20 aht,
Bmp280 bmp,
CompGuiX gui 
)
inline

Definition at line 18 of file sensor_manager.hpp.

18 {
19 aht_ = aht;
20 bmp_ = bmp;
21 gui_ = gui;
22
23 // Register callback for channel 0: NTC thermistor (external temperature)
24 ads->RegisterChannelCallback(0, [this](float voltage) {
25 constexpr float VCC = 3.3f; // Supply voltage
26 constexpr float R_REF = 100000.0f; // Reference resistor value (Ω)
27 float r_ntc =
28 R_REF * voltage / (VCC - voltage); // Calculate NTC resistance
29
30 // Steinhart-Hart approximation constants for thermistor
31 constexpr float B = 3950.0f; // B-value of thermistor
32 constexpr float T0 = 298.15f; // Reference temperature (Kelvin)
33 constexpr float R0 = 10000.0f; // Reference resistance at T0
34
35 // Convert resistance to temperature in Celsius
36 float temp = 1.0f / (1.0f / T0 + (1.0f / B) * log(r_ntc / R0)) - 273.15f;
37 temperature_ = temp;
38 });
39
40 // Register callback for channel 1: Photodiode (ambient light sensor)
41 ads->RegisterChannelCallback(1, [this](float voltage) {
42 constexpr float VCC = 3.3f;
43 constexpr float R_REF = 100000.0f;
44 float r_photo =
45 R_REF * voltage / (VCC - voltage); // Calculate resistance
46
47 // Empirical constants for light sensor calibration
48 constexpr float K = 1500000.0f;
49 constexpr float GAMMA = 1.5f;
50 float lux = K / pow(r_photo, GAMMA); // Convert resistance to lux
51
52 // Maintain a moving average with a deque of last 50 readings
53 light_queue_.push_front(lux);
54 if (light_queue_.size() > 50) {
55 float avg = 0.0f;
56 for (auto l : light_queue_) avg += l;
57 avg /= light_queue_.size();
58 light_ = avg;
59
60 // Reduce GUI update rate using a counter
61 static int counter = 0;
62 if (++counter > 5) {
63 if (gui_) gui_->SetLight(static_cast<uint8_t>(light_ / 20 + 1));
64 counter = 0;
65 }
66
67 // Keep the queue size within limit
68 light_queue_.pop_back();
69 }
70 });
71 }
void RegisterChannelCallback(int channel, std::function< void(float)> cb)
Registers a callback function for a specific channel.
Definition ads1115.hpp:97
void SetLight(uint8_t light)
Set display brightness (0-15)
Definition comp_gui.hpp:204

Field Documentation

◆ aht_

Aht20* SensorManager::aht_ = nullptr
private

Definition at line 92 of file sensor_manager.hpp.

◆ bmp_

Bmp280* SensorManager::bmp_ = nullptr
private

Definition at line 93 of file sensor_manager.hpp.

◆ gui_

CompGuiX* SensorManager::gui_ = nullptr
private

Definition at line 94 of file sensor_manager.hpp.

◆ light_

float SensorManager::light_ = 0.0f
private

Definition at line 98 of file sensor_manager.hpp.

◆ light_queue_

std::deque<float> SensorManager::light_queue_
private

Definition at line 101 of file sensor_manager.hpp.

◆ temperature_

float SensorManager::temperature_ = 0.0f
private

Definition at line 97 of file sensor_manager.hpp.


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