Compare commits

...

41 Commits

Author SHA1 Message Date
2c9131f197 Cleanup 2022-11-20 20:48:53 -08:00
40f6ddee8a Minor cleanup 2022-11-20 12:05:44 -08:00
0e446769ed README & Makefile 2022-11-19 10:13:14 -08:00
b478f2bf82 Updated hidapi 2022-11-03 11:00:27 -07:00
99c2c7d532 Added special character command 2022-08-10 09:07:04 -07:00
25dd1b5166 Language is now set on layer change 2022-08-03 18:06:10 -07:00
30422df0da Added README 2022-07-23 12:48:41 -07:00
dcfe86f661 Improved makefile 2022-07-23 12:48:35 -07:00
720c12ae87 Comments 2022-07-23 12:46:18 -07:00
dfa3cc6c5b Replaced simple dict file with hunspell,
added basic language support
2022-07-23 12:26:45 -07:00
665f0b6dae Fixed a stupid bug 2022-07-21 18:07:24 -07:00
cbea2cd663 Added basic spellchecker 2022-07-20 21:31:14 -07:00
65b5bb6400 Minor edits 2022-07-20 21:26:06 -07:00
f450a88e91 Updated submodule urls 2022-07-15 10:39:51 -07:00
314cfe8bdb Updated TODO 2022-07-11 09:40:48 -07:00
a3e4011a70 Tweaked FFT parameters 2022-07-11 09:39:43 -07:00
df3c0e6c98 Added libmpdclient and basic fifo sync code 2022-07-11 09:39:33 -07:00
928dc8d820 Updated TODO 2022-07-09 20:36:45 -07:00
3779f5fdd0 Updated submodule url 2022-07-09 08:30:02 -07:00
d1660bb6fd Added spdlog 2022-07-08 19:36:05 -07:00
50dfd244e7 Added basic speed limiter 2022-07-08 19:07:46 -07:00
afecfe5890 Added TODO 2022-07-08 17:00:20 -07:00
3c154a7679 Minor cleanup 2022-07-08 16:56:32 -07:00
a9eb47debe Removed draw_spectrum_bitmap 2022-07-08 16:42:05 -07:00
9d8edf41eb Prettified format string 2022-07-08 16:39:36 -07:00
4aa558eda7 Added connection management 2022-07-08 16:33:42 -07:00
fe00a30268 Removed extra scaling code 2022-07-08 10:45:08 -07:00
1225d414e3 Renamed scripts and executable 2022-07-08 10:42:57 -07:00
6f2c2e313e Upgraded hid interface 2022-07-08 10:41:19 -07:00
dacb8d820d Cleanup 2022-06-26 12:49:50 -07:00
5ea64d3e07 Comments 2022-06-26 12:47:43 -07:00
267a8a2ad6 Removed bitmap debug 2022-06-26 12:46:28 -07:00
60da6c131a Added ergodox hid manager 2022-06-26 12:39:02 -07:00
a447b5f8c8 Cleaned up makefile 2022-06-26 12:36:36 -07:00
4eff7eb277 Added basic HID intervace 2022-06-25 19:12:01 -07:00
15311cf9e5 Updated gitignore 2022-06-24 21:43:20 -07:00
58fd2821b5 Added test.cpp 2022-06-24 21:33:12 -07:00
2332e2980a Added TODO 2022-06-24 21:32:28 -07:00
bc1a6f3a85 Added FFT code 2022-06-24 21:28:03 -07:00
7655f1bdad Fixed a bug 2022-06-24 21:27:49 -07:00
952d03ee10 Rearranged files 2022-06-24 21:07:30 -07:00
29 changed files with 1742 additions and 15 deletions

4
.gitignore vendored
View File

@ -1,5 +1,7 @@
# Misc files
/.vscode
notes
# Build files and final binary
/build
test
hostdox

9
.gitmodules vendored Normal file
View File

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

View File

@ -1,10 +1,10 @@
TARGET_EXEC := test
TARGET_EXEC := hostdox
BUILD_DIR := ./build
SRC_DIRS := ./src
# False targets
# (these are the only ones you manually run)
all: $(TARGET_EXEC)
all: libs $(TARGET_EXEC)
run: all
./$(TARGET_EXEC)
@ -13,25 +13,73 @@ clean:
-rm -r $(BUILD_DIR)
@echo ""
.PHONY: clean all run
libs: $(BUILD_DIR)/hid.o $(BUILD_DIR)/libmpdclient.o
submodules:
git submodule update --init --recursive
.PHONY: clean all run libs submodules
#################################################
# Flags and autodetection
# -MMD and -MP generate makefiles with extension .d.
CPPFLAGS := -Wall -MMD -MP -I src
LDFLAGS := -l fftw3
CPPFLAGS := -MMD -MP \
-Wall \
-I src \
-I libs/hidapi/hidapi \
-I libs/spdlog/include \
-I libs/libmpdclient/include \
-I libs/libmpdclient/output \
$(shell pkg-config --cflags hunspell)
# udev: required by hidapi
LDFLAGS := \
-l fftw3 \
-l udev \
$(shell pkg-config --libs hunspell)
# 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)
# Turns ./build/a.cpp.o into ./build/a.cpp.d
# Turns src/a.cpp into build/src/a.cpp.o
SRC_OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
LIB_OBJS := $(BUILD_DIR)/hid.o $(BUILD_DIR)/libmpdclient.o
OBJS = $(SRC_OBJS) $(LIB_OBJS)
# Turns build/a.cpp.o into build/a.cpp.d
DEPS := $(OBJS:.o=.d)
#################################################
# Build targets
### Libraries
# Build hidapi
$(BUILD_DIR)/hid.o:
@mkdir -p $(BUILD_DIR)
@echo "Compiling hid.o"
@gcc -Wall -g -fpic -c \
-I libs/hidapi/hidapi \
`pkg-config libusb-1.0 --cflags` \
libs/hidapi/linux/hid.c \
-o $(BUILD_DIR)/hid.o
# Build libmpdclient
$(BUILD_DIR)/libmpdclient.o:
@mkdir -p $(BUILD_DIR)
@cd libs/libmpdclient && \
meson . output && \
ninja -C output
ln -s ../libs/libmpdclient/output/libmpdclient.so $(BUILD_DIR)/libmpdclient.o
### Source
# C++ build step
$(BUILD_DIR)/%.cpp.o: %.cpp
mkdir -p $(dir $@)
@ -42,6 +90,5 @@ $(BUILD_DIR)/%.cpp.o: %.cpp
$(TARGET_EXEC) : $(OBJS)
g++ $(OBJS) -o $@ $(LDFLAGS)
# Include generated makefiles
-include $(DEPS)

34
README.md Normal file
View File

@ -0,0 +1,34 @@
# Ergodox Host Interface
Host software for [Betalupi Ergodox](https://git.betalupi.com/Mark/QMK).
## Features:
- Music visualizer ([here](https://git.betalupi.com/Mark/hostdox/src/branch/master/src/signal_processing))
- Spell checking with hunspell
## Permissions
You'll need to run this binary as a regular user. To allow access to raw hid, add the following udev rule and reboot:
```conf
KERNEL=="hidraw*", ATTRS{idVendor}=="3297", ATTRS{idProduct}=="4976", TAG+="uaccess", GROUP="mark", MODE="660"
```
See the hidraw repo for more information.
## Dependencies:
Run:
- hunspell
Build:
- meson (for libmpdclient)
- ninja (for libmpdclient)
- gcc
## Building
``make submodules`` to initialize git submodules \
``make all`` to build, or ``make run`` to build and run.

1
libs/hidapi Submodule

Submodule libs/hidapi added at bb792a1f7e

1
libs/libmpdclient Submodule

Submodule libs/libmpdclient added at 7124a0ad48

1
libs/spdlog Submodule

Submodule libs/spdlog added at 6c95f4c816

106
src/commands.h Normal file
View File

@ -0,0 +1,106 @@
#pragma once
// Sent by host when connection is initiated.
//
// Packet structure:
// Data: | cmd |
// # of Bytes: | 1 |
#define CMD_HELLO 0x00
// Sent periodically by host to test connection.
// Keyboard should ignore this command.
//
// Packet structure:
// Data: | cmd |
// # of Bytes: | 1 |
#define CMD_RUTHERE 0x01
// Send keyboard state to host.
//
// Packet structure:
// Data: | cmd | anim state | layer state | layer layout |
// # of Bytes: | 1 | 1 | 4 | 1 |
//
// anim state:
// 0x00: RGBMatrix disabled
// 0x01: normal animation, no HID data.
// 0x02: FFT Animation
//
// layer state: layer state right now.
// This is a uint32_t, where each bit corresponds to a layer index.
// Lowest-order bit is base layer, highest bit is layer 31.
// Layer indices are defined by the LAYER_* enum in layer.h,
// host interface should have a matching enum.
// Make sure to update it when you change your layers!
//
// layer layout:
// The layout this layer was designed for.
// 0x00: en_us
// 0x01: russian
#define CMD_SEND_STATE 0x02
// Animation data. Sent by host.
//
// Packet structure:
// Data: | cmd | data type | data |
// # of Bytes: | 1 | 1 | ? |
//
// data type:
// Which animation this data is for. These are defined below.
//
// data:
// Animation data. Content depends on data type.
#define CMD_ANIM_DATA 0x03
// Data for FFT animation.
// Data segment consists of 10 bits, each representing the height of a column.
// Minimum height is 0, maximum is 250.
#define CMD_ANIM_DATA_fft 0x00
// Sent by keyboard to host when a complete word is typed.
// Host checks if this is a known word.
// If it is not, host responds with the same CMD (see below).
//
// Packet structure (sent by keyboard):
// Data: | cmd | word length | keycodes |
// # of Bytes: | 1 | 1 | ? |
//
// word length: number of bytes in `keycodes` block
//
//
// Packet structure (sent by host):
// Data: | cmd | typo? |
// # of Bytes: | 1 | 1 |
//
// typo: If this is 0x01, the word we got was a typo.
#define CMD_SPELLCHECK_WORD 0x04
// Sent by host when a "special char" key is pressed.
// Handled by host interface.
//
// Packet structure:
// Data: | cmd | character |
// # of Bytes: | 1 | 2 |
//
// character:
// uint16_t, character id
//
#define CMD_SPECIAL_CHAR 0x05

85
src/config.h Normal file
View File

@ -0,0 +1,85 @@
#pragma once
/*
General Setup
*/
// USB device params
#define HID_VENDOR_ID 0x3297
#define HID_PRODUCT_ID 0x4976
#define HID_USAGE 0x61
#define HID_USAGE_PAGE 0xFF60
// USB packet size, in bytes.
// Usually 32, but depends on keyboard.
#define RAW_EPSIZE 32
// How many milliseconds to wait between reconnect attempts
#define RECONNECT_SLEEP_MS 500
// Keyboard layers.
// These must have the same indices as
// your layers in QMK.
enum keyboard_layers {
LAYER_MAIN,
LAYER_RUSSIAN,
LAYER_SYMBOLS,
LAYER_SYMBOLS_RU,
LAYER_ARROWS,
LAYER_DESKTOP,
LAYER_FKEYS,
LAYER_KEYBOARD,
LAYER_NUMPAD
};
// Must match enum in keyboard
enum layer_layout_ids {
LAYOUT_NULL, // This layer doesn't care what keymap the OS is using
LAYOUT_EN, // This layer is designed for the standard keymapping
LAYOUT_RU,
};
// Sleep this many millis after each loop.
// Prevents absurd cpu usage.
#define LOOP_SLEEP_MS 20
/*
Autotype Special Characters
*/
//#define DISABLE_SPECIAL_CHAR
/*
Spellcheck
*/
#define DISABLE_SPELL
#ifndef DISABLE_SPELL
#define HUNSPELL_AFF_EN "/usr/share/hunspell/en_US.aff"
#define HUNSPELL_DIC_EN "/usr/share/hunspell/en_US.dic"
#endif
/*
Music Visualizer Animation
*/
#define DISABLE_VISUALIZER
#ifndef DISABLE_VISUALIZER
// How many keys in a column * resolution per key.
// this MUST fit inside a uint8_t (i.e, <= 255).
#define KB_RESOLUTION (5 * 50)
// How many resolution steps to skip at the top and bottom.
#define BOTTOM_SKIP 100
#define TOP_SKIP 0
// Spectrum visualizer range
#define MIN_HZ 100
#define MAX_HZ 5000
#endif

254
src/ergodox.cpp Normal file
View File

@ -0,0 +1,254 @@
#include "ergodox.hpp"
Ergodox::Ergodox(
unsigned short vendor_id,
unsigned short product_id,
unsigned short usage,
unsigned short usage_page
) :
vendor_id(vendor_id),
product_id(product_id),
usage(usage),
usage_page(usage_page),
handle(NULL),
connected(false)
{
hid_init();
}
/*
Initialize Ergodox interface.
Should be called once at start of execution. Arguments are the USB parameters of the keyboard.
Returns instance of singleton.
*/
Ergodox& Ergodox::init(
unsigned short vendor_id,
unsigned short product_id,
unsigned short usage,
unsigned short usage_page
) {
static bool has_been_initialized = false;
static Ergodox Instance(
vendor_id,
product_id,
usage,
usage_page
);
// This isn't strictly necessary, but there is no reason
// to call init() twice.
if (has_been_initialized) {
has_been_initialized = true;
throw std::runtime_error("Ergodox has already been initialized!");
}
return Instance;
}
// Instance is a static function variable,
// and will be deleted at end of program.
Ergodox::~Ergodox() {
disconnect();
hid_exit();
}
/*
Try to open this keyboard's hid device.
Throws a runtime error if device is already open.
If we successfully open a device, this method sends a
CMD_HELLO packet.
@returns True if connection was successful, false otherwise.
*/
bool Ergodox::try_connect() {
if (connected) {
throw std::runtime_error("Device already open");
}
struct hid_device_info *devs, *cur_dev;
const char *path_to_open = NULL;
devs = hid_enumerate(vendor_id, product_id);
cur_dev = devs;
while (cur_dev) {
if (cur_dev->vendor_id == vendor_id &&
cur_dev->product_id == product_id &&
cur_dev->usage == usage &&
cur_dev->usage_page == usage_page
) {
path_to_open = cur_dev->path;
break;
}
cur_dev = cur_dev->next;
}
// Return false if connection failed
if (path_to_open) {
connected = true;
handle = hid_open_path(path_to_open);
hid_set_nonblocking(handle, 1);
hid_free_enumeration(devs);
write(CMD_HELLO, NULL, 0);
#define MAX_STR 255
wchar_t wstr[MAX_STR];
char nstr[MAX_STR];
spdlog::info("Connected to device!");
// Read metadata
wstr[0] = 0x0000;
hid_get_manufacturer_string(handle, wstr, MAX_STR);
misc::to_narrow(wstr, nstr, MAX_STR);
spdlog::info("Manufacturer String: {0}", nstr);
wstr[0] = 0x0000;
hid_get_product_string(handle, wstr, MAX_STR);
misc::to_narrow(wstr, nstr, MAX_STR);
spdlog::info("Product String: {0}", nstr);
} else {
connected = false;
hid_free_enumeration(devs);
return false;
}
return connected;
}
// Close hid device if it is open.
void Ergodox::disconnect() {
connected = false;
if (handle != NULL) {
hid_close(handle);
handle = NULL;
}
}
/*
Send data to Ergodox.
@param cmd Command byte
@param data Data to send
@param data_len Data length. Must be shorter than packet_size.
@returns True if successful, false otherwise.
*/
bool Ergodox::write(uint8_t cmd, const uint8_t* data, uint8_t data_len) {
if (!connected) {
throw std::runtime_error("Not connected, cannot write!");
} else if (handle == NULL) {
throw std::runtime_error("Tried to write a null handle, something is very wrong.");
} else if (data_len > packet_size) {
throw std::runtime_error("Data length exceeds packet size");
}
if (data == NULL) {
data = {0x00};
}
uint8_t packet[packet_size];
packet[0] = 0x00; // Report number. Not seen by keyboard.
packet[1] = cmd; // First byte is always command
// Copy data into rest of packet
std::copy(data, data + data_len, packet + 2);
ssize_t res;
res = hid_write(handle, packet, packet_size + 1);
if (res < 0) {
spdlog::info("Lost device, disconnecting.");
disconnect();
return false;
}
return true;
}
/*
Read data from Ergodox into read_buf. If a CMD_SEND_STATE packet is received, process it and return false.
@returns True if a new command was received, false otherwise.
*/
bool Ergodox::read() {
if (!connected) {
throw std::runtime_error("Not connected, cannot read!");
} else if (handle == NULL) {
throw std::runtime_error("Tried to read a null handle, something is very wrong.");
}
ssize_t res;
res = hid_read(handle, read_buf, packet_size);
if (res == 0) {
return false;
} else if (res < 0) {
spdlog::info("Lost device, disconnecting.");
disconnect();
return false;
}
switch(read_buf[0]) {
// If keyboard sends a state packet, parse it.
case CMD_SEND_STATE:
// Byte 1: animation mode
if (animation_mode != read_buf[1]) {
spdlog::info("Mode set to 0x{0:02x}", read_buf[1]);
animation_mode = read_buf[1];
}
// Bytes 2,3,4,5: layer state
uint32_t new_layer_state =
(read_buf[5] << 24) |
(read_buf[4] << 16) |
(read_buf[3] << 8) |
(read_buf[2] << 0);
if (layer_state != new_layer_state) {
layer_state = new_layer_state;
spdlog::info("Layer set to 0b{0:032b}", layer_state);
}
// Byte 6: desired OS layout for layer
if (layer_layout != read_buf[6]) {
layer_layout = read_buf[6];
spdlog::info("Layout set to 0x{0:02x}", read_buf[6]);
}
// Main code should not parse state packets.
return false;
}
return true;
}
// Simple connectivity check
void Ergodox::test_connection() {
write(CMD_RUTHERE, NULL, 0);
}
// Block until a connection is established.
void Ergodox::connect_loop() {
spdlog::info("Trying to connect...");
while (!connected) {
try_connect();
std::this_thread::sleep_for(std::chrono::milliseconds(RECONNECT_SLEEP_MS));
}
}

92
src/ergodox.hpp Normal file
View File

@ -0,0 +1,92 @@
#pragma once
#include <stdexcept>
#include <cstdint>
#include <wchar.h>
// For sleep
#include <chrono>
#include <thread>
#include "spdlog/spdlog.h"
#include "hidapi.h"
#include "utility/misc.h"
#include "config.h"
#include "commands.h"
/*
A singleton Ergodox interface. Wraps all hidapi methods, including
hid_init() and hid_exit().
*/
class Ergodox {
public:
// USB Device parameters
const unsigned short vendor_id;
const unsigned short product_id;
const unsigned short usage;
const unsigned short usage_page;
const uint8_t packet_size = RAW_EPSIZE;
static Ergodox& init(
unsigned short vendor_id,
unsigned short product_id,
unsigned short usage,
unsigned short usage_page
);
~Ergodox();
bool try_connect();
void disconnect();
void test_connection();
void connect_loop();
bool read();
bool write(uint8_t cmd, const uint8_t* data, uint8_t data_len);
// Read buffer, len = packet_size.
// Filled by read().
uint8_t read_buf[RAW_EPSIZE];
// Getter methods
uint8_t get_animation_mode() const { return animation_mode; }
bool is_connected() const { return connected; }
uint32_t get_layer_state() const {return layer_state; }
uint8_t get_layer_layout() const { return layer_layout; }
bool is_layer_on(uint8_t layer) const { return (layer_state >> layer) % 2; }
private:
Ergodox(
unsigned short vendor_id,
unsigned short product_id,
unsigned short usage,
unsigned short usage_page
);
// Disable copy and assignment
//Ergodox(void);
//Ergodox(Ergodox& other);
//Ergodox& operator=(Ergodox& other);
// HID device.
// NULL if not opened.
hid_device* handle;
// Keyboard state variables.
// Updated by read().
// Which animation is the keyboard running right now?
// See CMD_SEND_STATE in commands.h for docs.
uint8_t animation_mode;
// Active layer bitmask.
// See CMD_SEND_STATE in commands.h for docs.
uint32_t layer_state;
// Desired layout for active layer.
// See CMD_SEND_STATE in commands.h for docs.
uint8_t layer_layout;
// Are we connected to a keyboard right now?
bool connected;
};

151
src/main.cpp Normal file
View File

@ -0,0 +1,151 @@
#include <stdio.h>
#include <cstdint>
#include <vector>
#include <stdexcept>
#include <string>
// For reading FIFO
#include <fcntl.h>
#include <unistd.h>
// For sleep
#include <thread>
// Local files
#include "ergodox.hpp"
#include "commands.h"
#include "config.h"
#include "spdlog/spdlog.h"
#ifndef DISABLE_SPECIAL_CHAR
#include "modules/special_chars.hpp"
#endif
#ifndef DISABLE_SPELL
#include "modules/spell.hpp"
#endif
#ifndef DISABLE_VISUALIZER
#include "modules/visualizer.hpp"
#endif
// 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
// HID interface wrapper
Ergodox Dox = Ergodox::init(
HID_VENDOR_ID,
HID_PRODUCT_ID,
HID_USAGE,
HID_USAGE_PAGE
);
int main(int argc, char *argv[]) {
spdlog::set_level(spdlog::level::info);
uint8_t last_layer_layout;
Dox.connect_loop();
#ifndef DISABLE_VISUALIZER
Visualizer::init();
#endif
while (1) {
if (Dox.is_connected()) {
#ifndef DISABLE_VISUALIZER
Visualizer::fn(Dox);
#endif
// 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) {
#ifndef DISABLE_SPELL
case CMD_SPELLCHECK_WORD: {
Spell::do_cmd(Dox);
break;
}
#endif
#ifndef DISABLE_SPECIAL_CHAR
case CMD_SPECIAL_CHAR: {
SpecialChars::do_cmd(Dox);
break;
}
#endif
}
}
// Switch layer if necessary.
if (
(last_layer_layout != Dox.get_layer_layout()) &&
(Dox.get_layer_layout() != LAYOUT_NULL)
) {
last_layer_layout = Dox.get_layer_layout();
switch(last_layer_layout) {
case (LAYOUT_EN):
std::system("awesome-client \"modules.ibus.set('en')\"");
break;
case (LAYOUT_RU):
std::system("awesome-client \"modules.ibus.set('ru')\"");
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)
);
}
#ifndef DISABLE_VISUALIZER
Visualizer::cleanup();
#endif
return 0;
}

View File

@ -0,0 +1,25 @@
#include "special_chars.hpp"
#ifndef DISABLE_SPECIAL_CHAR
namespace SpecialChars {
void do_cmd(Ergodox &Dox) {
// Bytes 1,2: char id
uint16_t char_id =
(Dox.read_buf[2] << 8) |
(Dox.read_buf[1] << 0);
spdlog::info("{0:d}", char_id);
if (char_id < (sizeof(special_chars) / sizeof(std::string))) {
std::system(
(
std::string("xdotool type \"") +
special_chars[char_id] +
std::string("\"")
).c_str()
);
}
}
}
#endif

View File

@ -0,0 +1,28 @@
#pragma once
#include "config.h"
#ifndef DISABLE_SPECIAL_CHAR
#include <string>
#include "ergodox.hpp"
namespace SpecialChars {
const std::string special_chars[] = {
// These characters are used to type
// symbols on layouts that don't have
// them.
"\\`",
"~",
"'",
"[",
"]",
"{",
"}",
"«",
"»"
};
void do_cmd(Ergodox &Dox);
}
#endif

133
src/modules/spell.cpp Normal file
View File

@ -0,0 +1,133 @@
#include "spell.hpp"
#ifndef DISABLE_SPELL
namespace Spell {
Hunspell* hun = new Hunspell(HUNSPELL_AFF_EN, HUNSPELL_DIC_EN);
uint8_t hid_buf[RAW_EPSIZE]; // Write buffer
// Replacement chars for now, since wide strings are hard.
char ru_kc_to_char(uint8_t keycode) {
switch (keycode) {
case KC_GRAVE:
return '~';//L"ё";
case KC_1:
return '1';//L"1";
case KC_2:
return '2';//L"2";
case KC_3:
return '3';//L"3";
case KC_4:
return '4';//L"4";
case KC_5:
return '5';//L"5";
case KC_6:
return '6';//L"6";
case KC_7:
return '7';//L"7";
case KC_8:
return '8';//L"8";
case KC_9:
return '9';//L"9";
case KC_0:
return '0';//L"0";
case KC_Q:
return '^';//L"й";
case KC_W:
return '*';//L"ц";
case KC_E:
return 'y';//L"у";
case KC_R:
return 'k';//L"к";
case KC_T:
return 'e';//L"е";
case KC_Y:
return 'H';//L"н";
case KC_U:
return 'g';//L"г";
case KC_I:
return 'w';//L"ш";
case KC_O:
return 'W';//L"щ";
case KC_P:
return 'z';//L"з";
case KC_LEFT_BRACKET:
return 'x';//L"х";
case KC_RIGHT_BRACKET:
return '!';//L"ъ";
case KC_A:
return 'f';//L"ф";
case KC_S:
return 'i';//L"ы";
case KC_D:
return 'B';//L"в";
case KC_F:
return 'a';//L"а";
case KC_G:
return 'n';//L"п";
case KC_H:
return 'p';//L"р";
case KC_J:
return 'o';//L"о";
case KC_K:
return 'l';//L"л";
case KC_L:
return 'd';//L"д";
case KC_SEMICOLON:
return 'j';//L"ж";
case KC_QUOTE:
return 'E';//L"э";
case KC_Z:
return 'R';//L"я";
case KC_X:
return 'q';//L"ч";
case KC_C:
return 'c';//L"с";
case KC_V:
return 'm';//L"м";
case KC_B:
return 'n';//L"и";
case KC_N:
return 't';//L"т";
case KC_M:
return '=';//L"ь";
case KC_COMMA:
return 'b';//L"б";
case KC_DOT:
return 'u';//L"ю";
default:
spdlog::warn("Unknown keycode passed to ru_kc_to_char");
return '?';//L"?";
}
}
void do_cmd(Ergodox &Dox) {
char word_chars[Dox.read_buf[1] + 1];
if (Dox.get_layer_layout() == LAYOUT_EN) {
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;
}
} else if (Dox.get_layer_layout() == LAYOUT_RU) {
for (int i=0; i < Dox.read_buf[1]; i++) {
word_chars[i] = ru_kc_to_char(Dox.read_buf[i + 2]);
}
}
word_chars[Dox.read_buf[1]] = 0x00; // Terminate with null char
std::string word = std::string(word_chars);
int dp = hun->spell(word);
if (!dp) {
memset(hid_buf, 0, sizeof(uint8_t) * Dox.packet_size);
hid_buf[0] = 0x01;
Dox.write(CMD_SPELLCHECK_WORD, hid_buf, Dox.packet_size);
//spdlog::info("Got typo: \"{0:s}\" not in dict", word);
}
}
}
#endif

181
src/modules/spell.hpp Normal file
View File

@ -0,0 +1,181 @@
#pragma once
#include "config.h"
#ifndef DISABLE_SPELL
#include "hunspell.hxx"
#include "ergodox.hpp"
namespace Spell {
char ru_kc_to_char(uint8_t keycode);
void do_cmd(Ergodox &Dox);
}
enum hid_keyboard_keypad_usage {
KC_NO = 0x00,
KC_ROLL_OVER,
KC_POST_FAIL,
KC_UNDEFINED,
KC_A,
KC_B,
KC_C,
KC_D,
KC_E,
KC_F,
KC_G,
KC_H,
KC_I,
KC_J,
KC_K,
KC_L,
KC_M, // 0x10
KC_N,
KC_O,
KC_P,
KC_Q,
KC_R,
KC_S,
KC_T,
KC_U,
KC_V,
KC_W,
KC_X,
KC_Y,
KC_Z,
KC_1,
KC_2,
KC_3, // 0x20
KC_4,
KC_5,
KC_6,
KC_7,
KC_8,
KC_9,
KC_0,
KC_ENTER,
KC_ESCAPE,
KC_BACKSPACE,
KC_TAB,
KC_SPACE,
KC_MINUS,
KC_EQUAL,
KC_LEFT_BRACKET,
KC_RIGHT_BRACKET, // 0x30
KC_BACKSLASH,
KC_NONUS_HASH,
KC_SEMICOLON,
KC_QUOTE,
KC_GRAVE,
KC_COMMA,
KC_DOT,
KC_SLASH,
KC_CAPS_LOCK,
KC_F1,
KC_F2,
KC_F3,
KC_F4,
KC_F5,
KC_F6,
KC_F7, // 0x40
KC_F8,
KC_F9,
KC_F10,
KC_F11,
KC_F12,
KC_PRINT_SCREEN,
KC_SCROLL_LOCK,
KC_PAUSE,
KC_INSERT,
KC_HOME,
KC_PAGE_UP,
KC_DELETE,
KC_END,
KC_PAGE_DOWN,
KC_RIGHT,
KC_LEFT, // 0x50
KC_DOWN,
KC_UP,
KC_NUM_LOCK,
KC_KP_SLASH,
KC_KP_ASTERISK,
KC_KP_MINUS,
KC_KP_PLUS,
KC_KP_ENTER,
KC_KP_1,
KC_KP_2,
KC_KP_3,
KC_KP_4,
KC_KP_5,
KC_KP_6,
KC_KP_7,
KC_KP_8, // 0x60
KC_KP_9,
KC_KP_0,
KC_KP_DOT,
KC_NONUS_BACKSLASH,
KC_APPLICATION,
KC_KB_POWER,
KC_KP_EQUAL,
KC_F13,
KC_F14,
KC_F15,
KC_F16,
KC_F17,
KC_F18,
KC_F19,
KC_F20,
KC_F21, // 0x70
KC_F22,
KC_F23,
KC_F24,
KC_EXECUTE,
KC_HELP,
KC_MENU,
KC_SELECT,
KC_STOP,
KC_AGAIN,
KC_UNDO,
KC_CUT,
KC_COPY,
KC_PASTE,
KC_FIND,
KC_KB_MUTE,
KC_KB_VOLUME_UP, // 0x80
KC_KB_VOLUME_DOWN,
KC_LOCKING_CAPS_LOCK,
KC_LOCKING_NUM_LOCK,
KC_LOCKING_SCROLL_LOCK,
KC_KP_COMMA,
KC_KP_EQUAL_AS400,
KC_INTERNATIONAL_1,
KC_INTERNATIONAL_2,
KC_INTERNATIONAL_3,
KC_INTERNATIONAL_4,
KC_INTERNATIONAL_5,
KC_INTERNATIONAL_6,
KC_INTERNATIONAL_7,
KC_INTERNATIONAL_8,
KC_INTERNATIONAL_9,
KC_LANGUAGE_1, // 0x90
KC_LANGUAGE_2,
KC_LANGUAGE_3,
KC_LANGUAGE_4,
KC_LANGUAGE_5,
KC_LANGUAGE_6,
KC_LANGUAGE_7,
KC_LANGUAGE_8,
KC_LANGUAGE_9,
KC_ALTERNATE_ERASE,
KC_SYSTEM_REQUEST,
KC_CANCEL,
KC_CLEAR,
KC_PRIOR,
KC_RETURN,
KC_SEPARATOR,
KC_OUT, // 0xA0
KC_OPER,
KC_CLEAR_AGAIN,
KC_CRSEL,
KC_EXSEL,
};
#endif

View File

@ -0,0 +1,80 @@
#include "visualizer.hpp"
#ifndef DISABLE_VISUALIZER
namespace Visualizer {
uint8_t hid_buf[RAW_EPSIZE]; // Write buffer
struct mpd_connection *conn;
FFT_Visualizer fft = FFT_Visualizer(
width, height, MIN_HZ, MAX_HZ
);
std::chrono::time_point<
std::chrono::steady_clock,
std::chrono::nanoseconds
> t;
std::chrono::time_point<
std::chrono::steady_clock,
std::chrono::nanoseconds
> last_fifo_sync;
// buffer size for waveform:
// (44100 / fps * 10), make 10 bigger for slower scrolling
//
// Double both buffer sizes if stereo
Buffer buf = Buffer(
"/tmp/mpd.fifo",
44100 / 2, // Keep 500ms of data in buffer
fft.compute_buffer_output_size()
);
void init() {
// Frame rate limiter
t = std::chrono::steady_clock::now();
last_fifo_sync = std::chrono::steady_clock::now();
conn = mpd_connection_new(NULL, 0, 0);
}
void cleanup() {
mpd_connection_free(Visualizer::conn);
}
void fn(Ergodox &Dox) {
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();
}
}
}
#endif

View File

@ -0,0 +1,23 @@
#pragma once
#include "config.h"
#ifndef DISABLE_VISUALIZER
#include "utility/buffer.hpp"
#include <chrono>
#include "mpd/client.h"
#include "signal_processing/fft.hpp"
#include "spdlog/spdlog.h"
#include "ergodox.hpp"
#include "commands.h"
namespace Visualizer {
const size_t width = 10;
const size_t height = BOTTOM_SKIP + KB_RESOLUTION + TOP_SKIP;
void cleanup();
void init();
void fn(Ergodox &Dox);
}
#endif

View File

@ -0,0 +1,313 @@
#include "fft.hpp"
FFT_Visualizer::FFT_Visualizer(
size_t width,
size_t height,
// Advanced options. These have default values.
double HZ_MIN,
double HZ_MAX,
uint32_t DFT_TOTAL_SIZE,
uint32_t DFT_NONZERO_SIZE,
double GAIN
):
width(width),
height(height),
HZ_MIN(HZ_MIN),
HZ_MAX(HZ_MAX),
DFT_TOTAL_SIZE(DFT_TOTAL_SIZE),
DFT_NONZERO_SIZE(DFT_NONZERO_SIZE),
GAIN(GAIN),
DYNAMIC_RANGE(100 - GAIN)
{
fftw_results = DFT_TOTAL_SIZE/2 + 1;
freq_magnitudes.resize(fftw_results);
partial_output.reserve(width);
output.resize(width);
GenLogspace();
fftw_input = static_cast<double *>(fftw_malloc(sizeof(double)*DFT_TOTAL_SIZE));
fftw_output = static_cast<fftw_complex *>(fftw_malloc(sizeof(fftw_complex)*fftw_results));
memset(fftw_input, 0, sizeof(double)*DFT_TOTAL_SIZE);
plan = fftw_plan_dft_r2c_1d(
DFT_TOTAL_SIZE,
fftw_input,
fftw_output,
FFTW_ESTIMATE
);
}
FFT_Visualizer::~FFT_Visualizer() {
fftw_destroy_plan(plan);
fftw_free(fftw_input);
fftw_free(fftw_output);
}
/*
Returns ideal size for this visualizer's Buffer's output.
*/
size_t FFT_Visualizer::compute_buffer_output_size() const {
return DFT_NONZERO_SIZE;
}
// Generate log-scaled vector of frequencies from HZ_MIN to HZ_MAX.
void FFT_Visualizer::GenLogspace() {
// Prepare vector
logspace.resize(width);
// Calculate number of extra bins needed between 0 HZ and HZ_MIN
//
// In logspace, divide the region between MAX and MIN into
// `width - 1` equal segments (fenceposts; this gives us `width` seperators)
const double d = (
(log10(HZ_MAX) - log10(HZ_MIN))
/
(width - 1)
);
// Count how many of these segments will fit between
// 0 and MIN (note that we're still in logspace).
// This is how many log-scaled intervals are outside
// our desired range of frequencies.
const size_t skip_bins = log10(HZ_MIN) / d;
// Calculate log scale size.
// We can't use the value of d here, because d is "anchored" to both MIN and MAX.
// The last bin should be equal to MAX, but there may not be a bin that is equal to MIN.
//
// So, we re-partition our logspace:
// Divide the distance between 0 and MAX into equal partitions.
const double log_scale = log10(HZ_MAX) / (skip_bins + width - 1);
// Exponential-map bins out of logspace, skipping those that are outside our range.
// Note that the first (skipped) bin is ALWAYS 1, since 10^(0 * log_scale) = 1.
// The last bin ALWAYS equals MAX.
for (size_t i = skip_bins; i < width + skip_bins; ++i) {
logspace[i - skip_bins] = pow(10, i * log_scale);
}
}
/*
Apply the Blackman window and copy buffer data.
@param output Where to write output data
@param input Raw data from buffer
@param data_length How much data there is. The lengths
of both input and output must be equal to this value.
*/
void FFT_Visualizer::ApplyWindow(
double* output,
const int16_t* input,
ssize_t data_length
) const {
// samples = length of input = length of output
// Constants.
// These give us low sidelobes and fast sidelobe rolloff.
// https://en.wikipedia.org/wiki/Window_function#Blackman_window
//
// See also:
// https://en.wikipedia.org/wiki/Spectral_leakage
const double alpha = 0.16;
const double a0 = (1 - alpha) / 2;
const double a1 = 0.5;
const double a2 = alpha / 2;
const double pi = 3.151592653;
const double window_width = DFT_NONZERO_SIZE - 1;
for (unsigned i = 0; i < data_length; ++i) {
double window = (
a0 -
a1*cos(
2*pi*i /
window_width
) +
a2*cos(
4*pi*i /
window_width
)
);
// Output values are between 1 and -1.
output[i] = (window * input[i]) / INT16_MAX;
}
}
/*
Not sure what this does yet.
Takes values in [0, DFT_TOTAL_SIZE/2],
returns values in [0, 44100 / 2].
@param bin An index of `freq_magnitudes`
*/
double FFT_Visualizer::Bin2Hz(size_t bin) const {
return bin*44100/DFT_TOTAL_SIZE;
}
/*
Fill empty bins with interpolated data.
@param target_x The index of the bin we want to fill.
@param last_bar_idx The index of the last non-empty and non-interpolated bin.
*/
double FFT_Visualizer::Interpolate(
size_t target_x,
size_t last_bar_idx
) {
const double x_next = partial_output[last_bar_idx].first;
const double h_next = partial_output[last_bar_idx].second;
double dh = 0;
if (last_bar_idx == 0) {
// no data points on left, linear extrapolation
if (last_bar_idx < partial_output.size()-1) {
const double x_next2 = partial_output[last_bar_idx + 1].first;
const double h_next2 = partial_output[last_bar_idx + 1].second;
dh = (h_next2 - h_next) / (x_next2 - x_next);
}
return h_next - dh*(x_next - target_x);
} else if (last_bar_idx == 1) {
// one data point on left, linear interpolation
const double x_prev = partial_output[last_bar_idx - 1].first;
const double h_prev = partial_output[last_bar_idx - 1].second;
dh = (h_next - h_prev) / (x_next - x_prev);
return h_next - dh*(x_next - target_x);
} else if (last_bar_idx < partial_output.size() - 1) {
// Two data points on both sides, cubic interpolation
// https://en.wikipedia.org/wiki/Cubic_Hermite_spline#Interpolation_on_an_arbitrary_interval
const double x_prev2 = partial_output[last_bar_idx - 2].first;
const double h_prev2 = partial_output[last_bar_idx - 2].second;
const double x_prev = partial_output[last_bar_idx - 1].first;
const double h_prev = partial_output[last_bar_idx - 1].second;
const double x_next2 = partial_output[last_bar_idx + 1].first;
const double h_next2 = partial_output[last_bar_idx + 1].second;
const double m0 = (h_prev - h_prev2) / (x_prev - x_prev2);
const double m1 = (h_next2 - h_next) / (x_next2 - x_next);
const double t = (target_x - x_prev) / (x_next - x_prev);
const double h00 = 2*t*t*t - 3*t*t + 1;
const double h10 = t*t*t - 2*t*t + t;
const double h01 = -2*t*t*t + 3*t*t;
const double h11 = t*t*t - t*t;
return h00*h_prev + h10*(x_next-x_prev)*m0 + h01*h_next + h11*(x_next-x_prev)*m1;
}
// Less than two data points on right, no interpolation.
// This should never happen unless you have a VERY low DFT size
return h_next;
}
/*
Read a buffer and update output array
*/
void FFT_Visualizer::update(
const Buffer& buf
) {
// Load data and execute FFT
ApplyWindow(fftw_input, buf.get_output().data(), buf.get_output().size());
fftw_execute(plan);
// Count magnitude of each frequency and normalize
// (fftw does not normalize)
for (size_t i = 0; i < fftw_results; ++i) {
freq_magnitudes[i] = sqrt(
fftw_output[i][0]*fftw_output[i][0]
+
fftw_output[i][1]*fftw_output[i][1]
) / (DFT_NONZERO_SIZE);
}
// Skip magnitudes not in log space
size_t cur_bin = 0;
while (cur_bin < fftw_results && Bin2Hz(cur_bin) < logspace[0]) {
++cur_bin;
}
partial_output.clear();
// Accumulate magnitudes into bins
for (size_t x = 0; x < width; x++) {
double bar_height = 0;
size_t count = 0;
// Check right bound
while (cur_bin < fftw_results && Bin2Hz(cur_bin) < logspace[x]) {
// Check left bound if not first index
if (x == 0 || Bin2Hz(cur_bin) >= logspace[x-1]) {
bar_height += freq_magnitudes[cur_bin];
++count;
}
++cur_bin;
}
// If bin is empty, we're done.
// Don't add this bin to partial_output,
// it will be interpolated later.
if (count == 0) {
continue;
}
// average bins
bar_height /= count;
// log scale heights
bar_height = (20 * log10(bar_height) + DYNAMIC_RANGE + GAIN) / DYNAMIC_RANGE;
// Scale bar height between 0 and height
bar_height = bar_height > 0 ? bar_height * (height-1) : 0;
bar_height = bar_height >= height ? (height-1) : bar_height;
// use emplace_back to prevent redundant copy operations.
// faster than push_back, in this case.
partial_output.emplace_back(x, bar_height);
}
size_t bar_idx = 0;
for (size_t x = 0; x < width; x++) {
const size_t bar_x = partial_output[bar_idx].first;
const double bar_height = partial_output[bar_idx].second;
if (x == bar_x) {
// This data point exists, add it to output array.
output[x] = (size_t) bar_height;
if (bar_idx < partial_output.size() - 1) {
bar_idx++;
}
} else {
// data point does not exist, we need to interpolate
// This check shouldn't be necessary, but sometimes
// Interpolate throws out a negative value.
//
// figure out why!
double i = Interpolate(x, bar_idx);
output[x] = (size_t) (i > 0 ? i : 0);
}
}
}

View File

@ -0,0 +1,121 @@
#pragma once
#include <stdio.h>
#include <cstdint>
#include <vector>
#include <math.h>
#include <cstring> // memset
#include <fftw3.h>
#include "utility/buffer.hpp"
class FFT_Visualizer {
public:
FFT_Visualizer(
size_t width,
size_t height,
// Advanced options you probably shouldn't touch
double HZ_MIN = 20,
double HZ_MAX = 20000,
uint32_t DFT_TOTAL_SIZE = 1 << 15,
// #define conf_spectrum_dft_size 2 (between 1 and 5, inclusive)
uint32_t DFT_NONZERO_SIZE = (2048 * (2*2 + 4)),
double GAIN = 10
);
~FFT_Visualizer();
void update(
const Buffer& buf
);
size_t compute_buffer_output_size() const;
const std::vector<size_t>& get_output() {
return output;
};
private:
///
// Visualizer parameters
///
// Horizontal resolution
const size_t width;
// Vertical resolution
const size_t height;
// Leftmost frequency in spectrum
const double HZ_MIN;
// Rightmost frequency in spectrum
const double HZ_MAX;
// Not sure what this does
const uint32_t DFT_TOTAL_SIZE;
// Not sure what this does, either
const uint32_t DFT_NONZERO_SIZE;
// Visualization spectrum gain
// tune if bars are too small
const double GAIN;
// Not sure what this does
const double DYNAMIC_RANGE;
///
// FFTW resources
///
fftw_plan plan;
// How many output values fftw will give us
size_t fftw_results;
// Input array. This is filled with buffer data
// that has been filtered through a window.
double* fftw_input;
// FFT output array.
fftw_complex *fftw_output;
///
// Intermediate values
///
// The magnitudes of the complex values fftw returns.
std::vector<double> freq_magnitudes;
// An array of frequency bins, scaled logarithmically.
std::vector<double> logspace;
// Nearly-complete output.
// This is necessary because some frequency bins
// may be empty, and need to be interpolated
std::vector<
std::pair<
size_t,
double
>
> partial_output;
// Output vector, with empty bins interpolated.
// The maximum possible value of an element in this
// vector is `height`.
std::vector<size_t> output;
///
// Helper methods
// See definition for docs.
///
void GenLogspace();
double Bin2Hz(size_t bin) const;
void ApplyWindow(
double *output,
const int16_t *input,
ssize_t samples
) const;
double Interpolate(
size_t x,
size_t h_idx
);
};

View File

@ -4,8 +4,8 @@
#include <cstdint>
#include <vector>
#include "bitmap/bitmap.hpp"
#include "buffer/buffer.hpp"
#include "utility/bitmap.hpp"
#include "utility/buffer.hpp"
void waveform_generate(

View File

@ -1,12 +1,16 @@
#include "bitmap.hpp"
// Simple bitmap library.
// Used to debug music visualizer.
Bitmap::Bitmap(size_t w, size_t h) {
this->width = w;
this->height = h;
this->data.reserve(h);
this->data.resize(h);
for (size_t r = 0; r < h; r++) {
data[r].reserve(w);
data[r].resize(w);
}
clear();
@ -67,7 +71,9 @@ void Bitmap::setpixel(
}
// TODO: format filename
//char f[100];
//snprintf(f, 100, "/tmp/%i.bmp", c);
void Bitmap::save(const char *filename) const {
uint8_t header[54] = {

28
src/utility/misc.cpp Normal file
View File

@ -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;
}
}

6
src/utility/misc.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <stdio.h>
namespace misc {
size_t to_narrow(const wchar_t * src, char * dest, size_t dest_len);
}