PS2_X lib Example works with Uno/Mega but not with ZERO=M0 or DUE=M3

the lib by
Bill Porter madsci1016

can be compiled for UNO AVR but seems to be bugged or outdated for ARM CORTEX Boards (M0=Zero, M3=Due and/or different ones like Adafruit M4):

lib example code:

#include <PS2X_lib.h>  //for v1.6

/******************************************************************
 * set pins connected to PS2 controller:
 *   - 1e column: original 
 *   - 2e colmun: Stef?
 * replace pin numbers by the ones you use
 ******************************************************************/
#define PS2_DAT        13  //14    
#define PS2_CMD        11  //15
#define PS2_SEL        10  //16
#define PS2_CLK        12  //17

/******************************************************************
 * select modes of PS2 controller:
 *   - pressures = analog reading of push-butttons 
 *   - rumble    = motor rumbling
 * uncomment 1 of the lines for each mode selection
 ******************************************************************/
//#define pressures   true
#define pressures   false
//#define rumble      true
#define rumble      false

PS2X ps2x; // create PS2 Controller Class

//right now, the library does NOT support hot pluggable controllers, meaning 
//you must always either restart your Arduino after you connect the controller, 
//or call config_gamepad(pins) again after connecting the controller.

int error = 0;
byte type = 0;
byte vibrate = 0;

void setup(){
 
  Serial.begin(57600);
  
  delay(300);  //added delay to give wireless ps2 module some time to startup, before configuring it
   
  //CHANGES for v1.6 HERE!!! **************PAY ATTENTION*************
  
  //setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?) check for error
  error = ps2x.config_gamepad(PS2_CLK, PS2_CMD, PS2_SEL, PS2_DAT, pressures, rumble);
  
  if(error == 0){
    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");
    Serial.println("Try out all the buttons, X will vibrate the controller, faster as you press harder;");
    Serial.println("holding L1 or R1 will print out the analog stick values.");
    Serial.println("Note: Go to www.billporter.info for updates and to report bugs.");
  }  
  else if(error == 1)
    Serial.println("No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips");
   
  else if(error == 2)
    Serial.println("Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips");

  else if(error == 3)
    Serial.println("Controller refusing to enter Pressures mode, may not support it. ");
  
//  Serial.print(ps2x.Analog(1), HEX);
  
  type = ps2x.readType(); 
  switch(type) {
    case 0:
      Serial.print("Unknown Controller type found ");
      break;
    case 1:
      Serial.print("DualShock Controller found ");
      break;
    case 2:
      Serial.print("GuitarHero Controller found ");
      break;
 case 3:
      Serial.print("Wireless Sony DualShock Controller found ");
      break;
   }
}

void loop() {
  /* You must Read Gamepad to get new values and set vibration values
     ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
     if you don't enable the rumble, use ps2x.read_gamepad(); with no values
     You should call this at least once a second
   */  
  if(error == 1) //skip loop if no controller found
    return; 
  
  if(type == 2){ //Guitar Hero Controller
    ps2x.read_gamepad();          //read controller 
   
    if(ps2x.ButtonPressed(GREEN_FRET))
      Serial.println("Green Fret Pressed");
    if(ps2x.ButtonPressed(RED_FRET))
      Serial.println("Red Fret Pressed");
    if(ps2x.ButtonPressed(YELLOW_FRET))
      Serial.println("Yellow Fret Pressed");
    if(ps2x.ButtonPressed(BLUE_FRET))
      Serial.println("Blue Fret Pressed");
    if(ps2x.ButtonPressed(ORANGE_FRET))
      Serial.println("Orange Fret Pressed"); 

    if(ps2x.ButtonPressed(STAR_POWER))
      Serial.println("Star Power Command");
    
    if(ps2x.Button(UP_STRUM))          //will be TRUE as long as button is pressed
      Serial.println("Up Strum");
    if(ps2x.Button(DOWN_STRUM))
      Serial.println("DOWN Strum");
 
    if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
      Serial.println("Start is being held");
    if(ps2x.Button(PSB_SELECT))
      Serial.println("Select is being held");
    
    if(ps2x.Button(ORANGE_FRET)) {     // print stick value IF TRUE
      Serial.print("Wammy Bar Position:");
      Serial.println(ps2x.Analog(WHAMMY_BAR), DEC); 
    } 
  }
  else { //DualShock Controller
    ps2x.read_gamepad(false, vibrate); //read controller and set large motor to spin at 'vibrate' speed
    
    if(ps2x.Button(PSB_START))         //will be TRUE as long as button is pressed
      Serial.println("Start is being held");
    if(ps2x.Button(PSB_SELECT))
      Serial.println("Select is being held");      

    if(ps2x.Button(PSB_PAD_UP)) {      //will be TRUE as long as button is pressed
      Serial.print("Up held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_UP), DEC);
    }
    if(ps2x.Button(PSB_PAD_RIGHT)){
      Serial.print("Right held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_RIGHT), DEC);
    }
    if(ps2x.Button(PSB_PAD_LEFT)){
      Serial.print("LEFT held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_LEFT), DEC);
    }
    if(ps2x.Button(PSB_PAD_DOWN)){
      Serial.print("DOWN held this hard: ");
      Serial.println(ps2x.Analog(PSAB_PAD_DOWN), DEC);
    }   

    vibrate = ps2x.Analog(PSAB_CROSS);  //this will set the large motor vibrate speed based on how hard you press the blue (X) button
    if (ps2x.NewButtonState()) {        //will be TRUE if any button changes state (on to off, or off to on)
      if(ps2x.Button(PSB_L3))
        Serial.println("L3 pressed");
      if(ps2x.Button(PSB_R3))
        Serial.println("R3 pressed");
      if(ps2x.Button(PSB_L2))
        Serial.println("L2 pressed");
      if(ps2x.Button(PSB_R2))
        Serial.println("R2 pressed");
      if(ps2x.Button(PSB_TRIANGLE))
        Serial.println("Triangle pressed");        
    }

    if(ps2x.ButtonPressed(PSB_CIRCLE))               //will be TRUE if button was JUST pressed
      Serial.println("Circle just pressed");
    if(ps2x.NewButtonState(PSB_CROSS))               //will be TRUE if button was JUST pressed OR released
      Serial.println("X just changed");
    if(ps2x.ButtonReleased(PSB_SQUARE))              //will be TRUE if button was JUST released
      Serial.println("Square just released");     

    if(ps2x.Button(PSB_L1) || ps2x.Button(PSB_R1)) { //print stick values if either is TRUE
      Serial.print("Stick Values:");
      Serial.print(ps2x.Analog(PSS_LY), DEC); //Left stick, Y axis. Other options: LX, RY, RX  
      Serial.print(",");
      Serial.print(ps2x.Analog(PSS_LX), DEC); 
      Serial.print(",");
      Serial.print(ps2x.Analog(PSS_RY), DEC); 
      Serial.print(",");
      Serial.println(ps2x.Analog(PSS_RX), DEC); 
    }     
  }
  delay(50);  
}

does anyone know how to fix that fix that for ARM Cortex?

(error messages in following post because of (annoying) 9000 char limitation)

Compilation for target Board Arduino DUE (Programming Port):

compile errors:

Arduino: 1.8.8 (Windows 7), Board: "Arduino Due (Programming Port)"

D:\arduino\arduino-builder -dump-prefs -logger=machine -hardware D:\arduino\hardware -hardware D:\arduino\portable\packages -tools D:\arduino\tools-builder -tools D:\arduino\hardware\tools\avr -tools D:\arduino\portable\packages -built-in-libraries D:\arduino\libraries -libraries D:\arduino\portable\sketchbook\libraries -fqbn=arduino:sam:arduino_due_x_dbg -vid-pid=0X239A_0X8022 -ide-version=10808 -build-path C:\Users\hw\AppData\Local\Temp\arduino_build_721665 -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.bossac.path=D:\arduino\portable\packages\arduino\tools\bossac\1.6.1-arduino -prefs=runtime.tools.bossac-1.6.1-arduino.path=D:\arduino\portable\packages\arduino\tools\bossac\1.6.1-arduino -prefs=runtime.tools.arm-none-eabi-gcc.path=D:\arduino\portable\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1 -prefs=runtime.tools.arm-none-eabi-gcc-4.8.3-2014q1.path=D:\arduino\portable\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1 -verbose D:\arduino\portable\sketchbook\libraries\PS2X_lib\examples\PS2X_Example\PS2X_Example.ino
D:\arduino\arduino-builder -compile -logger=machine -hardware D:\arduino\hardware -hardware D:\arduino\portable\packages -tools D:\arduino\tools-builder -tools D:\arduino\hardware\tools\avr -tools D:\arduino\portable\packages -built-in-libraries D:\arduino\libraries -libraries D:\arduino\portable\sketchbook\libraries -fqbn=arduino:sam:arduino_due_x_dbg -vid-pid=0X239A_0X8022 -ide-version=10808 -build-path C:\Users\hw\AppData\Local\Temp\arduino_build_721665 -warnings=none -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.bossac.path=D:\arduino\portable\packages\arduino\tools\bossac\1.6.1-arduino -prefs=runtime.tools.bossac-1.6.1-arduino.path=D:\arduino\portable\packages\arduino\tools\bossac\1.6.1-arduino -prefs=runtime.tools.arm-none-eabi-gcc.path=D:\arduino\portable\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1 -prefs=runtime.tools.arm-none-eabi-gcc-4.8.3-2014q1.path=D:\arduino\portable\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1 -verbose D:\arduino\portable\sketchbook\libraries\PS2X_lib\examples\PS2X_Example\PS2X_Example.ino
Using board 'arduino_due_x_dbg' from platform in folder: D:\arduino\portable\packages\arduino\hardware\sam\1.6.11
Using core 'arduino' from platform in folder: D:\arduino\portable\packages\arduino\hardware\sam\1.6.11

Build-Optionen wurden verändert, alles wird neu kompiliert
Detecting libraries used...
"D:\\arduino\\portable\\packages\\arduino\\tools\\arm-none-eabi-gcc\\4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -w -x c++ -E -CC -mcpu=cortex-m3 -mthumb -DF_CPU=84000000L -DARDUINO=10808 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON "-DUSB_MANUFACTURER=\"Arduino LLC\"" "-DUSB_PRODUCT=\"Arduino Due\"" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/libsam" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/CMSIS/CMSIS/Include/" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/CMSIS/Device/ATMEL/" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\cores\\arduino" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\variants\\arduino_due_x" "C:\\Users\\hw\\AppData\\Local\\Temp\\arduino_build_721665\\sketch\\PS2X_Example.ino.cpp" -o nul
"D:\\arduino\\portable\\packages\\arduino\\tools\\arm-none-eabi-gcc\\4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -w -x c++ -E -CC -mcpu=cortex-m3 -mthumb -DF_CPU=84000000L -DARDUINO=10808 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON "-DUSB_MANUFACTURER=\"Arduino LLC\"" "-DUSB_PRODUCT=\"Arduino Due\"" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/libsam" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/CMSIS/CMSIS/Include/" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/CMSIS/Device/ATMEL/" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\cores\\arduino" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\variants\\arduino_due_x" "-ID:\\arduino\\portable\\sketchbook\\libraries\\PS2X_lib" "C:\\Users\\hw\\AppData\\Local\\Temp\\arduino_build_721665\\sketch\\PS2X_Example.ino.cpp" -o nul
"D:\\arduino\\portable\\packages\\arduino\\tools\\arm-none-eabi-gcc\\4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections -nostdlib -fno-threadsafe-statics --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -Dprintf=iprintf -w -x c++ -E -CC -mcpu=cortex-m3 -mthumb -DF_CPU=84000000L -DARDUINO=10808 -DARDUINO_SAM_DUE -DARDUINO_ARCH_SAM -D__SAM3X8E__ -mthumb -DUSB_VID=0x2341 -DUSB_PID=0x003e -DUSBCON "-DUSB_MANUFACTURER=\"Arduino LLC\"" "-DUSB_PRODUCT=\"Arduino Due\"" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/libsam" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/CMSIS/CMSIS/Include/" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\system/CMSIS/Device/ATMEL/" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\cores\\arduino" "-ID:\\arduino\\portable\\packages\\arduino\\hardware\\sam\\1.6.11\\variants\\arduino_due_x" "-ID:\\arduino\\portable\\sketchbook\\libraries\\PS2X_lib" "D:\\arduino\\portable\\sketchbook\\libraries\\PS2X_lib\\PS2X_lib.cpp" -o nul
D:\arduino\portable\sketchbook\libraries\PS2X_lib\PS2X_lib.cpp:5:20: fatal error: avr/io.h: No such file or directory

 #include <avr/io.h>

                    ^

compilation terminated.

Bibliothek PS2X_lib im Ordner: D:\arduino\portable\sketchbook\libraries\PS2X_lib (legacy) wird verwendet
exit status 1
Fehler beim Kompilieren für das Board Arduino Due (Programming Port).

As the path of the missing file indicates, the library is written specifically for the AVR architecture of the Uno/Mega, and is not compatible with other architectures. You would need to modify the library to port it to work on other architectures. You'll need to examine the library's source code to determine how difficult that would be. It all depends on how much AVR-specific code it uses.

pert:
... to determine how difficult that would be.

I did that: horribly difficult. That's a task for a pro... :-/

Yeah, it can definitely involve spending some quality time with a datasheet. It's actually a real testament to the Arduino project that we so rarely need to deal with this sort of thing. Quite often, we can use the same sketch on multiple platforms with no, or minimal, changes. The nice high-level API we use in our sketches stays the same even when the low level code underneath is quite different.

pert:
Yeah, it can definitely involve spending some quality time with a datasheet. It's actually a real testament to the Arduino project that we so rarely need to deal with this sort of thing. Quite often, we can use the same sketch on multiple platforms with no, or minimal, changes. The nice high-level API we use in our sketches stays the same even when the low level code underneath is quite different.

Actually that is exactly the reason why I use the Arduino IDE, just 1 code using a couple of libs which fit all platforms (AVRs, ARMs, ESPs) - otherwise Arduino was of no interest to me.
So tbh, that is currently a massive drawback by the PS2X Lib.
I would highly appreciate if one could fix that issue for the PS2X lib.

I have got it to basically run on Feather M0 Lora with a rather small change

Basically I went through PS2X_lib.cpp and PS2X_lib.h and replaced anything that looks like this:

#ifdef ESP8266

with this:

#if defined(ESP8266) || defined(ARDUINO_ARCH_SAMD)

I am using fully updated libraries as of 1/24/2020 and the latest Ps2x off of github GitHub - madsci1016/Arduino-PS2X: Read a Playstation 2 Gamepad or Guitar Hero Controller using an Arduino

It displays button presses + stick position in the Serial monitor, just like for Arduino.

Startup is a little odd, as it doesnt necessarily report it found a dualshock. Also if I recompile and re-upload the sketch it will complain it cant find a serial port, and serial monitor wont work. I have to power off the Feather and reconnect to usb to get Serial monitor back.

My process was as follows:

  1. I verified that my wired PS2 controller (third party, not Sony) was working on a genuine Arduino with the Ps2x example code. (It needed only 3.3v , Ground, and pins 10,11,12,13 connected as per the instructions on Bill Porter's site. No pullup resistors or 5v connection was required, fortunately. There is a troubleshooting page that goes through these options if standard connection fails).

1.a I found that many of the pictures on the web have the 4 digital io connections listed in a different order from the pin assignments found in the main PS2 Example code, I had to manually check that the Arduino pin numbers 12, 11, 10, 13, which is PS2_CLK PS2_CMD PS2_SEL and PS2_DAT in the example code, were connected to the Ps2 controller Clock, Command, Attention, and Data wires, by double checking the physical location of the wires within the controller connector.

1.b. I had also verified my feather worked, with Blink and some other examples.

  1. I hooked the Feather to the PS2 controller exactly like the Arduino: Connected wires Gnd to ground, 3.3v to Power 3.3v, and pins 10, 11, 12, 13 to PS2_Sel/Attention, Ps2_Cmd/Command, PS2_Clk/Clock, and PS2_Dat/Data wires on the ps2 controller.

2.b. I restarted the Arduino IDE, reset board to feather m0, set Serial to 57600 like in the example code, and ran it