FluxSand 1.0
FluxSand - Interactive Digital Hourglass
Loading...
Searching...
No Matches
mode_manager.hpp
1#pragma once
2
3#include <chrono>
4#include <iostream>
5
6// Handles application modes and manages stopwatch/timer logic.
8 public:
9 // Enumeration of available UI modes.
10 enum class Mode : uint8_t {
11 TIME, // Clock display
12 HUMIDITY, // Humidity display
13 TEMPERATURE, // Temperature display
14 STOPWATCH, // Stopwatch mode
15 TIMER, // Countdown timer mode
16 MODE_NUM // Total number of modes (used for cycling)
17 };
18
19 // Constructor: start in TIME mode.
20 ModeManager() : mode_(Mode::TIME) {}
21
22 // Cycle to the next mode in order (looping back after the last one).
23 void NextMode() {
24 mode_ = static_cast<Mode>((static_cast<int>(mode_) + 1) %
25 static_cast<int>(Mode::MODE_NUM));
26 }
27
28 // Get the current mode.
29 Mode GetMode() const { return mode_; }
30
31 // Set the current mode.
32 void SetMode(Mode mode) { mode_ = mode; }
33
34 // Set or query display orientation (portrait/landscape).
35 void SetLandscape(bool val) { landscape_ = val; }
36 bool IsLandscape() const { return landscape_; }
37
38 // --- Stopwatch Functionality ---
39
40 // Start the stopwatch if not already running.
41 void StartStopwatch() {
42 if (!stopwatch_running_) {
43 stopwatch_start_time_ = std::chrono::steady_clock::now();
44 stopwatch_running_ = true;
45 mode_ = Mode::STOPWATCH;
46 std::cout << "Stopwatch started\n";
47 }
48 }
49
50 // Stop the stopwatch and reset elapsed time.
51 void StopStopwatch() {
52 if (stopwatch_running_) {
53 stopwatch_running_ = false;
54 stopwatch_elapsed_sec_ = 0;
55 std::cout << "Stopwatch stopped\n";
56 }
57 }
58
59 // Get the number of seconds elapsed on the stopwatch.
60 int64_t GetStopwatchSeconds() const {
61 int64_t display_sec = stopwatch_elapsed_sec_;
62 if (stopwatch_running_) {
63 auto now = std::chrono::steady_clock::now();
64 display_sec += std::chrono::duration_cast<std::chrono::seconds>(
65 now - stopwatch_start_time_)
66 .count();
67 }
68 return display_sec;
69 }
70
71 // Check if the stopwatch is currently running.
72 bool IsStopwatchRunning() const { return stopwatch_running_; }
73
74 // --- Timer Functionality ---
75
76 // Start the countdown timer for a given duration (in seconds).
77 void StartTimer(int duration_sec) {
78 timer_duration_sec_ = duration_sec;
79 max_duration_sec_ = duration_sec;
80 timer_start_time_ = std::chrono::steady_clock::now();
81 timer_active_ = true;
82 mode_ = Mode::TIMER;
83 std::cout << "Timer started for " << duration_sec << " seconds\n";
84 }
85
86 // Stop the timer immediately and reset its duration.
87 void StopTimer() {
88 timer_active_ = false;
89 timer_duration_sec_ = 0;
90 std::cout << "Timer stopped\n";
91 }
92
93 // Get remaining time (in seconds). Auto-stops when expired.
94 int64_t GetRemainingTimerSeconds() {
95 int64_t remaining = timer_duration_sec_;
96 if (timer_active_) {
97 auto now = std::chrono::steady_clock::now();
98 int64_t elapsed = std::chrono::duration_cast<std::chrono::seconds>(
99 now - timer_start_time_)
100 .count();
101 if (elapsed >= timer_duration_sec_) {
102 remaining = 0;
103 std::cout << "Timer finished\n";
104 } else {
105 remaining -= elapsed;
106 }
107 }
108 return remaining;
109 }
110
111 // Check if the timer is currently active.
112 bool IsTimerRunning() const { return timer_active_; }
113
114 // Get the maximum duration the timer was set to (used for animation ratio).
115 int GetMaxTimerDuration() const { return max_duration_sec_; }
116
117 // Adjust timer duration (before starting), with clamping.
118 void AdjustTimer(int delta_sec) {
119 if (!timer_active_) {
120 timer_duration_sec_ = std::clamp(timer_duration_sec_ + delta_sec, 0,
121 60 * 99 - 1); // max 99 minutes
122 }
123 }
124
125 private:
126 Mode mode_; // Current display mode
127 bool landscape_ = false; // Display orientation
128
129 // Stopwatch state
130 bool stopwatch_running_ = false;
131 std::chrono::steady_clock::time_point stopwatch_start_time_;
132 int64_t stopwatch_elapsed_sec_ = 0;
133
134 // Timer state
135 bool timer_active_ = false;
136 std::chrono::steady_clock::time_point timer_start_time_;
137 int timer_duration_sec_ = 0;
138 int max_duration_sec_ = 0;
139};