Compare commits
No commits in common. "3c154a767969345d5e4c1767aa1e9b5ece94dc99" and "9d8edf41eb1ab77e4e4d263f3f6e45cbe979c45a" have entirely different histories.
3c154a7679
...
9d8edf41eb
21
src/config.h
21
src/config.h
|
@ -1,26 +1,5 @@
|
||||||
#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
|
|
|
@ -220,12 +220,3 @@ 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));
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,11 +3,8 @@
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
// For sleep
|
|
||||||
#include <chrono>
|
|
||||||
#include <thread>
|
|
||||||
|
|
||||||
#include "hidapi.h"
|
#include "hidapi.h"
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "commands.h"
|
#include "commands.h"
|
||||||
|
|
||||||
|
@ -38,7 +35,6 @@ 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);
|
||||||
|
|
85
src/main.cpp
85
src/main.cpp
|
@ -5,6 +5,9 @@
|
||||||
// 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"
|
||||||
|
@ -12,36 +15,45 @@
|
||||||
#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:
|
||||||
|
// 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
|
||||||
//
|
//
|
||||||
// FFT:
|
// MPD interface
|
||||||
// stereo support (and maybe different bitrates?)
|
// hid interface to keyboard
|
||||||
// Optimization: don't copy filename in buffer?
|
// beat detection
|
||||||
// understand consumption rate
|
|
||||||
// understand BIN2HZ
|
|
||||||
// understand values and sizes (DFT_TOTAL, DFT_NONZERO, etc)
|
void draw_spectrum_bitmap(
|
||||||
// note that wave and spectrum have different sizes
|
const std::vector<size_t>& waveform,
|
||||||
// clear fft when not in use
|
Bitmap& bitmap
|
||||||
//
|
) {
|
||||||
// MPD interface
|
for (size_t x = 0; x < waveform.size(); x++) {
|
||||||
//
|
for (size_t y = 0; y < waveform[x]; y++) {
|
||||||
// Keyboard interface:
|
bitmap.setpixel(bitmap.get_height() - y - 1, x, 0xFF, 0x00, 0x00);
|
||||||
// Frame rate limit
|
}
|
||||||
// Clean up reconnect code
|
}
|
||||||
// Better log messages
|
}
|
||||||
//
|
|
||||||
// Get parameters from keyboard (width, height, etc)
|
|
||||||
//
|
|
||||||
// Later:
|
// How many keys in a column * resolution per key.
|
||||||
// beat detection
|
// this MUST fit inside a uint8_t.
|
||||||
// waveform animation
|
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[]) {
|
||||||
|
@ -53,7 +65,8 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
// FFT generator
|
// FFT generator
|
||||||
FFT_Visualizer fft = FFT_Visualizer(
|
FFT_Visualizer fft = FFT_Visualizer(
|
||||||
width, height, MIN_HZ, MAX_HZ
|
width, height,
|
||||||
|
100, 5000
|
||||||
);
|
);
|
||||||
|
|
||||||
// Audio buffer
|
// Audio buffer
|
||||||
|
@ -65,16 +78,20 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
// HID interface wrapper
|
// HID interface wrapper
|
||||||
Ergodox Dox = Ergodox::init(
|
Ergodox Dox = Ergodox::init(
|
||||||
HID_VENDOR_ID,
|
0x3297,
|
||||||
HID_PRODUCT_ID,
|
0x4976,
|
||||||
HID_USAGE,
|
0x61,
|
||||||
HID_USAGE_PAGE
|
0xFF60
|
||||||
);
|
);
|
||||||
|
|
||||||
// Write buffer
|
// Write buffer
|
||||||
uint8_t hid_buf[Dox.packet_size];
|
uint8_t hid_buf[Dox.packet_size];
|
||||||
|
|
||||||
Dox.connect_loop();
|
wprintf(L"Trying to connect...\n");
|
||||||
|
while (!Dox.is_connected()) {
|
||||||
|
Dox.try_connect();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
@ -87,11 +104,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;
|
||||||
|
@ -115,7 +132,11 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Dox.connect_loop();
|
wprintf(L"Trying to connect...\n");
|
||||||
|
while (!Dox.is_connected()) {
|
||||||
|
Dox.try_connect();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue