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 make _build E=usb
ps2: ps2:
make _build E=ps2 make _build E=ps2
mixed:
make _build E=mixed
_build: _build:
rm -f .current rm -f .current
platformio run --environment $(E) platformio run --environment $(E)

View File

@ -24,7 +24,8 @@ lib_deps =
HID-Project@2.6.1 HID-Project@2.6.1
build_flags = build_flags =
${common.build_flags} ${common.build_flags}
-DHID_USB -DHID_USB_KBD
-DHID_USB_MOUSE
extra_scripts = post:patch.py extra_scripts = post:patch.py
[env:ps2] [env:ps2]
@ -37,6 +38,22 @@ lib_deps =
git+https://github.com/Harvie/ps2dev#v0.0.3 git+https://github.com/Harvie/ps2dev#v0.0.3
build_flags = build_flags =
${common.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_CLOCK_PIN=7
-DPS2_KBD_DATA_PIN=5 -DPS2_KBD_DATA_PIN=5

View File

@ -24,12 +24,12 @@
#include <TimerOne.h> #include <TimerOne.h>
#include "inline.h" #include "inline.h"
#if defined(HID_USB)
#if defined(HID_USB_KBD) || defined(HID_USB_MOUSE)
# include "usb/hid.h" # include "usb/hid.h"
#elif defined(HID_PS2) #endif
#ifdef HID_PS2_KBD
# include "ps2/hid.h" # include "ps2/hid.h"
#else
# error HID type is not selected
#endif #endif
@ -68,33 +68,37 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#if defined(HID_USB) #ifdef HID_USB_KBD
UsbHid hid; UsbHidKeyboard hid_kbd;
#elif defined(HID_PS2) #elif defined(HID_PS2_KBD)
Ps2Hid hid; Ps2HidKeyboard hid_kbd;
#else #endif
# error HID type is not selected #ifdef HID_USB_MOUSE
UsbHidMouse hid_mouse;
#endif #endif
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
INLINE uint8_t cmdResetHid(const uint8_t *buffer) { // 0 bytes INLINE uint8_t cmdResetHid(const uint8_t *buffer) { // 0 bytes
# ifdef HID_USB # ifdef HID_USB_KBD
hid.reset(); hid_kbd.reset();
# endif
# ifdef HID_USB_MOUSE
hid_mouse.reset();
# endif # endif
return PROTO_RESP_OK; return PROTO_RESP_OK;
} }
INLINE uint8_t cmdKeyEvent(const uint8_t *buffer) { // 2 bytes 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; return PROTO_RESP_OK;
} }
INLINE uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 1 byte INLINE uint8_t cmdMouseButtonEvent(const uint8_t *buffer) { // 1 byte
# ifdef HID_USB # ifdef HID_USB_MOUSE
uint8_t state = buffer[0]; 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_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_RIGHT_SELECT, state & PROTO_CMD_MOUSE_BUTTON_RIGHT_STATE,
state & PROTO_CMD_MOUSE_BUTTON_MIDDLE_SELECT, state & PROTO_CMD_MOUSE_BUTTON_MIDDLE_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 INLINE uint8_t cmdMouseMoveEvent(const uint8_t *buffer) { // 4 bytes
# ifdef HID_USB # ifdef HID_USB_MOUSE
int x = (int)buffer[0] << 8; int x = (int)buffer[0] << 8;
x |= (int)buffer[1]; x |= (int)buffer[1];
x = (x + 32768) / 2; // See /kvmd/apps/otg/hid/keyboard.py for details 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 |= (int)buffer[3];
y = (y + 32768) / 2; // See /kvmd/apps/otg/hid/keyboard.py for details y = (y + 32768) / 2; // See /kvmd/apps/otg/hid/keyboard.py for details
hid.sendMouseMove(x, y); hid_mouse.sendMouseMove(x, y);
# endif # endif
return PROTO_RESP_OK; return PROTO_RESP_OK;
} }
INLINE uint8_t cmdMouseWheelEvent(const uint8_t *buffer) { // 2 bytes INLINE uint8_t cmdMouseWheelEvent(const uint8_t *buffer) { // 2 bytes
# ifdef HID_USB # ifdef HID_USB_MOUSE
hid.sendMouseWheel(buffer[1]); // Y only, X is not supported hid_mouse.sendMouseWheel(buffer[1]); // Y only, X is not supported
# endif # endif
return PROTO_RESP_OK; return PROTO_RESP_OK;
} }
INLINE uint8_t cmdPongLeds(const uint8_t *buffer) { // 0 bytes 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_CAPS,
PROTO_RESP_PONG_SCROLL, PROTO_RESP_PONG_SCROLL,
PROTO_RESP_PONG_NUM PROTO_RESP_PONG_NUM
@ -190,7 +194,10 @@ void intRecvTimedOut() {
} }
void setup() { void setup() {
hid.begin(); hid_kbd.begin();
# ifdef HID_USB_MOUSE
hid_mouse.begin();
# endif
Timer1.attachInterrupt(intRecvTimedOut); Timer1.attachInterrupt(intRecvTimedOut);
CMD_SERIAL.begin(CMD_SERIAL_SPEED); CMD_SERIAL.begin(CMD_SERIAL_SPEED);
@ -201,8 +208,8 @@ void loop() {
unsigned index = 0; unsigned index = 0;
while (true) { while (true) {
# ifdef HID_PS2 # ifdef HID_PS2_KBD
hid.periodic(); hid_kbd.periodic();
# endif # endif
if (CMD_SERIAL.available() > 0) { if (CMD_SERIAL.available() > 0) {

View File

@ -33,11 +33,11 @@
// #define PS2_KBD_DATA_PIN 5 // #define PS2_KBD_DATA_PIN 5
class Ps2Hid { class Ps2HidKeyboard {
// https://wiki.osdev.org/PS/2_Keyboard // https://wiki.osdev.org/PS/2_Keyboard
public: public:
Ps2Hid() : _dev(PS2_KBD_CLOCK_PIN, PS2_KBD_DATA_PIN) {} Ps2HidKeyboard() : _dev(PS2_KBD_CLOCK_PIN, PS2_KBD_DATA_PIN) {}
void begin() { void begin() {
_dev.keyboard_init(); _dev.keyboard_init();

View File

@ -30,18 +30,16 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
class UsbHid { class UsbHidKeyboard {
public: public:
UsbHid() {} UsbHidKeyboard() {}
void begin() { void begin() {
BootKeyboard.begin(); BootKeyboard.begin();
SingleAbsoluteMouse.begin();
} }
void reset() { INLINE void reset() {
BootKeyboard.releaseAll(); BootKeyboard.releaseAll();
SingleAbsoluteMouse.releaseAll();
} }
INLINE void sendKey(uint8_t code, bool state) { 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) { INLINE uint8_t getLedsAs(uint8_t caps, uint8_t scroll, uint8_t num) {
uint8_t leds = BootKeyboard.getLeds(); uint8_t leds = BootKeyboard.getLeds();
uint8_t result = 0; uint8_t result = 0;
@ -80,9 +59,41 @@ class UsbHid {
if (leds & LED_NUM_LOCK) result |= num; if (leds & LED_NUM_LOCK) result |= num;
return result; 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: private:
INLINE void sendMouseButton(uint8_t button, bool state) { INLINE void _sendMouseButton(uint8_t button, bool state) {
if (state) SingleAbsoluteMouse.press(button); if (state) SingleAbsoluteMouse.press(button);
else SingleAbsoluteMouse.release(button); else SingleAbsoluteMouse.release(button);
} }