From 50dfd244e727eb81ea8e63a44265b22960127631 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 8 Jul 2022 19:07:46 -0700 Subject: [PATCH] Added basic speed limiter --- src/main.cpp | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index aaf5808..30aa4fe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,9 @@ // For reading FIFO #include #include +// For sleep +#include +#include // Local files #include "utility/bitmap.hpp" @@ -78,28 +81,41 @@ int main(int argc, char *argv[]) { Dox.connect_loop(); + // Frame rate limiter + std::chrono::time_point< + std::chrono::steady_clock, + std::chrono::nanoseconds + > t = std::chrono::steady_clock::now(); + //t += std::chrono::milliseconds(30); + //this_thread::sleep_until(t); + + while (1) { if (Dox.is_connected()) { - if (Dox.get_animation_mode() == 0x02) { - buf.update(); - fft.update(buf); + if (std::chrono::steady_clock::now() > t + std::chrono::milliseconds(30)) { + if (Dox.get_animation_mode() == 0x02) { + buf.update(); + fft.update(buf); - hid_buf[0] = CMD_ANIM_DATA_fft; - for (size_t i = 0; i < 10; i++) { - // Get height from fft, apply bottom_skip - ssize_t h = fft.get_output()[i] - BOTTOM_SKIP; + hid_buf[0] = CMD_ANIM_DATA_fft; + for (size_t i = 0; i < 10; i++) { + // Get height from fft, apply bottom_skip + ssize_t h = fft.get_output()[i] - BOTTOM_SKIP; - // Enforce max and min - // max implicitly enforces top_skip - h = h>KB_RESOLUTION ? KB_RESOLUTION : h; - h = h<0 ? 0 : h; + // Enforce max and min + // max implicitly enforces top_skip + h = h>KB_RESOLUTION ? KB_RESOLUTION : h; + h = h<0 ? 0 : h; - hid_buf[i + 1] = h; + hid_buf[i + 1] = h; + } } - } - Dox.write(CMD_ANIM_DATA, hid_buf, Dox.packet_size); + Dox.write(CMD_ANIM_DATA, hid_buf, Dox.packet_size); + + t = std::chrono::steady_clock::now(); + } // Dox.write might detect that we've been disconnected, // and Dox.read will fail if we are.