PS2 Controller Read/Write

Hi

I’m lookin at the code for the PS2 generated program. I think I understand the “shiftin” command. It with that the buttons on the PS2 controller is read right? However I dont understand what the rest of the code does. For instance what is read from the controller here “shiftin DAT,CLK,FASTLSBPOST,[DS2Mode\8]”, and what is this comparison? “if DS2Mode <> PadMode then” and what are all the things read out to the controller after that.

[code] low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8]
shiftin DAT,CLK,FASTLSBPOST,[DS2Mode\8]
high SEL
pause 1

low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8]	
shiftin DAT,CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, |
	DualShock(4)\8, DualShock(5)\8, DualShock(6)\8]
high SEL
pause 1	

if DS2Mode <> PadMode then
	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$1\8,$43\8,$0\8,$1\8,$0\8] ;CONFIG_MODE_ENTER
	high SEL
	pause 1

	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
	high SEL
	pause 1

	;low SEL
	;shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE
	;high SEL
	;pause 1

	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
	high SEL
	pause 1

	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
	high SEL
	pause 1

	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
	high SEL
	pause 288

	goto main
endif[/code]

I think that checks to see if the controller is in analog mode.

Is that what decides if the sticks will work or not? And what is the comparison done further down in the code, with PadMode.

Yep. I’m not exactly sure, but I’m just guessing that the comparison is for to check if the controller is in analog mode and if it’s not, which is where the “Not” operator comes in (<>), then it sets the controller into analog mode.


      low SEL
      shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
      high SEL
      pause 1

      ;low SEL
      ;shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE
      ;high SEL
      ;pause 1

      low SEL
      shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
      high SEL
      pause 1

      low SEL
      shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
      high SEL
      pause 1

      low SEL
      shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
      high SEL
      pause 288

Also, the above is the controller setting various settings.

That’s the way I read it. wow (?) has has it.

Otherwise one has to press the analog button on the PS2 joystick just right to get it into analog mode. Analog mode means we get 0-255 type values for the joystick, not just on/off (I think).

Run the PS2/BB2 test program to read the PS2 and see the values.

Alan KM6VV

So if I understand you correctly, in this line it reads the Analog button on the PS2 Controller.

shiftin DAT,CLK,FASTLSBPOST,[DS2Mode\8]

And in this line the program checks if the controller is in analog mode by comparing the value it read from the Analog button with a constant hex value, PadMode.

if DS2Mode <> PadMode then

And the rest of the code is just settings sendt to the controller to set it in analog mode if it is not already in analog mode?

[code]low SEL
shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8] ;SET_MODE_AND_LOCK
high SEL
pause 1

  ;low SEL 
  ;shiftout CMD,CLK,FASTLSBPRE,$01\8,$4D\8,$00\8,$00\8,$01\8,$FF\8,$FF\8,$FF\8,$FF\8] ;VIBRATION_ENABLE 
  ;high SEL 
  ;pause 1 

  low SEL 
  shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE 
  high SEL 
  pause 1 

  low SEL 
  shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE 
  high SEL 
  pause 1 

  low SEL 
  shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT 
  high SEL 
  pause 288[/code]

If this is the case, why bother to check? Why not just set it in analog mode right away?

That’s about it. It actually reads a “code” for analog mode (explained in the comments).

You could, but I take it that it might take a couple of tries to get it into analog mode, hence the loop.

And I had my brand new PS2/ BB2 fail last night! I had the BB2/PS2 test program running, even a pair of servos connected up, and the RCVR stopped hearing the wireless joystick! At first I thought it was the battery in the joystick, but a new set didn’t help. I’ve only got one LED illuminated on the RCVR. No program changes, no wiring changes. Guess I’ll have to drag out the 'scope and see what’s happening tonight.

I use Schmart Board pre-made jumper wires, not the available (huge) connector and wiring harness. They’ve been working fine on my hexapod. I suspect them first.

Alan KM6VV

To follow on here to what Wow and others have said and help confuse the issue. :laughing:

The code first queries the PS2 to see what mode it is in. If it finds that it is in mode hex 79, then it knows from the high 4 bits of the value 7 that it is in analog mode and from the low 4 bits that it has 9 hex words of extra data that will be returned by the PS2. The format of these packets is described in some other posts and websites such as: curiousinventor.com/guides/ps2.

For all of the programs that I have currently uses on the Atom Pro, we could easily rely on the mode hex 73 as we typicall only read in the states of the buttons (2 bytes) and the four bytes for the joysticks. The extra bytes that are returned in mode 79 are used to find out how much force is on each of the buttons.

So for most programs, the initialization could be simplfied to check the high nible for a value of 7 and if it is not, then output the commands:
Config_mode_enter, Set_mode_and_lock, config_mode_exit.

Sorry if I confused everyone here :blush:

Kurt

Interesting! Seems to be more information out there on the PS2 then a year ago. I wasn’t aware of the significance of the bits in the mode.

Shorter packets might be better, if one doesn’t need the additional information!

Alan KM6VV

I dind’t understand nything you wrote here at first, but after reading a little more about the controller I think I understand it better. By using $79 we check if its in a mode where we receive 18 extra bytes of data after the header. But we only need 6 bytes of data to read the buttons and the sticks.

I have put in some new comments in the code trying to identify each part and what it does.

[code] ’ PlayStation 2 Controller Query

' Check Controller Mode
low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8]
shiftin DAT,CLK,FASTLSBPOST,[DS2Mode\8]
high SEL
pause 1

;Collects data from PS2 --> Reads Buttons (1-2) & Sticks (3-6)  (DualShock(0)?????)
low SEL
shiftout CMD,CLK,FASTLSBPRE,$1\8,$42\8]	
shiftin DAT,CLK,FASTLSBPOST,[DualShock(0)\8, DualShock(1)\8, DualShock(2)\8, DualShock(3)\8, |
	DualShock(4)\8, DualShock(5)\8, DualShock(6)\8]
high SEL
pause 1	

if DS2Mode <> PadMode then

	' Enter Config/Read Mode (escape mode), (Enter Config Mode --> Nr.3 = $01\8, Exit Config Mode --> Nr.3 = $01\8)
	' Tells the controller to allow/read configurations??
	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$01\8,$00\8]
	high SEL
	pause 1

	' Set Major Mode, $44\8 (DualShock --> Nr.4 = $01\8, Digital --> Nr.4 = $00\8), Disables Pressure-Sensitive Buttons
	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$44\8,$00\8,$01\8,$03\8,$00\8,$00\8,$00\8,$00\8]
	high SEL
	pause 1

	' ????
	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$4F\8,$00\8,$FF\8,$FF\8,$03\8,$00\8,$00\8,$00\8] ;SET_DS2_NATIVE_MODE
	high SEL
	pause 1
	
	' ????
	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$5A\8,$5A\8,$5A\8,$5A\8,$5A\8] ;CONFIG_MODE_EXIT_DS2_NATIVE
	high SEL
	pause 1

	' Exit Config/Read Mode (escape mode), (Enter Config Mode --> Nr.3 = $01\8, Exit Config Mode --> Nr.3 = $01\8)
	' Tells the controller to stop allowing/reading configurations???
	low SEL
	shiftout CMD,CLK,FASTLSBPRE,$01\8,$43\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8,$00\8] ;CONFIG_MODE_EXIT
	high SEL
	pause 288

[/code]

Did i understand it right based on my comments?? I think i understand every part except the SET_DS2_NATIVE_MODE and the CONFIG_MODE_EXIT_DS2_NATIVE instructions. I used this website docs.google.com/View?docid=ddbmmwds_5cw4pk3 to find some more info. It has this to say about the $4f instruction

I did not qite understand how one specifies which data one want and not want.