diff --git a/src/config.h b/src/config.h index d60ad56..991bb50 100644 --- a/src/config.h +++ b/src/config.h @@ -1,5 +1,11 @@ #pragma once + +// Sleep this many millis after each loop. +// Prevents absurd cpu usage. +#define LOOP_SLEEP_MS 20 + + // USB device params #define HID_VENDOR_ID 0x3297 #define HID_PRODUCT_ID 0x4976 diff --git a/src/ergodox.cpp b/src/ergodox.cpp index 157d22f..95010fb 100644 --- a/src/ergodox.cpp +++ b/src/ergodox.cpp @@ -206,10 +206,9 @@ bool Ergodox::read() { // If keyboard sends a state packet, parse it. case CMD_SEND_STATE: if (animation_mode != read_buf[1]) { - spdlog::info("Mode set to {0:x}", read_buf[1]); + spdlog::info("Mode set to 0x{0:02x}", read_buf[1]); animation_mode = read_buf[1]; } - // Main code should not parse state packets. return false; } diff --git a/src/main.cpp b/src/main.cpp index 9e75cd7..f2aeb5f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -119,7 +119,6 @@ int main(int argc, char *argv[]) { 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; @@ -129,11 +128,11 @@ int main(int argc, char *argv[]) { h = h>KB_RESOLUTION ? KB_RESOLUTION : h; h = h<0 ? 0 : h; - hid_buf[i + 1] = h; + hid_buf[i] = 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(); } @@ -152,9 +151,16 @@ int main(int argc, char *argv[]) { break; } } + } else { Dox.connect_loop(); } + + // Sleep for a bit so we don't consume + // 100% of a cpu. + std::this_thread::sleep_for( + std::chrono::milliseconds(LOOP_SLEEP_MS) + ); } mpd_connection_free(conn);