mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2025-12-12 17:20:30 +08:00
changed hid protocol, added mouse support for hid
This commit is contained in:
parent
f83b4f674a
commit
9c5a8d122c
@ -4,22 +4,30 @@
|
||||
#include "inline.h"
|
||||
#include "keymap.h"
|
||||
|
||||
#define CMD_SERIAL Serial1
|
||||
#define CMD_SERIAL Serial
|
||||
#define CMD_SERIAL_SPEED 115200
|
||||
|
||||
#define INLINE inline __attribute__((always_inline))
|
||||
#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() {
|
||||
CMD_SERIAL.read(); // unused now
|
||||
CMD_SERIAL.read(); // unused now
|
||||
CMD_SERIAL.read(); // unused now
|
||||
// -----------------------------------------------------------------------------
|
||||
INLINE void cmdResetHid() { // 0 bytes
|
||||
CMD_SERIAL.read(); // unused
|
||||
CMD_SERIAL.read(); // unused
|
||||
CMD_SERIAL.read(); // unused
|
||||
CMD_SERIAL.read(); // unused
|
||||
Keyboard.releaseAll();
|
||||
AbsoluteMouse.releaseAll();
|
||||
}
|
||||
|
||||
INLINE void cmdKeyEvent() {
|
||||
uint8_t state = CMD_SERIAL.read();
|
||||
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) {
|
||||
Keyboard.press(code);
|
||||
@ -29,17 +37,61 @@ INLINE void cmdKeyEvent() {
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
AbsoluteMouse.moveTo(0, 0);
|
||||
AbsoluteMouse.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) {
|
||||
AbsoluteMouse.press(MOUSE_LEFT);
|
||||
} else {
|
||||
AbsoluteMouse.release(MOUSE_LEFT);
|
||||
}
|
||||
}
|
||||
if (state & CMD_MOUSE_RIGHT) {
|
||||
if (state & CMD_MOUSE_RIGHT_STATE) {
|
||||
AbsoluteMouse.press(MOUSE_RIGHT);
|
||||
} else {
|
||||
AbsoluteMouse.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
|
||||
AbsoluteMouse.move(0, 0, delta_y);
|
||||
CMD_SERIAL.println(delta_y);
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
void setup() {
|
||||
CMD_SERIAL.begin(CMD_SERIAL_SPEED);
|
||||
Keyboard.begin();
|
||||
AbsoluteMouse.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (CMD_SERIAL.available() >= 4) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,10 +116,10 @@ class Hid(multiprocessing.Process):
|
||||
assert len(key_bytes) == 1, (event, key_bytes)
|
||||
tty.write(
|
||||
b"\01"
|
||||
+ (b"\01" if event.state else b"\00")
|
||||
+ key_bytes
|
||||
+ b"\00"
|
||||
+ (b"\01" if event.state else b"\00")
|
||||
+ b"\00\00"
|
||||
)
|
||||
|
||||
def __send_clear_hid(self, tty: serial.Serial) -> None:
|
||||
tty.write(b"\00\00\00\00")
|
||||
tty.write(b"\00\00\00\00\00")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user