FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
sensor_manager.hpp
1#pragma once
2
3#include <cmath>
4#include <cstdint>
5#include <deque>
6#include <functional>
7
8#include "ads1115.hpp"
9#include "aht20.hpp"
10#include "bmp280.hpp"
11#include "comp_gui.hpp"
12
13// Manages environmental sensors and signal processing for temperature,
14// humidity, pressure, and ambient light. Also handles GUI brightness updates.
16 public:
17 // Initialize the sensor manager with connected sensor instances and GUI.
18 void Init(Ads1115<2>* ads, Aht20* aht, Bmp280* bmp, CompGuiX* gui) {
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 }
72
73 // Accessor for external thermistor temperature (processed via ADS1115).
74 float GetTemperature() const { return temperature_; }
75
76 // Accessor for smoothed light level in lux.
77 float GetLight() const { return light_; }
78
79 // Accessor for humidity from AHT20 sensor.
80 float GetHumidity() const { return aht_ ? aht_->GetHumidity() : 0.0f; }
81
82 // Accessor for ambient temperature from AHT20 (used for compensation).
83 float GetCompensatedTemperature() const {
84 return aht_ ? aht_->GetTemperature() : 0.0f;
85 }
86
87 // Accessor for barometric pressure from BMP280 sensor.
88 float GetPressure() const { return bmp_ ? bmp_->ReadPressure() : 0.0f; }
89
90 private:
91 // Sensor and GUI component references
92 Aht20* aht_ = nullptr;
93 Bmp280* bmp_ = nullptr;
94 CompGuiX* gui_ = nullptr;
95
96 // Cached sensor values
97 float temperature_ = 0.0f; // External thermistor temperature
98 float light_ = 0.0f; // Smoothed ambient light in lux
99
100 // Queue for moving average light smoothing
101 std::deque<float> light_queue_;
102};
ADS1115 ADC driver with GPIO interrupt-based channel cycling.
Definition ads1115.hpp:22
void RegisterChannelCallback(int channel, std::function< void(float)> cb)
Registers a callback function for a specific channel.
Definition ads1115.hpp:97
float ReadPressure()
Definition bmp280.hpp:73
LED Matrix GUI Controller with Dual Orientation Support and Sand Animation Physics.
Definition comp_gui.hpp:15
void SetLight(uint8_t light)
Set display brightness (0-15)
Definition comp_gui.hpp:204