FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
inference_handler.hpp
1#pragma once
2
3#include "bsp_pwm.hpp"
4#include "comp_gui.hpp"
5#include "comp_inference.hpp"
6#include "mode_manager.hpp"
7
8// Handles gesture inference results and maps them to application actions.
9// Uses callbacks and mode state to update GUI and behavior.
11 public:
12 // Initializes the inference handler with required components and callbacks.
13 void Init(InferenceEngine* inference, ModeManager* mode_manager,
14 CompGuiX* gui, PWM* buzzer,
15 std::function<void(int)> startTimerCallback,
16 std::function<void()> stopTimerCallback) {
17 mode_manager_ = mode_manager;
18 gui_ = gui;
19 buzzer_ = buzzer;
20 startTimerCallback_ = std::move(startTimerCallback);
21 stopTimerCallback_ = std::move(stopTimerCallback);
22
23 // Register a callback to receive gesture inference results
24 inference->RegisterDataCallback([this](ModelOutput result) {
25 std::cout << "New Gesture: " << LABELS.find(result)->second << '\n';
26
27 // Play a confirmation beep when a gesture is detected
28 buzzer_->PlayNote(PWM::NoteName::C, 7, 300);
29
30 // Get current mode and orientation
31 auto mode = mode_manager_->GetMode();
32 bool landscape = mode_manager_->IsLandscape();
33
34 // Handle each gesture type and map to corresponding behavior
35 switch (result) {
36 case ModelOutput::TILT_RIGHT:
37 // In timer mode (not running), increase timer duration
38 if (mode == ModeManager::Mode::TIMER &&
39 !mode_manager_->IsTimerRunning()) {
40 mode_manager_->AdjustTimer(+300);
41 }
42 // In time mode (portrait), switch to landscape
43 else if (mode == ModeManager::Mode::TIME && !landscape) {
44 mode_manager_->SetLandscape(true);
45 }
46 // In timer mode (portrait and running), switch to landscape and
47 // disable sand animation
48 else if (mode == ModeManager::Mode::TIMER && !landscape &&
49 mode_manager_->IsTimerRunning()) {
50 mode_manager_->SetLandscape(true);
51 gui_->SandDisable();
52 }
53 break;
54
55 case ModelOutput::TILT_LEFT:
56 // In timer mode (not running), decrease timer duration
57 if (mode == ModeManager::Mode::TIMER &&
58 !mode_manager_->IsTimerRunning()) {
59 mode_manager_->AdjustTimer(-300);
60 }
61 // In time mode (landscape), switch to portrait
62 else if (mode == ModeManager::Mode::TIME && landscape) {
63 mode_manager_->SetLandscape(false);
64 }
65 // In timer mode (landscape and running), switch to portrait and
66 // enable sand animation
67 else if (mode == ModeManager::Mode::TIMER && landscape &&
68 mode_manager_->IsTimerRunning()) {
69 mode_manager_->SetLandscape(false);
70 gui_->SandEnable();
71 }
72 break;
73
74 case ModelOutput::SHAKE_FORWARD:
75 // Start timer if in timer mode and not already running
76 if (mode == ModeManager::Mode::TIMER &&
77 !mode_manager_->IsTimerRunning()) {
78 startTimerCallback_(mode_manager_->GetRemainingTimerSeconds());
79 }
80 break;
81
82 case ModelOutput::SHAKE_BACKWARD:
83 // Stop timer and disable sand animation if in timer mode
84 if (mode == ModeManager::Mode::TIMER) {
85 stopTimerCallback_();
86 gui_->SandDisable();
87 }
88 break;
89
90 default:
91 break;
92 }
93 });
94 }
95
96 private:
97 ModeManager* mode_manager_ = nullptr; // Mode state handler
98 CompGuiX* gui_ = nullptr; // GUI interface
99 PWM* buzzer_ = nullptr; // Buzzer interface
100 std::function<void(int)> startTimerCallback_; // Callback to start timer
101 std::function<void()> stopTimerCallback_; // Callback to stop timer
102};
LED Matrix GUI Controller with Dual Orientation Support and Sand Animation Physics.
Definition comp_gui.hpp:15
void SandDisable()
Disable sand simulation.
Definition comp_gui.hpp:77
void SandEnable()
Enable sand simulation.
Definition comp_gui.hpp:74
PWM driver with Beep and PlayNote functionality.
Definition bsp_pwm.hpp:21
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