Minor cleanup

master
Mark 2022-07-08 16:56:32 -07:00
parent a9eb47debe
commit 3c154a7679
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 68 additions and 41 deletions

View File

@ -1,5 +1,26 @@
#pragma once #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. // USB packet size, in bytes.
// Usually 32, but depends on keyboard. // Usually 32, but depends on keyboard.
#define RAW_EPSIZE 32 #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

View File

@ -220,3 +220,12 @@ bool Ergodox::read() {
void Ergodox::test_connection() { void Ergodox::test_connection() {
write(CMD_RUTHERE, NULL, 0); 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));
}
}

View File

@ -3,8 +3,11 @@
#include <stdexcept> #include <stdexcept>
#include <cstdint> #include <cstdint>
#include <wchar.h> #include <wchar.h>
#include "hidapi.h" // For sleep
#include <chrono>
#include <thread>
#include "hidapi.h"
#include "config.h" #include "config.h"
#include "commands.h" #include "commands.h"
@ -35,6 +38,7 @@ class Ergodox {
bool try_connect(); bool try_connect();
void disconnect(); void disconnect();
void test_connection(); void test_connection();
void connect_loop();
bool read(); bool read();
bool write(uint8_t cmd, const uint8_t* data, uint8_t data_len); bool write(uint8_t cmd, const uint8_t* data, uint8_t data_len);

View File

@ -5,9 +5,6 @@
// For reading FIFO // For reading FIFO
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
// For sleep
#include <chrono>
#include <thread>
// Local files // Local files
#include "utility/bitmap.hpp" #include "utility/bitmap.hpp"
@ -15,31 +12,36 @@
#include "signal_processing/fft.hpp" #include "signal_processing/fft.hpp"
#include "ergodox.hpp" #include "ergodox.hpp"
#include "commands.h" #include "commands.h"
#include "config.h"
// TODO: // TODO:
//
// FFT:
// stereo support (and maybe different bitrates?) // stereo support (and maybe different bitrates?)
// Optimization: don't copy filename in buffer? // Optimization: don't copy filename in buffer?
// understand consumption rate // understand consumption rate
// understand BIN2HZ // understand BIN2HZ
// understand values and sizes (DFT_TOTAL, DFT_NONZERO, etc) // understand values and sizes (DFT_TOTAL, DFT_NONZERO, etc)
// note that wave and spectrum have different sizes // note that wave and spectrum have different sizes
// clear fft when not in use
// //
// MPD interface // MPD interface
//
// Keyboard interface:
// Frame rate limit
// Clean up reconnect code
// Better log messages
//
// Get parameters from keyboard (width, height, etc)
//
// Later:
// beat detection // beat detection
// waveform animation
// 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;
const size_t width = 10; 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[]) { int main(int argc, char *argv[]) {
@ -51,8 +53,7 @@ int main(int argc, char *argv[]) {
// FFT generator // FFT generator
FFT_Visualizer fft = FFT_Visualizer( FFT_Visualizer fft = FFT_Visualizer(
width, height, width, height, MIN_HZ, MAX_HZ
100, 5000
); );
// Audio buffer // Audio buffer
@ -64,20 +65,16 @@ int main(int argc, char *argv[]) {
// HID interface wrapper // HID interface wrapper
Ergodox Dox = Ergodox::init( Ergodox Dox = Ergodox::init(
0x3297, HID_VENDOR_ID,
0x4976, HID_PRODUCT_ID,
0x61, HID_USAGE,
0xFF60 HID_USAGE_PAGE
); );
// Write buffer // Write buffer
uint8_t hid_buf[Dox.packet_size]; uint8_t hid_buf[Dox.packet_size];
wprintf(L"Trying to connect...\n"); Dox.connect_loop();
while (!Dox.is_connected()) {
Dox.try_connect();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
while (1) { while (1) {
@ -90,11 +87,11 @@ int main(int argc, char *argv[]) {
hid_buf[0] = CMD_ANIM_DATA_fft; hid_buf[0] = CMD_ANIM_DATA_fft;
for (size_t i = 0; i < 10; i++) { for (size_t i = 0; i < 10; i++) {
// Get height from fft, apply bottom_skip // 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 // Enforce max and min
// max implicitly enforces top_skip // max implicitly enforces top_skip
h = h>kb_resolution ? kb_resolution : h; h = h>KB_RESOLUTION ? KB_RESOLUTION : h;
h = h<0 ? 0 : h; h = h<0 ? 0 : h;
hid_buf[i + 1] = h; hid_buf[i + 1] = h;
@ -118,11 +115,7 @@ int main(int argc, char *argv[]) {
} }
} }
} else { } else {
wprintf(L"Trying to connect...\n"); Dox.connect_loop();
while (!Dox.is_connected()) {
Dox.try_connect();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
} }
} }