-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathps2host.cpp
130 lines (113 loc) · 2.17 KB
/
ps2host.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "ps2host.h"
#include "flags.h"
#include "constants.h"
#include "WProgram.h"
#include "keyState.h"
#include "keymap.h"
void ps2ClockChangedHandler()
{
if (digitalReadFast(CLOCK) == LOW)
{
ReadPS2BitNow = true;
}
}
#define UNKNOWN -1
#define MAX_BITS 11
uint8_t bits[MAX_BITS];
uint8_t AllBytes[MAX_SCANCODE_LEN];
uint8_t bitCount = 0;
int numBytesTotal = UNKNOWN;
int byteCount = 0;
void readPs2Byte();
void readPs2Bit()
{
bits[bitCount] = digitalReadFast(DATA);
bitCount++;
if (bitCount == MAX_BITS)
{
readPs2Byte();
bitCount = 0;
}
}
void readFirstByte(uint8_t code);
void readSecondByte(uint8_t code);
void readThirdByte(uint8_t code);
void readPs2Byte()
{
uint8_t code = 0;
// Read 8 bits with the
// least significant bit first.
for (int index = 8; index >= 1; index--)
{
code = code << 1;
code += bits[index] ? 1 : 0;
}
AllBytes[byteCount] = code;
byteCount++;
if (numBytesTotal == UNKNOWN)
{
if (byteCount == 1)
{
readFirstByte(code);
}
else if (byteCount == 2)
{
readSecondByte(code);
}
else if (byteCount == 3)
{
readThirdByte(code);
}
}
if (byteCount < numBytesTotal)
{
return; // We know how many bytes we need, so just keep reading more.
}
if (byteCount == numBytesTotal) // Scancode complete.
{
memcpy(&LastScancode, &AllBytes, sizeof(uint8_t) * MAX_SCANCODE_LEN);
ScancodeReady = true;
// Reset.
numBytesTotal = UNKNOWN;
byteCount = 0;
}
}
void readFirstByte(uint8_t code)
{
switch (code)
{
case 0xF0: // F0 start is always a 2 byte break code.
numBytesTotal = 2;
break;
case 0xE0: // Pass to 2nd byte.
numBytesTotal = UNKNOWN;
break;
case 0xE1:
numBytesTotal = 8; // Must be pause key.
break;
default: // Anything else must be 1 byte makecode.
numBytesTotal = 1;
break;
}
}
void readSecondByte(uint8_t code)
{
switch (code)
{
case 0xF0:
numBytesTotal = UNKNOWN; // Pass to 3rd byte.
break;
default: // Anything else must be 2 bytes total.
numBytesTotal = 2;
break;
}
}
void readThirdByte(uint8_t code)
{
switch (code)
{
default: // Must be a 3 byte code.
numBytesTotal = 3;
break;
}
}