Added basic speed limiter

master
Mark 2022-07-08 19:07:46 -07:00
parent afecfe5890
commit 50dfd244e7
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
1 changed files with 30 additions and 14 deletions

View File

@ -5,6 +5,9 @@
// For reading FIFO
#include <fcntl.h>
#include <unistd.h>
// For sleep
#include <chrono>
#include <thread>
// 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.