Added basic HID intervace

master
Mark 2022-06-25 19:12:01 -07:00
parent 15311cf9e5
commit 4eff7eb277
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 98 additions and 11 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libs/hidapi"]
path = libs/hidapi
url = ssh://git@git.betalupi.com:33/mirrors-libs/hidapi.git

View File

@ -13,34 +13,52 @@ clean:
-rm -r $(BUILD_DIR) -rm -r $(BUILD_DIR)
@echo "" @echo ""
.PHONY: clean all run
libs: $(BUILD_DIR)/hid.o
.PHONY: clean all run libs
################################################# #################################################
# Flags and autodetection # Flags and autodetection
# -MMD and -MP generate makefiles with extension .d. # -MMD and -MP generate makefiles with extension .d.
CPPFLAGS := -Wall -MMD -MP -I src CPPFLAGS := -Wall -MMD -MP -I src -I libs/hidapi/hidapi
LDFLAGS := -l fftw3 LDFLAGS := -l fftw3 -l udev
# Find all cpp files in source dirs # Find all cpp files in source dirs
SRCS := $(shell find $(SRC_DIRS) -name '*.cpp') SRCS := $(shell find $(SRC_DIRS) -name '*.cpp')
# Turns ./build/a.cpp into ./build/a.cpp.o # Turns ./build/a.cpp into ./build/a.cpp.o
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
LIB_OBJS := $(BUILD_DIR)/hid.o
# Turns ./build/a.cpp.o into ./build/a.cpp.d # Turns ./build/a.cpp.o into ./build/a.cpp.d
DEPS := $(OBJS:.o=.d) DEPS := $(OBJS:.o=.d)
################################################# #################################################
# Build targets # Build targets
HIDAPI_PATH := libs/hidapi
# Build hidapi
$(BUILD_DIR)/hid.o:
@mkdir -p $(BUILD_DIR)
@echo "Compiling hid.o"
@gcc -Wall -g -fpic -c \
-I $(HIDAPI_PATH)/hidapi \
`pkg-config libusb-1.0 --cflags` \
\
$(HIDAPI_PATH)/linux/hid.c \
-o $(BUILD_DIR)/hid.o
# C++ build step # C++ build step
$(BUILD_DIR)/%.cpp.o: %.cpp $(BUILD_DIR)/%.cpp.o: %.cpp $(BUILD_DIR)/hid.o
mkdir -p $(dir $@) mkdir -p $(dir $@)
g++ $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ g++ $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
# Final build step # Final build step
$(TARGET_EXEC) : $(OBJS) $(TARGET_EXEC) : $(OBJS)
g++ $(OBJS) -o $@ $(LDFLAGS) g++ $(OBJS) $(LIB_OBJS) -o $@ $(LDFLAGS)
# Include generated makefiles # Include generated makefiles

1
libs/hidapi Submodule

@ -0,0 +1 @@
Subproject commit eaa5c7c6f13e212b1936c2d4a7dc372b289aa9e0

View File

@ -8,6 +8,9 @@
// Math libs // Math libs
#include <math.h> #include <math.h>
#include <fftw3.h> #include <fftw3.h>
// HIDapi
#include <wchar.h>
#include "hidapi.h"
// Local files // Local files
#include "utility/bitmap.hpp" #include "utility/bitmap.hpp"
@ -15,6 +18,12 @@
#include "signal_processing/fft.hpp" #include "signal_processing/fft.hpp"
#define HID_VEND 0x3297
#define HID_PROD 0x4976
#define HID_USAGE 0x61
#define HID_PAGE 0xff60
// TODO: // TODO:
// 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?
@ -28,6 +37,34 @@
// beat detection // 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( void draw_spectrum_bitmap(
@ -41,12 +78,22 @@ void draw_spectrum_bitmap(
} }
} }
const size_t width = 300; const size_t width = 12;
const size_t height = 100; const size_t height = 150;
int main(int argc, char *argv[]) { 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: // buffer size for waveform:
// (44100 / fps * 10), make 10 bigger for slower scrolling // (44100 / fps * 10), make 10 bigger for slower scrolling
// //
@ -54,7 +101,7 @@ int main(int argc, char *argv[]) {
FFT_Visualizer fft = FFT_Visualizer( FFT_Visualizer fft = FFT_Visualizer(
width, height, width, height,
20, 20000 100, 10000
); );
std::vector<size_t> waveform; std::vector<size_t> waveform;
@ -62,6 +109,7 @@ int main(int argc, char *argv[]) {
Buffer buf = Buffer( Buffer buf = Buffer(
"/tmp/mpd.fifo", "/tmp/mpd.fifo",
//"/home/mark/Workbench/sospi/pulse.fifo",
44100 / 2, // Keep 500ms of data in buffer 44100 / 2, // Keep 500ms of data in buffer
fft.compute_buffer_output_size() fft.compute_buffer_output_size()
); );
@ -69,10 +117,27 @@ int main(int argc, char *argv[]) {
Bitmap b = Bitmap(width, height); Bitmap b = Bitmap(width, height);
while (1) { while (1) {
b.clear(); //b.clear();
buf.update(); buf.update();
fft.update(buf); fft.update(buf);
draw_spectrum_bitmap(fft.get_output(), b); //draw_spectrum_bitmap(fft.get_output(), b);
b.save("/tmp/o.bmp"); //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;
} }