Added basic HID intervace
This commit is contained in:
77
src/test.cpp
77
src/test.cpp
@ -8,6 +8,9 @@
|
||||
// Math libs
|
||||
#include <math.h>
|
||||
#include <fftw3.h>
|
||||
// HIDapi
|
||||
#include <wchar.h>
|
||||
#include "hidapi.h"
|
||||
|
||||
// Local files
|
||||
#include "utility/bitmap.hpp"
|
||||
@ -15,6 +18,12 @@
|
||||
#include "signal_processing/fft.hpp"
|
||||
|
||||
|
||||
#define HID_VEND 0x3297
|
||||
#define HID_PROD 0x4976
|
||||
#define HID_USAGE 0x61
|
||||
#define HID_PAGE 0xff60
|
||||
|
||||
|
||||
// TODO:
|
||||
// stereo support (and maybe different bitrates?)
|
||||
// Optimization: don't copy filename in buffer?
|
||||
@ -28,6 +37,34 @@
|
||||
// beat detection
|
||||
|
||||
|
||||
hid_device* get_keyboard() {
|
||||
struct hid_device_info *devs, *cur_dev;
|
||||
const char *path_to_open = NULL;
|
||||
hid_device *handle = NULL;
|
||||
|
||||
devs = hid_enumerate(HID_VEND, HID_PROD);
|
||||
cur_dev = devs;
|
||||
while (cur_dev) {
|
||||
if (cur_dev->vendor_id == HID_VEND &&
|
||||
cur_dev->product_id == HID_PROD &&
|
||||
cur_dev->usage == HID_USAGE&&
|
||||
cur_dev->usage_page == HID_PAGE
|
||||
) {
|
||||
path_to_open = cur_dev->path;
|
||||
break;
|
||||
}
|
||||
cur_dev = cur_dev->next;
|
||||
}
|
||||
|
||||
if (path_to_open) {
|
||||
handle = hid_open_path(path_to_open);
|
||||
} else {
|
||||
wprintf(L"Could not find device, exiting.\n");
|
||||
exit(1);
|
||||
}
|
||||
hid_free_enumeration(devs);
|
||||
return handle;
|
||||
}
|
||||
|
||||
|
||||
void draw_spectrum_bitmap(
|
||||
@ -41,12 +78,22 @@ void draw_spectrum_bitmap(
|
||||
}
|
||||
}
|
||||
|
||||
const size_t width = 300;
|
||||
const size_t height = 100;
|
||||
const size_t width = 12;
|
||||
const size_t height = 150;
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
|
||||
// HID connect
|
||||
int res;
|
||||
uint8_t hid_buf[65];
|
||||
hid_device *handle;
|
||||
// Initialize the hidapi library
|
||||
hid_init();
|
||||
// Get Ergodox
|
||||
handle = get_keyboard();
|
||||
|
||||
// buffer size for waveform:
|
||||
// (44100 / fps * 10), make 10 bigger for slower scrolling
|
||||
//
|
||||
@ -54,7 +101,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
FFT_Visualizer fft = FFT_Visualizer(
|
||||
width, height,
|
||||
20, 20000
|
||||
100, 10000
|
||||
);
|
||||
|
||||
std::vector<size_t> waveform;
|
||||
@ -62,6 +109,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
Buffer buf = Buffer(
|
||||
"/tmp/mpd.fifo",
|
||||
//"/home/mark/Workbench/sospi/pulse.fifo",
|
||||
44100 / 2, // Keep 500ms of data in buffer
|
||||
fft.compute_buffer_output_size()
|
||||
);
|
||||
@ -69,10 +117,27 @@ int main(int argc, char *argv[]) {
|
||||
Bitmap b = Bitmap(width, height);
|
||||
|
||||
while (1) {
|
||||
b.clear();
|
||||
//b.clear();
|
||||
buf.update();
|
||||
fft.update(buf);
|
||||
draw_spectrum_bitmap(fft.get_output(), b);
|
||||
b.save("/tmp/o.bmp");
|
||||
//draw_spectrum_bitmap(fft.get_output(), b);
|
||||
//b.save("/tmp/o.bmp");
|
||||
|
||||
hid_buf[0] = 0x01;
|
||||
hid_buf[1] = 0x02;
|
||||
for (size_t i = 0; i < 10; i++) {
|
||||
ssize_t h = fft.get_output()[i + 1] - 10;
|
||||
h = h>100 ? 100 : h;
|
||||
h = h<0 ? 0 : h;
|
||||
hid_buf[i + 2] = h;
|
||||
}
|
||||
|
||||
res = hid_write(handle, hid_buf, 12);
|
||||
}
|
||||
|
||||
// Close the device
|
||||
hid_close(handle);
|
||||
// Finalize the hidapi library
|
||||
hid_exit();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user