PS2X_Example_ESP32.ino 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <PS2X_lib.h> //for v1.6
  2. /******************************************************************
  3. * set pins connected to PS2 controller:
  4. * - 1e column: original
  5. * - 2e colmun: Stef?
  6. * replace pin numbers by the ones you use
  7. ******************************************************************/
  8. // ESP32 pin
  9. // https://github.com/espressif/arduino-esp32/blob/master/docs/esp32_pinmap.png
  10. #define PS2_DAT 19 //MISO 19
  11. #define PS2_CMD 23 //MOSI 23
  12. #define PS2_SEL 5 //SS 5
  13. #define PS2_CLK 18 //SLK 18
  14. /******************************************************************
  15. * select modes of PS2 controller:
  16. * - pressures = analog reading of push-butttons
  17. * - rumble = motor rumbling
  18. * uncomment 1 of the lines for each mode selection
  19. ******************************************************************/
  20. #define pressures false
  21. #define rumble false
  22. PS2X ps2x; // create PS2 Controller Class
  23. //right now, the library does NOT support hot pluggable controllers, meaning
  24. //you must always either restart your Arduino after you connect the controller,
  25. //or call config_gamepad(pins) again after connecting the controller.
  26. int error = -1;
  27. byte type = 0;
  28. byte vibrate = 0;
  29. int tryNum = 1;
  30. void setup(){
  31. // 115200
  32. Serial.begin(115200);
  33. //added delay to give wireless ps2 module some time to startup, before configuring it
  34. //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  35. while (error != 0) {
  36. delay(1000);// 1 second wait
  37. //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  38. error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
  39. Serial.print("#try config ");
  40. Serial.println(tryNum);
  41. tryNum ++;
  42. }
  43. Serial.println(ps2x.Analog(1), HEX);
  44. type = ps2x.readType();
  45. switch(type) {
  46. case 0:
  47. Serial.println(" Unknown Controller type found ");
  48. break;
  49. case 1:
  50. Serial.println(" DualShock Controller found ");
  51. break;
  52. case 2:
  53. Serial.println(" GuitarHero Controller found ");
  54. break;
  55. case 3:
  56. Serial.println(" Wireless Sony DualShock Controller found ");
  57. break;
  58. }
  59. }
  60. void loop() {
  61. if(type == 1){ //DualShock Controller
  62. ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
  63. //will be TRUE as long as button is pressed
  64. if(ps2x.Button(PSB_START))
  65. Serial.println("Start is being held");
  66. if(ps2x.Button(PSB_SELECT))
  67. Serial.println("Select is being held");
  68. //will be TRUE as long as button is pressed
  69. if(ps2x.Button(PSB_PAD_UP)) {
  70. Serial.print("Up held this hard: ");
  71. Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
  72. }
  73. if(ps2x.Button(PSB_PAD_RIGHT)){
  74. Serial.print("Right held this hard: ");
  75. Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
  76. }
  77. if(ps2x.Button(PSB_PAD_LEFT)){
  78. Serial.print("LEFT held this hard: ");
  79. Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
  80. }
  81. if(ps2x.Button(PSB_PAD_DOWN)){
  82. Serial.print("DOWN held this hard: ");
  83. Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
  84. }
  85. vibrate = ps2x.Analog(PSAB_CROSS); //this will set the large motor vibrate speed based on how hard you press the blue (X) button
  86. if (ps2x.NewButtonState()) { //will be TRUE if any button changes state (on to off, or off to on)
  87. if(ps2x.Button(PSB_L3))
  88. Serial.println("L3 pressed");
  89. if(ps2x.Button(PSB_R3))
  90. Serial.println("R3 pressed");
  91. if(ps2x.Button(PSB_L2))
  92. Serial.println("L2 pressed");
  93. if(ps2x.Button(PSB_R2))
  94. Serial.println("R2 pressed");
  95. if(ps2x.Button(PSB_TRIANGLE))
  96. Serial.println("△ pressed");
  97. }
  98. //△□○×
  99. if(ps2x.ButtonPressed(PSB_CIRCLE)) //will be TRUE if button was JUST pressed
  100. Serial.println("○ just pressed");
  101. if(ps2x.NewButtonState(PSB_CROSS)) //will be TRUE if button was JUST pressed OR released
  102. Serial.println("× just changed");
  103. if(ps2x.ButtonReleased(PSB_SQUARE)) //will be TRUE if button was JUST released
  104. Serial.println("□ just released");
  105. if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
  106. Serial.print("Stick Values:");
  107. Serial.print(ps2x.Analog(PSS_LY)); //Left stick, Y axis. Other options: LX, RY, RX
  108. Serial.print(",");
  109. Serial.print(ps2x.Analog(PSS_LX), DEC);
  110. Serial.print(",");
  111. Serial.print(ps2x.Analog(PSS_RY), DEC);
  112. Serial.print(",");
  113. Serial.println(ps2x.Analog(PSS_RX), DEC);
  114. }
  115. }
  116. delay(50);
  117. }