-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathPsxDriver.h
318 lines (284 loc) · 10.5 KB
/
PsxDriver.h
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
/*******************************************************************************
* This file is part of PsxNewLib. *
* *
* Copyright (C) 2019-2020 by SukkoPera <software@sukkology.net> *
* *
* PsxNewLib is free software: you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* PsxNewLib is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with PsxNewLib. If not, see http://www.gnu.org/licenses. *
******************************************************************************/
/**
* \file PsxDriver.h
* \author SukkoPera <software@sukkology.net>
* \date 27 Jan 2020
* \brief Playstation controller driver
*
* Please refer to the GitHub page and wiki for any information:
* https://github.com/SukkoPera/PsxNewLib
*/
#pragma once
#include "PsxOptions.h"
// Uncomment this to have all byte exchanges logged to serial
//~ #define DUMP_COMMS
/** \brief PSX Driver Interface
*
* This is the base class defining the low-level primitives required for talking
* to PSX controllers. It is partially abstract, so it is not supposed to be
* instantiated directly.
*/
class PsxDriver {
protected:
/** \brief Size of internal communication buffer
*
* This can be sized after the longest command reply, which is 32 bytes
* (used by the PS1 MultiTap, for instance), plus the usual 3-byte header.
*/
static const byte BUFFER_SIZE = 35;
/** \brief Internal communication buffer
*
* This is used to hold replies received from the controller.
*/
byte inputBuffer[BUFFER_SIZE];
/** \brief Time last interaction with the controller took place at.
*
* We don't want to flood the controller, this helps us behave.
*/
unsigned long lastCmdTime;
/** \brief Attention Interval
*
* The interval that must elapse between the end of a command and the
* beginning of the following one (us).
*
* This can be decreased but might cause erratic readings or disconnects.
*
* \sa MIN_ATTN_INTERVAL
*/
byte attnInterval = MIN_ATTN_INTERVAL;
/** \brief Transfer a single byte to/from the controller
*
* This function must be implemented by derived classes and must transfer
* a single <i>command</i> byte to the controller and read back a single
* <i>data</i> byte.
*
* \param[in] out The command byte to send the controller
* \return The data byte returned by the controller
*/
virtual byte shiftInOut (const byte out) = 0;
public:
/** \brief Assert the Attention line
*
* This function must be implemented by derived classes and must set the
* Attention line \a low so that the controller will pay attention to what
* we will send.
*/
virtual void attention () = 0;
/** \brief Deassert the Attention line
*
* This function must be implemented by derived classes and must set the
* Attention line \a high so that the controller will no longer pay
* attention to what we will send.
*/
virtual void noAttention () = 0;
/** \brief Check if the acknowledge pulse was received
*
* This function must be implemented by derived classes and must return true
* after the Acknowledge pulse has been received (i.e.: both the falling and
* rising edges have been seen).
*
* This function MUST NOT block.
*/
virtual boolean acknowledged () = 0;
/** \brief Set the Attention Interval
*
* By default the driver will take care of not sending commands to the
* controller too often. If you have a somehow faster controller (?) or if
* you want to take care of this yourself, feel free to decrease this.
*
* \sa getAttentionInterval
* \sa MIN_ATTN_INTERVAL
*/
void setAttentionInterval (const byte us) {
attnInterval = us;
}
/** \brief Get the current Attention Interval
*
* \sa setAttentionInterval
*/
byte getAttentionInterval () const {
return attnInterval;
}
virtual void selectController () {
while (millis () - lastCmdTime <= attnInterval)
;
attention ();
delayMicroseconds (ATTN_DELAY);
}
virtual void deselectController () {
noAttention ();
lastCmdTime = millis ();
}
/** \brief Transfer several bytes to/from the controller
*
* This function transfers an array of <i>command</i> bytes to the
* controller and reads back an equally sized array of <i>data</i> bytes.
*
* \param[in] out The command bytes to send the controller
* \param[in] outLen Length of \a out, might be less than \a in, in which
* case padding bytes are generated automatically
* \param[out] in The data bytes returned by the controller, must be sized
* to hold at least \a len bytes
* \param[in] len The amount of bytes to be exchanged
* \param[in] needLastAck true if the last byte send must be acknowledged
* (i.e. if other bytes will follow)
* \return true if the transmission took place correctly (i.e.: all bytes
* were acknowledged)
*/
boolean shiftInOut (const byte *out, const byte outLen,
byte *in, const byte len,
const boolean needLastAck) {
boolean ret = true;
#ifdef DUMP_COMMS
byte inbuf[len];
#endif
for (byte i = 0; i < len; ++i) {
byte tmp = shiftInOut (out != NULL && i < outLen ? out[i] : PADDING_BYTE);
#ifdef DUMP_COMMS
inbuf[i] = tmp;
#endif
if (in != NULL) {
in[i] = tmp;
}
if (i < len - 1 || needLastAck) {
unsigned long start = micros ();
while (!acknowledged () && micros () - start < INTER_CMD_BYTE_TIMEOUT)
;
if (!acknowledged ()) {
ret = false;
}
}
}
#ifdef DUMP_COMMS
Serial.print (F("<-- "));
for (byte i = 0; i < len; ++i) {
if ((out != NULL && i < outLen && out[i] < 0x10) ||
(!(out != NULL && i < outLen) && PADDING_BYTE < 0x10)) {
Serial.print (0);
}
Serial.print (out != NULL && i < outLen ? out[i] : PADDING_BYTE, HEX);
Serial.print (' ');
}
Serial.println ();
Serial.print (F("--> "));
for (byte i = 0; i < len; ++i) {
if (inbuf[i] < 0x10)
Serial.print (0);
Serial.print (inbuf[i], HEX);
Serial.print (' ');
}
if (!ret) {
Serial.println (F("!ACK"));
} else {
Serial.println ();
}
#endif
return ret;
}
/** \brief Transfer several bytes to/from the controller
*
* This function transfers an array of <i>command</i> bytes to the
* controller and reads back the full reply of <i>data</i> bytes. The size
* of the reply is calculated automatically and padding bytes (0x5A) are
* appended to the outgoing message if it is shorter.
*
* The reply is stored in an internal buffer and will be valid until the
* next call to this function, so make sure to save anything if is needed.
*
* \param[out] out The data bytes returned by the controller, must be sized
* to hold at least \a len bytes
* \param[in] len The amount of bytes to be exchanged
* \return A pointer to a buffer containing the reply, whose size can be
* calculated with getReplyLength()
*/
byte *autoShift (const byte *out, const byte len) {
boolean txOk = false;
if (len >= 3 && len <= BUFFER_SIZE) {
/* All commands have at least 3 bytes, so shift out those first
*
* Note that the whole concept of "autoshift" revolves around us
* sending the smallest number of bytes for the command and then
* receiving as many as the reply tells us. This means that strictly
* speaking at this point we have no idea whether the command will
* be longer than 3 bytes - which we need to know in order to wait
* for the last ack or not - but I think we can safely assume so,
* since there's no way the reply can say it's shorter than 2 bytes
* (0 actually means 16!).
*/
txOk = shiftInOut (out, 3, inputBuffer, 3, true);
if (txOk && isValidReply (inputBuffer)) {
/* Reply is good, calculate length. This won't include the 3
* bytes we have already send, so it's basically the number of
* bytes we still have to exchange.
*/
const byte replyLen = getReplyLength (inputBuffer);
if (replyLen > 0) {
// Shift out rest of command
if (replyLen <= BUFFER_SIZE - 3) {
// Part of reply is still missing and we have space for it
txOk = shiftInOut (out + 3, len - 3, inputBuffer + 3, replyLen, false);
} else {
// Reply incomplete but not enough space available
txOk = false;
}
} else {
// The whole reply was gathered, txOk is already true
}
} else {
txOk = false;
}
}
return txOk ? inputBuffer : nullptr;
}
/** \brief Get reply length
*
* Calculates the length of a command reply, in bytes
*
* \param[in] buf The buffer containing the reply, must be at least 2 bytes
* long
* \return The calculated length
*/
inline static byte getReplyLength (const byte *buf) {
const byte n = buf[1] & 0x0F;
return (n == 0 ? 16 : n) * 2;
}
inline static boolean isValidReply (const byte *status) {
//~ return status[0] != 0xFF || status[1] != 0xFF || status[2] != 0xFF;
return status[1] != 0xFF && (status[2] == 0x5A || status[2] == 0x00);
//~ return /* status[0] == 0xFF && */ status[1] != 0xFF && status[2] == 0x5A;
}
/** \brief Initialize library
*
* This function shall be called before any others, it will initialize the
* communication and return if a supported controller was found. It shall
* also be called to reinitialize the communication whenever the controller
* is unplugged.
*
* Derived classes can override this function if they need to perform
* additional initializations, but shall call it on return.
*
* \return true if a supported controller was found, false otherwise
*/
virtual boolean begin () {
// Not much to do for the moment, but please make sure to call in subclasses
lastCmdTime = 0;
return true;
}
};