-
Notifications
You must be signed in to change notification settings - Fork 9
/
Trackpoint.cpp
223 lines (202 loc) · 5.59 KB
/
Trackpoint.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
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
// For interfacing with a TrackPoint 4, or a compatible device.
//
// Based on the Arduino PS/2 library (`ps2.h`, `ps2.cpp`), written by Chris J.
// Kiick in January 2008 and released into the public domain. As of August
// 2013, it is available on:
//
// <http://playground.arduino.cc/componentLib/Ps2mouse>
//
// Limitations according to `ps2.cpp`:
//
// * 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.
// Copyright (C) 2013 [Felix E. Klee](mailto:[email protected])
//
// Based on Chris J. Kiick's Arduino PS/2 library, released in January 2008
// into the public domain.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "Trackpoint.h"
Trackpoint::Trackpoint(byte clkPin, byte dataPin, byte resetPin) :
_resetPin(resetPin), _clkPin(clkPin), _dataPin(dataPin)
{
gohi(_clkPin);
gohi(_dataPin);
}
// errors are ignored
void Trackpoint::writeToRamLocation(byte location, byte value) {
write(0xe2);
read(); // ACK
write(0x81);
read(); // ACK
write(location);
read(); // ACK
write(value);
read(); // ACK
}
// undefined in case of error
byte Trackpoint::readFromRamLocation(byte location) {
write(0xe2);
read(); // ACK
write(0x80);
read(); // ACK
write(location);
read(); // ACK
return read();
}
void Trackpoint::setSensitivityFactor(byte sensitivityFactor) {
writeToRamLocation(0x4a, sensitivityFactor);
}
byte Trackpoint::sensitivityFactor() {
return readFromRamLocation(0x4a);
}
void Trackpoint::setRemoteMode() {
write(0xf0);
read();
}
void Trackpoint::reset() {
pinMode(_resetPin, OUTPUT);
digitalWrite(_resetPin, HIGH);
delay(2000); // empirical value
digitalWrite(_resetPin, LOW);
}
Trackpoint::DataReport Trackpoint::readData() {
DataReport d;
write(0xeb);
read(); // ACK
d.state = read();
d.x = read();
d.y = read();
return d;
}
// Comments and code below mostly unchanged from `ps2.h`.
/*
* 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 Trackpoint::gohi(byte pin) {
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
void Trackpoint::golo(byte pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
/* write a byte to the PS2 device */
void Trackpoint::write(byte data) {
byte i;
byte parity = 1;
gohi(_dataPin);
gohi(_clkPin);
delayMicroseconds(300);
golo(_clkPin);
delayMicroseconds(300);
golo(_dataPin);
delayMicroseconds(10);
gohi(_clkPin); // start bit
/* wait for device to take control of clock */
while (digitalRead(_clkPin) == HIGH)
; // this loop intentionally left blank
// clear to send data
for (i=0; i < 8; i++)
{
if (data & 0x01)
{
gohi(_dataPin);
} else {
golo(_dataPin);
}
// wait for clock
while (digitalRead(_clkPin) == LOW)
;
while (digitalRead(_clkPin) == HIGH)
;
parity = parity ^ (data & 0x01);
data = data >> 1;
}
// parity bit
if (parity)
{
gohi(_dataPin);
} else {
golo(_dataPin);
}
// clock cycle - like ack.
while (digitalRead(_clkPin) == LOW)
;
while (digitalRead(_clkPin) == HIGH)
;
// stop bit
gohi(_dataPin);
delayMicroseconds(50);
while (digitalRead(_clkPin) == HIGH)
;
// mode switch
while ((digitalRead(_clkPin) == LOW) || (digitalRead(_dataPin) == LOW))
;
// hold up incoming data
golo(_clkPin);
}
/*
* read a byte of data from the ps2 device. Ignores parity.
*/
byte Trackpoint::read(void) {
byte data = 0x00;
byte i;
byte bit = 0x01;
// start clock
gohi(_clkPin);
gohi(_dataPin);
delayMicroseconds(50);
while (digitalRead(_clkPin) == HIGH)
;
delayMicroseconds(5); // not sure why.
while (digitalRead(_clkPin) == LOW)
; // eat start bit
for (i=0; i < 8; i++)
{
while (digitalRead(_clkPin) == HIGH)
;
if (digitalRead(_dataPin) == HIGH)
{
data = data | bit;
}
while (digitalRead(_clkPin) == LOW)
;
bit = bit << 1;
}
// eat parity bit, ignore it.
while (digitalRead(_clkPin) == HIGH)
;
while (digitalRead(_clkPin) == LOW)
;
// eat stop bit
while (digitalRead(_clkPin) == HIGH)
;
while (digitalRead(_clkPin) == LOW)
;
golo(_clkPin); // hold incoming data
return data;
}