using HID-Project library

This commit is contained in:
Devaev Maxim
2018-07-11 05:29:30 +00:00
parent 157828997a
commit c6b6e54875
11 changed files with 375 additions and 54 deletions

View File

@@ -0,0 +1,84 @@
AltLeft: 79
AltRight: 83
ArrowDown: 75
ArrowLeft: 74
ArrowRight: 73
ArrowUp: 76
Backquote: 49
Backslash: 46
Backspace: 39
BracketLeft: 44
BracketRight: 45
CapsLock: 53
Comma: 50
ControlLeft: 77
ControlRight: 81
Delete: 70
Digit0: 36
Digit1: 27
Digit2: 28
Digit3: 29
Digit4: 30
Digit5: 31
Digit6: 32
Digit7: 33
Digit8: 34
Digit9: 35
End: 71
Enter: 37
Equal: 43
Escape: 38
F1: 54
F10: 63
F11: 64
F12: 65
F2: 55
F3: 56
F4: 57
F5: 58
F6: 59
F7: 60
F8: 61
F9: 62
Home: 68
Insert: 67
KeyA: 1
KeyB: 2
KeyC: 3
KeyD: 4
KeyE: 5
KeyF: 6
KeyG: 7
KeyH: 8
KeyI: 9
KeyJ: 10
KeyK: 11
KeyL: 12
KeyM: 13
KeyN: 14
KeyO: 15
KeyP: 16
KeyQ: 17
KeyR: 18
KeyS: 19
KeyT: 20
KeyU: 21
KeyV: 22
KeyW: 23
KeyX: 24
KeyY: 25
KeyZ: 26
MetaLeft: 80
MetaRight: 84
Minus: 42
PageDown: 72
PageUp: 69
Period: 51
PrintScreen: 66
Quote: 48
Semicolon: 47
ShiftLeft: 78
ShiftRight: 82
Slash: 52
Space: 41
Tab: 40

View File

@@ -1,13 +1,14 @@
import re
import asyncio
import multiprocessing
import multiprocessing.queues
import queue
import pkgutil
from typing import Dict
from typing import Set
from typing import NamedTuple
from typing import Union
import yaml
import serial
from .logging import get_logger
@@ -16,50 +17,23 @@ from . import gpio
# =====
def _get_keymap() -> Dict[str, int]:
return yaml.load(pkgutil.get_data(__name__, "data/keymap.yaml").decode()) # type: ignore
_KEYMAP = _get_keymap()
def _keymap(key: str) -> bytes:
code = _KEYMAP.get(key)
return (bytes([code]) if code else b"") # type: ignore
class _KeyEvent(NamedTuple):
key: str
state: bool
def _key_to_bytes(key: str) -> bytes:
# https://www.arduino.cc/reference/en/language/functions/usb/keyboard/
# Also locate Keyboard.h
match = re.match(r"(Digit|Key)([0-9A-Z])", key)
code: Union[str, int, None]
if match:
code = match.group(2)
else:
code = { # type: ignore
"Escape": 0xB1, "Backspace": 0xB2,
"Tab": 0xB3, "Enter": 0xB0,
"Insert": 0xD1, "Delete": 0xD4,
"Home": 0xD2, "End": 0xD5,
"PageUp": 0xD3, "PageDown": 0xD6,
"ArrowLeft": 0xD8, "ArrowRight": 0xD7,
"ArrowUp": 0xDA, "ArrowDown": 0xD9,
"CapsLock": 0xC1,
"ShiftLeft": 0x81, "ShiftRight": 0x85,
"ControlLeft": 0x80, "ControlRight": 0x84,
"AltLeft": 0x82, "AltRight": 0x86,
"MetaLeft": 0x83, "MetaRight": 0x87,
"Backquote": "`", "Minus": "-", "Equal": "=", "Space": " ",
"BracketLeft": "[", "BracketRight": "]", "Semicolon": ";", "Quote": "'",
"Comma": ",", "Period": ".", "Slash": "/", "Backslash": "\\",
"F1": 0xC2, "F2": 0xC3, "F3": 0xC4, "F4": 0xC5,
"F5": 0xC6, "F6": 0xC7, "F7": 0xC8, "F8": 0xC9,
"F9": 0xCA, "F10": 0xCB, "F11": 0xCC, "F12": 0xCD,
}.get(key)
if isinstance(code, str):
return bytes(code, encoding="ascii") # type: ignore
elif isinstance(code, int):
return bytes([code])
return b""
class Hid(multiprocessing.Process):
def __init__(
self,
@@ -138,14 +112,15 @@ class Hid(multiprocessing.Process):
raise
def __send_key_event(self, tty: serial.Serial, event: _KeyEvent) -> None:
key_bytes = _key_to_bytes(event.key)
key_bytes = _keymap(event.key)
if key_bytes:
assert len(key_bytes) == 1, (event, key_bytes)
tty.write(
b"\01"
+ (b"\01" if event.state else b"\00")
+ key_bytes
+ b"\00"
)
def __send_clear_hid(self, tty: serial.Serial) -> None:
tty.write(b"\00")
tty.write(b"\00\00\00\00")

View File

@@ -26,6 +26,10 @@ def main() -> None:
"kvmd.extras.wscli",
],
package_data={
"kvmd": ["data/*.yaml"],
},
entry_points={
"console_scripts": [
"kvmd = kvmd:main",