FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
input_handler.hpp
1#pragma once
2
3#include <functional>
4#include <iostream>
5#include <semaphore>
6
7#include "bsp_pwm.hpp"
8#include "comp_gui.hpp"
9#include "mode_manager.hpp"
10
11// Handles input from physical buttons, including mode switching,
12// stopwatch toggle, and timer stop operations.
14 public:
15 // Initialize input handler with hardware references and logic callbacks.
16 void Init(Gpio* btn1, Gpio* btn2, PWM* buzzer, CompGuiX* gui,
17 ModeManager* mode_manager, std::function<void()> onStopwatchToggle,
18 std::function<void()> onTimerStop) {
19 mode_manager_ = mode_manager;
20 gui_ = gui;
21 buzzer_ = buzzer;
22 onStopwatchToggle_ = std::move(onStopwatchToggle);
23 onTimerStop_ = std::move(onTimerStop);
24
25 // Configure Button 1 interrupt callback:
26 // - Switches to next mode
27 // - Disables sand animation (if any)
28 // - Plays a short beep
30 gpio_int_sem_1_.release(); // Release semaphore to indicate interrupt
31 mode_manager_->NextMode(); // Cycle to the next mode
32 gui_->SandDisable(); // Turn off sand effect
33 std::cout << "Button 1\n";
34 buzzer_->PlayNote(PWM::NoteName::C, 7, 50); // Feedback sound
35 });
36
37 // Configure Button 2 interrupt callback:
38 // - Plays a short beep
39 // - Depending on the current mode:
40 // - In stopwatch mode: toggle start/stop
41 // - In timer mode: stop timer and disable sand animation
43 gpio_int_sem_2_.release(); // Release semaphore to indicate interrupt
44 std::cout << "Button 2\n";
45 buzzer_->PlayNote(PWM::NoteName::C, 7, 50); // Feedback sound
46
47 auto mode = mode_manager_->GetMode();
48 if (mode == ModeManager::Mode::STOPWATCH) {
49 onStopwatchToggle_(); // Toggle stopwatch (start/stop)
50 } else if (mode == ModeManager::Mode::TIMER &&
51 mode_manager_->IsTimerRunning()) {
52 onTimerStop_(); // Stop timer
53 gui_->SandDisable(); // Stop sand animation
54 }
55 });
56 }
57
58 private:
59 ModeManager* mode_manager_ = nullptr; // Pointer to mode manager
60 CompGuiX* gui_ = nullptr; // Pointer to GUI handler
61 PWM* buzzer_ = nullptr; // Pointer to PWM buzzer
62
63 std::function<void()> onStopwatchToggle_; // Callback for stopwatch control
64 std::function<void()> onTimerStop_; // Callback for stopping timer
65
66 // Semaphores for tracking button interrupts (optional use in threaded
67 // context)
68 std::binary_semaphore gpio_int_sem_1_{0};
69 std::binary_semaphore gpio_int_sem_2_{0};
70};
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 EnableInterruptRisingEdgeWithCallback(Callback cb)
Definition bsp_gpio.hpp:109
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