#include #include #include #include // For reading FIFO #include #include // For sleep #include #include // MPD client #include "mpd/client.h" #include // Local files #include "utility/bitmap.hpp" #include "utility/buffer.hpp" #include "signal_processing/fft.hpp" #include "ergodox.hpp" #include "commands.h" #include "config.h" #include "spdlog/spdlog.h" #include "dict.hpp" // TODO: // // MPD connection error handling // Cleaner building // // FFT: // 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 // clear fft when not in use // // // Keyboard interface: // Fix segfault // Clean up reconnect code // Better log messages, compiled spdlog // // Get parameters from keyboard (width, height, etc) // // Later: // beat detection // waveform animation // pcm from pulse const size_t width = 10; const size_t height = BOTTOM_SKIP + KB_RESOLUTION + TOP_SKIP; int main(int argc, char *argv[]) { spdlog::info("Loading dictionary..."); load_file(); spdlog::info("done!"); spdlog::set_level(spdlog::level::info); // buffer size for waveform: // (44100 / fps * 10), make 10 bigger for slower scrolling // // Double both buffer sizes if stereo // FFT generator FFT_Visualizer fft = FFT_Visualizer( width, height, MIN_HZ, MAX_HZ ); // Audio buffer Buffer buf = Buffer( "/tmp/mpd.fifo", 44100 / 2, // Keep 500ms of data in buffer fft.compute_buffer_output_size() ); // HID interface wrapper Ergodox Dox = Ergodox::init( HID_VENDOR_ID, HID_PRODUCT_ID, HID_USAGE, HID_USAGE_PAGE ); // Write buffer uint8_t hid_buf[Dox.packet_size]; Dox.connect_loop(); // Frame rate limiter std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > t = std::chrono::steady_clock::now(); std::chrono::time_point< std::chrono::steady_clock, std::chrono::nanoseconds > last_fifo_sync = std::chrono::steady_clock::now(); struct mpd_connection *conn = mpd_connection_new(NULL, 0, 0); while (1) { memset(hid_buf, 0, sizeof(uint8_t) * Dox.packet_size); if (Dox.is_connected()) { if (std::chrono::steady_clock::now() > t + std::chrono::milliseconds(30)) { if (Dox.get_animation_mode() == 0x02) { // Animation data type hid_buf[1] = CMD_ANIM_DATA_fft; if (std::chrono::steady_clock::now() > last_fifo_sync + std::chrono::seconds(10)) { mpd_run_disable_output(conn, 1); mpd_run_enable_output(conn, 1); last_fifo_sync = std::chrono::steady_clock::now(); spdlog::info("Synchronized fifo"); } buf.update(); fft.update(buf); for (size_t i = 0; i < 10; i++) { // Get height from fft, apply bottom_skip ssize_t h = fft.get_output()[i] - BOTTOM_SKIP; // Enforce max and min // max implicitly enforces top_skip h = h>KB_RESOLUTION ? KB_RESOLUTION : h; h = h<0 ? 0 : h; hid_buf[i + 1] = h; } Dox.write(CMD_ANIM_DATA, hid_buf, Dox.packet_size); } t = std::chrono::steady_clock::now(); } // Dox.write might detect that we've been disconnected, // and Dox.read will fail if we are. // This check prevents it from doing that, and instead jumps to reconnect. if (!Dox.is_connected()) { continue; } // Read a packet if there is a packet to read if (Dox.read()) { uint8_t cmd = Dox.read_buf[0]; switch(cmd) { case CMD_SPELLCHECK_WORD: char word_chars[Dox.read_buf[1] + 1]; for (int i=0; i < Dox.read_buf[1]; i++) { // A in ascii: // a in ascii: 0x61 // KC_A: 0x04 word_chars[i] = Dox.read_buf[i + 2] + 0x5D; } word_chars[Dox.read_buf[1]] = 0x00; // Terminate with null char std::string word = std::string(word_chars); if (word_dict.find(word) == word_dict.end()) { hid_buf[0] = 0x01; Dox.write(CMD_SPELLCHECK_WORD, hid_buf, Dox.packet_size); spdlog::info("Got typo: \"{0:s}\" not in dict", word); } break; } } } else { Dox.connect_loop(); } // Sleep for a bit so we don't consume // 100% of a cpu. std::this_thread::sleep_for( std::chrono::milliseconds(LOOP_SLEEP_MS) ); } mpd_connection_free(conn); return 0; }