-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPS2MouseHandler.cpp
More file actions
352 lines (314 loc) · 8.68 KB
/
PS2MouseHandler.cpp
File metadata and controls
352 lines (314 loc) · 8.68 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <Arduino.h>
#include "HardwareSerial.h"
#include "PS2MouseHandler.h"
PS2MouseHandler::PS2MouseHandler(int clock_pin, int data_pin, int mode) {
_clock_pin = clock_pin;
_data_pin = data_pin;
_mode = mode;
_initialised = false;
_disabled = true;
_enabled = false;
_device_id = 0x00;
_no_mouse = false;
_last_status = 0; // will hold copy of status byte
}
int PS2MouseHandler::clock_pin() {
return _clock_pin;
}
int PS2MouseHandler::data_pin() {
return _data_pin;
}
int PS2MouseHandler::device_id () {
return _device_id;
}
int PS2MouseHandler::status () {
return _status;
}
int PS2MouseHandler::x_movement () {
return _x_movement;
}
int PS2MouseHandler::y_movement () {
return _y_movement;
}
int PS2MouseHandler::z_movement () {
return _z_movement;
}
bool PS2MouseHandler::button(int button_num){
return (bool)( _status & get_button_mask(button_num));
}
bool PS2MouseHandler::clicked(int button_num){
if (button(button_num) && !(bool)( _last_status & get_button_mask(button_num))) {
return true;
}
else {
return false;
}
}
uint8_t PS2MouseHandler::get_button_mask(int button){
uint8_t mask = 0;
switch(button){
case 0: // left
mask = 0x01;
break;
case 1: // middle
mask = 0x04;
break;
case 2: // right
mask = 0x02;
break;
default:
mask = 0;
break;
}
return mask;
}
int PS2MouseHandler::initialise() {
int counter = 0;
int return_value = 0;
// poll mouse to get a connection
do {
return_value = try_initialise();
counter ++;
} while ((return_value != 0) && (counter < 10));
return return_value;
}
int PS2MouseHandler::try_initialise() {
pull_high(_clock_pin); // idle state
pull_high(_data_pin);
delay(500); // let mouse power on
write(0xff); // Send Reset to the mouse
if (_no_mouse){
return 100; // no mouse
}
uint8_t bat_code = read_byte(); // Read BAT completion code
if (bat_code == 0xFC){ // error?
_no_mouse = true;
return 101; // BAT failed
}
read_byte(); // Device ID - ignore for now - should be 0x00 for mouse
// set scroll wheel mode if available
set_sample_rate(200, true);
set_sample_rate(100, true);
set_sample_rate(80, true);
_device_id = get_device_id(); // 0x00 = no scroll wheel
if (_mode == PS2_MOUSE_REMOTE) {
set_remote_mode();
} else {
enable_data_reporting(); // Tell the mouse to start sending data again
}
delayMicroseconds(100);
_initialised = 1;
get_data(); // initial read of mouse data
_last_status = _status; // suppress false button presses
return 0; // OK
}
void PS2MouseHandler::set_mode(int data) {
if (_mode == PS2_MOUSE_STREAM) {
disable_data_reporting(); // Tell the mouse to stop sending data.
}
write(data); // Send Set Mode
read_byte(); // Read Ack byte
if (_mode == PS2_MOUSE_STREAM) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
if (_initialised) {
delayMicroseconds(100);
}
}
int PS2MouseHandler:: get_device_id(){
write(0xf2); // Ask mouse for device ID.
int id = read_byte(); // Read first byte - gives overall device id
if (id == 0xFA) {
id = read_byte(); // first byte was really the ack byte (as per PS/2 spec), so read it again
}
read_byte(); // try to read second byte if sent - refines device type - not needed
return id;
}
void PS2MouseHandler::set_remote_mode() {
set_mode(0xf0);
_mode = PS2_MOUSE_REMOTE;
}
void PS2MouseHandler::set_stream_mode() {
set_mode(0xea);
_mode = PS2_MOUSE_STREAM;
}
void PS2MouseHandler::set_sample_rate(int rate, bool ignore_sample_rate) {
if (_mode == PS2_MOUSE_STREAM && !ignore_sample_rate) {
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 == PS2_MOUSE_STREAM && !ignore_sample_rate) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
delayMicroseconds(100);
}
void PS2MouseHandler::set_scaling_2_1() {
set_mode(0xe7); // Set the scaling to 2:1
}
void PS2MouseHandler::set_scaling_1_1() {
set_mode(0xe6); // set the scaling to 1:1
}
// This only effects data reporting in Stream mode.
void PS2MouseHandler::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 PS2MouseHandler::disable_data_reporting() {
if (!_disabled) {
write(0xf5); // Send disable data reporting
read_byte(); // Read Ack Byte
_disabled = true;
}
}
void PS2MouseHandler::set_resolution(int resolution) {
if (_mode == PS2_MOUSE_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 == PS2_MOUSE_STREAM) {
disable_data_reporting();
}
delayMicroseconds(100);
}
void PS2MouseHandler::write(int data) {
char i;
char parity = 1;
unsigned long start_time = millis();
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
// wait for mouse to take control of clock
while (digitalRead(_clock_pin)) {
if (millis() - start_time >= 100) {
// no connection to mouse
pull_high(_data_pin); // back to waiting
_no_mouse = true;
return;
}
}
_no_mouse = false; // connection to mouse OK
// 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);
}
// wait for clock cycle
while (!digitalRead(_clock_pin)) {;}
while (digitalRead(_clock_pin)) {;}
pull_high(_data_pin); // release data line
while (digitalRead(_data_pin)) {;} // wait for mouse to take over data line
while (digitalRead(_clock_pin)) {;} // wait for mouse to take over clock
while ((!digitalRead(_clock_pin)) && (!digitalRead(_data_pin))) {;} // wait for mouse to release clock and data
}
void PS2MouseHandler::hold_incoming_data(){
pull_low(_clock_pin);
}
void PS2MouseHandler::get_data() {
_last_status = _status; // save copy of status byte
write(0xeb); // Send Read Data
read_byte(); // Read Ack Byte
_status = read(); // Status byte
_x_movement = read_movement_9(bitRead(_status, 4)); // X Movement Packet
_y_movement = read_movement_9(bitRead(_status, 5)); // Y Movement Packet
if (_device_id > 0){
// read scroll wheel
_z_movement = read_movement_z(); // Z Movement Packet
}
else {
_z_movement = 0;
};
}
uint8_t PS2MouseHandler::read() {
return read_byte();
}
uint8_t PS2MouseHandler::read_byte() {
uint8_t data = 0;
unsigned long start_time = millis();
pull_high(_clock_pin);
pull_high(_data_pin);
delayMicroseconds(50);
// read start bit but check for timeout
while (digitalRead(_clock_pin)) {
if (millis() - start_time > 100) {
// timeout
return 0;
}
}
// delayMicroseconds(5);
while (!digitalRead(_clock_pin)) {;}
// read data bits
for (int i = 0; i < 8; i++) {
bitWrite(data, i, read_bit());
}
read_bit(); // Partiy Bit
read_bit(); // Stop bit should be 1
return data;
}
int PS2MouseHandler::read_bit() {
while (digitalRead(_clock_pin)) {;}
int bit = digitalRead(_data_pin);
while (!digitalRead(_clock_pin)) {;}
return bit;
}
int16_t PS2MouseHandler::read_movement_9(bool sign_bit) {
// movement data is a 9 bit signed int using status bit and data reading
int16_t value = read();
// use status bit to get sign of reading
if (sign_bit) {
// fill upper byte with 1's for negative number
value |= 0xFF00;
}
return value;
}
int8_t PS2MouseHandler::read_movement_z() {
// z data can be mixed with extra button data so only the lower nibble is movement
// 4 bit signed
uint8_t value = read(); // an 8 bit unsigned value
// test bit 3 for sign
if(bitRead(value, 3)){
// negative - set upper nibble to 1's
value |= 0xF0;
}
else {
// positive - set upper nibble to 0's
value &= 0x0F;
}
return value;
}
void PS2MouseHandler::pull_low(int pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void PS2MouseHandler::pull_high(int pin) {
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}