diff options
Diffstat (limited to 'hid/src/ps2/hid.h')
-rw-r--r-- | hid/src/ps2/hid.h | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/hid/src/ps2/hid.h b/hid/src/ps2/hid.h new file mode 100644 index 00000000..09b87009 --- /dev/null +++ b/hid/src/ps2/hid.h @@ -0,0 +1,92 @@ +/***************************************************************************** +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 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 "../inline.h" + +#include "keymap.h" + +// #define PS2_KBD_CLOCK_PIN 3 +// #define PS2_KBD_DATA_PIN 2 + + +class Ps2Hid { + // https://wiki.osdev.org/PS/2_Keyboard + + public: + Ps2Hid() : _dev(PS2_KBD_CLOCK_PIN, PS2_KBD_DATA_PIN) {} + + void begin() { + _dev.keyboard_init(); + } + + INLINE void periodic() { + _dev.keyboard_handle(&_leds); + } + + INLINE void sendKey(uint8_t code, bool state) { + Ps2KeyType ps2_type; + uint8_t ps2_code; + + keymapPs2(code, &ps2_type, &ps2_code); + if (ps2_type != PS2_KEY_TYPE_UNKNOWN) { + // Не отправлялась часть нажатий. Когда clock на нуле, комп не принимает ничего от клавы. + // Этот костыль понижает процент пропущенных нажатий. + while (digitalRead(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; + } + } + } + } + + INLINE uint8_t getLedsAs(uint8_t caps, uint8_t scroll, uint8_t num) { + uint8_t result = 0; + + periodic(); + if (_leds & 0b00000100) result |= caps; + if (_leds & 0b00000001) result |= scroll; + if (_leds & 0b00000010) result |= num; + return result; + } + + private: + PS2dev _dev; + uint8_t _leds = 0; +}; |