-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPs2Protocol.cpp
More file actions
203 lines (170 loc) · 5.33 KB
/
Ps2Protocol.cpp
File metadata and controls
203 lines (170 loc) · 5.33 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
* Copyright 2021 Marco Martin <mart@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2, 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 Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "Ps2Protocol.hh"
void Ps2Protocol_::begin(uint8_t clockPin, uint8_t dataPin)
{
m_clockPin = clockPin;
m_dataPin = dataPin;
pinMode(m_clockPin, INPUT_PULLUP);
pinMode(m_dataPin, INPUT_PULLUP);
m_scancodeCacheReadPos = 0;
m_scancodeCacheWritePos = 0;
for (uint32_t i = 0; i < PS2MESSAGE_CACHE_SIZE; ++i) {
m_scancodeCache[i] = 0;
m_scancodeCacheUnreadBits[i] = false;
}
reset();
attachInterrupt(digitalPinToInterrupt(clockPin), Ps2Protocol_::ps2Interrupt, FALLING);
}
void Ps2Protocol_::reset()
{
m_incomingMessage = 0;
m_outgoingMessage = 0;
m_bitCount = 0;
m_parityCount = 0;
m_lastInterruptMillis = millis();
m_readMode = true;
}
void Ps2Protocol_::ps2Interrupt()
{
if (Ps2Protocol.m_readMode) {
readBitFromInterrupt();
} else {
writeBitFromInterrupt();
}
}
void Ps2Protocol_::readBitFromInterrupt()
{
uint8_t val = digitalRead(Ps2Protocol.m_dataPin);
const uint32_t currentMillis = millis();
// Either too much time is passed since last interrupt: treat the interrupt as the start of a new message
// Or bits for a new message are already arriving
if (currentMillis - Ps2Protocol.m_lastInterruptMillis > 250) {
Ps2Protocol.reset();
}
Ps2Protocol.m_lastInterruptMillis = currentMillis;
++Ps2Protocol.m_bitCount;
switch (Ps2Protocol.m_bitCount) {
// The first incoming bit must always be 0, if not, protocol error;
case 1:
if (val != 0) {
Ps2Protocol.reset();
return;
}
break;
// Data bits
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
Ps2Protocol.m_incomingMessage |= (val << (Ps2Protocol.m_bitCount - 2));
Ps2Protocol.m_parityCount += val;
break;
// Check parity bit
case 10:
if (val == (Ps2Protocol.m_parityCount & 1)) {
Ps2Protocol.reset();
return;
}
break;
// Put scancode into buffer if
case 11:
if (val == 0) {
Ps2Protocol.reset();
}
Ps2Protocol.m_scancodeCacheWritePos = (Ps2Protocol.m_scancodeCacheWritePos + 1) % PS2MESSAGE_CACHE_SIZE;
Ps2Protocol.m_scancodeCache[Ps2Protocol.m_scancodeCacheWritePos] = Ps2Protocol.m_incomingMessage;
Ps2Protocol.m_scancodeCacheUnreadBits[Ps2Protocol.m_scancodeCacheWritePos] = true;
Ps2Protocol.reset();
break;
default:
Ps2Protocol.reset();
}
}
void Ps2Protocol_::writeBitFromInterrupt()
{
uint8_t currentBit = 0;
++Ps2Protocol.m_bitCount;
switch (Ps2Protocol.m_bitCount) {
case 1:
digitalWrite(Ps2Protocol.m_dataPin, LOW);
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
currentBit = (Ps2Protocol.m_outgoingMessage >> (Ps2Protocol.m_bitCount - 2)) & 1;
digitalWrite(Ps2Protocol.m_dataPin, currentBit);
Ps2Protocol.m_parityCount += currentBit;
break;
case 10:
digitalWrite(Ps2Protocol.m_dataPin, (~Ps2Protocol.m_parityCount & 1));
break;
case 11:
//Ps2Protocol.reset();
pinMode(Ps2Protocol.m_dataPin, INPUT_PULLUP);
break;
case 12:
Ps2Protocol.reset();
break;
default:
Ps2Protocol.reset();
}
}
uint8_t Ps2Protocol_::popScancode()
{
m_scancodeCacheReadPos = (m_scancodeCacheReadPos + 1) % PS2MESSAGE_CACHE_SIZE;
if (m_scancodeCacheUnreadBits[m_scancodeCacheReadPos]) {
m_scancodeCacheUnreadBits[m_scancodeCacheReadPos] = false;
// Serial.println(m_scancodeCache[m_scancodeCacheReadPos]);
return m_scancodeCache[m_scancodeCacheReadPos];
}
return 0;
}
void Ps2Protocol_::sendMessage(uint8_t message) {
Serial.println("Sending");
reset();
noInterrupts();
m_readMode = false;
pinMode(m_dataPin, OUTPUT);
pinMode(m_clockPin, OUTPUT);
digitalWrite(m_dataPin, HIGH);
digitalWrite(m_clockPin, HIGH);
delayMicroseconds(10);
// Tell The keyboard we are about to send a message
digitalWrite(m_clockPin, LOW);
delayMicroseconds(60);
digitalWrite(m_dataPin, LOW);
// write of the bits will be done at clock ticks from the keyboard
pinMode(m_clockPin, INPUT_PULLUP);
m_outgoingMessage = message;
// pinMode(m_dataPin, OUTPUT);
// Header bit is 0
//digitalWrite(m_dataPin, LOW);
interrupts();
}
Ps2Protocol_ Ps2Protocol;