From d1660bb6fd78ff4bf062a8cc3c22e51ca005b303 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 8 Jul 2022 19:36:05 -0700 Subject: [PATCH] Added spdlog --- .gitmodules | 3 +++ Makefile | 3 ++- libs/spdlog | 1 + src/ergodox.cpp | 18 ++++++++++-------- src/ergodox.hpp | 2 ++ src/main.cpp | 6 +++++- src/utility/misc.cpp | 28 ++++++++++++++++++++++++++++ src/utility/misc.h | 6 ++++++ 8 files changed, 57 insertions(+), 10 deletions(-) create mode 160000 libs/spdlog create mode 100644 src/utility/misc.cpp create mode 100644 src/utility/misc.h diff --git a/.gitmodules b/.gitmodules index b67db5d..e9a364b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "libs/hidapi"] path = libs/hidapi url = ssh://git@git.betalupi.com:33/mirrors-libs/hidapi.git +[submodule "libs/spdlog"] + path = libs/spdlog + url = ssh://git@git.betalupi.com:33/Reflector/spdlog.git diff --git a/Makefile b/Makefile index 11c1741..d0f80d5 100644 --- a/Makefile +++ b/Makefile @@ -25,7 +25,8 @@ libs: $(BUILD_DIR)/hid.o CPPFLAGS := -MMD -MP \ -Wall \ -I src \ - -I libs/hidapi/hidapi + -I libs/hidapi/hidapi \ + -I libs/spdlog/include # udev: required by hidapi LDFLAGS := -l fftw3 -l udev diff --git a/libs/spdlog b/libs/spdlog new file mode 160000 index 0000000..6c95f4c --- /dev/null +++ b/libs/spdlog @@ -0,0 +1 @@ +Subproject commit 6c95f4c8168401badff89d0c16e3887ea91ea9ad diff --git a/src/ergodox.cpp b/src/ergodox.cpp index 480568c..157d22f 100644 --- a/src/ergodox.cpp +++ b/src/ergodox.cpp @@ -1,6 +1,5 @@ #include "ergodox.hpp" - Ergodox::Ergodox( unsigned short vendor_id, unsigned short product_id, @@ -102,18 +101,21 @@ bool Ergodox::try_connect() { #define MAX_STR 255 wchar_t wstr[MAX_STR]; + char nstr[MAX_STR]; - wprintf(L"\nConnected to device!\n"); + spdlog::info("Connected to device!"); // Read metadata wstr[0] = 0x0000; hid_get_manufacturer_string(handle, wstr, MAX_STR); - wprintf(L"Manufacturer String: %ls\n", wstr); + misc::to_narrow(wstr, nstr, MAX_STR); + spdlog::info("Manufacturer String: {0}", nstr); wstr[0] = 0x0000; hid_get_product_string(handle, wstr, MAX_STR); - wprintf(L"Product String: %ls\n", wstr); + misc::to_narrow(wstr, nstr, MAX_STR); + spdlog::info("Product String: {0}", nstr); } else { connected = false; @@ -168,7 +170,7 @@ bool Ergodox::write(uint8_t cmd, const uint8_t* data, uint8_t data_len) { res = hid_write(handle, packet, packet_size + 1); if (res < 0) { - wprintf(L"Lost device, disconnecting.\n"); + spdlog::info("Lost device, disconnecting."); disconnect(); return false; } @@ -194,7 +196,7 @@ bool Ergodox::read() { if (res == 0) { return false; } else if (res < 0) { - wprintf(L"Lost device, disconnecting.\n"); + spdlog::info("Lost device, disconnecting."); disconnect(); return false; } @@ -204,7 +206,7 @@ bool Ergodox::read() { // If keyboard sends a state packet, parse it. case CMD_SEND_STATE: if (animation_mode != read_buf[1]) { - wprintf(L"Mode set to 0x%02X\n", read_buf[1]); + spdlog::info("Mode set to {0:x}", read_buf[1]); animation_mode = read_buf[1]; } @@ -223,7 +225,7 @@ void Ergodox::test_connection() { // Block until a connection is established. void Ergodox::connect_loop() { - wprintf(L"Trying to connect...\n"); + spdlog::info("Trying to connect..."); while (!connected) { try_connect(); std::this_thread::sleep_for(std::chrono::milliseconds(RECONNECT_SLEEP_MS)); diff --git a/src/ergodox.hpp b/src/ergodox.hpp index f6ea0d2..1769cac 100644 --- a/src/ergodox.hpp +++ b/src/ergodox.hpp @@ -7,7 +7,9 @@ #include #include +#include "spdlog/spdlog.h" #include "hidapi.h" +#include "utility/misc.h" #include "config.h" #include "commands.h" diff --git a/src/main.cpp b/src/main.cpp index 30aa4fe..b8fa935 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,6 +17,8 @@ #include "commands.h" #include "config.h" +#include "spdlog/spdlog.h" + // TODO: // @@ -34,7 +36,7 @@ // Keyboard interface: // Frame rate limit // Clean up reconnect code -// Better log messages +// Better log messages, compiled spdlog // // Get parameters from keyboard (width, height, etc) // @@ -50,6 +52,8 @@ const size_t height = BOTTOM_SKIP + KB_RESOLUTION + TOP_SKIP; int main(int argc, char *argv[]) { + spdlog::set_level(spdlog::level::info); + // buffer size for waveform: // (44100 / fps * 10), make 10 bigger for slower scrolling // diff --git a/src/utility/misc.cpp b/src/utility/misc.cpp new file mode 100644 index 0000000..a8164cc --- /dev/null +++ b/src/utility/misc.cpp @@ -0,0 +1,28 @@ +#include "misc.h" + + +namespace misc { + size_t to_narrow(const wchar_t * src, char * dest, size_t dest_len) { + size_t i; + wchar_t code; + + i = 0; + + while (src[i] != '\0' && i < (dest_len - 1)) { + code = src[i]; + if (code < 128) { + dest[i] = char(code); + } else { + dest[i] = '?'; + if (code >= 0xD800 && code <= 0xD8FF) { + i++; + } + } + i++; + } + + dest[i] = '\0'; + return i - 1; + } + +} \ No newline at end of file diff --git a/src/utility/misc.h b/src/utility/misc.h new file mode 100644 index 0000000..19ba043 --- /dev/null +++ b/src/utility/misc.h @@ -0,0 +1,6 @@ +#pragma once +#include + +namespace misc { + size_t to_narrow(const wchar_t * src, char * dest, size_t dest_len); +} \ No newline at end of file