-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps2dev.cpp
190 lines (153 loc) · 3.59 KB
/
ps2dev.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
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
/*
* ps2dev.cpp - an interface library for ps2 host.
* limitations:
* we do not handle parity errors.
* The timing constants are hard coded from the spec. Data rate is
* not impressive.
* probably lots of room for optimization.
*/
//#include "WProgram.h"
#include "Arduino.h"
#include "ps2dev.h"
//since for the device side we are going to be in charge of the clock,
//the two defines below are how long each _phase_ of the clock cycle is
#define CLKFULL 40
// we make changes in the middle of a phase, this how long from the
// start of phase to the when we drive the data line
#define CLKHALF 20
/*
* the clock and data pins can be wired directly to the clk and data pins
* of the PS2 connector. No external parts are needed.
*/
PS2dev::PS2dev(int clk, int data)
{
_ps2clk = clk;
_ps2data = data;
gohi(_ps2clk);
gohi(_ps2data);
}
/*
* according to some code I saw, these functions will
* correctly set the clock and data pins for
* various conditions. It's done this way so you don't need
* pullup resistors.
*/
void
PS2dev::gohi(int pin)
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
void
PS2dev::golo(int pin)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
int PS2dev::write(unsigned char data)
{
unsigned char i;
unsigned char parity = 1;
// Serial.print("sending ");
//Serial.println(data,HEX);
if (digitalRead(_ps2clk) == LOW) {
return -1;
}
if (digitalRead(_ps2data) == LOW) {
return -2;
}
golo(_ps2data);
delayMicroseconds(CLKHALF);
// device sends on falling clock
golo(_ps2clk); // start bit
delayMicroseconds(CLKFULL);
gohi(_ps2clk);
delayMicroseconds(CLKHALF);
for (i=0; i < 8; i++)
{
if (data & 0x01)
{
gohi(_ps2data);
} else {
golo(_ps2data);
}
delayMicroseconds(CLKHALF);
golo(_ps2clk);
delayMicroseconds(CLKFULL);
gohi(_ps2clk);
delayMicroseconds(CLKHALF);
parity = parity ^ (data & 0x01);
data = data >> 1;
}
// parity bit
if (parity)
{
gohi(_ps2data);
} else {
golo(_ps2data);
}
delayMicroseconds(CLKHALF);
golo(_ps2clk);
delayMicroseconds(CLKFULL);
gohi(_ps2clk);
delayMicroseconds(CLKHALF);
// stop bit
gohi(_ps2data);
delayMicroseconds(CLKHALF);
golo(_ps2clk);
delayMicroseconds(CLKFULL);
gohi(_ps2clk);
delayMicroseconds(CLKHALF);
delayMicroseconds(50);
return 0;
}
int PS2dev::read(unsigned char * value)
{
unsigned char data = 0x00;
unsigned char i;
unsigned char bit = 0x01;
unsigned char parity = 1;
//wait for data line to go low
while (digitalRead(_ps2data) == HIGH) {
}
//wait for clock line to go high
while (digitalRead(_ps2clk) == LOW) {
}
delayMicroseconds(CLKHALF);
golo(_ps2clk);
delayMicroseconds(CLKFULL);
gohi(_ps2clk);
delayMicroseconds(CLKHALF);
for (i=0; i < 8; i++)
{
if (digitalRead(_ps2data) == HIGH)
{
data = data | bit;
} else {
}
bit = bit << 1;
delayMicroseconds(CLKHALF);
golo(_ps2clk);
delayMicroseconds(CLKFULL);
gohi(_ps2clk);
delayMicroseconds(CLKHALF);
parity = parity ^ (data & 0x01);
}
// we do the delay at the end of the loop, so at this point we have
// already done the delay for the parity bit
// stop bit
delayMicroseconds(CLKHALF);
golo(_ps2clk);
delayMicroseconds(CLKFULL);
gohi(_ps2clk);
delayMicroseconds(CLKHALF);
delayMicroseconds(CLKHALF);
golo(_ps2data);
golo(_ps2clk);
delayMicroseconds(CLKFULL);
gohi(_ps2clk);
delayMicroseconds(CLKHALF);
gohi(_ps2data);
*value = data;
return 0;
}