From 4eff7eb2772fe0158b2488593c7e71bb234e01c6 Mon Sep 17 00:00:00 2001 From: Mark Date: Sat, 25 Jun 2022 19:12:01 -0700 Subject: [PATCH] Added basic HID intervace --- .gitmodules | 3 ++ Makefile | 28 +++++++++++++++---- libs/hidapi | 1 + src/test.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 .gitmodules create mode 160000 libs/hidapi diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b67db5d --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "libs/hidapi"] + path = libs/hidapi + url = ssh://git@git.betalupi.com:33/mirrors-libs/hidapi.git diff --git a/Makefile b/Makefile index 6d924df..41b2856 100644 --- a/Makefile +++ b/Makefile @@ -13,34 +13,52 @@ clean: -rm -r $(BUILD_DIR) @echo "" -.PHONY: clean all run + +libs: $(BUILD_DIR)/hid.o + +.PHONY: clean all run libs ################################################# # Flags and autodetection # -MMD and -MP generate makefiles with extension .d. -CPPFLAGS := -Wall -MMD -MP -I src -LDFLAGS := -l fftw3 +CPPFLAGS := -Wall -MMD -MP -I src -I libs/hidapi/hidapi +LDFLAGS := -l fftw3 -l udev # Find all cpp files in source dirs SRCS := $(shell find $(SRC_DIRS) -name '*.cpp') # Turns ./build/a.cpp into ./build/a.cpp.o OBJS := $(SRCS:%=$(BUILD_DIR)/%.o) +LIB_OBJS := $(BUILD_DIR)/hid.o # Turns ./build/a.cpp.o into ./build/a.cpp.d DEPS := $(OBJS:.o=.d) ################################################# # 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 -$(BUILD_DIR)/%.cpp.o: %.cpp +$(BUILD_DIR)/%.cpp.o: %.cpp $(BUILD_DIR)/hid.o mkdir -p $(dir $@) g++ $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@ # Final build step $(TARGET_EXEC) : $(OBJS) - g++ $(OBJS) -o $@ $(LDFLAGS) + g++ $(OBJS) $(LIB_OBJS) -o $@ $(LDFLAGS) + # Include generated makefiles diff --git a/libs/hidapi b/libs/hidapi new file mode 160000 index 0000000..eaa5c7c --- /dev/null +++ b/libs/hidapi @@ -0,0 +1 @@ +Subproject commit eaa5c7c6f13e212b1936c2d4a7dc372b289aa9e0 diff --git a/src/test.cpp b/src/test.cpp index bfc6d37..c993ceb 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -8,6 +8,9 @@ // Math libs #include #include +// HIDapi +#include +#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 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; } \ No newline at end of file