summaryrefslogtreecommitdiffhomepage
path: root/Timer.h
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2025-01-03 12:27:14 +0100
committerRoland Reichwein <mail@reichwein.it>2025-01-03 12:27:14 +0100
commit05895c86bddf50aacb3bb5e6a6bcc073965341ef (patch)
treed7627562c8ca35c8689d0a6612a2cdab2cdc4ae4 /Timer.h
parent4af400141af0c97c4e4bcd47acf78107a17eafbe (diff)
Add first UI
Diffstat (limited to 'Timer.h')
-rw-r--r--Timer.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/Timer.h b/Timer.h
new file mode 100644
index 0000000..81192d6
--- /dev/null
+++ b/Timer.h
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <boost/signals2.hpp>
+
+#include <chrono>
+
+using namespace std::chrono_literals;
+
+using clock_type = std::chrono::high_resolution_clock;
+
+class Timer
+{
+public:
+ Timer(std::chrono::milliseconds interval, bool cyclic) : m_start_time(clock_type::now()), m_interval(interval), m_running(false), m_cyclic(cyclic)
+ {}
+
+ // connect to this signal
+ boost::signals2::signal<void()> elapsed;
+
+ void start()
+ {
+ m_running = true;
+ m_start_time = clock_type::now();
+ }
+
+ void stop()
+ {
+ m_running = false;
+ }
+
+ bool is_elapsed()
+ {
+ return m_start_time + m_interval < clock_type::now();
+ }
+
+ void update()
+ {
+ if (m_running && is_elapsed()) {
+ elapsed();
+ if (m_cyclic) {
+ start();
+ } else {
+ stop();
+ }
+ }
+ }
+
+private:
+ std::chrono::time_point<clock_type> m_start_time;
+ std::chrono::milliseconds m_interval;
+ bool m_running;
+ bool m_cyclic;
+};
+