-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLinkPS2Mouse.hpp
279 lines (246 loc) · 7.37 KB
/
LinkPS2Mouse.hpp
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
#ifndef LINK_PS2_MOUSE_H
#define LINK_PS2_MOUSE_H
// --------------------------------------------------------------------------
// A PS/2 Mouse Adapter for the GBA.
// (Based on https://github.com/kristopher/PS2-Mouse-Arduino, MIT license)
// --------------------------------------------------------------------------
// Usage:
// - 1) Include this header in your main.cpp file and add:
// LinkPS2Mouse* linkPS2Mouse = new LinkPS2Mouse();
// - 2) Add the required interrupt service routines:
// irq_init(NULL);
// irq_add(II_TIMER2, NULL);
// - 3) Initialize the library with:
// linkPS2Mouse->activate();
// - 4) Get a report:
// int data[3];
// linkPS2Mouse->report(data);
// if ((data[0] & LINK_PS2_MOUSE_LEFT_CLICK) != 0)
// ; // handle LEFT click
// data[1] // X movement
// data[2] // Y movement
// --------------------------------------------------------------------------
// considerations:
// - `activate()` or `report(...)` could freeze the system if not connected!
// - detecting timeouts using interrupts is the user's responsibility!
// --------------------------------------------------------------------------
// ____________
// | Pinout |
// |PS/2 --- GBA|
// |------------|
// |CLOCK -> SI |
// |DATA --> SO |
// |VCC ---> VCC|
// |GND ---> GND|
// --------------------------------------------------------------------------
#ifndef LINK_DEVELOPMENT
#pragma GCC system_header
#endif
#include "_link_common.hpp"
static volatile char LINK_PS2_MOUSE_VERSION[] = "LinkPS2Mouse/v7.0.3";
#define LINK_PS2_MOUSE_LEFT_CLICK 0b001
#define LINK_PS2_MOUSE_RIGHT_CLICK 0b010
#define LINK_PS2_MOUSE_MIDDLE_CLICK 0b100
/**
* @brief A PS/2 Mouse Adapter for the GBA.
*/
class LinkPS2Mouse {
private:
using u32 = unsigned int;
using u16 = unsigned short;
using u8 = unsigned char;
using s16 = signed short;
static constexpr int RCNT_GPIO = 0b1000000000000000;
static constexpr int SI_DIRECTION = 0b1000000;
static constexpr int SO_DIRECTION = 0b10000000;
static constexpr int SI_DATA = 0b100;
static constexpr int SO_DATA = 0b1000;
static constexpr int TO_TICKS = 17;
LinkPS2Mouse() = delete;
public:
/**
* @brief Constructs a new LinkPS2Mouse object.
* @param waitTimerId `(0~3)` GBA Timer used for delays.
*/
explicit LinkPS2Mouse(u8 waitTimerId) { this->waitTimerId = waitTimerId; }
/**
* @brief Returns whether the library is active or not.
*/
[[nodiscard]] bool isActive() { return isEnabled; }
/**
* @brief Activates the library.
* \warning Could freeze the system if nothing is connected!
* \warning Detect timeouts using timer interrupts!
*/
void activate() {
deactivate();
setClockHigh();
setDataHigh();
waitMilliseconds(20);
write(0xff); // send reset to the mouse
readByte(); // read ack byte
waitMilliseconds(20); // not sure why this needs the delay
readByte(); // blank
readByte(); // blank
waitMilliseconds(20); // not sure why this needs the delay
enableDataReporting(); // tell the mouse to start sending data
waitMicroseconds(100);
isEnabled = true;
}
/**
* @brief Deactivates the library.
*/
void deactivate() {
isEnabled = false;
Link::_REG_RCNT = RCNT_GPIO;
Link::_REG_SIOCNT = 0;
}
/**
* @brief Fills the `data` int array with a report. The first int contains
* *clicks* that you can check against the bitmasks
* `LINK_PS2_MOUSE_LEFT_CLICK`, `LINK_PS2_MOUSE_MIDDLE_CLICK`, and
* `LINK_PS2_MOUSE_RIGHT_CLICK`. The second int is the *X movement*, and the
* third int is the *Y movement*.
* @param data The array to be filled with data.
*/
void report(int (&data)[3]) {
write(0xeb); // send read data
readByte(); // read ack byte
data[0] = readByte(); // status bit
data[1] = readMovementX(data[0]); // X movement packet
data[2] = readMovementY(data[0]); // Y movement packet
}
private:
u8 waitTimerId;
volatile bool isEnabled = false;
void enableDataReporting() {
write(0xf4); // send enable data reporting
readByte(); // read ack byte
}
s16 readMovementX(int status) {
s16 x = readByte();
if ((status & (1 << 4)) != 0)
// negative
for (u32 i = 8; i < 16; i++)
x |= 1 << i;
return x;
}
s16 readMovementY(int status) {
s16 y = readByte();
if ((status & (1 << 5)) != 0)
// negative
for (u32 i = 8; i < 16; i++)
y |= 1 << i;
return y;
}
void write(u8 data) {
u8 parity = 1;
setDataHigh();
setClockHigh();
waitMicroseconds(300);
setClockLow();
waitMicroseconds(300);
setDataLow();
waitMicroseconds(10);
setClockHigh(); // (start bit)
while (getClock())
; // wait for mouse to take control of clock
// clock is low, and we are clear to send data
for (u32 i = 0; i < 8; i++) {
if (data & 0x01)
setDataHigh();
else
setDataLow();
// wait for clock cycle
while (!getClock())
;
while (getClock())
;
parity = parity ^ (data & 0x01);
data = data >> 1;
}
// parity
if (parity)
setDataHigh();
else
setDataLow();
while (!getClock())
;
while (getClock())
;
setDataHigh();
waitMicroseconds(50);
while (getClock())
;
while (!getClock() || !getData())
; // wait for mouse to switch modes
setClockLow(); // put a hold on the incoming data.
}
u8 readByte() {
u8 data = 0;
setClockHigh();
setDataHigh();
waitMicroseconds(50);
while (getClock())
;
while (!getClock())
; // eat start bit
for (u32 i = 0; i < 8; i++) {
data |= readBit() << i;
}
readBit(); // parity bit
readBit(); // stop bit should be 1
setClockLow();
return data;
}
volatile bool readBit() {
while (getClock())
;
volatile bool bit = getData();
while (!getClock())
;
return bit;
}
void waitMilliseconds(u16 milliseconds) {
u16 ticksOf1024Cycles = milliseconds * TO_TICKS;
Link::_REG_TM[waitTimerId].start = -ticksOf1024Cycles;
Link::_REG_TM[waitTimerId].cnt =
Link::_TM_ENABLE | Link::_TM_IRQ | Link::_TM_FREQ_1024;
Link::_IntrWait(1, Link::_TIMER_IRQ_IDS[waitTimerId]);
Link::_REG_TM[waitTimerId].cnt = 0;
}
void waitMicroseconds(u16 microseconds) {
u16 cycles = microseconds * TO_TICKS;
Link::_REG_TM[waitTimerId].start = -cycles;
Link::_REG_TM[waitTimerId].cnt =
Link::_TM_ENABLE | Link::_TM_IRQ | Link::_TM_FREQ_1;
Link::_IntrWait(1, Link::_TIMER_IRQ_IDS[waitTimerId]);
Link::_REG_TM[waitTimerId].cnt = 0;
}
volatile bool getClock() {
Link::_REG_RCNT &= ~SI_DIRECTION;
return (Link::_REG_RCNT & SI_DATA) >> 0;
}
volatile bool getData() {
Link::_REG_RCNT &= ~SO_DIRECTION;
return (Link::_REG_RCNT & SO_DATA) >> 1;
}
void setClockHigh() {
Link::_REG_RCNT |= SI_DIRECTION;
Link::_REG_RCNT |= SI_DATA;
}
void setClockLow() {
Link::_REG_RCNT |= SI_DIRECTION;
Link::_REG_RCNT &= ~SI_DATA;
}
void setDataHigh() {
Link::_REG_RCNT |= SO_DIRECTION;
Link::_REG_RCNT |= SO_DATA;
}
void setDataLow() {
Link::_REG_RCNT |= SO_DIRECTION;
Link::_REG_RCNT &= ~SO_DATA;
}
};
extern LinkPS2Mouse* linkPS2Mouse;
#endif // LINK_PS2_MOUSE_H