-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPsx_analog.cpp
218 lines (181 loc) · 5.64 KB
/
Psx_analog.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
/* Analog PSX Controller Decoder Library (Psx_analog.cpp)
Written by: Kieran Levin Mar 8th, 2009
Based upon the work by Kevin Ahrendt June 22nd, 2008
Analog protocal implemented with information from
http://www.curiousinventor.com/guides/ps2
Fixed reverse shifting in (MSB->LSB) now reads analog values....
Controller protocol implemented using Andrew J McCubbin's analysis.
http://www.gamesx.com/controldata/psxcont/psxcont.htm
Shift command is based on tutorial examples for ShiftIn and ShiftOut
functions both written by Carlyn Maw and Tom Igoe
http://www.arduino.cc/en/Tutorial/ShiftIn
http://www.arduino.cc/en/Tutorial/ShiftOut
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
--------------------------------------------------------------------------------
Standard Digital Pad
BYTE CMND DATA
01 0x01 idle
02 0x42 0x41
03 idle 0x5A Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7
04 idle data SLCT STRT UP RGHT DOWN LEFT
05 idle data L2 R2 L1 R1 /\ O X |_|
All Buttons active low.
--------------------------------------------------------------------------------
Analogue Controller in Red Mode
BYTE CMND DATA
01 0x01 idle
02 0x42 0x73
03 idle 0x5A Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7
04 idle data SLCT JOYR JOYL STRT UP RGHT DOWN LEFT
05 idle data L2 R2 L1 R1 /\ O X |_|
06 idle data Right Joy 0x00 = Left 0xFF = Right
07 idle data Right Joy 0x00 = Up 0xFF = Down
08 idle data Left Joy 0x00 = Left 0xFF = Right
09 idle data Left Joy 0x00 = Up 0xFF = Down
--------------------------------------------------------------------------------
*/
#include "Psx_analog.h"
#include "Spi.h"
Psx::Psx()
{
}
#define CLKDELAY 20
#define WAIT 100
byte Psx::shift(byte _dataOut) { return Spi.transfer(_dataOut,WAIT);}
/*
byte Psx::shift(byte _dataOut) // Does the actual shifting, both in and out simultaneously
{
_temp = 0;
_dataIn = 0;
for (_i = 0; _i <= 7; _i++)
{
digitalWrite(_clockPin, LOW);
if ( _dataOut & (1 << _i) ) digitalWrite(_cmndPin, HIGH); // Writes out the _dataOut bits
else digitalWrite(_cmndPin, LOW);
_temp = digitalRead(_dataPin); // Reads the data pin
if (_temp)
{
_dataIn = _dataIn | (B00000001 << _i); // Shifts the read data into _dataIn
}
digitalWrite(_clockPin, HIGH);
}
return _dataIn;
}
*/
void Psx::setupPins(byte dataPin, byte cmndPin, byte attPin, byte clockPin)
{
pinMode(dataPin, INPUT);
digitalWrite(dataPin, HIGH); // Turn on internal pull-up
_dataPin = dataPin;
pinMode(cmndPin, OUTPUT);
_cmndPin = cmndPin;
pinMode(clockPin, OUTPUT);
_clockPin = clockPin;
digitalWrite(_clockPin, HIGH);
pinMode(attPin, OUTPUT);
_attPin = attPin;
digitalWrite(_attPin, HIGH);
Motorsmall = 0x00;
Motorlarge = 0x00;
//setup SPI
Spi.mode((1<<SPR1) | (1<<SPR0) | (1<<DORD) | (1<<CPOL) | (1<<CPHA));
SPSR = SPSR | (0<<SPI2X);
}
unsigned int Psx::poll()
{
digitalWrite(_attPin, LOW);
shift(0x01);
Controller_mode = shift(0x42);
shift(0x00);
digital_buttons = (~shift(Motorsmall) & 0x00FF); //motor (change value to FF to turn on motor in dualshock controller)
digital_buttons |= (~shift(Motorlarge) << 8); //motor (Large motor, will turn on for values over 40)
if (0x70 == (Controller_mode & 0xF0))
{
Right_x = shift(0x00);
Right_y = shift(0x00);
Left_x = shift(0x00);
Left_y = shift(0x00);
}
digitalWrite(_attPin, HIGH);
return digital_buttons;
}
void Psx::Config_Enter()
{
digitalWrite(_attPin, LOW); //goto configuration mode
shift(0x01);
shift(0x43);
shift(0x00);
shift(0x01);
shift(0x00);
digitalWrite(_attPin, HIGH);
}
void Psx::Config_Exit()
{
digitalWrite(_attPin, LOW); //exit config mode
shift(0x01);
shift(0x43);
shift(0x00);
shift(0x00);
shift(0x5A);
shift(0x5A);
shift(0x5A);
shift(0x5A);
shift(0x5A);
digitalWrite(_attPin, HIGH);
}
//modes are 0x01 analog| 0x00 digital
void Psx::Config_Mode(byte mode)
{
digitalWrite(_attPin, LOW);
shift(0x01);
shift(0x44);
shift(0x00);
shift(mode);
shift(0x03); //lock analog/digital mode select button on controller
shift(0x00);
shift(0x00);
shift(0x00);
shift(0x00);
digitalWrite(_attPin, HIGH);
}
void Psx::Config_dualshock()
{
digitalWrite(_attPin, LOW); //setup motor command mapping
shift(0x01);
shift(0x4D);
shift(0x00);
shift(0x00);
shift(0x01);
shift(0xFF);
shift(0xFF);
shift(0xFF);
shift(0xFF);
digitalWrite(_attPin, HIGH);
}
byte Psx::initcontroller(byte controller_mode)
{
poll();
Config_Enter();
Config_Enter();
Config_Mode(controller_mode);
Config_Mode(controller_mode);
Config_dualshock();
Config_Exit();
Config_Exit();
Config_Exit();
Config_Exit();
poll();
poll();
poll();
poll();
return Controller_mode;
}