-
Notifications
You must be signed in to change notification settings - Fork 3
/
PSX2USB_mod.ino
178 lines (149 loc) · 6.26 KB
/
PSX2USB_mod.ino
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
/*
* Copyright (C) 2020 by Eddie
*
*/
/*******************************************************************************
* 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. *
*******************************************************************************
*
* This sketch shows how the library can be used to turn a PSX controller into
* an USB one, using an Arduino Leonardo or Micro and the excellent
* ArduinoJoystickLibrary.
*
* For details on ArduinoJoystickLibrary, see
* https://github.com/MHeironimus/ArduinoJoystickLibrary.
*/
#include <PsxControllerBitBang.h>
#include <Joystick.h>
/* We must use the bit-banging interface, as SPI pins are only available on the
* ICSP header on the Leonardo.
*
*/
const byte PIN_PS2_ATT = 11;
const byte PIN_PS2_CMD = 9;
const byte PIN_PS2_DAT = 8;
const byte PIN_PS2_CLK = 10;
const unsigned long POLLING_INTERVAL = 1000U / 50U;
// Send debug messages to serial port
//#define ENABLE_SERIAL_DEBUG
PsxControllerBitBang<PIN_PS2_ATT, PIN_PS2_CMD, PIN_PS2_DAT, PIN_PS2_CLK> psx;
Joystick_ usbStick (
JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_GAMEPAD,
24, // buttonCount // my change
0, // hatSwitchCount (0-2) // my change
true, // includeXAxis
true, // includeYAxis
false, // includeZAxis
true, // includeRxAxis
true, // includeRyAxis
false, // includeRzAxis
false, // includeRudder
false, // includeThrottle
false, // includeAccelerator
false, // includeBrake
false // includeSteering
);
#ifdef ENABLE_SERIAL_DEBUG
#define dstart(spd) do {Serial.begin (spd); while (!Serial) {digitalWrite (LED_BUILTIN, (millis () / 500) % 2);}} while (0);
#define debug(...) Serial.print (__VA_ARGS__)
#define debugln(...) Serial.println (__VA_ARGS__)
#else
#define dstart(...)
#define debug(...)
#define debugln(...)
#endif
boolean haveController = false;
#define toDegrees(rad) (rad * 180.0 / PI)
#define deadify(var, thres) (abs (var) > thres ? (var) : 0)
/** \brief Dead zone for analog sticks
*
* If the analog stick moves less than this value from the center position, it
* is considered still.
*
* \sa ANALOG_IDLE_VALUE
*/
const byte ANALOG_DEAD_ZONE = 50U;
void setup () {
// Lit the builtin led whenever buttons are pressed
pinMode (LED_BUILTIN, OUTPUT);
// Init Joystick library
usbStick.begin (false); // We'll call sendState() manually to minimize lag
// This way we can output the same range of values we get from the PSX controller
usbStick.setXAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE);
usbStick.setYAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE);
usbStick.setRxAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE);
usbStick.setRyAxisRange (ANALOG_MIN_VALUE, ANALOG_MAX_VALUE);
dstart (115200);
debugln (F("Ready!"));
}
void loop () {
static unsigned long last = 0;
if (millis () - last >= POLLING_INTERVAL) {
last = millis ();
if (!haveController) {
if (psx.begin ()) {
debugln (F("Controller found!"));
if (!psx.enterConfigMode ()) {
debugln (F("Cannot enter config mode"));
} else {
// Try to enable analog sticks
if (!psx.enableAnalogSticks ()) {
debugln (F("Cannot enable analog sticks"));
}
if (!psx.exitConfigMode ()) {
debugln (F("Cannot exit config mode"));
}
}
haveController = true;
}
} else {
if (!psx.read ()) {
debugln (F("Controller lost :("));
haveController = false;
} else {
byte x, y;
// Buttons first!
// my changes
usbStick.setButton (0, psx.buttonPressed (PSB_SQUARE));
usbStick.setButton (1, psx.buttonPressed (PSB_CROSS));
usbStick.setButton (2, psx.buttonPressed (PSB_CIRCLE));
usbStick.setButton (3, psx.buttonPressed (PSB_TRIANGLE));
usbStick.setButton (4, psx.buttonPressed (PSB_L1));
usbStick.setButton (5, psx.buttonPressed (PSB_R1));
usbStick.setButton (13, psx.buttonPressed (PSB_L2));
usbStick.setButton (14, psx.buttonPressed (PSB_R2));
usbStick.setButton (8, psx.buttonPressed (PSB_SELECT));
usbStick.setButton (9, psx.buttonPressed (PSB_START));
usbStick.setButton (10, psx.buttonPressed (PSB_L3));
usbStick.setButton (11, psx.buttonPressed (PSB_R3));
usbStick.setButton (17, psx.buttonPressed (PSB_PAD_UP));
usbStick.setButton (18, psx.buttonPressed (PSB_PAD_DOWN));
usbStick.setButton (19, psx.buttonPressed (PSB_PAD_LEFT));
usbStick.setButton (20, psx.buttonPressed (PSB_PAD_RIGHT));
usbStick.setXAxis(ANALOG_IDLE_VALUE);
usbStick.setYAxis(ANALOG_IDLE_VALUE);
usbStick.setRxAxis(ANALOG_IDLE_VALUE);
usbStick.setRyAxis(ANALOG_IDLE_VALUE);
// end of my changes
// All done, send data for real!
usbStick.sendState ();
}
}
}
}