mixed mode for arduino hid

This commit is contained in:
Devaev Maxim 2020-08-12 12:39:04 +03:00
parent 9ee24e5cf4
commit 37b3ac74d5
5 changed files with 91 additions and 54 deletions

View File

@ -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)

View File

@ -24,7 +24,8 @@ lib_deps =
HID-Project@2.6.1
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}
HID-Project@2.6.1
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

View File

@ -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) {

View File

@ -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();

View File

@ -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,25 +50,6 @@ class UsbHid {
}
}
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);
}
INLINE void sendMouseMove(int x, int y) {
SingleAbsoluteMouse.moveTo(x, y);
}
INLINE void sendMouseWheel(int delta_y) {
// delta_x is not supported by hid-project now
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;
@ -80,9 +59,41 @@ class UsbHid {
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);
}
INLINE void sendMouseMove(int x, int y) {
SingleAbsoluteMouse.moveTo(x, y);
}
INLINE void sendMouseWheel(int delta_y) {
// delta_x is not supported by hid-project now
SingleAbsoluteMouse.move(0, 0, delta_y);
}
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);
}