-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyboard.cpp
103 lines (92 loc) · 3.21 KB
/
keyboard.cpp
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
95
96
97
98
99
100
101
102
103
#include "keyboard.h"
#include <stdbool.h>
#include "ps2.h"
#include "asm_functions.h"
#include "interrupt.h"
#include "printk.h"
#define KEYBOARD_BUFFER_SIZE 1024
extern "C" void keyboard_isr_wrapper(void);
// Fn keys mapped from 201 - 212
const char scancode2[0x84] = {
0, 209, 0, 205, 203, 201, 202, 212, 0, 210, 208,206, 204, '\t', '`', 0, // 0x0 - 0xf
0, 0, 0, 0, 0, 'q', '1', 0, 0, 0, 'z', 's', 'a', 'w', '2', 0, // 0x10 - 0x1f
0, 'c', 'x', 'd', 'e', '4','3', 0, 0, ' ', 'v', 'f', 't', 'r', '5', 0, // 0x20 - 0x2f
0, 'n', 'b', 'h', 'g', 'y', '6', 0, 0, 0, 'm', 'j', 'u', '7', '8', 0, // 0x30 - 0x3f
0, ',', 'k', 'i', 'o', '0', '9', 0, 0, '.', '/', 'l', ';', 'p', '-', 0, // 0x40 - 0x4f
0, 0, '\'', 0, '[', '=', 0, 0, 0, 0, '\n', ']', 0, '\\', 0, 0, // 0x50 - 0x5f
0, 0, 0, 0, 0, 0, '\b', 0, 0, '1', 0, '4', '7', 0, 0, 0, // 0x60 - 0x6f
'0', '.', '2', '5', '6', '8', 0x1b, 0, 211, '+', '3', '-', '*', '9', 0, 0, // 0x70 - 0x7f
0, 0, 0, 207}; // 0x80 - 0x83
class Keyboard {
public:
private:
static Keyboard keyboard;
Keyboard();
volatile bool shift;
volatile bool caps;
volatile bool release;
friend void keyboard_interrupt_handler(void);
};
Keyboard Keyboard::keyboard;
Keyboard::Keyboard() {
shift = false;
caps = false;
release = false;
};
void init_keyboard() {
uint8_t ack;
// Reset/test keyboard
do {
ps2_poll_command(PS2_DATA_PORT, PS2_KB_RESET_TEST_CMD);
ack = ps2_poll_read();
} while (ack == PS2_KB_RESEND);
if (ps2_poll_read() != PS2_KB_TEST_SUCCESS)
printk("Error resetting/testing keyboard\n");
// Set scancode to set 2
do {
ps2_poll_command(PS2_DATA_PORT, PS2_KB_SCANCODE_CMD);
ack = ps2_poll_read();
} while (ack == PS2_KB_RESEND);
do {
ps2_poll_command(PS2_DATA_PORT, PS2_KB_SET_SCANCODE2);
ack = ps2_poll_read();
} while (ack == PS2_KB_RESEND);
register_isr(keyboard_isr_wrapper, 0x21);
IRQ_clear_mask(1);
printk("Successfully initialized keyboard\n");
}
void keyboard_interrupt_handler(void) {
uint8_t scancode = inb(PS2_DATA_PORT);
unsigned char value;
switch (scancode) {
case 0x12: // Left Shift
Keyboard::keyboard.shift = !Keyboard::keyboard.shift;
value = 0;
break;
case 0x58: // Caps Lock
Keyboard::keyboard.caps = !Keyboard::keyboard.caps;
value = 0;
break;
case 0x59: // Right Shift
Keyboard::keyboard.shift = !Keyboard::keyboard.shift;
value = 0;
break;
case 0xf0: // Release
Keyboard::keyboard.release = true;
value = 0;
break;
default:
value = scancode2[scancode];
break;
};
if (!value) {
return;
} else if (Keyboard::keyboard.release) {
Keyboard::keyboard.release = false;
} else {
if ((Keyboard::keyboard.shift || Keyboard::keyboard.caps) && value >= 'a' && value <= 'z')
value -= ('a' - 'A');
printk("%c", value);
}
return;
}