-
Notifications
You must be signed in to change notification settings - Fork 0
/
Psx.cpp
79 lines (60 loc) · 2.02 KB
/
Psx.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
/* PSX Controller Decoder Library (Psx.cpp)
Written by: Kevin Ahrendt June 22nd, 2008
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/>.
*/
#include "Psx.h"
Psx::Psx()
{
}
byte Psx::shift(byte _dataOut) // Does the actual shifting, both in and out simultaneously
{
for (_i = 0; _i <= 7; _i++)
{
digitalWrite(_clockPin, LOW);
digitalWrite(_cmndPin, bitRead(_dataOut, _i));
digitalWrite(_clockPin, HIGH);
bitWrite(_dataIn, 7 - _i, digitalRead(_dataPin));
}
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(attPin, OUTPUT);
_attPin = attPin;
digitalWrite(_attPin, HIGH);
pinMode(clockPin, OUTPUT);
_clockPin = clockPin;
digitalWrite(_clockPin, HIGH);
}
unsigned int Psx::read()
{
digitalWrite(_attPin, LOW);
shift(0x01);
shift(0x42);
shift(0xFF);
_data1 = ~shift(0xFF);
_data2 = ~shift(0xFF);
digitalWrite(_attPin, HIGH);
_dataOut = (_data2 << 8) | _data1;
return _dataOut;
}