summaryrefslogtreecommitdiff
path: root/hid/src/ps2/hid.h
blob: 3f226090eab4dfc5faa751da809966da92608add (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*****************************************************************************
#                                                                            #
#    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 "keymap.h"

// #define PS2_KBD_CLOCK_PIN	7
// #define PS2_KBD_DATA_PIN		5


class Ps2HidKeyboard {
	// https://wiki.osdev.org/PS/2_Keyboard

	public:
		Ps2HidKeyboard() : _dev(PS2_KBD_CLOCK_PIN, PS2_KBD_DATA_PIN) {}

		void begin() {
			_dev.keyboard_init();
		}

		bool isOnline() {
			return true;
		}

		void periodic() {
			_dev.keyboard_handle(&_leds);
		}

		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;
					}
				}
			}
		}

		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;
};