summaryrefslogtreecommitdiff
path: root/hid
diff options
context:
space:
mode:
authortomaszduda23 <[email protected]>2022-07-10 04:43:54 +0900
committerGitHub <[email protected]>2022-07-09 22:43:54 +0300
commit38fae01cc01a0e984f52c004ade79256d8c90e17 (patch)
treefca48f9a0208aa5c0d79ed7f1da012b285d18025 /hid
parent793edf8203ec7ea5aba934558ea26054628a5618 (diff)
add keyboard interface (#95)
Diffstat (limited to 'hid')
-rw-r--r--hid/lib/drivers/driver.h27
-rw-r--r--hid/lib/drivers/keyboard.h42
-rw-r--r--hid/lib/drivers/mouse.h6
-rw-r--r--hid/src/main.cpp61
-rw-r--r--hid/src/ps2/hid.h14
-rw-r--r--hid/src/usb/hid.h14
6 files changed, 89 insertions, 75 deletions
diff --git a/hid/lib/drivers/driver.h b/hid/lib/drivers/driver.h
index 520c7456..ea209c2e 100644
--- a/hid/lib/drivers/driver.h
+++ b/hid/lib/drivers/driver.h
@@ -25,18 +25,21 @@
namespace DRIVERS {
- enum type {
- USB_MOUSE_ABSOLUTE,
- USB_MOUSE_RELATIVE,
- USB_MOUSE_ABSOLUTE_WIN98,
- };
+ enum type {
+ DUMMY = 0,
+ USB_MOUSE_ABSOLUTE,
+ USB_MOUSE_RELATIVE,
+ USB_MOUSE_ABSOLUTE_WIN98,
+ USB_KEYBOARD,
+ PS2_KEYBOARD,
+ };
- class Driver {
- public:
- Driver(type _type) : _type(_type) {}
- uint8_t getType() { return _type; }
+ class Driver {
+ public:
+ Driver(type _type) : _type(_type) {}
+ uint8_t getType() { return _type; }
- private:
- type _type;
- };
+ private:
+ type _type;
+ };
}
diff --git a/hid/lib/drivers/keyboard.h b/hid/lib/drivers/keyboard.h
index 44ffe61a..39517f68 100644
--- a/hid/lib/drivers/keyboard.h
+++ b/hid/lib/drivers/keyboard.h
@@ -26,9 +26,41 @@
namespace DRIVERS {
- typedef struct {
- bool caps;
- bool scroll;
- bool num;
- } KeyboardLedsState;
+ typedef struct {
+ bool caps;
+ bool scroll;
+ bool num;
+ } KeyboardLedsState;
+
+
+ struct Keyboard : public Driver {
+ using Driver::Driver;
+
+ virtual void begin() {}
+
+ /**
+ * Release all keys
+ */
+ virtual void clear() {}
+
+ /**
+ * Sends key
+ * @param code ???
+ * @param state true pressed, false released
+ */
+ virtual void sendKey(uint8_t code, bool state) {}
+
+ virtual void periodic() {}
+
+ /**
+ * False if online or unknown. Otherwise true.
+ */
+ virtual bool isOffline() { return false; }
+
+ virtual KeyboardLedsState getLeds() {
+ KeyboardLedsState result = {};
+ return result;
+ }
+
+ };
}
diff --git a/hid/lib/drivers/mouse.h b/hid/lib/drivers/mouse.h
index 8f76b041..3ec29ee9 100644
--- a/hid/lib/drivers/mouse.h
+++ b/hid/lib/drivers/mouse.h
@@ -26,7 +26,7 @@
namespace DRIVERS {
- class Mouse : public Driver {
- using Driver::Driver;
- };
+ class Mouse : public Driver {
+ using Driver::Driver;
+ };
}
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) {
diff --git a/hid/src/ps2/hid.h b/hid/src/ps2/hid.h
index 6fce88b6..809f9d2d 100644
--- a/hid/src/ps2/hid.h
+++ b/hid/src/ps2/hid.h
@@ -32,21 +32,21 @@
// #define HID_PS2_KBD_DATA_PIN 5
-class Ps2Keyboard {
+class Ps2Keyboard : public DRIVERS::Keyboard {
// https://wiki.osdev.org/PS/2_Keyboard
public:
- Ps2Keyboard() : _dev(HID_PS2_KBD_CLOCK_PIN, HID_PS2_KBD_DATA_PIN) {}
+ Ps2Keyboard() : DRIVERS::Keyboard(DRIVERS::PS2_KEYBOARD), _dev(HID_PS2_KBD_CLOCK_PIN, HID_PS2_KBD_DATA_PIN) {}
- void begin() {
+ void begin() override {
_dev.keyboard_init();
}
- void periodic() {
+ void periodic() override {
_dev.keyboard_handle(&_leds);
}
- void sendKey(uint8_t code, bool state) {
+ void sendKey(uint8_t code, bool state) override {
Ps2KeyType ps2_type;
uint8_t ps2_code;
@@ -75,11 +75,11 @@ class Ps2Keyboard {
}
}
- bool isOffline() {
+ bool isOffline() override {
return false;
}
- KeyboardLedsState getLeds() {
+ KeyboardLedsState getLeds() override {
periodic();
KeyboardLedsState result = {
.caps = _leds & 0b00000100,
diff --git a/hid/src/usb/hid.h b/hid/src/usb/hid.h
index 6e5d7279..a3641565 100644
--- a/hid/src/usb/hid.h
+++ b/hid/src/usb/hid.h
@@ -69,15 +69,15 @@ using namespace DRIVERS;
#endif
-class UsbKeyboard {
+class UsbKeyboard : public DRIVERS::Keyboard {
public:
- UsbKeyboard() {}
+ UsbKeyboard() : DRIVERS::Keyboard(DRIVERS::USB_KEYBOARD) {}
- void begin() {
+ void begin() override {
_kbd.begin();
}
- void periodic() {
+ void periodic() override {
# ifdef HID_USB_CHECK_ENDPOINT
static unsigned long prev_ts = 0;
if (is_micros_timed_out(prev_ts, 50000)) {
@@ -92,11 +92,11 @@ class UsbKeyboard {
# endif
}
- void clear() {
+ void clear() override {
_kbd.releaseAll();
}
- void sendKey(uint8_t code, bool state) {
+ void sendKey(uint8_t code, bool state) override {
KeyboardKeycode usb_code = keymapUsb(code);
if (usb_code != KEY_ERROR_UNDEFINED) {
if (state ? _kbd.add(usb_code) : _kbd.remove(usb_code)) {
@@ -107,7 +107,7 @@ class UsbKeyboard {
CLS_IS_OFFLINE(_kbd)
- KeyboardLedsState getLeds() {
+ KeyboardLedsState getLeds() override {
uint8_t leds = _kbd.getLeds();
KeyboardLedsState result = {
.caps = leds & LED_CAPS_LOCK,