Compare commits
2 Commits
f450a88e91
...
cbea2cd663
Author | SHA1 | Date |
---|---|---|
Mark | cbea2cd663 | |
Mark | 65b5bb6400 |
File diff suppressed because it is too large
Load Diff
|
@ -27,6 +27,23 @@
|
|||
// 0x02: FFT Animation
|
||||
#define CMD_SEND_STATE 0x02
|
||||
|
||||
// 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
|
||||
|
||||
// Animation data. Sent by host.
|
||||
//
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
#pragma once
|
||||
|
||||
|
||||
// Sleep this many millis after each loop.
|
||||
// Prevents absurd cpu usage.
|
||||
#define LOOP_SLEEP_MS 20
|
||||
|
||||
|
||||
// USB device params
|
||||
#define HID_VENDOR_ID 0x3297
|
||||
#define HID_PRODUCT_ID 0x4976
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
#include "dict.hpp"
|
||||
|
||||
std::unordered_set<std::string> word_dict;
|
||||
|
||||
void load_file() {
|
||||
std::ifstream file("resources/google-10000-english-usa-no-swears.txt");
|
||||
if (file.is_open()) {
|
||||
std::string line;
|
||||
while (std::getline(file, line)) {
|
||||
// using printf() in all tests for consistency
|
||||
//printf("%s", line.c_str());
|
||||
word_dict.insert(line.c_str());
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
#include <unordered_set>
|
||||
#include <string>
|
||||
|
||||
extern std::unordered_set<std::string> word_dict;
|
||||
|
||||
|
||||
void load_file();
|
|
@ -206,10 +206,9 @@ bool Ergodox::read() {
|
|||
// If keyboard sends a state packet, parse it.
|
||||
case CMD_SEND_STATE:
|
||||
if (animation_mode != read_buf[1]) {
|
||||
spdlog::info("Mode set to {0:x}", read_buf[1]);
|
||||
spdlog::info("Mode set to 0x{0:02x}", read_buf[1]);
|
||||
animation_mode = read_buf[1];
|
||||
}
|
||||
|
||||
// Main code should not parse state packets.
|
||||
return false;
|
||||
}
|
||||
|
|
39
src/main.cpp
39
src/main.cpp
|
@ -11,6 +11,8 @@
|
|||
// MPD client
|
||||
#include "mpd/client.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
// Local files
|
||||
#include "utility/bitmap.hpp"
|
||||
#include "utility/buffer.hpp"
|
||||
|
@ -21,6 +23,7 @@
|
|||
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
#include "dict.hpp"
|
||||
|
||||
// TODO:
|
||||
//
|
||||
|
@ -58,6 +61,11 @@ 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:
|
||||
|
@ -119,7 +127,6 @@ int main(int argc, char *argv[]) {
|
|||
buf.update();
|
||||
fft.update(buf);
|
||||
|
||||
hid_buf[0] = CMD_ANIM_DATA_fft;
|
||||
for (size_t i = 0; i < 10; i++) {
|
||||
// Get height from fft, apply bottom_skip
|
||||
ssize_t h = fft.get_output()[i] - BOTTOM_SKIP;
|
||||
|
@ -129,11 +136,11 @@ int main(int argc, char *argv[]) {
|
|||
h = h>KB_RESOLUTION ? KB_RESOLUTION : h;
|
||||
h = h<0 ? 0 : h;
|
||||
|
||||
hid_buf[i + 1] = h;
|
||||
hid_buf[i] = h;
|
||||
}
|
||||
Dox.write(CMD_ANIM_DATA, hid_buf, Dox.packet_size);
|
||||
}
|
||||
|
||||
Dox.write(CMD_ANIM_DATA, hid_buf, Dox.packet_size);
|
||||
|
||||
t = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
@ -148,13 +155,37 @@ int main(int argc, char *argv[]) {
|
|||
uint8_t cmd = Dox.read_buf[0];
|
||||
|
||||
switch(cmd) {
|
||||
case CMD_SEND_STATE:
|
||||
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);
|
||||
|
|
Loading…
Reference in New Issue