diff options
author | tomaszduda23 <[email protected]> | 2022-07-11 08:01:57 +0900 |
---|---|---|
committer | GitHub <[email protected]> | 2022-07-11 02:01:57 +0300 |
commit | 1afd96cdb932ccb1df0f0ed7e6662080f2618a26 (patch) | |
tree | f747158fd5b2a1fd9f26568302d9ffb8a4dddb30 /hid/lib | |
parent | 67547636cfae08121154179266bd1c42f59a72cb (diff) |
add factory for avr (#98)
Diffstat (limited to 'hid/lib')
-rw-r--r-- | hid/lib/drivers-avr/factory.cpp | 61 | ||||
-rw-r--r-- | hid/lib/drivers-avr/ps2/hid.h | 95 | ||||
-rw-r--r-- | hid/lib/drivers-avr/ps2/keymap.h | 148 | ||||
-rw-r--r-- | hid/lib/drivers-avr/ps2/keymap.h.mako | 44 | ||||
-rw-r--r-- | hid/lib/drivers-avr/spi.cpp | 83 | ||||
-rw-r--r-- | hid/lib/drivers-avr/spi.h | 31 | ||||
-rw-r--r-- | hid/lib/drivers-avr/usb/hid.h | 229 | ||||
-rw-r--r-- | hid/lib/drivers-avr/usb/keymap.h | 139 | ||||
-rw-r--r-- | hid/lib/drivers-avr/usb/keymap.h.mako | 35 | ||||
-rw-r--r-- | hid/lib/drivers/aum.h | 48 | ||||
-rw-r--r-- | hid/lib/drivers/factory.h | 33 | ||||
-rw-r--r-- | hid/lib/drivers/tools.cpp | 30 | ||||
-rw-r--r-- | hid/lib/drivers/tools.h | 26 |
13 files changed, 1002 insertions, 0 deletions
diff --git a/hid/lib/drivers-avr/factory.cpp b/hid/lib/drivers-avr/factory.cpp new file mode 100644 index 00000000..d58e6c96 --- /dev/null +++ b/hid/lib/drivers-avr/factory.cpp @@ -0,0 +1,61 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + +#include "usb/hid.h" +#include "ps2/hid.h" +#include "factory.h" + +namespace DRIVERS +{ + + Keyboard *Factory::makeKeyboard(type _type) { + switch (_type) { + +# ifdef HID_WITH_USB + case USB_KEYBOARD: + return new UsbKeyboard(); +# endif + +# ifdef HID_WITH_PS2 + case PS2_KEYBOARD: + return new Ps2Keyboard(); +# endif + default: + return new Keyboard(DUMMY); + } + } + + Mouse *Factory::makeMouse(type _type) { + switch(_type) + { +# ifdef HID_WITH_USB + case USB_MOUSE_ABSOLUTE: + case USB_MOUSE_ABSOLUTE_WIN98: + return new UsbMouseAbsolute(_type); + case USB_MOUSE_RELATIVE: + return new UsbMouseRelative(); +# endif + default: + return new Mouse(DRIVERS::DUMMY); + } + } + +} diff --git a/hid/lib/drivers-avr/ps2/hid.h b/hid/lib/drivers-avr/ps2/hid.h new file mode 100644 index 00000000..809f9d2d --- /dev/null +++ b/hid/lib/drivers-avr/ps2/hid.h @@ -0,0 +1,95 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + +#include <Arduino.h> +#include <ps2dev.h> + +#include "keyboard.h" +#include "keymap.h" + +// #define HID_PS2_KBD_CLOCK_PIN 7 +// #define HID_PS2_KBD_DATA_PIN 5 + + +class Ps2Keyboard : public DRIVERS::Keyboard { + // https://wiki.osdev.org/PS/2_Keyboard + + public: + Ps2Keyboard() : DRIVERS::Keyboard(DRIVERS::PS2_KEYBOARD), _dev(HID_PS2_KBD_CLOCK_PIN, HID_PS2_KBD_DATA_PIN) {} + + void begin() override { + _dev.keyboard_init(); + } + + void periodic() override { + _dev.keyboard_handle(&_leds); + } + + void sendKey(uint8_t code, bool state) override { + Ps2KeyType ps2_type; + uint8_t ps2_code; + + keymapPs2(code, &ps2_type, &ps2_code); + if (ps2_type != PS2_KEY_TYPE_UNKNOWN) { + // Не отправлялась часть нажатий. Когда clock на нуле, комп не принимает ничего от клавы. + // Этот костыль понижает процент пропущенных нажатий. + while (digitalRead(HID_PS2_KBD_CLOCK_PIN) == 0) {}; + if (state) { + switch (ps2_type) { + case PS2_KEY_TYPE_REG: _dev.keyboard_press(ps2_code); break; + case PS2_KEY_TYPE_SPEC: _dev.keyboard_press_special(ps2_code); break; + case PS2_KEY_TYPE_PRINT: _dev.keyboard_press_printscreen(); break; + case PS2_KEY_TYPE_PAUSE: _dev.keyboard_pausebreak(); break; + case PS2_KEY_TYPE_UNKNOWN: break; + } + } else { + switch (ps2_type) { + case PS2_KEY_TYPE_REG: _dev.keyboard_release(ps2_code); break; + case PS2_KEY_TYPE_SPEC: _dev.keyboard_release_special(ps2_code); break; + case PS2_KEY_TYPE_PRINT: _dev.keyboard_release_printscreen(); break; + case PS2_KEY_TYPE_PAUSE: break; + case PS2_KEY_TYPE_UNKNOWN: break; + } + } + } + } + + bool isOffline() override { + return false; + } + + KeyboardLedsState getLeds() override { + periodic(); + KeyboardLedsState result = { + .caps = _leds & 0b00000100, + .scroll = _leds & 0b00000001, + .num = _leds & 0b00000010, + }; + return result; + } + + private: + PS2dev _dev; + uint8_t _leds = 0; +}; diff --git a/hid/lib/drivers-avr/ps2/keymap.h b/hid/lib/drivers-avr/ps2/keymap.h new file mode 100644 index 00000000..a2b6f8fd --- /dev/null +++ b/hid/lib/drivers-avr/ps2/keymap.h @@ -0,0 +1,148 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + + +enum Ps2KeyType : uint8_t { + PS2_KEY_TYPE_UNKNOWN = 0, + PS2_KEY_TYPE_REG = 1, + PS2_KEY_TYPE_SPEC = 2, + PS2_KEY_TYPE_PRINT = 3, + PS2_KEY_TYPE_PAUSE = 4, +}; + + +void keymapPs2(uint8_t code, Ps2KeyType *ps2_type, uint8_t *ps2_code) { + *ps2_type = PS2_KEY_TYPE_UNKNOWN; + *ps2_code = 0; + + switch (code) { + case 1: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 28; return; // KEY_A + case 2: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 50; return; // KEY_B + case 3: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 33; return; // KEY_C + case 4: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 35; return; // KEY_D + case 5: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 36; return; // KEY_E + case 6: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 43; return; // KEY_F + case 7: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 52; return; // KEY_G + case 8: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 51; return; // KEY_H + case 9: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 67; return; // KEY_I + case 10: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 59; return; // KEY_J + case 11: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 66; return; // KEY_K + case 12: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 75; return; // KEY_L + case 13: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 58; return; // KEY_M + case 14: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 49; return; // KEY_N + case 15: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 68; return; // KEY_O + case 16: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 77; return; // KEY_P + case 17: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 21; return; // KEY_Q + case 18: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 45; return; // KEY_R + case 19: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 27; return; // KEY_S + case 20: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 44; return; // KEY_T + case 21: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 60; return; // KEY_U + case 22: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 42; return; // KEY_V + case 23: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 29; return; // KEY_W + case 24: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 34; return; // KEY_X + case 25: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 53; return; // KEY_Y + case 26: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 26; return; // KEY_Z + case 27: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 22; return; // KEY_1 + case 28: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 30; return; // KEY_2 + case 29: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 38; return; // KEY_3 + case 30: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 37; return; // KEY_4 + case 31: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 46; return; // KEY_5 + case 32: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 54; return; // KEY_6 + case 33: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 61; return; // KEY_7 + case 34: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 62; return; // KEY_8 + case 35: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 70; return; // KEY_9 + case 36: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 69; return; // KEY_0 + case 37: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 90; return; // KEY_ENTER + case 38: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 118; return; // KEY_ESC + case 39: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 102; return; // KEY_BACKSPACE + case 40: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 13; return; // KEY_TAB + case 41: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 41; return; // KEY_SPACE + case 42: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 78; return; // KEY_MINUS + case 43: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 85; return; // KEY_EQUAL + case 44: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 84; return; // KEY_LEFT_BRACE + case 45: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 91; return; // KEY_RIGHT_BRACE + case 46: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 93; return; // KEY_BACKSLASH + case 47: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 76; return; // KEY_SEMICOLON + case 48: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 82; return; // KEY_QUOTE + case 49: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 14; return; // KEY_TILDE + case 50: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 65; return; // KEY_COMMA + case 51: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 73; return; // KEY_PERIOD + case 52: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 74; return; // KEY_SLASH + case 53: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 88; return; // KEY_CAPS_LOCK + case 54: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 5; return; // KEY_F1 + case 55: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 6; return; // KEY_F2 + case 56: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 4; return; // KEY_F3 + case 57: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 12; return; // KEY_F4 + case 58: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 3; return; // KEY_F5 + case 59: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 11; return; // KEY_F6 + case 60: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 131; return; // KEY_F7 + case 61: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 10; return; // KEY_F8 + case 62: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 1; return; // KEY_F9 + case 63: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 9; return; // KEY_F10 + case 64: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 120; return; // KEY_F11 + case 65: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 7; return; // KEY_F12 + case 66: *ps2_type = PS2_KEY_TYPE_PRINT; *ps2_code = 255; return; // KEY_PRINT + case 67: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 112; return; // KEY_INSERT + case 68: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 108; return; // KEY_HOME + case 69: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 125; return; // KEY_PAGE_UP + case 70: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 113; return; // KEY_DELETE + case 71: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 105; return; // KEY_END + case 72: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 122; return; // KEY_PAGE_DOWN + case 73: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 116; return; // KEY_RIGHT_ARROW + case 74: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 107; return; // KEY_LEFT_ARROW + case 75: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 114; return; // KEY_DOWN_ARROW + case 76: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 117; return; // KEY_UP_ARROW + case 77: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 20; return; // KEY_LEFT_CTRL + case 78: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 18; return; // KEY_LEFT_SHIFT + case 79: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 17; return; // KEY_LEFT_ALT + case 80: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 31; return; // KEY_LEFT_GUI + case 81: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 20; return; // KEY_RIGHT_CTRL + case 82: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 89; return; // KEY_RIGHT_SHIFT + case 83: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 17; return; // KEY_RIGHT_ALT + case 84: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 39; return; // KEY_RIGHT_GUI + case 85: *ps2_type = PS2_KEY_TYPE_PAUSE; *ps2_code = 255; return; // KEY_PAUSE + case 86: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 126; return; // KEY_SCROLL_LOCK + case 87: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 119; return; // KEY_NUM_LOCK + case 88: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 47; return; // KEY_MENU + case 89: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 74; return; // KEYPAD_DIVIDE + case 90: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 124; return; // KEYPAD_MULTIPLY + case 91: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 123; return; // KEYPAD_SUBTRACT + case 92: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 121; return; // KEYPAD_ADD + case 93: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 90; return; // KEYPAD_ENTER + case 94: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 105; return; // KEYPAD_1 + case 95: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 114; return; // KEYPAD_2 + case 96: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 122; return; // KEYPAD_3 + case 97: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 107; return; // KEYPAD_4 + case 98: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 115; return; // KEYPAD_5 + case 99: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 116; return; // KEYPAD_6 + case 100: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 108; return; // KEYPAD_7 + case 101: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 117; return; // KEYPAD_8 + case 102: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 125; return; // KEYPAD_9 + case 103: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 112; return; // KEYPAD_0 + case 104: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 113; return; // KEYPAD_DOT + case 105: *ps2_type = PS2_KEY_TYPE_SPEC; *ps2_code = 94; return; // KEY_POWER + case 106: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 97; return; // KEY_NON_US + case 107: *ps2_type = PS2_KEY_TYPE_REG; *ps2_code = 106; return; // KEY_INTERNATIONAL3 + } +} diff --git a/hid/lib/drivers-avr/ps2/keymap.h.mako b/hid/lib/drivers-avr/ps2/keymap.h.mako new file mode 100644 index 00000000..bc4ecb47 --- /dev/null +++ b/hid/lib/drivers-avr/ps2/keymap.h.mako @@ -0,0 +1,44 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + + +enum Ps2KeyType : uint8_t { + PS2_KEY_TYPE_UNKNOWN = 0, + PS2_KEY_TYPE_REG = 1, + PS2_KEY_TYPE_SPEC = 2, + PS2_KEY_TYPE_PRINT = 3, + PS2_KEY_TYPE_PAUSE = 4, +}; + +<%! import operator %> +void keymapPs2(uint8_t code, Ps2KeyType *ps2_type, uint8_t *ps2_code) { + *ps2_type = PS2_KEY_TYPE_UNKNOWN; + *ps2_code = 0; + + switch (code) { +% for km in sorted(keymap, key=operator.attrgetter("mcu_code")): + case ${km.mcu_code}: *ps2_type = PS2_KEY_TYPE_${km.ps2_key.type.upper()}; *ps2_code = ${km.ps2_key.code}; return; // ${km.arduino_name} +% endfor + } +} diff --git a/hid/lib/drivers-avr/spi.cpp b/hid/lib/drivers-avr/spi.cpp new file mode 100644 index 00000000..e2b56928 --- /dev/null +++ b/hid/lib/drivers-avr/spi.cpp @@ -0,0 +1,83 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#include "spi.h" + +#include <Arduino.h> +#include <SPI.h> + + +static volatile uint8_t _spi_in[8] = {0}; +static volatile uint8_t _spi_in_index = 0; + +static volatile uint8_t _spi_out[8] = {0}; +static volatile uint8_t _spi_out_index = 0; + + +void spiBegin() { + pinMode(MISO, OUTPUT); + SPCR = (1 << SPE) | (1 << SPIE); // Slave, SPI En, IRQ En +} + +bool spiReady() { + return (!_spi_out[0] && _spi_in_index == 8); +} + +const uint8_t *spiGet() { + return (const uint8_t *)_spi_in; +} + +void spiWrite(const uint8_t *data) { + // Меджик в нулевом байте разрешает начать ответ + for (int index = 7; index >= 0; --index) { + _spi_out[index] = data[index]; + } +} + + +ISR(SPI_STC_vect) { + uint8_t in = SPDR; + if (_spi_out[0] && _spi_out_index < 8) { + SPDR = _spi_out[_spi_out_index]; + if (!(SPSR & (1 << WCOL))) { + ++_spi_out_index; + if (_spi_out_index == 8) { + _spi_out_index = 0; + _spi_in_index = 0; + _spi_out[0] = 0; + } + } + } else { + static bool receiving = false; + if (!receiving && in != 0) { + receiving = true; + } + if (receiving && _spi_in_index < 8) { + _spi_in[_spi_in_index] = in; + ++_spi_in_index; + } + if (_spi_in_index == 8) { + receiving = false; + } + SPDR = 0; + } +} diff --git a/hid/lib/drivers-avr/spi.h b/hid/lib/drivers-avr/spi.h new file mode 100644 index 00000000..fc9337e5 --- /dev/null +++ b/hid/lib/drivers-avr/spi.h @@ -0,0 +1,31 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + +#include <Arduino.h> + + +void spiBegin(); +bool spiReady(); +const uint8_t *spiGet(); +void spiWrite(const uint8_t *data); diff --git a/hid/lib/drivers-avr/usb/hid.h b/hid/lib/drivers-avr/usb/hid.h new file mode 100644 index 00000000..0bc5cb2d --- /dev/null +++ b/hid/lib/drivers-avr/usb/hid.h @@ -0,0 +1,229 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + +#include <Arduino.h> +#include <HID-Project.h> + +#include "keyboard.h" +#include "mouse.h" +#include "tools.h" +#ifdef AUM +# include "aum.h" +#endif +#include "keymap.h" + +using namespace DRIVERS; + +// ----------------------------------------------------------------------------- +#ifdef HID_USB_CHECK_ENDPOINT +// https://github.com/arduino/ArduinoCore-avr/blob/2f67c916f6ab6193c404eebe22efe901e0f9542d/cores/arduino/USBCore.cpp#L249 +// https://sourceforge.net/p/arduinomidilib/svn/41/tree/branch/3.1/Teensy/teensy_core/usb_midi/usb_api.cpp#l103 +# ifdef AUM +# define CHECK_AUM_USB { if (!aumIsUsbConnected()) { return true; } } +# else +# define CHECK_AUM_USB +# endif +# define CLS_IS_OFFLINE(_hid) \ + bool isOffline() override { \ + CHECK_AUM_USB; \ + uint8_t ep = _hid.getPluggedEndpoint(); \ + uint8_t intr_state = SREG; \ + cli(); \ + UENUM = ep & 7; \ + bool rw_allowed = UEINTX & (1 << RWAL); \ + SREG = intr_state; \ + if (rw_allowed) { \ + return false; \ + } \ + return true; \ + } +# define CHECK_HID_EP { if (isOffline()) return; } + +#else +# define CLS_IS_OFFLINE(_hid) \ + bool isOffline() override { \ + return false; \ + } +# define CHECK_HID_EP + +#endif + +class UsbKeyboard : public DRIVERS::Keyboard { + public: + UsbKeyboard() : DRIVERS::Keyboard(DRIVERS::USB_KEYBOARD) {} + + void begin() override { + _kbd.begin(); + } + + void periodic() override { +# ifdef HID_USB_CHECK_ENDPOINT + static unsigned long prev_ts = 0; + if (is_micros_timed_out(prev_ts, 50000)) { + static bool prev_online = true; + bool online = !isOffline(); + if (!_sent || (online && !prev_online)) { + _sendCurrent(); + } + prev_online = online; + prev_ts = micros(); + } +# endif + } + + void clear() override { + _kbd.releaseAll(); + } + + 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)) { + _sendCurrent(); + } + } + } + + CLS_IS_OFFLINE(_kbd) + + KeyboardLedsState getLeds() override { + uint8_t leds = _kbd.getLeds(); + KeyboardLedsState result = { + .caps = leds & LED_CAPS_LOCK, + .scroll = leds & LED_SCROLL_LOCK, + .num = leds & LED_NUM_LOCK, + }; + return result; + } + + private: + BootKeyboard_ _kbd; + bool _sent = true; + + void _sendCurrent() { +# ifdef HID_USB_CHECK_ENDPOINT + if (isOffline()) { + _sent = false; + } else { +# endif + _sent = (_kbd.send() >= 0); +# ifdef HID_USB_CHECK_ENDPOINT + } +# endif + } +}; + +#define CLS_SEND_BUTTONS \ + void sendButtons( \ + bool left_select, bool left_state, \ + bool right_select, bool right_state, \ + bool middle_select, bool middle_state, \ + bool up_select, bool up_state, \ + bool down_select, bool down_state \ + ) override { \ + if (left_select) _sendButton(MOUSE_LEFT, left_state); \ + if (right_select) _sendButton(MOUSE_RIGHT, right_state); \ + if (middle_select) _sendButton(MOUSE_MIDDLE, middle_state); \ + if (up_select) _sendButton(MOUSE_PREV, up_state); \ + if (down_select) _sendButton(MOUSE_NEXT, down_state); \ + } + +class UsbMouseAbsolute : public DRIVERS::Mouse { + public: + UsbMouseAbsolute(DRIVERS::type _type) : Mouse(_type) {} + + void begin() override { + _mouse.begin(); + _mouse.setWin98FixEnabled(getType() == DRIVERS::USB_MOUSE_ABSOLUTE_WIN98); + } + + void clear() override { + _mouse.releaseAll(); + } + + CLS_SEND_BUTTONS + + void sendMove(int x, int y) override { + CHECK_HID_EP; + _mouse.moveTo(x, y); + } + + void sendWheel(int delta_y) override { + // delta_x is not supported by hid-project now + CHECK_HID_EP; + _mouse.move(0, 0, delta_y); + } + + CLS_IS_OFFLINE(_mouse) + + private: + SingleAbsoluteMouse_ _mouse; + + void _sendButton(uint8_t button, bool state) { + CHECK_HID_EP; + if (state) _mouse.press(button); + else _mouse.release(button); + } +}; + +class UsbMouseRelative : public DRIVERS::Mouse { + public: + UsbMouseRelative() : DRIVERS::Mouse(DRIVERS::USB_MOUSE_RELATIVE) {} + + void begin() override { + _mouse.begin(); + } + + void clear() override { + _mouse.releaseAll(); + } + + CLS_SEND_BUTTONS + + void sendRelative(int x, int y) override { + CHECK_HID_EP; + _mouse.move(x, y, 0); + } + + void sendWheel(int delta_y) override { + // delta_x is not supported by hid-project now + CHECK_HID_EP; + _mouse.move(0, 0, delta_y); + } + + CLS_IS_OFFLINE(_mouse) + + private: + BootMouse_ _mouse; + + void _sendButton(uint8_t button, bool state) { + CHECK_HID_EP; + if (state) _mouse.press(button); + else _mouse.release(button); + } +}; + +#undef CLS_SEND_BUTTONS +#undef CLS_IS_OFFLINE +#undef CHECK_HID_EP diff --git a/hid/lib/drivers-avr/usb/keymap.h b/hid/lib/drivers-avr/usb/keymap.h new file mode 100644 index 00000000..224a181c --- /dev/null +++ b/hid/lib/drivers-avr/usb/keymap.h @@ -0,0 +1,139 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + +#include <HID-Project.h> + + +KeyboardKeycode keymapUsb(uint8_t code) { + switch (code) { + case 1: return KEY_A; + case 2: return KEY_B; + case 3: return KEY_C; + case 4: return KEY_D; + case 5: return KEY_E; + case 6: return KEY_F; + case 7: return KEY_G; + case 8: return KEY_H; + case 9: return KEY_I; + case 10: return KEY_J; + case 11: return KEY_K; + case 12: return KEY_L; + case 13: return KEY_M; + case 14: return KEY_N; + case 15: return KEY_O; + case 16: return KEY_P; + case 17: return KEY_Q; + case 18: return KEY_R; + case 19: return KEY_S; + case 20: return KEY_T; + case 21: return KEY_U; + case 22: return KEY_V; + case 23: return KEY_W; + case 24: return KEY_X; + case 25: return KEY_Y; + case 26: return KEY_Z; + case 27: return KEY_1; + case 28: return KEY_2; + case 29: return KEY_3; + case 30: return KEY_4; + case 31: return KEY_5; + case 32: return KEY_6; + case 33: return KEY_7; + case 34: return KEY_8; + case 35: return KEY_9; + case 36: return KEY_0; + case 37: return KEY_ENTER; + case 38: return KEY_ESC; + case 39: return KEY_BACKSPACE; + case 40: return KEY_TAB; + case 41: return KEY_SPACE; + case 42: return KEY_MINUS; + case 43: return KEY_EQUAL; + case 44: return KEY_LEFT_BRACE; + case 45: return KEY_RIGHT_BRACE; + case 46: return KEY_BACKSLASH; + case 47: return KEY_SEMICOLON; + case 48: return KEY_QUOTE; + case 49: return KEY_TILDE; + case 50: return KEY_COMMA; + case 51: return KEY_PERIOD; + case 52: return KEY_SLASH; + case 53: return KEY_CAPS_LOCK; + case 54: return KEY_F1; + case 55: return KEY_F2; + case 56: return KEY_F3; + case 57: return KEY_F4; + case 58: return KEY_F5; + case 59: return KEY_F6; + case 60: return KEY_F7; + case 61: return KEY_F8; + case 62: return KEY_F9; + case 63: return KEY_F10; + case 64: return KEY_F11; + case 65: return KEY_F12; + case 66: return KEY_PRINT; + case 67: return KEY_INSERT; + case 68: return KEY_HOME; + case 69: return KEY_PAGE_UP; + case 70: return KEY_DELETE; + case 71: return KEY_END; + case 72: return KEY_PAGE_DOWN; + case 73: return KEY_RIGHT_ARROW; + case 74: return KEY_LEFT_ARROW; + case 75: return KEY_DOWN_ARROW; + case 76: return KEY_UP_ARROW; + case 77: return KEY_LEFT_CTRL; + case 78: return KEY_LEFT_SHIFT; + case 79: return KEY_LEFT_ALT; + case 80: return KEY_LEFT_GUI; + case 81: return KEY_RIGHT_CTRL; + case 82: return KEY_RIGHT_SHIFT; + case 83: return KEY_RIGHT_ALT; + case 84: return KEY_RIGHT_GUI; + case 85: return KEY_PAUSE; + case 86: return KEY_SCROLL_LOCK; + case 87: return KEY_NUM_LOCK; + case 88: return KEY_MENU; + case 89: return KEYPAD_DIVIDE; + case 90: return KEYPAD_MULTIPLY; + case 91: return KEYPAD_SUBTRACT; + case 92: return KEYPAD_ADD; + case 93: return KEYPAD_ENTER; + case 94: return KEYPAD_1; + case 95: return KEYPAD_2; + case 96: return KEYPAD_3; + case 97: return KEYPAD_4; + case 98: return KEYPAD_5; + case 99: return KEYPAD_6; + case 100: return KEYPAD_7; + case 101: return KEYPAD_8; + case 102: return KEYPAD_9; + case 103: return KEYPAD_0; + case 104: return KEYPAD_DOT; + case 105: return KEY_POWER; + case 106: return KEY_NON_US; + case 107: return KEY_INTERNATIONAL3; + default: return KEY_ERROR_UNDEFINED; + } +} diff --git a/hid/lib/drivers-avr/usb/keymap.h.mako b/hid/lib/drivers-avr/usb/keymap.h.mako new file mode 100644 index 00000000..fe295bea --- /dev/null +++ b/hid/lib/drivers-avr/usb/keymap.h.mako @@ -0,0 +1,35 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + +#include <HID-Project.h> + +<%! import operator %> +KeyboardKeycode keymapUsb(uint8_t code) { + switch (code) { +% for km in sorted(keymap, key=operator.attrgetter("mcu_code")): + case ${km.mcu_code}: return ${km.arduino_name}; +% endfor + default: return KEY_ERROR_UNDEFINED; + } +} diff --git a/hid/lib/drivers/aum.h b/hid/lib/drivers/aum.h new file mode 100644 index 00000000..42b379aa --- /dev/null +++ b/hid/lib/drivers/aum.h @@ -0,0 +1,48 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + +#include <digitalWriteFast.h> + + +inline void aumInit() { + pinModeFast(AUM_IS_USB_POWERED_PIN, INPUT); + pinModeFast(AUM_SET_USB_VBUS_PIN, OUTPUT); + pinModeFast(AUM_SET_USB_CONNECTED_PIN, OUTPUT); + digitalWriteFast(AUM_SET_USB_CONNECTED_PIN, HIGH); +} + +inline void aumProxyUsbVbus() { + bool vbus = digitalReadFast(AUM_IS_USB_POWERED_PIN); + if (digitalReadFast(AUM_SET_USB_VBUS_PIN) != vbus) { + digitalWriteFast(AUM_SET_USB_VBUS_PIN, vbus); + } +} + +inline void aumSetUsbConnected(bool connected) { + digitalWriteFast(AUM_SET_USB_CONNECTED_PIN, connected); +} + +inline bool aumIsUsbConnected() { + return digitalReadFast(AUM_SET_USB_CONNECTED_PIN); +} diff --git a/hid/lib/drivers/factory.h b/hid/lib/drivers/factory.h new file mode 100644 index 00000000..c787347a --- /dev/null +++ b/hid/lib/drivers/factory.h @@ -0,0 +1,33 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + +#pragma once + +#include "keyboard.h" +#include "mouse.h" + +namespace DRIVERS { + + struct Factory { + static Keyboard *makeKeyboard(type _type); + static Mouse *makeMouse(type _type); + }; +} diff --git a/hid/lib/drivers/tools.cpp b/hid/lib/drivers/tools.cpp new file mode 100644 index 00000000..7f0e29ca --- /dev/null +++ b/hid/lib/drivers/tools.cpp @@ -0,0 +1,30 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + +#include <Arduino.h> + +bool is_micros_timed_out(unsigned long start_ts, unsigned long timeout) { + unsigned long now = micros(); + return ( + (now >= start_ts && now - start_ts > timeout) + || (now < start_ts && ((unsigned long)-1) - start_ts + now > timeout) + ); +} diff --git a/hid/lib/drivers/tools.h b/hid/lib/drivers/tools.h new file mode 100644 index 00000000..0f11c7c3 --- /dev/null +++ b/hid/lib/drivers/tools.h @@ -0,0 +1,26 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2022 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + + +bool is_micros_timed_out(unsigned long start_ts, unsigned long timeout); |