diff options
Diffstat (limited to 'hid')
-rw-r--r-- | hid/Makefile | 2 | ||||
-rw-r--r-- | hid/platformio.ini | 21 | ||||
-rw-r--r-- | hid/src/main.cpp | 57 | ||||
-rw-r--r-- | hid/src/ps2/hid.h | 4 | ||||
-rw-r--r-- | hid/src/usb/hid.h | 49 |
5 files changed, 85 insertions, 48 deletions
diff --git a/hid/Makefile b/hid/Makefile index b82d2673..0d07db84 100644 --- a/hid/Makefile +++ b/hid/Makefile @@ -2,6 +2,8 @@ usb: make _build E=usb ps2: make _build E=ps2 +mixed: + make _build E=mixed _build: rm -f .current platformio run --environment $(E) diff --git a/hid/platformio.ini b/hid/platformio.ini index b5319230..c6c7c34b 100644 --- a/hid/platformio.ini +++ b/hid/platformio.ini @@ -24,7 +24,8 @@ lib_deps = build_flags = ${common.build_flags} - -DHID_USB + -DHID_USB_KBD + -DHID_USB_MOUSE extra_scripts = post:patch.py [env:ps2] @@ -37,6 +38,22 @@ lib_deps = git+https://github.com/Harvie/ps2dev#v0.0.3 build_flags = ${common.build_flags} - -DHID_PS2 + -DHID_PS2_KBD + -DPS2_KBD_CLOCK_PIN=7 + -DPS2_KBD_DATA_PIN=5 + +[env:mixed] +platform = atmelavr +board = micro +framework = arduino +upload_port = /dev/ttyACM0 +lib_deps = + ${common.lib_deps} + git+https://github.com/Harvie/ps2dev#v0.0.3 +build_flags = + ${common.build_flags} + -DHID_PS2_KBD + -DHID_USB_MOUSE -DPS2_KBD_CLOCK_PIN=7 -DPS2_KBD_DATA_PIN=5 diff --git a/hid/src/main.cpp b/hid/src/main.cpp index 1e9f9602..08ec3f65 100644 --- a/hid/src/main.cpp +++ b/hid/src/main.cpp @@ -24,12 +24,12 @@ #include <TimerOne.h> #include "inline.h" -#if defined(HID_USB) + +#if defined(HID_USB_KBD) || defined(HID_USB_MOUSE) # include "usb/hid.h" -#elif defined(HID_PS2) +#endif +#ifdef HID_PS2_KBD # include "ps2/hid.h" -#else -# error HID type is not selected #endif @@ -68,33 +68,37 @@ // ----------------------------------------------------------------------------- -#if defined(HID_USB) - UsbHid hid; -#elif defined(HID_PS2) - Ps2Hid hid; -#else -# error HID type is not selected +#ifdef HID_USB_KBD + UsbHidKeyboard hid_kbd; +#elif defined(HID_PS2_KBD) + Ps2HidKeyboard hid_kbd; +#endif +#ifdef HID_USB_MOUSE + UsbHidMouse hid_mouse; #endif // ----------------------------------------------------------------------------- INLINE uint8_t cmdResetHid(const uint8_t *buffer) { // 0 bytes -# ifdef HID_USB - hid.reset(); +# ifdef HID_USB_KBD + hid_kbd.reset(); +# endif +# ifdef HID_USB_MOUSE + hid_mouse.reset(); # endif return PROTO_RESP_OK; } INLINE uint8_t cmdKeyEvent(const uint8_t *buffer) { // 2 bytes - hid.sendKey(buffer[0], buffer[1]); + hid_kbd.sendKey(buffer[0], buffer[1]); return PROTO_RESP_OK; } INLINE uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 1 byte -# ifdef HID_USB +# ifdef HID_USB_MOUSE uint8_t state = buffer[0]; - hid.sendMouseButtons( + hid_mouse.sendMouseButtons( state & PROTO_CMD_MOUSE_BUTTON_LEFT_SELECT, state & PROTO_CMD_MOUSE_BUTTON_LEFT_STATE, state & PROTO_CMD_MOUSE_BUTTON_RIGHT_SELECT, state & PROTO_CMD_MOUSE_BUTTON_RIGHT_STATE, state & PROTO_CMD_MOUSE_BUTTON_MIDDLE_SELECT, state & PROTO_CMD_MOUSE_BUTTON_MIDDLE_STATE @@ -104,7 +108,7 @@ INLINE uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 1 byte } INLINE uint8_t cmdMouseMoveEvent(const uint8_t *buffer) { // 4 bytes -# ifdef HID_USB +# ifdef HID_USB_MOUSE int x = (int)buffer[0] << 8; x |= (int)buffer[1]; x = (x + 32768) / 2; // See /kvmd/apps/otg/hid/keyboard.py for details @@ -113,20 +117,20 @@ INLINE uint8_t cmdMouseMoveEvent(const uint8_t *buffer) { // 4 bytes y |= (int)buffer[3]; y = (y + 32768) / 2; // See /kvmd/apps/otg/hid/keyboard.py for details - hid.sendMouseMove(x, y); + hid_mouse.sendMouseMove(x, y); # endif return PROTO_RESP_OK; } INLINE uint8_t cmdMouseWheelEvent(const uint8_t *buffer) { // 2 bytes -# ifdef HID_USB - hid.sendMouseWheel(buffer[1]); // Y only, X is not supported +# ifdef HID_USB_MOUSE + hid_mouse.sendMouseWheel(buffer[1]); // Y only, X is not supported # endif return PROTO_RESP_OK; } INLINE uint8_t cmdPongLeds(const uint8_t *buffer) { // 0 bytes - return ((uint8_t) PROTO_RESP_PONG_PREFIX) | hid.getLedsAs( + return ((uint8_t) PROTO_RESP_PONG_PREFIX) | hid_kbd.getLedsAs( PROTO_RESP_PONG_CAPS, PROTO_RESP_PONG_SCROLL, PROTO_RESP_PONG_NUM @@ -190,7 +194,10 @@ void intRecvTimedOut() { } void setup() { - hid.begin(); + hid_kbd.begin(); +# ifdef HID_USB_MOUSE + hid_mouse.begin(); +# endif Timer1.attachInterrupt(intRecvTimedOut); CMD_SERIAL.begin(CMD_SERIAL_SPEED); @@ -201,8 +208,8 @@ void loop() { unsigned index = 0; while (true) { -# ifdef HID_PS2 - hid.periodic(); +# ifdef HID_PS2_KBD + hid_kbd.periodic(); # endif if (CMD_SERIAL.available() > 0) { @@ -212,7 +219,7 @@ void loop() { crc |= (uint16_t)buffer[7]; if (makeCrc16(buffer, 6) == crc) { -# define HANDLE(_handler) { sendCmdResponse(_handler(buffer + 2)); break; } +# define HANDLE(_handler) { sendCmdResponse(_handler(buffer + 2)); break; } switch (buffer[1]) { case PROTO_CMD_RESET_HID: HANDLE(cmdResetHid); case PROTO_CMD_KEY_EVENT: HANDLE(cmdKeyEvent); @@ -223,7 +230,7 @@ void loop() { case PROTO_CMD_REPEAT: sendCmdResponse(); break; default: sendCmdResponse(PROTO_RESP_INVALID_ERROR); break; } -# undef HANDLE +# undef HANDLE } else { sendCmdResponse(PROTO_RESP_CRC_ERROR); } diff --git a/hid/src/ps2/hid.h b/hid/src/ps2/hid.h index d7057c03..d12a16e4 100644 --- a/hid/src/ps2/hid.h +++ b/hid/src/ps2/hid.h @@ -33,11 +33,11 @@ // #define PS2_KBD_DATA_PIN 5 -class Ps2Hid { +class Ps2HidKeyboard { // https://wiki.osdev.org/PS/2_Keyboard public: - Ps2Hid() : _dev(PS2_KBD_CLOCK_PIN, PS2_KBD_DATA_PIN) {} + Ps2HidKeyboard() : _dev(PS2_KBD_CLOCK_PIN, PS2_KBD_DATA_PIN) {} void begin() { _dev.keyboard_init(); diff --git a/hid/src/usb/hid.h b/hid/src/usb/hid.h index 0fedc3ee..48e99d28 100644 --- a/hid/src/usb/hid.h +++ b/hid/src/usb/hid.h @@ -30,18 +30,16 @@ // ----------------------------------------------------------------------------- -class UsbHid { +class UsbHidKeyboard { public: - UsbHid() {} + UsbHidKeyboard() {} void begin() { BootKeyboard.begin(); - SingleAbsoluteMouse.begin(); } - void reset() { + INLINE void reset() { BootKeyboard.releaseAll(); - SingleAbsoluteMouse.releaseAll(); } INLINE void sendKey(uint8_t code, bool state) { @@ -52,14 +50,37 @@ class UsbHid { } } + INLINE uint8_t getLedsAs(uint8_t caps, uint8_t scroll, uint8_t num) { + uint8_t leds = BootKeyboard.getLeds(); + uint8_t result = 0; + + if (leds & LED_CAPS_LOCK) result |= caps; + if (leds & LED_SCROLL_LOCK) result |= scroll; + if (leds & LED_NUM_LOCK) result |= num; + return result; + } +}; + +class UsbHidMouse { + public: + UsbHidMouse() {} + + void begin() { + SingleAbsoluteMouse.begin(); + } + + INLINE void reset() { + SingleAbsoluteMouse.releaseAll(); + } + INLINE void sendMouseButtons( bool left_select, bool left_state, bool right_select, bool right_state, bool middle_select, bool middle_state ) { - if (left_select) sendMouseButton(MOUSE_LEFT, left_state); - if (right_select) sendMouseButton(MOUSE_RIGHT, right_state); - if (middle_select) sendMouseButton(MOUSE_MIDDLE, middle_state); + if (left_select) _sendMouseButton(MOUSE_LEFT, left_state); + if (right_select) _sendMouseButton(MOUSE_RIGHT, right_state); + if (middle_select) _sendMouseButton(MOUSE_MIDDLE, middle_state); } INLINE void sendMouseMove(int x, int y) { @@ -71,18 +92,8 @@ class UsbHid { SingleAbsoluteMouse.move(0, 0, delta_y); } - INLINE uint8_t getLedsAs(uint8_t caps, uint8_t scroll, uint8_t num) { - uint8_t leds = BootKeyboard.getLeds(); - uint8_t result = 0; - - if (leds & LED_CAPS_LOCK) result |= caps; - if (leds & LED_SCROLL_LOCK) result |= scroll; - if (leds & LED_NUM_LOCK) result |= num; - return result; - } - private: - INLINE void sendMouseButton(uint8_t button, bool state) { + INLINE void _sendMouseButton(uint8_t button, bool state) { if (state) SingleAbsoluteMouse.press(button); else SingleAbsoluteMouse.release(button); } |