diff options
author | Roland Reichwein <mail@reichwein.it> | 2025-01-03 12:27:14 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2025-01-03 12:27:14 +0100 |
commit | 05895c86bddf50aacb3bb5e6a6bcc073965341ef (patch) | |
tree | d7627562c8ca35c8689d0a6612a2cdab2cdc4ae4 | |
parent | 4af400141af0c97c4e4bcd47acf78107a17eafbe (diff) |
Add first UI
-rw-r--r-- | Makefile | 9 | ||||
-rw-r--r-- | Timer.cpp | 1 | ||||
-rw-r--r-- | Timer.h | 54 | ||||
-rw-r--r-- | UI.cpp | 18 | ||||
-rw-r--r-- | UI.h | 9 | ||||
-rw-r--r-- | main.cpp | 19 |
6 files changed, 106 insertions, 4 deletions
@@ -2,7 +2,14 @@ TARGET=click default: $(TARGET) -SRCS=main.cpp MIDI.cpp PCM.cpp ClickStream.cpp config.cpp +SRCS= \ + main.cpp \ + MIDI.cpp \ + PCM.cpp \ + ClickStream.cpp \ + config.cpp \ + UI.cpp \ + Timer.cpp OBJS=$(SRCS:.cpp=.o) diff --git a/Timer.cpp b/Timer.cpp new file mode 100644 index 0000000..afe5d24 --- /dev/null +++ b/Timer.cpp @@ -0,0 +1 @@ +#include "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; +}; + @@ -0,0 +1,18 @@ +#include "UI.h" + +#include <iostream> + +void UI::draw() +{ + std::cout << std::endl; + std::cout << "- -- BPM +" << std::endl; + std::cout << "Mode: Click __/__ (Clock Internal)" << std::endl; + std::cout << "Status:" << std::endl; + std::cout << " Alive/not alive" << std::endl; + std::cout << " CPU: --% --% ..." << std::endl; + std::cout << " Notes/Channels: -- -- -- ... (Choose)" << std::endl; + std::cout << " Timestamp: ------" << std::endl; + std::cout << " Active sensing: ---" << std::endl; + std::cout << " Clock: ____" << std::endl; + std::cout << " Click: ____" << std::endl; +} @@ -0,0 +1,9 @@ +#pragma once + +class UI +{ +public: + UI(){} + + void draw(); +}; @@ -1,10 +1,13 @@ #include "ClickStream.h" #include "MIDI.h" #include "PCM.h" +#include "Timer.h" +#include "UI.h" #include "config.h" -#include <cstdint> +#include <chrono> #include <cmath> +#include <cstdint> #include <iostream> #include <limits> #include <exception> @@ -17,6 +20,7 @@ #include <boost/signals2.hpp> +using namespace std::chrono_literals; using namespace std::string_literals; double diff_timespec(const struct timespec *time1, const struct timespec *time0) { @@ -30,10 +34,16 @@ int main(void) MIDI midi; ClickStream stream; PCM pcm{stream}; + UI ui; pcm.write(); + Timer timer_300ms(300ms, true); + timer_300ms.start(); + + // Signal-Slot Connections: midi.signal_click.connect([&](){stream.click();}); + timer_300ms.elapsed.connect([&](){ui.draw();}); while (true) { //std::cout << "Main loop entered." << std::endl; @@ -46,8 +56,8 @@ int main(void) FD_SET(pcm.fd(), &write_set); struct timeval timeout; - timeout.tv_sec = 1; - timeout.tv_usec = 0; + timeout.tv_sec = 0; + timeout.tv_usec = 300000; int result = select(FD_SETSIZE, &read_set, &write_set, NULL, &timeout); if (result < 0) { @@ -68,6 +78,9 @@ int main(void) //std::cout << "DEBUG: WRITE" << std::endl; pcm.write(); } + + // handle timers, TODO: make updates more efficient at scale + timer_300ms.update(); } } catch (const std::exception& ex) { std::cerr << "Error: " << ex.what() << std::endl; |