refactoring

This commit is contained in:
Maxim Devaev 2022-07-09 23:00:13 +03:00
parent 38fae01cc0
commit ad6a387941
5 changed files with 38 additions and 22 deletions

View File

@ -19,12 +19,13 @@
# # # #
*****************************************************************************/ *****************************************************************************/
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
namespace DRIVERS {
namespace DRIVERS {
enum type { enum type {
DUMMY = 0, DUMMY = 0,
USB_MOUSE_ABSOLUTE, USB_MOUSE_ABSOLUTE,

View File

@ -19,20 +19,21 @@
# # # #
*****************************************************************************/ *****************************************************************************/
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include "driver.h" #include "driver.h"
namespace DRIVERS {
namespace DRIVERS {
typedef struct { typedef struct {
bool caps; bool caps;
bool scroll; bool scroll;
bool num; bool num;
} KeyboardLedsState; } KeyboardLedsState;
struct Keyboard : public Driver { struct Keyboard : public Driver {
using Driver::Driver; using Driver::Driver;
@ -55,10 +56,12 @@ namespace DRIVERS {
/** /**
* False if online or unknown. Otherwise true. * False if online or unknown. Otherwise true.
*/ */
virtual bool isOffline() { return false; } virtual bool isOffline() {
return false;
}
virtual KeyboardLedsState getLeds() { virtual KeyboardLedsState getLeds() {
KeyboardLedsState result = {}; KeyboardLedsState result = {0};
return result; return result;
} }

View File

@ -19,13 +19,15 @@
# # # #
*****************************************************************************/ *****************************************************************************/
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include "driver.h" #include "driver.h"
namespace DRIVERS {
namespace DRIVERS {
class Mouse : public Driver { class Mouse : public Driver {
using Driver::Driver; using Driver::Driver;
}; };

View File

@ -113,12 +113,18 @@ static void _initOutputs() {
uint8_t kbd = outputs & PROTO::OUTPUTS1::KEYBOARD::MASK; uint8_t kbd = outputs & PROTO::OUTPUTS1::KEYBOARD::MASK;
switch (kbd) { switch (kbd) {
# ifdef HID_WITH_USB # ifdef HID_WITH_USB
case PROTO::OUTPUTS1::KEYBOARD::USB: _kbd = new UsbKeyboard(); break; case PROTO::OUTPUTS1::KEYBOARD::USB:
_kbd = new UsbKeyboard();
break;
# endif # endif
# ifdef HID_WITH_PS2 # ifdef HID_WITH_PS2
case PROTO::OUTPUTS1::KEYBOARD::PS2: _kbd = new Ps2Keyboard(); break; case PROTO::OUTPUTS1::KEYBOARD::PS2:
_kbd = new Ps2Keyboard();
break;
# endif # endif
default: _kbd = new DRIVERS::Keyboard(DRIVERS::DUMMY); break; default:
_kbd = new DRIVERS::Keyboard(DRIVERS::DUMMY);
break;
} }
uint8_t mouse = outputs & PROTO::OUTPUTS1::MOUSE::MASK; uint8_t mouse = outputs & PROTO::OUTPUTS1::MOUSE::MASK;
@ -130,7 +136,9 @@ static void _initOutputs() {
case PROTO::OUTPUTS1::MOUSE::USB_WIN98: case PROTO::OUTPUTS1::MOUSE::USB_WIN98:
_usb_mouse_abs = new UsbMouseAbsolute(DRIVERS::USB_MOUSE_ABSOLUTE_WIN98); _usb_mouse_abs = new UsbMouseAbsolute(DRIVERS::USB_MOUSE_ABSOLUTE_WIN98);
break; break;
case PROTO::OUTPUTS1::MOUSE::USB_REL: _usb_mouse_rel = new UsbMouseRelative(); break; case PROTO::OUTPUTS1::MOUSE::USB_REL:
_usb_mouse_rel = new UsbMouseRelative();
break;
# endif # endif
} }
@ -146,7 +154,9 @@ static void _initOutputs() {
# endif # endif
_usb_mouse_abs->begin(); _usb_mouse_abs->begin();
break; break;
case PROTO::OUTPUTS1::MOUSE::USB_REL: _usb_mouse_rel->begin(); break; case PROTO::OUTPUTS1::MOUSE::USB_REL:
_usb_mouse_rel->begin();
break;
# endif # endif
} }
} }
@ -274,24 +284,23 @@ static void _sendResponse(uint8_t code) {
} }
response[2] = PROTO::OUTPUTS1::DYNAMIC; response[2] = PROTO::OUTPUTS1::DYNAMIC;
# endif # endif
if (DRIVERS::DUMMY != _kbd->getType()) { if (_kbd->getType() != DRIVERS::DUMMY) {
response[1] |= (_kbd->isOffline() ? PROTO::PONG::KEYBOARD_OFFLINE : 0); response[1] |= (_kbd->isOffline() ? PROTO::PONG::KEYBOARD_OFFLINE : 0);
KeyboardLedsState leds = _kbd->getLeds(); KeyboardLedsState leds = _kbd->getLeds();
response[1] |= (leds.caps ? PROTO::PONG::CAPS : 0); response[1] |= (leds.caps ? PROTO::PONG::CAPS : 0);
response[1] |= (leds.num ? PROTO::PONG::NUM : 0); response[1] |= (leds.num ? PROTO::PONG::NUM : 0);
response[1] |= (leds.scroll ? PROTO::PONG::SCROLL : 0); response[1] |= (leds.scroll ? PROTO::PONG::SCROLL : 0);
switch (_kbd->getType()) switch (_kbd->getType()) {
{ case DRIVERS::USB_KEYBOARD:
case DRIVERS::USB_KEYBOARD: response[2] |= PROTO::OUTPUTS1::KEYBOARD::USB;
response[2] |= PROTO::OUTPUTS1::KEYBOARD::USB; break;
break; case DRIVERS::PS2_KEYBOARD:
case DRIVERS::PS2_KEYBOARD: response[2] |= PROTO::OUTPUTS1::KEYBOARD::PS2;
response[2] |= PROTO::OUTPUTS1::KEYBOARD::PS2; break;
break;
} }
} }
if (_usb_mouse_abs) { if (_usb_mouse_abs) {
response[1] |= _usb_mouse_abs->isOffline() ? PROTO::PONG::MOUSE_OFFLINE : 0; response[1] |= (_usb_mouse_abs->isOffline() ? PROTO::PONG::MOUSE_OFFLINE : 0);
if (_usb_mouse_abs->getType() == DRIVERS::USB_MOUSE_ABSOLUTE_WIN98) { if (_usb_mouse_abs->getType() == DRIVERS::USB_MOUSE_ABSOLUTE_WIN98) {
response[2] |= PROTO::OUTPUTS1::MOUSE::USB_WIN98; response[2] |= PROTO::OUTPUTS1::MOUSE::USB_WIN98;
} else { } else {

View File

@ -69,6 +69,7 @@ using namespace DRIVERS;
#endif #endif
class UsbKeyboard : public DRIVERS::Keyboard { class UsbKeyboard : public DRIVERS::Keyboard {
public: public:
UsbKeyboard() : DRIVERS::Keyboard(DRIVERS::USB_KEYBOARD) {} UsbKeyboard() : DRIVERS::Keyboard(DRIVERS::USB_KEYBOARD) {}
@ -110,7 +111,7 @@ class UsbKeyboard : public DRIVERS::Keyboard {
KeyboardLedsState getLeds() override { KeyboardLedsState getLeds() override {
uint8_t leds = _kbd.getLeds(); uint8_t leds = _kbd.getLeds();
KeyboardLedsState result = { KeyboardLedsState result = {
.caps = leds & LED_CAPS_LOCK, .caps = leds & LED_CAPS_LOCK,
.scroll = leds & LED_SCROLL_LOCK, .scroll = leds & LED_SCROLL_LOCK,
.num = leds & LED_NUM_LOCK, .num = leds & LED_NUM_LOCK,
}; };