diff options
author | Devaev Maxim <[email protected]> | 2018-09-26 02:20:58 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2018-09-26 02:20:58 +0300 |
commit | 089bac2bcd1e49ba77aacd6853ef93e03a051b96 (patch) | |
tree | 0d3082726e8bef2c39ec66cb83bf05a54e945ffe /kvmd | |
parent | 940989b6e9a3fe31bd236e4bed96e1d4778a403b (diff) |
moved hid to kvmd
Diffstat (limited to 'kvmd')
-rw-r--r-- | kvmd/Makefile | 6 | ||||
-rwxr-xr-x | kvmd/genmap.py | 36 | ||||
-rw-r--r-- | kvmd/hid/.gitignore | 2 | ||||
-rw-r--r-- | kvmd/hid/Makefile | 18 | ||||
-rw-r--r-- | kvmd/hid/platformio.ini | 19 | ||||
-rw-r--r-- | kvmd/hid/src/inline.h | 3 | ||||
-rw-r--r-- | kvmd/hid/src/keymap.h | 98 | ||||
-rw-r--r-- | kvmd/hid/src/main.cpp | 98 | ||||
-rw-r--r-- | kvmd/keymap.in | 109 | ||||
-rw-r--r-- | kvmd/tox.ini | 8 |
10 files changed, 393 insertions, 4 deletions
diff --git a/kvmd/Makefile b/kvmd/Makefile index aa654735..db5c4429 100644 --- a/kvmd/Makefile +++ b/kvmd/Makefile @@ -40,6 +40,10 @@ shell: make run TESTENV_CMD=/bin/bash +regen: + python3 genmap.py + + release: make clean make tox @@ -66,7 +70,9 @@ push: clean: rm -rf build site dist pkg src *.egg-info kvmd-*.tar.gz find -name __pycache__ | xargs rm -rf + make -C hid clean clean-all: clean rm -rf .tox .mypy_cache + make -C hid clean-all diff --git a/kvmd/genmap.py b/kvmd/genmap.py new file mode 100755 index 00000000..f01d6077 --- /dev/null +++ b/kvmd/genmap.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 + + +import operator + +from typing import Tuple +from typing import List + +import yaml + + +# ===== +def main() -> None: + keymap: List[Tuple[int, str, str]] = [] + with open("keymap.in") as keymap_file: + for row in keymap_file: + if not row.startswith("#"): + parts = row.split() + keymap.append((int(parts[0]), parts[1], parts[2])) + + with open("kvmd/data/keymap.yaml", "w") as kvmd_yaml_file: + yaml.dump({ + js_key: code + for (code, _, js_key) in sorted(keymap, key=operator.itemgetter(2)) + }, kvmd_yaml_file, indent=4, default_flow_style=False) + + with open("hid/src/keymap.h", "w") as hid_header_file: + hid_header_file.write("#pragma once\n\n#include <HID-Project.h>\n\n#include \"inline.h\"\n\n\n") + hid_header_file.write("INLINE KeyboardKeycode keymap(uint8_t code) {\n\tswitch(code) {\n") + for (code, hid_key, _) in sorted(keymap, key=operator.itemgetter(1)): + hid_header_file.write("\t\tcase %d: return %s;\n" % (code, hid_key)) + hid_header_file.write("\t\tdefault: return KEY_ERROR_UNDEFINED;\n\t}\n}\n") + + +if __name__ == "__main__": + main() diff --git a/kvmd/hid/.gitignore b/kvmd/hid/.gitignore new file mode 100644 index 00000000..c6d216a4 --- /dev/null +++ b/kvmd/hid/.gitignore @@ -0,0 +1,2 @@ +/.pioenvs/ +/.piolibdeps/ diff --git a/kvmd/hid/Makefile b/kvmd/hid/Makefile new file mode 100644 index 00000000..0d94b010 --- /dev/null +++ b/kvmd/hid/Makefile @@ -0,0 +1,18 @@ +all: + @ cat Makefile + +build: + platformio run + +update: + platformio platform update + +install: upload +upload: + platformio run --target upload + +serial: + platformio serialports monitor + +clean: + rm -rf .pioenvs .piolibdeps diff --git a/kvmd/hid/platformio.ini b/kvmd/hid/platformio.ini new file mode 100644 index 00000000..13036b9d --- /dev/null +++ b/kvmd/hid/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + +[env:micro] +platform = atmelavr +board = micro +framework = arduino +upload_port = /dev/ttyACM0 +monitor_speed = 115200 + +lib_deps = diff --git a/kvmd/hid/src/inline.h b/kvmd/hid/src/inline.h new file mode 100644 index 00000000..5b642a83 --- /dev/null +++ b/kvmd/hid/src/inline.h @@ -0,0 +1,3 @@ +#pragma once + +#define INLINE inline __attribute__((always_inline)) diff --git a/kvmd/hid/src/keymap.h b/kvmd/hid/src/keymap.h new file mode 100644 index 00000000..c2302601 --- /dev/null +++ b/kvmd/hid/src/keymap.h @@ -0,0 +1,98 @@ +#pragma once + +#include <HID-Project.h> + +#include "inline.h" + + +INLINE KeyboardKeycode keymap(uint8_t code) { + switch(code) { + case 36: return KEY_0; + case 27: return KEY_1; + case 28: return KEY_2; + case 29: return KEY_3; + case 30: return KEY_4; + case 31: return KEY_5; + case 32: return KEY_6; + case 33: return KEY_7; + case 34: return KEY_8; + case 35: return KEY_9; + case 1: return KEY_A; + case 2: return KEY_B; + case 46: return KEY_BACKSLASH; + case 39: return KEY_BACKSPACE; + case 3: return KEY_C; + case 53: return KEY_CAPS_LOCK; + case 50: return KEY_COMMA; + case 4: return KEY_D; + case 70: return KEY_DELETE; + case 75: return KEY_DOWN_ARROW; + case 5: return KEY_E; + case 71: return KEY_END; + case 37: return KEY_ENTER; + case 43: return KEY_EQUAL; + case 38: return KEY_ESC; + case 6: return KEY_F; + case 54: return KEY_F1; + case 63: return KEY_F10; + case 64: return KEY_F11; + case 65: return KEY_F12; + case 55: return KEY_F2; + case 56: return KEY_F3; + case 57: return KEY_F4; + case 58: return KEY_F5; + case 59: return KEY_F6; + case 60: return KEY_F7; + case 61: return KEY_F8; + case 62: return KEY_F9; + case 7: return KEY_G; + case 8: return KEY_H; + case 68: return KEY_HOME; + case 9: return KEY_I; + case 67: return KEY_INSERT; + case 10: return KEY_J; + case 11: return KEY_K; + case 12: return KEY_L; + case 79: return KEY_LEFT_ALT; + case 74: return KEY_LEFT_ARROW; + case 44: return KEY_LEFT_BRACE; + case 77: return KEY_LEFT_CTRL; + case 80: return KEY_LEFT_GUI; + case 78: return KEY_LEFT_SHIFT; + case 13: return KEY_M; + case 42: return KEY_MINUS; + case 14: return KEY_N; + case 15: return KEY_O; + case 16: return KEY_P; + case 72: return KEY_PAGE_DOWN; + case 69: return KEY_PAGE_UP; + case 85: return KEY_PAUSE; + case 51: return KEY_PERIOD; + case 66: return KEY_PRINT; + case 17: return KEY_Q; + case 48: return KEY_QUOTE; + case 18: return KEY_R; + case 83: return KEY_RIGHT_ALT; + case 73: return KEY_RIGHT_ARROW; + case 45: return KEY_RIGHT_BRACE; + case 81: return KEY_RIGHT_CTRL; + case 84: return KEY_RIGHT_GUI; + case 82: return KEY_RIGHT_SHIFT; + case 19: return KEY_S; + case 86: return KEY_SCROLL_LOCK; + case 47: return KEY_SEMICOLON; + case 52: return KEY_SLASH; + case 41: return KEY_SPACE; + case 20: return KEY_T; + case 40: return KEY_TAB; + case 49: return KEY_TILDE; + case 21: return KEY_U; + case 76: return KEY_UP_ARROW; + case 22: return KEY_V; + case 23: return KEY_W; + case 24: return KEY_X; + case 25: return KEY_Y; + case 26: return KEY_Z; + default: return KEY_ERROR_UNDEFINED; + } +} diff --git a/kvmd/hid/src/main.cpp b/kvmd/hid/src/main.cpp new file mode 100644 index 00000000..7bfced13 --- /dev/null +++ b/kvmd/hid/src/main.cpp @@ -0,0 +1,98 @@ +#include <Arduino.h> +#include <HID-Project.h> + +#include "inline.h" +#include "keymap.h" + +#define CMD_SERIAL Serial1 +#define CMD_SERIAL_SPEED 115200 + +#define CMD_MOUSE_LEFT 0b10000000 +#define CMD_MOUSE_LEFT_STATE 0b00001000 +#define CMD_MOUSE_RIGHT 0b01000000 +#define CMD_MOUSE_RIGHT_STATE 0b00000100 + + +// ----------------------------------------------------------------------------- +INLINE void cmdResetHid() { // 0 bytes + CMD_SERIAL.read(); // unused + CMD_SERIAL.read(); // unused + CMD_SERIAL.read(); // unused + CMD_SERIAL.read(); // unused + BootKeyboard.releaseAll(); + SingleAbsoluteMouse.releaseAll(); +} + +INLINE void cmdKeyEvent() { // 2 bytes + KeyboardKeycode code = keymap((uint8_t)CMD_SERIAL.read()); + uint8_t state = CMD_SERIAL.read(); + CMD_SERIAL.read(); // unused + CMD_SERIAL.read(); // unused + if (code != KEY_ERROR_UNDEFINED) { + if (state) { + BootKeyboard.press(code); + } else { + BootKeyboard.release(code); + } + } +} + +INLINE void cmdMouseMoveEvent() { // 4 bytes + int x = (int)CMD_SERIAL.read() << 8; + x |= (int)CMD_SERIAL.read(); + int y = (int)CMD_SERIAL.read() << 8; + y |= (int)CMD_SERIAL.read(); + SingleAbsoluteMouse.moveTo(x, y); +} + +INLINE void cmdMouseButtonEvent() { // 1 byte + uint8_t state = CMD_SERIAL.read(); + CMD_SERIAL.read(); // unused + CMD_SERIAL.read(); // unused + CMD_SERIAL.read(); // unused + if (state & CMD_MOUSE_LEFT) { + if (state & CMD_MOUSE_LEFT_STATE) { + SingleAbsoluteMouse.press(MOUSE_LEFT); + } else { + SingleAbsoluteMouse.release(MOUSE_LEFT); + } + } + if (state & CMD_MOUSE_RIGHT) { + if (state & CMD_MOUSE_RIGHT_STATE) { + SingleAbsoluteMouse.press(MOUSE_RIGHT); + } else { + SingleAbsoluteMouse.release(MOUSE_RIGHT); + } + } +} + +INLINE void cmdMouseWheelEvent() { // 2 bytes + CMD_SERIAL.read(); // delta_x is not supported by hid-project now + signed char delta_y = CMD_SERIAL.read(); + CMD_SERIAL.read(); // unused + CMD_SERIAL.read(); // unused + SingleAbsoluteMouse.move(0, 0, delta_y); +} + + +// ----------------------------------------------------------------------------- +void setup() { + CMD_SERIAL.begin(CMD_SERIAL_SPEED); + BootKeyboard.begin(); + SingleAbsoluteMouse.begin(); + CMD_SERIAL.write(0); +} + +void loop() { + if (CMD_SERIAL.available() >= 5) { + switch ((uint8_t)CMD_SERIAL.read()) { + case 0: cmdResetHid(); break; + case 1: cmdKeyEvent(); break; + case 2: cmdMouseMoveEvent(); break; + case 3: cmdMouseButtonEvent(); break; + case 4: cmdMouseWheelEvent(); break; + default: break; + } + CMD_SERIAL.write(0); + } +} diff --git a/kvmd/keymap.in b/kvmd/keymap.in new file mode 100644 index 00000000..da9ddf25 --- /dev/null +++ b/kvmd/keymap.in @@ -0,0 +1,109 @@ +# https://github.com/NicoHood/HID/blob/master/src/HID-APIs/ImprovedKeylayouts.h +# https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code +1 KEY_A KeyA +2 KEY_B KeyB +3 KEY_C KeyC +4 KEY_D KeyD +5 KEY_E KeyE +6 KEY_F KeyF +7 KEY_G KeyG +8 KEY_H KeyH +9 KEY_I KeyI +10 KEY_J KeyJ +11 KEY_K KeyK +12 KEY_L KeyL +13 KEY_M KeyM +14 KEY_N KeyN +15 KEY_O KeyO +16 KEY_P KeyP +17 KEY_Q KeyQ +18 KEY_R KeyR +19 KEY_S KeyS +20 KEY_T KeyT +21 KEY_U KeyU +22 KEY_V KeyV +23 KEY_W KeyW +24 KEY_X KeyX +25 KEY_Y KeyY +26 KEY_Z KeyZ +27 KEY_1 Digit1 +28 KEY_2 Digit2 +29 KEY_3 Digit3 +30 KEY_4 Digit4 +31 KEY_5 Digit5 +32 KEY_6 Digit6 +33 KEY_7 Digit7 +34 KEY_8 Digit8 +35 KEY_9 Digit9 +36 KEY_0 Digit0 +37 KEY_ENTER Enter +38 KEY_ESC Escape +39 KEY_BACKSPACE Backspace +40 KEY_TAB Tab +41 KEY_SPACE Space +42 KEY_MINUS Minus +43 KEY_EQUAL Equal +44 KEY_LEFT_BRACE BracketLeft +45 KEY_RIGHT_BRACE BracketRight +46 KEY_BACKSLASH Backslash +47 KEY_SEMICOLON Semicolon +48 KEY_QUOTE Quote +49 KEY_TILDE Backquote +50 KEY_COMMA Comma +51 KEY_PERIOD Period +52 KEY_SLASH Slash +53 KEY_CAPS_LOCK CapsLock +54 KEY_F1 F1 +55 KEY_F2 F2 +56 KEY_F3 F3 +57 KEY_F4 F4 +58 KEY_F5 F5 +59 KEY_F6 F6 +60 KEY_F7 F7 +61 KEY_F8 F8 +62 KEY_F9 F9 +63 KEY_F10 F10 +64 KEY_F11 F11 +65 KEY_F12 F12 +66 KEY_PRINT PrintScreen +67 KEY_INSERT Insert +68 KEY_HOME Home +69 KEY_PAGE_UP PageUp +70 KEY_DELETE Delete +71 KEY_END End +72 KEY_PAGE_DOWN PageDown +73 KEY_RIGHT_ARROW ArrowRight +74 KEY_LEFT_ARROW ArrowLeft +75 KEY_DOWN_ARROW ArrowDown +76 KEY_UP_ARROW ArrowUp +77 KEY_LEFT_CTRL ControlLeft +78 KEY_LEFT_SHIFT ShiftLeft +79 KEY_LEFT_ALT AltLeft +80 KEY_LEFT_GUI MetaLeft +81 KEY_RIGHT_CTRL ControlRight +82 KEY_RIGHT_SHIFT ShiftRight +83 KEY_RIGHT_ALT AltRight +84 KEY_RIGHT_GUI MetaRight +85 KEY_PAUSE Pause +86 KEY_SCROLL_LOCK ScrollLock +# KEY_NON_US_NUM +# KEY_NUM_LOCK +# KEYPAD_DIVIDE +# KEYPAD_MULTIPLY +# KEYPAD_SUBTRACT +# KEYPAD_ADD +# KEYPAD_ENTER +# KEYPAD_1 +# KEYPAD_2 +# KEYPAD_3 +# KEYPAD_4 +# KEYPAD_5 +# KEYPAD_6 +# KEYPAD_7 +# KEYPAD_8 +# KEYPAD_9 +# KEYPAD_0 +# KEYPAD_DOT +# KEY_NON_US +# KEY_APPLICATION +# KEY_MENU diff --git a/kvmd/tox.ini b/kvmd/tox.ini index a22805d0..b99660fb 100644 --- a/kvmd/tox.ini +++ b/kvmd/tox.ini @@ -6,26 +6,26 @@ skipsdist = True basepython = python3.7 [testenv:flake8] -commands = flake8 kvmd +commands = flake8 kvmd genmap.py deps = flake8 flake8-double-quotes -rtestenv/requirements.txt [testenv:pylint] -commands = pylint --output-format=colorized --reports=no kvmd +commands = pylint --output-format=colorized --reports=no kvmd genmap.py deps = pylint -rtestenv/requirements.txt [testenv:mypy] -commands = mypy kvmd +commands = mypy kvmd genmap.py deps = mypy -rtestenv/requirements.txt [testenv:vulture] -commands = vulture kvmd vulture-wl.py +commands = vulture kvmd genmap.py vulture-wl.py deps = vulture -rtestenv/requirements.txt |