Adapter now detects controller change and supports hotplugging

This commit is contained in:
2025-05-09 16:25:23 +02:00
parent bafb8e0cef
commit 9495e2f597
+44 -45
View File
@@ -19,17 +19,17 @@
PS2X ps2x; PS2X ps2x;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int error = 0; int error = -1;
byte type = 0; byte type = 0;
byte vibrate = 0; byte vibrate = 0;
byte waitTick = 0;
char waitTextOutput[32];
bool isControllerConnected;
void setup() { void setup() {
Serial.begin(57600); Serial.begin(57600);
delay(300); delay(300);
// TODO check in loop to detect disconnected and reconnected controllers
error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_ATT, PS2_DAT, pressures, rumble);
pinMode(17, OUTPUT); pinMode(17, OUTPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
@@ -39,40 +39,23 @@ void setup() {
display.setTextColor(WHITE); display.setTextColor(WHITE);
display.cp437(true); display.cp437(true);
display.clearDisplay(); display.setTextSize(2);
display.setCursor(0, 0);
display.write("hello ps2!");
display.display(); display.display();
switch (error) {
case 0:
onControllerDetected();
break;
case 1:
Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
break;
case 2:
Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");
break;
case 3:
Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
break;
}
type = ps2x.readType();
if (type == 0) {
Serial.println("DualShock controller found");
digitalWrite(17, HIGH);
XInput.setJoystickRange(0, 255);
}
} }
void loop() { void loop() {
if (error == 1 || type != 0) { if (!(error == 0 && type == 0)) {
return; // Skip loop on error checkControllerConnection();
delay(50);
return;
} }
ps2x.read_gamepad(false, vibrate); isControllerConnected = ps2x.read_gamepad(false, vibrate);
if (!isControllerConnected) {
error = -1;
}
// Face buttons // Face buttons
// Triangle ∆ // Triangle ∆
@@ -193,18 +176,34 @@ void loop() {
XInput.setJoystick(JOY_RIGHT, ps2x.Analog(PSS_RX), 255 - ps2x.Analog(PSS_RY)); XInput.setJoystick(JOY_RIGHT, ps2x.Analog(PSS_RX), 255 - ps2x.Analog(PSS_RY));
} }
void onControllerDetected() { void checkControllerConnection() {
Serial.print("Found Controller, configured successful "); error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_ATT, PS2_DAT, pressures, rumble);
Serial.print("pressures = ");
if (pressures) { display.setCursor(0, 0);
Serial.println("true "); display.clearDisplay();
} else { switch (error) {
Serial.println("false"); case 0:
} type = ps2x.readType();
Serial.print("rumble = "); if (type == 0) {
if (rumble) { display.write("Found\nDualShock 2!");
Serial.println("true)"); XInput.setJoystickRange(0, 255);
} else { } else {
Serial.println("false"); display.write("Controller\nnot\nrecognised");
}
break;
default:
memset(waitTextOutput, 0, sizeof(waitTextOutput));
strcat(waitTextOutput, "Looking\nfor\ncontroller");
for (byte i = 0; i < waitTick; i += 1) {
strcat(waitTextOutput, ".");
}
display.write(waitTextOutput);
waitTick += 1;
if (waitTick == 4) {
waitTick = 0;
}
break;
} }
display.display();
} }