Compare commits

...

2 Commits

Author SHA1 Message Date
Mark 3c154a7679
Minor cleanup 2022-07-08 16:56:32 -07:00
Mark a9eb47debe
Removed draw_spectrum_bitmap 2022-07-08 16:42:05 -07:00
4 changed files with 68 additions and 55 deletions

View File

@ -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
// 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() {
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 <cstdint>
#include <wchar.h>
#include "hidapi.h"
// For sleep
#include <chrono>
#include <thread>
#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);

View File

@ -5,9 +5,6 @@
// For reading FIFO
#include <fcntl.h>
#include <unistd.h>
// For sleep
#include <chrono>
#include <thread>
// Local files
#include "utility/bitmap.hpp"
@ -15,45 +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
// hid interface to keyboard
// beat detection
void draw_spectrum_bitmap(
const std::vector<size_t>& waveform,
Bitmap& bitmap
) {
for (size_t x = 0; x < waveform.size(); x++) {
for (size_t y = 0; y < waveform[x]; y++) {
bitmap.setpixel(bitmap.get_height() - y - 1, x, 0xFF, 0x00, 0x00);
}
}
}
// 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[]) {
@ -65,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
@ -78,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) {
@ -104,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;
@ -132,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();
}
}