From 3c154a767969345d5e4c1767aa1e9b5ece94dc99 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 8 Jul 2022 16:56:32 -0700 Subject: [PATCH] Minor cleanup --- src/config.h | 23 +++++++++++++++- src/ergodox.cpp | 9 +++++++ src/ergodox.hpp | 6 ++++- src/main.cpp | 71 ++++++++++++++++++++++--------------------------- 4 files changed, 68 insertions(+), 41 deletions(-) diff --git a/src/config.h b/src/config.h index 63cd111..6547144 100644 --- a/src/config.h +++ b/src/config.h @@ -1,5 +1,26 @@ #pragma once +// USB device params +#define HID_VENDOR_ID 0x3297 +#define HID_PRODUCT_ID 0x4976 +#define HID_USAGE 0x61 +#define HID_USAGE_PAGE 0xFF60 + // USB packet size, in bytes. // Usually 32, but depends on keyboard. -#define RAW_EPSIZE 32 \ No newline at end of file +#define RAW_EPSIZE 32 + +// How many milliseconds to wait between reconnect attempts +#define RECONNECT_SLEEP_MS 500 + +// How many keys in a column * resolution per key. +// this MUST fit inside a uint8_t (i.e, <= 255). +#define KB_RESOLUTION (5 * 50) + +// How many resolution steps to skip at the top and bottom. +#define BOTTOM_SKIP 25 +#define TOP_SKIP 50 + +// Spectrum visualizer range +#define MIN_HZ 100 +#define MAX_HZ 5000 \ No newline at end of file diff --git a/src/ergodox.cpp b/src/ergodox.cpp index 02ad164..480568c 100644 --- a/src/ergodox.cpp +++ b/src/ergodox.cpp @@ -219,4 +219,13 @@ bool Ergodox::read() { // Simple connectivity check void Ergodox::test_connection() { write(CMD_RUTHERE, NULL, 0); +} + +// Block until a connection is established. +void Ergodox::connect_loop() { + wprintf(L"Trying to connect...\n"); + while (!connected) { + try_connect(); + std::this_thread::sleep_for(std::chrono::milliseconds(RECONNECT_SLEEP_MS)); + } } \ No newline at end of file diff --git a/src/ergodox.hpp b/src/ergodox.hpp index b388d72..f6ea0d2 100644 --- a/src/ergodox.hpp +++ b/src/ergodox.hpp @@ -3,8 +3,11 @@ #include #include #include -#include "hidapi.h" +// For sleep +#include +#include +#include "hidapi.h" #include "config.h" #include "commands.h" @@ -35,6 +38,7 @@ class Ergodox { bool try_connect(); void disconnect(); void test_connection(); + void connect_loop(); bool read(); bool write(uint8_t cmd, const uint8_t* data, uint8_t data_len); diff --git a/src/main.cpp b/src/main.cpp index 15d13ad..0afe6f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,9 +5,6 @@ // For reading FIFO #include #include -// For sleep -#include -#include // Local files #include "utility/bitmap.hpp" @@ -15,31 +12,36 @@ #include "signal_processing/fft.hpp" #include "ergodox.hpp" #include "commands.h" +#include "config.h" // TODO: -// stereo support (and maybe different bitrates?) -// Optimization: don't copy filename in buffer? -// understand consumption rate -// understand BIN2HZ -// understand values and sizes (DFT_TOTAL, DFT_NONZERO, etc) -// note that wave and spectrum have different sizes // -// MPD interface -// beat detection - - -// How many keys in a column * resolution per key. -// this MUST fit inside a uint8_t. -const uint8_t kb_resolution = 5 * 50; - -// How many resolution steps to skip at the top and bottom. -const size_t bottom_skip = 25; -const size_t top_skip = 50; +// FFT: +// stereo support (and maybe different bitrates?) +// Optimization: don't copy filename in buffer? +// understand consumption rate +// understand BIN2HZ +// understand values and sizes (DFT_TOTAL, DFT_NONZERO, etc) +// note that wave and spectrum have different sizes +// clear fft when not in use +// +// MPD interface +// +// Keyboard interface: +// Frame rate limit +// Clean up reconnect code +// Better log messages +// +// Get parameters from keyboard (width, height, etc) +// +// Later: +// beat detection +// waveform animation const size_t width = 10; -const size_t height = bottom_skip + kb_resolution + top_skip; +const size_t height = BOTTOM_SKIP + KB_RESOLUTION + TOP_SKIP; int main(int argc, char *argv[]) { @@ -51,8 +53,7 @@ int main(int argc, char *argv[]) { // FFT generator FFT_Visualizer fft = FFT_Visualizer( - width, height, - 100, 5000 + width, height, MIN_HZ, MAX_HZ ); // Audio buffer @@ -64,20 +65,16 @@ int main(int argc, char *argv[]) { // HID interface wrapper Ergodox Dox = Ergodox::init( - 0x3297, - 0x4976, - 0x61, - 0xFF60 + HID_VENDOR_ID, + HID_PRODUCT_ID, + HID_USAGE, + HID_USAGE_PAGE ); // Write buffer uint8_t hid_buf[Dox.packet_size]; - wprintf(L"Trying to connect...\n"); - while (!Dox.is_connected()) { - Dox.try_connect(); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } + Dox.connect_loop(); while (1) { @@ -90,11 +87,11 @@ int main(int argc, char *argv[]) { 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; + 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>KB_RESOLUTION ? KB_RESOLUTION : h; h = h<0 ? 0 : h; hid_buf[i + 1] = h; @@ -118,11 +115,7 @@ int main(int argc, char *argv[]) { } } } else { - wprintf(L"Trying to connect...\n"); - while (!Dox.is_connected()) { - Dox.try_connect(); - std::this_thread::sleep_for(std::chrono::milliseconds(500)); - } + Dox.connect_loop(); } }