diff --git a/ps2-arduino.ino b/ps2-arduino.ino index c0b12dd..b9c5fec 100644 --- a/ps2-arduino.ino +++ b/ps2-arduino.ino @@ -19,17 +19,17 @@ PS2X ps2x; Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); -int error = 0; +int error = -1; byte type = 0; byte vibrate = 0; +byte waitTick = 0; +char waitTextOutput[32]; +bool isControllerConnected; void setup() { Serial.begin(57600); 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); if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { @@ -39,40 +39,23 @@ void setup() { display.setTextColor(WHITE); display.cp437(true); - display.clearDisplay(); - display.setCursor(0, 0); - display.write("hello ps2!"); + display.setTextSize(2); 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() { - if (error == 1 || type != 0) { - return; // Skip loop on error - } + if (!(error == 0 && type == 0)) { + checkControllerConnection(); - ps2x.read_gamepad(false, vibrate); + + delay(50); + return; + } + + isControllerConnected = ps2x.read_gamepad(false, vibrate); + if (!isControllerConnected) { + error = -1; + } // Face buttons // Triangle ∆ @@ -193,18 +176,34 @@ void loop() { XInput.setJoystick(JOY_RIGHT, ps2x.Analog(PSS_RX), 255 - ps2x.Analog(PSS_RY)); } -void onControllerDetected() { - Serial.print("Found Controller, configured successful "); - Serial.print("pressures = "); - if (pressures) { - Serial.println("true "); - } else { - Serial.println("false"); - } - Serial.print("rumble = "); - if (rumble) { - Serial.println("true)"); - } else { - Serial.println("false"); +void checkControllerConnection() { + error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_ATT, PS2_DAT, pressures, rumble); + + display.setCursor(0, 0); + display.clearDisplay(); + switch (error) { + case 0: + type = ps2x.readType(); + if (type == 0) { + display.write("Found\nDualShock 2!"); + XInput.setJoystickRange(0, 255); + } else { + 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(); }