summaryrefslogtreecommitdiffhomepage
path: root/UI.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2025-01-04 17:51:59 +0100
committerRoland Reichwein <mail@reichwein.it>2025-01-04 17:51:59 +0100
commit2e793141e5434043205763c70d3a597cf2d78eeb (patch)
tree8b6a14273fcc98ff7bf118cf63574257a637547f /UI.cpp
parent624a69994d89c71b1ce59dd3b2117a235400be2f (diff)
Separate clocks
Diffstat (limited to 'UI.cpp')
-rw-r--r--UI.cpp87
1 files changed, 79 insertions, 8 deletions
diff --git a/UI.cpp b/UI.cpp
index 0adb48b..933f2de 100644
--- a/UI.cpp
+++ b/UI.cpp
@@ -11,6 +11,9 @@
#include <fmt/color.h>
#include <fmt/format.h>
+#include <stdio.h>
+#include <sys/poll.h>
+
const int midi_monitor_max_size = 3;
using namespace std::chrono_literals;
@@ -62,6 +65,31 @@ UI::UI(Config& config):
{
}
+bool UI::key_available() {
+ struct pollfd fds{};
+ int ret;
+ fds.fd = 0; // stdin
+ fds.events = POLLIN;
+ ret = poll(&fds, 1, 0);
+ if (ret == 0)
+ return false;
+ else if (ret == 1)
+ return true;
+ else
+ log_cout << "Error" << std::endl;
+ return false;
+}
+
+int read_key() {
+ char buf[1];
+ int got = read(0, buf, 1);
+ if (got != 1) {
+ log_cout << "Bad input size: " << std::to_string(got) << std::endl;
+ }
+
+ return buf[0];
+}
+
void UI::draw()
{
std::vector<int> cpuloads = get_cpu_loads();
@@ -71,28 +99,40 @@ void UI::draw()
int mode = m_config.get_mode();
int channel = m_config.get_midi_channel();
int note = m_config.get_midi_note();
- int bpm = m_config.get_bpm();
+ int internal_bpm = m_config.get_bpm();
+ int bpm;
+ if (mode == 0) {
+ bpm = m_note_bpm;
+ } else if (mode == 1) {
+ bpm = m_clock_bpm;
+ } else if (mode == 2) {
+ bpm = m_config.get_bpm();
+ }
+
int output = m_config.get_output();
int active_sensing_detected = (clock_type::now() - m_active_sensing_timestamp) < 2s ? 1 : 0;
+ fmt::text_style editable_color{fg(fmt::color::crimson) | fmt::emphasis::bold};
+ fmt::text_style value_color{fg(fmt::color::blue) | fmt::emphasis::bold};
+
// clear screen
std::cout << "\x1B[2J\x1B[H";
//std::cout << std::endl;
- std::cout << fmt::format("- {:3} BPM +", bpm) << std::endl;
+ std::cout << "- " << fmt::format(editable_color, "{:3}", bpm) << " BPM +" << std::endl;
std::cout << "Mode: ";
for (int i = 0; i < mode_names.size(); ++i) {
if (i == mode) {
- std::cout << fmt::format(fg(fmt::color::crimson) | fmt::emphasis::bold, " {}", mode_names[i]);
+ std::cout << fmt::format(editable_color, " {}", mode_names[i]);
} else {
std::cout << fmt::format(" {}", mode_names[i]);
}
}
std::cout << std::endl;
- std::cout << fmt::format("MIDI Note: {}/{}", channel, note);
+ std::cout << "MIDI Note: " << fmt::format(editable_color, "{}/{}", channel, note);
std::cout << std::endl;
- std::cout << "Audio Output: " << fmt::format(fg(fmt::color::crimson) | fmt::emphasis::bold, "{}", output_names[output]);
+ std::cout << "Audio Output: " << fmt::format(editable_color, "{}", output_names[output]);
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Status:" << std::endl;
@@ -111,7 +151,7 @@ void UI::draw()
int count{};
for (const auto& i: m_midi_monitor) {
if (count == 0) {
- std::cout << fmt::format(fg(fmt::color::crimson) | fmt::emphasis::bold, " {}/{}", i.first, i.second);
+ std::cout << fmt::format(editable_color, " {}/{}", i.first, i.second);
} else {
std::cout << fmt::format(" {}/{}", i.first, i.second);
}
@@ -123,11 +163,11 @@ void UI::draw()
}
std::cout << std::endl;
std::cout << fmt::format(" MIDI Timestamp: {}", m_midi_timestamp) << std::endl;
- std::cout << " MIDI Active Sensing: " << fmt::format(fg(fmt::color::blue) | fmt::emphasis::bold, "{}", active_sensing_names[active_sensing_detected]) << std::endl;
+ std::cout << " MIDI Active Sensing: " << fmt::format(value_color, "{}", active_sensing_names[active_sensing_detected]) << std::endl;
std::cout << fmt::format(" MIDI Events/s: {}", midi_events_per_second) << std::endl;
std::cout << fmt::format(" MIDI Clock: {:3} BPM", m_clock_bpm) << std::endl;
std::cout << fmt::format(" MIDI Click: {:3} BPM", m_note_bpm) << std::endl;
- std::cout << fmt::format(" Internal: {:3} BPM", bpm) << std::endl;
+ std::cout << fmt::format(" Internal: {:3} BPM", internal_bpm) << std::endl;
std::cout << fmt::format(" Main loops/s: {}", main_loops_per_second) << std::endl;
@@ -135,6 +175,37 @@ void UI::draw()
std::cout << log_cout.get_log() << std::endl;
}
+void UI::handle_input()
+{
+ if (key_available()) {
+ char c;
+ std::cin >> c;
+
+ debug_cout << "Key: " << std::to_string(c) << std::endl;
+
+ if (c == '+') {
+ if (m_config.get_bpm() < 240) {
+ m_config.set_bpm(m_config.get_bpm() + 1);
+ }
+ } else if (c == '-') {
+ if (m_config.get_bpm() > 10) {
+ m_config.set_bpm(m_config.get_bpm() - 1);
+ }
+ } else if (c == 'm') {
+ m_config.set_mode((m_config.get_mode() + 1) % 3);
+ } else if (c == 'n') {
+ if (!m_midi_monitor.empty()) {
+ m_config.set_midi_channel(m_midi_monitor.front().first);
+ m_config.set_midi_note(m_midi_monitor.front().second);
+ }
+ } else if (c == 'o') {
+ m_config.set_output(1 - m_config.get_output());
+ } else {
+ log_cout << fmt::format("Unknown key: {}", c) << std::endl;
+ }
+ }
+}
+
void UI::count_main_loops()
{
m_main_loops.count();