summaryrefslogtreecommitdiff
path: root/hid/src
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2020-11-15 23:16:46 +0300
committerDevaev Maxim <[email protected]>2020-11-15 23:16:46 +0300
commitf5250bb0e90b005128bd5025604cc41d3c3f7392 (patch)
tree75fc6b4d1e89c0dcb0727fa7e16d961d0f8c2fd8 /hid/src
parentd99771b2cd0110c9a5fcf9279184451c51a2d120 (diff)
display endpoints state
Diffstat (limited to 'hid/src')
-rw-r--r--hid/src/main.cpp33
-rw-r--r--hid/src/proto.h12
-rw-r--r--hid/src/ps2/hid.h4
-rw-r--r--hid/src/usb/hid.h28
4 files changed, 51 insertions, 26 deletions
diff --git a/hid/src/main.cpp b/hid/src/main.cpp
index 1bcd2162..f87c719f 100644
--- a/hid/src/main.cpp
+++ b/hid/src/main.cpp
@@ -59,19 +59,30 @@
// -----------------------------------------------------------------------------
-uint8_t cmdResetHid(const uint8_t *buffer) { // 0 bytes
+uint8_t cmdPong(const uint8_t *_=NULL) { // 0 bytes
+ return (
+ ((uint8_t)PROTO_RESP_PONG_PREFIX)
+ | hid_kbd.getLedsAs(PROTO_RESP_PONG_CAPS, PROTO_RESP_PONG_SCROLL, PROTO_RESP_PONG_NUM)
+ | (hid_kbd.isOnline() ? 0 : PROTO_RESP_PONG_KEYBOARD_OFFLINE)
+# ifdef HID_USB_MOUSE
+ | (hid_mouse.isOnline() ? 0 : PROTO_RESP_PONG_MOUSE_OFFLINE)
+# endif
+ );
+}
+
+uint8_t cmdResetHid(const uint8_t *_) { // 0 bytes
# ifdef HID_USB_KBD
hid_kbd.reset();
# endif
# ifdef HID_USB_MOUSE
hid_mouse.reset();
# endif
- return PROTO_RESP_OK;
+ return cmdPong();
}
uint8_t cmdKeyEvent(const uint8_t *buffer) { // 2 bytes
hid_kbd.sendKey(buffer[0], buffer[1]);
- return PROTO_RESP_OK;
+ return cmdPong();
}
uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 2 bytes
@@ -91,7 +102,7 @@ uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 2 bytes
);
# undef MOUSE_PAIR
# endif
- return PROTO_RESP_OK;
+ return cmdPong();
}
uint8_t cmdMouseMoveEvent(const uint8_t *buffer) { // 4 bytes
@@ -106,22 +117,14 @@ uint8_t cmdMouseMoveEvent(const uint8_t *buffer) { // 4 bytes
hid_mouse.sendMove(x, y);
# endif
- return PROTO_RESP_OK;
+ return cmdPong();
}
uint8_t cmdMouseWheelEvent(const uint8_t *buffer) { // 2 bytes
# ifdef HID_USB_MOUSE
hid_mouse.sendWheel(buffer[1]); // Y only, X is not supported
# endif
- return PROTO_RESP_OK;
-}
-
-uint8_t cmdPongLeds(const uint8_t *buffer) { // 0 bytes
- return ((uint8_t) PROTO_RESP_PONG_PREFIX) | hid_kbd.getLedsAs(
- PROTO_RESP_PONG_CAPS,
- PROTO_RESP_PONG_SCROLL,
- PROTO_RESP_PONG_NUM
- );
+ return cmdPong();
}
uint8_t handleCmdBuffer(const uint8_t *buffer) { // 8 bytes
@@ -136,7 +139,7 @@ uint8_t handleCmdBuffer(const uint8_t *buffer) { // 8 bytes
case PROTO_CMD_MOUSE_BUTTON_EVENT: HANDLE(cmdMouseButtonEvent);
case PROTO_CMD_MOUSE_MOVE_EVENT: HANDLE(cmdMouseMoveEvent);
case PROTO_CMD_MOUSE_WHEEL_EVENT: HANDLE(cmdMouseWheelEvent);
- case PROTO_CMD_PING: HANDLE(cmdPongLeds);
+ case PROTO_CMD_PING: HANDLE(cmdPong);
case PROTO_CMD_REPEAT: return 0;
default: return PROTO_RESP_INVALID_ERROR;
}
diff --git a/hid/src/proto.h b/hid/src/proto.h
index 36502d4f..617711dd 100644
--- a/hid/src/proto.h
+++ b/hid/src/proto.h
@@ -26,16 +26,18 @@
#define PROTO_MAGIC 0x33
#define PROTO_CRC_POLINOM 0xA001
-#define PROTO_RESP_OK 0x20
+// #define PROTO_RESP_OK 0x20 // Legacy
#define PROTO_RESP_NONE 0x24
#define PROTO_RESP_CRC_ERROR 0x40
#define PROTO_RESP_INVALID_ERROR 0x45
#define PROTO_RESP_TIMEOUT_ERROR 0x48
-#define PROTO_RESP_PONG_PREFIX 0x80
-#define PROTO_RESP_PONG_CAPS 0b00000001
-#define PROTO_RESP_PONG_SCROLL 0b00000010
-#define PROTO_RESP_PONG_NUM 0b00000100
+#define PROTO_RESP_PONG_PREFIX 0x80
+#define PROTO_RESP_PONG_CAPS 0b00000001
+#define PROTO_RESP_PONG_SCROLL 0b00000010
+#define PROTO_RESP_PONG_NUM 0b00000100
+#define PROTO_RESP_PONG_KEYBOARD_OFFLINE 0b00001000
+#define PROTO_RESP_PONG_MOUSE_OFFLINE 0b00010000
#define PROTO_CMD_PING 0x01
#define PROTO_CMD_REPEAT 0x02
diff --git a/hid/src/ps2/hid.h b/hid/src/ps2/hid.h
index eca2bf4b..3f226090 100644
--- a/hid/src/ps2/hid.h
+++ b/hid/src/ps2/hid.h
@@ -41,6 +41,10 @@ class Ps2HidKeyboard {
_dev.keyboard_init();
}
+ bool isOnline() {
+ return true;
+ }
+
void periodic() {
_dev.keyboard_handle(&_leds);
}
diff --git a/hid/src/usb/hid.h b/hid/src/usb/hid.h
index f0016931..d48d0fc2 100644
--- a/hid/src/usb/hid.h
+++ b/hid/src/usb/hid.h
@@ -40,9 +40,9 @@ static bool _checkEndpoint(uint8_t ep) {
SREG = intr_state;
return rw_allowed;
}
-# define CHECK_HID_EP(_hid) { if (!_checkEndpoint(_hid.getPluggedEndpoint())) return; }
+# define CHECK_HID_EP { if (!isOnline()) return; }
#else
-# define CHECK_HID_EP(_hid)
+# define CHECK_HID_EP
#endif
class UsbHidKeyboard {
@@ -53,12 +53,20 @@ class UsbHidKeyboard {
BootKeyboard.begin();
}
+ bool isOnline() {
+# ifdef CHECK_ENDPOINT
+ return _checkEndpoint(BootKeyboard.getPluggedEndpoint());
+# else
+ return true;
+# endif
+ }
+
void reset() {
BootKeyboard.releaseAll();
}
void sendKey(uint8_t code, bool state) {
- CHECK_HID_EP(BootKeyboard);
+ CHECK_HID_EP;
KeyboardKeycode usb_code = keymapUsb(code);
if (usb_code != KEY_ERROR_UNDEFINED) {
if (state) BootKeyboard.press(usb_code);
@@ -85,6 +93,14 @@ class UsbHidMouse {
SingleAbsoluteMouse.begin();
}
+ bool isOnline() {
+# ifdef CHECK_ENDPOINT
+ return _checkEndpoint(SingleAbsoluteMouse.getPluggedEndpoint());
+# else
+ return true;
+# endif
+ }
+
void reset() {
SingleAbsoluteMouse.releaseAll();
}
@@ -104,19 +120,19 @@ class UsbHidMouse {
}
void sendMove(int x, int y) {
- CHECK_HID_EP(SingleAbsoluteMouse);
+ CHECK_HID_EP;
SingleAbsoluteMouse.moveTo(x, y);
}
void sendWheel(int delta_y) {
- CHECK_HID_EP(SingleAbsoluteMouse);
+ CHECK_HID_EP;
// 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);
+ CHECK_HID_EP;
if (state) SingleAbsoluteMouse.press(button);
else SingleAbsoluteMouse.release(button);
}