forked from kristopher/PS2-Mouse-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPS2Mouse.cpp
More file actions
276 lines (247 loc) · 6.56 KB
/
PS2Mouse.cpp
File metadata and controls
276 lines (247 loc) · 6.56 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WConstants.h"
#endif
#include "HardwareSerial.h"
#include "PS2Mouse.h"
PS2Mouse::PS2Mouse(int clock_pin, int data_pin, int mode) {
_clock_pin = clock_pin;
_data_pin = data_pin;
_mode = mode;
_initialized = false;
_disabled = true;
_enabled = false;
}
int PS2Mouse::clock_pin() {
return _clock_pin;
}
int PS2Mouse::data_pin() {
return _data_pin;
}
void PS2Mouse::initialize() {
pull_high(_clock_pin);
pull_high(_data_pin);
delay(20);
write(0xff); // Send Reset to the mouse
read_byte(); // Read ack byte
delay(20); // Not sure why this needs the delay
read_byte(); // blank
read_byte(); // blank
delay(20); // Not sure why this needs the delay
if (_mode == REMOTE) {
set_remote_mode();
} else {
enable_data_reporting(); // Tell the mouse to start sending data again
}
delayMicroseconds(100);
_initialized = 1;
}
void PS2Mouse::set_mode(int data) {
if (_mode == STREAM) {
disable_data_reporting(); // Tell the mouse to stop sending data.
}
write(data); // Send Set Mode
read_byte(); // Read Ack byte
if (_mode == STREAM) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
if (_initialized) {
delayMicroseconds(100);
}
}
void PS2Mouse::set_remote_mode() {
set_mode(0xf0);
_mode = REMOTE;
}
void PS2Mouse::set_stream_mode() {
set_mode(0xea);
_mode = STREAM;
}
void PS2Mouse::set_sample_rate(int rate) {
if (_mode == STREAM) {
disable_data_reporting(); // Tell the mouse to stop sending data.
}
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(rate); // Send Set Sample Rate
read_byte(); // Read ack byte
if (_mode == STREAM) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
delayMicroseconds(100);
}
void PS2Mouse::set_scaling_2_1() {
set_mode(0xe7); // Set the scaling to 2:1
}
void PS2Mouse::set_scaling_1_1() {
set_mode(0xe6); // set the scaling to 1:1
}
// This only effects data reporting in Stream mode.
void PS2Mouse::enable_data_reporting() {
if (!_enabled) {
write(0xf4); // Send enable data reporting
read_byte(); // Read Ack Byte
_enabled = true;
}
}
// Disabling data reporting in Stream Mode will make it behave like Remote Mode
void PS2Mouse::disable_data_reporting() {
if (!_disabled) {
write(0xf5); // Send disable data reporting
read_byte(); // Read Ack Byte
_disabled = true;
}
}
void PS2Mouse::set_resolution(int resolution) {
if (_mode == STREAM) {
enable_data_reporting();
}
write(0xe8); // Send Set Resolution
read_byte(); // Read ack Byte
write(resolution); // Send resolution setting
read_byte(); // Read ack Byte
if (_mode == STREAM) {
disable_data_reporting();
}
delayMicroseconds(100);
}
void PS2Mouse::write(int data) {
char i;
char parity = 1;
pull_high(_data_pin);
pull_high(_clock_pin);
delayMicroseconds(300);
pull_low(_clock_pin);
delayMicroseconds(300);
pull_low(_data_pin);
delayMicroseconds(10);
pull_high(_clock_pin); // Start Bit
while (digitalRead(_clock_pin)) {;} // wait for mouse to take control of clock)
// clock is low, and we are clear to send data
for (i=0; i < 8; i++) {
if (data & 0x01) {
pull_high(_data_pin);
} else {
pull_low(_data_pin);
}
// wait for clock cycle
while (!digitalRead(_clock_pin)) {;}
while (digitalRead(_clock_pin)) {;}
parity = parity ^ (data & 0x01);
data = data >> 1;
}
// parity
if (parity) {
pull_high(_data_pin);
} else {
pull_low(_data_pin);
}
while (!digitalRead(_clock_pin)) {;}
while (digitalRead(_clock_pin)) {;}
pull_high(_data_pin);
delayMicroseconds(50);
while (digitalRead(_clock_pin)) {;}
while ((!digitalRead(_clock_pin)) || (!digitalRead(_data_pin))) {;} // wait for mouse to switch modes
pull_low(_clock_pin); // put a hold on the incoming data.
}
int * PS2Mouse::report(int data[]) {
write(0xeb); // Send Read Data
read_byte(); // Read Ack Byte
data[0] = read(); // Status bit
data[1] = read_movement_x(data[0]); // X Movement Packet
data[2] = read_movement_y(data[0]); // Y Movement Packet
return data;
}
int PS2Mouse::read() {
return read_byte();
}
int PS2Mouse::read_byte() {
int data = 0;
pull_high(_clock_pin);
pull_high(_data_pin);
delayMicroseconds(50);
while (digitalRead(_clock_pin)) {;}
delayMicroseconds(5); // not sure why.
while (!digitalRead(_clock_pin)) {;} // eat start bit
for (int i = 0; i < 8; i++) {
bitWrite(data, i, read_bit());
}
read_bit(); // Partiy Bit
read_bit(); // Stop bit should be 1
pull_low(_clock_pin);
return data;
}
int PS2Mouse::read_bit() {
while (digitalRead(_clock_pin)) {;}
int bit = digitalRead(_data_pin);
while (!digitalRead(_clock_pin)) {;}
return bit;
}
int PS2Mouse::read_movement_x(int status) {
int x = read();
if (bitRead(status, 4)) {
for(int i = 8; i < 16; ++i) {
x |= (1<<i);
}
}
return x;
}
int PS2Mouse::read_movement_y(int status) {
int y = read();
if (bitRead(status, 5)) {
for(int i = 8; i < 16; ++i) {
y |= (1<<i);
}
}
return y;
}
int PS2IMouse::read_movement_z() {
int z = read();
if (bitRead(z, 5)) {
for(int i = 4; i < 16; ++i) {
z |= (1<<i);
}
}
return z;
}
void PS2Mouse::pull_low(int pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void PS2Mouse::pull_high(int pin) {
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
PS2IMouse::PS2IMouse(int clock_pin, int data_pin, int mode): PS2Mouse(clock_pin, data_pin, mode)
{
}
void PS2IMouse::initialize() {
PS2Mouse::initialize();
msMode();
}
void PS2IMouse::msMode() {
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(200); // Send Set Sample Rate
read_byte(); // Read ack byte
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(100); // Send Set Sample Rate
read_byte(); // Read ack byte
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(80); // Send Set Sample Rate
read_byte(); // Read ack byte
write(0xf2); // Get device ID.
read_byte(); // Read should be 03
}
int * PS2IMouse::report(int data[]) {
write(0xeb); // Send Read Data
read_byte(); // Read Ack Byte
data[0] = read(); // Status bit
data[1] = read_movement_x(data[0]); // X Movement Packet
data[2] = read_movement_y(data[0]); // Y Movement Packet
data[3] = read_movement_z(); // Z Movement Packet
return data;
}