From 38fae01cc01a0e984f52c004ade79256d8c90e17 Mon Sep 17 00:00:00 2001 From: tomaszduda23 Date: Sun, 10 Jul 2022 04:43:54 +0900 Subject: add keyboard interface (#95) --- hid/src/main.cpp | 61 +++++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 41 deletions(-) (limited to 'hid/src/main.cpp') diff --git a/hid/src/main.cpp b/hid/src/main.cpp index ce2d8da9..168cdc17 100644 --- a/hid/src/main.cpp +++ b/hid/src/main.cpp @@ -49,11 +49,10 @@ // ----------------------------------------------------------------------------- -static UsbKeyboard *_usb_kbd = NULL; static UsbMouseAbsolute *_usb_mouse_abs = NULL; static UsbMouseRelative *_usb_mouse_rel = NULL; -static Ps2Keyboard *_ps2_kbd = NULL; +static DRIVERS::Keyboard *_kbd = nullptr; #ifdef HID_DYNAMIC static bool _reset_required = false; @@ -114,11 +113,12 @@ static void _initOutputs() { uint8_t kbd = outputs & PROTO::OUTPUTS1::KEYBOARD::MASK; switch (kbd) { # ifdef HID_WITH_USB - case PROTO::OUTPUTS1::KEYBOARD::USB: _usb_kbd = new UsbKeyboard(); break; + case PROTO::OUTPUTS1::KEYBOARD::USB: _kbd = new UsbKeyboard(); break; # endif # ifdef HID_WITH_PS2 - case PROTO::OUTPUTS1::KEYBOARD::PS2: _ps2_kbd = new Ps2Keyboard(); break; + case PROTO::OUTPUTS1::KEYBOARD::PS2: _kbd = new Ps2Keyboard(); break; # endif + default: _kbd = new DRIVERS::Keyboard(DRIVERS::DUMMY); break; } uint8_t mouse = outputs & PROTO::OUTPUTS1::MOUSE::MASK; @@ -136,14 +136,7 @@ static void _initOutputs() { USBDevice.attach(); - switch (kbd) { -# ifdef HID_WITH_USB - case PROTO::OUTPUTS1::KEYBOARD::USB: _usb_kbd->begin(); break; -# endif -# ifdef HID_WITH_PS2 - case PROTO::OUTPUTS1::KEYBOARD::PS2: _ps2_kbd->begin(); break; -# endif - } + _kbd->begin(); switch (mouse) { # ifdef HID_WITH_USB @@ -181,9 +174,7 @@ static void _cmdSetConnected(const uint8_t *data) { // 1 byte } static void _cmdClearHid(const uint8_t *_) { // 0 bytes - if (_usb_kbd) { - _usb_kbd->clear(); - } + _kbd->clear(); if (_usb_mouse_abs) { _usb_mouse_abs->clear(); } else if (_usb_mouse_rel) { @@ -192,11 +183,7 @@ static void _cmdClearHid(const uint8_t *_) { // 0 bytes } static void _cmdKeyEvent(const uint8_t *data) { // 2 bytes - if (_usb_kbd) { - _usb_kbd->sendKey(data[0], data[1]); - } else if (_ps2_kbd) { - _ps2_kbd->sendKey(data[0], data[1]); - } + _kbd->sendKey(data[0], data[1]); } static void _cmdMouseButtonEvent(const uint8_t *data) { // 2 bytes @@ -287,20 +274,21 @@ static void _sendResponse(uint8_t code) { } response[2] = PROTO::OUTPUTS1::DYNAMIC; # endif - if (_usb_kbd) { - response[1] |= (_usb_kbd->isOffline() ? PROTO::PONG::KEYBOARD_OFFLINE : 0); - KeyboardLedsState leds = _usb_kbd->getLeds(); + if (DRIVERS::DUMMY != _kbd->getType()) { + response[1] |= (_kbd->isOffline() ? PROTO::PONG::KEYBOARD_OFFLINE : 0); + KeyboardLedsState leds = _kbd->getLeds(); response[1] |= (leds.caps ? PROTO::PONG::CAPS : 0); response[1] |= (leds.num ? PROTO::PONG::NUM : 0); response[1] |= (leds.scroll ? PROTO::PONG::SCROLL : 0); - response[2] |= PROTO::OUTPUTS1::KEYBOARD::USB; - } else if (_ps2_kbd) { - response[1] |= (_ps2_kbd->isOffline() ? PROTO::PONG::KEYBOARD_OFFLINE : 0); - KeyboardLedsState leds = _usb_kbd->getLeds(); - response[1] |= (leds.caps ? PROTO::PONG::CAPS : 0); - response[1] |= (leds.num ? PROTO::PONG::NUM : 0); - response[1] |= (leds.scroll ? PROTO::PONG::SCROLL : 0); - response[2] |= PROTO::OUTPUTS1::KEYBOARD::PS2; + switch (_kbd->getType()) + { + case DRIVERS::USB_KEYBOARD: + response[2] |= PROTO::OUTPUTS1::KEYBOARD::USB; + break; + case DRIVERS::PS2_KEYBOARD: + response[2] |= PROTO::OUTPUTS1::KEYBOARD::PS2; + break; + } } if (_usb_mouse_abs) { response[1] |= _usb_mouse_abs->isOffline() ? PROTO::PONG::MOUSE_OFFLINE : 0; @@ -363,16 +351,7 @@ int main() { aumProxyUsbVbus(); # endif -# ifdef HID_WITH_USB - if (_usb_kbd) { - _usb_kbd->periodic(); - } -# endif -# ifdef HID_WITH_PS2 - if (_ps2_kbd) { - _ps2_kbd->periodic(); - } -# endif + _kbd->periodic(); # ifdef CMD_SERIAL if (CMD_SERIAL.available() > 0) { -- cgit v1.2.3