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

Public Member Functions

 FluxSand (PWM *pwm_buzzer, Gpio *gpio_user_button_1, Gpio *gpio_user_button_2, CompGuiX *gui, Bmp280 *bmp280, Aht20 *aht20, Ads1115< 2 > *ads1115, AHRS *ahrs, InferenceEngine *inference)
 
void Run ()
 
void RunUnitTest ()
 

Private Member Functions

void StartStopwatch ()
 
void StopStopwatch ()
 
void StartTimer (int duration_sec)
 
void StopTimer ()
 

Private Attributes

PWMpwm_buzzer_
 
Gpiogpio_user_button_1_
 
Gpiogpio_user_button_2_
 
CompGuiXgui_
 
Bmp280bmp280_
 
Aht20aht20_
 
Ads1115< 2 > * ads1115_
 
AHRSahrs_
 
InferenceEngineinference_
 
ModeManager mode_manager_
 
SensorManager sensor_manager_
 
InferenceHandler inference_handler_
 
InputHandler input_handler_
 

Detailed Description

Definition at line 25 of file fluxsand.hpp.

Constructor & Destructor Documentation

◆ FluxSand()

FluxSand::FluxSand ( PWM pwm_buzzer,
Gpio gpio_user_button_1,
Gpio gpio_user_button_2,
CompGuiX gui,
Bmp280 bmp280,
Aht20 aht20,
Ads1115< 2 > *  ads1115,
AHRS ahrs,
InferenceEngine inference 
)
inline

Definition at line 29 of file fluxsand.hpp.

32 : pwm_buzzer_(pwm_buzzer),
33 gpio_user_button_1_(gpio_user_button_1),
34 gpio_user_button_2_(gpio_user_button_2),
35 gui_(gui),
36 bmp280_(bmp280),
37 aht20_(aht20),
38 ads1115_(ads1115),
39 ahrs_(ahrs),
40 inference_(inference) {
41 // Wait for system stabilization or hardware warm-up
42 std::this_thread::sleep_for(std::chrono::milliseconds(6000));
43
44 // Prepare the buzzer
45 pwm_buzzer_->SetDutyCycle(0.0f);
46 pwm_buzzer_->Enable();
47
48 // Initialize sensor manager with sensor instances and GUI
49 sensor_manager_.Init(ads1115_, aht20_, bmp280_, gui_);
50
51 // Initialize inference handler with inference engine and mode manager
52 // and bind timer control callbacks
53 inference_handler_.Init(
54 inference_, &mode_manager_, gui_, pwm_buzzer_,
55 [this](int duration) { StartTimer(duration); },
56 [this]() { StopTimer(); });
57
58 // Initialize input handler with buttons and callbacks for stopwatch/timer
59 input_handler_.Init(
60 gpio_user_button_1_, gpio_user_button_2_, pwm_buzzer_, gui_,
61 &mode_manager_,
62 [this]() {
63 if (mode_manager_.IsStopwatchRunning())
64 StopStopwatch();
65 else
66 StartStopwatch();
67 },
68 [this]() { StopTimer(); });
69 }
void SetDutyCycle(float percent)
Set the duty cycle of PWM output.
Definition bsp_pwm.hpp:87
void Enable()
Enable the PWM output signal.
Definition bsp_pwm.hpp:95

Member Function Documentation

◆ Run()

void FluxSand::Run ( )
inline

Definition at line 72 of file fluxsand.hpp.

72 {
73 // Get current system time
74 auto now = std::chrono::system_clock::now();
75 std::time_t t = std::chrono::system_clock::to_time_t(now);
76 std::tm* local_time = std::localtime(&t);
77 int hour = local_time->tm_hour;
78 int minute = local_time->tm_min;
79
80 // Update GUI with the current tilt angle
81 gui_->SetGravityDegree(ahrs_->eulr_.rol);
82
83 // Get current UI mode and orientation
84 auto mode = mode_manager_.GetMode();
85 bool landscape = mode_manager_.IsLandscape();
86
87 switch (mode) {
88 case ModeManager::Mode::TIME:
89 // Render clock in portrait or landscape orientation
90 if (landscape)
91 gui_->RenderTimeLandscape(hour, minute);
92 else
93 gui_->RenderTimePortrait(hour, minute);
94 break;
95
96 case ModeManager::Mode::HUMIDITY:
97 // Display humidity reading
98 gui_->RenderHumidity(
99 static_cast<uint8_t>(sensor_manager_.GetHumidity()));
100 break;
101
102 case ModeManager::Mode::TEMPERATURE:
103 // Display compensated temperature, offset for calibration
104 gui_->RenderTemperature(static_cast<uint8_t>(
105 sensor_manager_.GetCompensatedTemperature()));
106 break;
107
108 case ModeManager::Mode::STOPWATCH: {
109 // Display stopwatch time with a 100-minute limit
110 int64_t display_sec = mode_manager_.GetStopwatchSeconds();
111 if (display_sec >= 100 * 60 - 1) display_sec = 100 * 60 - 1;
112 gui_->RenderTimeLandscape(display_sec / 60, display_sec % 60);
113 break;
114 }
115
116 case ModeManager::Mode::TIMER: {
117 // Handle timer countdown and animation
118 int64_t remaining = mode_manager_.GetRemainingTimerSeconds();
119
120 // If timer finished, trigger buzzer and disable sand animation
121 if (remaining == 0 && mode_manager_.IsTimerRunning()) {
122 pwm_buzzer_->PlayNote(PWM::NoteName::C, 8, 1000);
123 gui_->SandDisable();
124 mode_manager_.StopTimer();
125 }
126
127 if (landscape) {
128 // Show remaining time in landscape format
129 gui_->RenderTimeLandscapeMS(remaining / 60, remaining % 60);
130 } else if (mode_manager_.IsTimerRunning()) {
131 // In portrait mode, animate sand flow
132 gui_->SandEnable();
133 int target_grid_down_count =
134 128 - static_cast<int>(128.0f * static_cast<float>(remaining) /
135 static_cast<float>(
136 mode_manager_.GetMaxTimerDuration()));
137 target_grid_down_count = std::clamp(target_grid_down_count, 0, 128);
138
139 // Only move sand if more sand needs to fall
140 if (target_grid_down_count > gui_->grid_down_.Count()) {
141 SandGrid::MoveSand(&gui_->grid_up_, &gui_->grid_down_,
142 gui_->gravity_deg_);
143 }
144 } else {
145 // If timer not running, show static time
146 gui_->RenderTimePortraitMS(remaining / 60, remaining % 60);
147 }
148 break;
149 }
150
151 default:
152 std::cout << "Unknown mode\n";
153 }
154
155 // Small delay to prevent high CPU usage
156 std::this_thread::sleep_for(std::chrono::milliseconds(5));
157 }
void RenderTimePortraitMS(uint8_t minutes, uint8_t seconds)
Render MM:SS with dynamic colon.
Definition comp_gui.hpp:263
void RenderTimeLandscapeMS(uint8_t minutes, uint8_t seconds)
Render MM:SS with blinking colon.
Definition comp_gui.hpp:219
void SandDisable()
Disable sand simulation.
Definition comp_gui.hpp:77
void RenderTimePortrait(uint8_t hour, uint8_t minute)
Render HH:MM in portrait orientation.
Definition comp_gui.hpp:233
void RenderTimeLandscape(uint8_t hour, uint8_t minute)
Render HH:MM in landscape orientation.
Definition comp_gui.hpp:207
void SandEnable()
Enable sand simulation.
Definition comp_gui.hpp:74
SandGrid grid_down_
Sand particle containers.
Definition comp_gui.hpp:70
float gravity_deg_
Gravity direction (degrees)
Definition comp_gui.hpp:71
void SetGravityDegree(float gravity_angle)
Set gravity direction for sand physics.
Definition comp_gui.hpp:63
void PlayNote(NoteName note, uint32_t octave, uint32_t duration_ms)
Play a musical note based on note name and octave.
Definition bsp_pwm.hpp:121

◆ RunUnitTest()

void FluxSand::RunUnitTest ( )
inline

Definition at line 159 of file fluxsand.hpp.

159 {
160 std::cout << "[FluxSand::UnitTest] Starting application unit test...\n";
161
162 auto t0 = std::chrono::high_resolution_clock::now();
163
164 // Simulate a full Run() frame for each mode
165 for (int i = 0; i <= static_cast<int>(ModeManager::Mode::TIMER); ++i) {
166 ModeManager::Mode mode = static_cast<ModeManager::Mode>(i);
167 std::cout << std::format(" [Test] Simulating mode: {}\n", i);
168
169 // Force mode and sub-states
170 mode_manager_.SetMode(mode);
171 switch (mode) {
172 case ModeManager::Mode::STOPWATCH:
173 mode_manager_.StartStopwatch();
174 break;
175 case ModeManager::Mode::TIMER:
176 mode_manager_.StartTimer(30); // 30s dummy countdown
177 break;
178 default:
179 break;
180 }
181
182 auto start = std::chrono::high_resolution_clock::now();
183 Run(); // Run one cycle
184 auto end = std::chrono::high_resolution_clock::now();
185 float elapsed =
186 std::chrono::duration<float, std::milli>(end - start).count();
187 std::cout << std::format(" → Run() completed in {:.2f} ms\n", elapsed);
188 }
189
190 auto t1 = std::chrono::high_resolution_clock::now();
191 float total = std::chrono::duration<float, std::milli>(t1 - t0).count();
192 std::cout << std::format(
193 "[FluxSand::UnitTest] ✅ All modes tested in {:.2f} ms\n", total);
194 }

◆ StartStopwatch()

void FluxSand::StartStopwatch ( )
inlineprivate

Definition at line 198 of file fluxsand.hpp.

198{ mode_manager_.StartStopwatch(); }

◆ StartTimer()

void FluxSand::StartTimer ( int  duration_sec)
inlineprivate

Definition at line 202 of file fluxsand.hpp.

202 {
203 mode_manager_.StartTimer(duration_sec);
204 gui_->Reset();
205 }
void Reset()
Reset sand simulation.
Definition comp_gui.hpp:391

◆ StopStopwatch()

void FluxSand::StopStopwatch ( )
inlineprivate

Definition at line 199 of file fluxsand.hpp.

199{ mode_manager_.StopStopwatch(); }

◆ StopTimer()

void FluxSand::StopTimer ( )
inlineprivate

Definition at line 206 of file fluxsand.hpp.

206{ mode_manager_.StopTimer(); }

Field Documentation

◆ ads1115_

Ads1115<2>* FluxSand::ads1115_
private

Definition at line 215 of file fluxsand.hpp.

◆ ahrs_

AHRS* FluxSand::ahrs_
private

Definition at line 216 of file fluxsand.hpp.

◆ aht20_

Aht20* FluxSand::aht20_
private

Definition at line 214 of file fluxsand.hpp.

◆ bmp280_

Bmp280* FluxSand::bmp280_
private

Definition at line 213 of file fluxsand.hpp.

◆ gpio_user_button_1_

Gpio* FluxSand::gpio_user_button_1_
private

Definition at line 210 of file fluxsand.hpp.

◆ gpio_user_button_2_

Gpio* FluxSand::gpio_user_button_2_
private

Definition at line 211 of file fluxsand.hpp.

◆ gui_

CompGuiX* FluxSand::gui_
private

Definition at line 212 of file fluxsand.hpp.

◆ inference_

InferenceEngine* FluxSand::inference_
private

Definition at line 217 of file fluxsand.hpp.

◆ inference_handler_

InferenceHandler FluxSand::inference_handler_
private

Definition at line 222 of file fluxsand.hpp.

◆ input_handler_

InputHandler FluxSand::input_handler_
private

Definition at line 223 of file fluxsand.hpp.

◆ mode_manager_

ModeManager FluxSand::mode_manager_
private

Definition at line 220 of file fluxsand.hpp.

◆ pwm_buzzer_

PWM* FluxSand::pwm_buzzer_
private

Definition at line 209 of file fluxsand.hpp.

◆ sensor_manager_

SensorManager FluxSand::sensor_manager_
private

Definition at line 221 of file fluxsand.hpp.


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