mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 09:01:54 +08:00
check usb endpoint
This commit is contained in:
@@ -22,12 +22,29 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <HID-Project.h>
|
||||
|
||||
#include "keymap.h"
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
#ifdef CHECK_ENDPOINT
|
||||
static bool _checkEndpoint(uint8_t ep) {
|
||||
// https://github.com/arduino/ArduinoCore-avr/blob/2f67c916f6ab6193c404eebe22efe901e0f9542d/cores/arduino/USBCore.cpp#L249
|
||||
// https://sourceforge.net/p/arduinomidilib/svn/41/tree/branch/3.1/Teensy/teensy_core/usb_midi/usb_api.cpp#l103
|
||||
uint8_t intr_state = SREG;
|
||||
cli();
|
||||
UENUM = ep & 7;
|
||||
bool rw_allowed = UEINTX & (1 << RWAL);
|
||||
SREG = intr_state;
|
||||
return rw_allowed;
|
||||
}
|
||||
# define CHECK_HID_EP(_hid) { if (!_checkEndpoint(_hid.getPluggedEndpoint())) return; }
|
||||
#else
|
||||
# define CHECK_HID_EP(_hid)
|
||||
#endif
|
||||
|
||||
class UsbHidKeyboard {
|
||||
public:
|
||||
UsbHidKeyboard() {}
|
||||
@@ -41,6 +58,7 @@ class UsbHidKeyboard {
|
||||
}
|
||||
|
||||
void sendKey(uint8_t code, bool state) {
|
||||
CHECK_HID_EP(BootKeyboard);
|
||||
KeyboardKeycode usb_code = keymapUsb(code);
|
||||
if (usb_code != KEY_ERROR_UNDEFINED) {
|
||||
if (state) BootKeyboard.press(usb_code);
|
||||
@@ -86,17 +104,22 @@ class UsbHidMouse {
|
||||
}
|
||||
|
||||
void sendMove(int x, int y) {
|
||||
CHECK_HID_EP(SingleAbsoluteMouse);
|
||||
SingleAbsoluteMouse.moveTo(x, y);
|
||||
}
|
||||
|
||||
void sendWheel(int delta_y) {
|
||||
CHECK_HID_EP(SingleAbsoluteMouse);
|
||||
// delta_x is not supported by hid-project now
|
||||
SingleAbsoluteMouse.move(0, 0, delta_y);
|
||||
}
|
||||
|
||||
private:
|
||||
void _sendButton(uint8_t button, bool state) {
|
||||
CHECK_HID_EP(SingleAbsoluteMouse);
|
||||
if (state) SingleAbsoluteMouse.press(button);
|
||||
else SingleAbsoluteMouse.release(button);
|
||||
}
|
||||
};
|
||||
|
||||
#undef CHECK_HID_EP
|
||||
|
||||
Reference in New Issue
Block a user