Shop OBEX P1 Docs P2 Docs Learn Events
Abbreviated PS2 Game Controller in C — Parallax Forums

Abbreviated PS2 Game Controller in C

Hal AlbachHal Albach Posts: 747
edited 2015-08-06 21:38 in Propeller 1
Over the past few weeks I have worked on and off on getting my newly acquired wireless PS2 game controllers to work with my Propeller Activity Board, and then eventually over to the  Propeller Project Board USB which is the brains in my bot project I recently called "Slammer".  I'm going to try to post the code (my first attempt on this new forum) in case anyone is interested in using it, improving on it, or show me ways to make it better.  First I would like to thank Andy Lindsay for his work on PS2 Controller code he wrote earlier this year which helped me tremendously.  I have to say that was my first ever foray into structures, which was a whole new learning experience for me.  In that code Andy threw down a challenge to come up with a full duplex spi function to replace the shift_in and shift_out that he was using.  I borrowed some work from Prof.  Dariusz Caban, of the Silesian University which I found on the internet and applied his function to my code - slightly modified.

/*
PS2_SPI.c

Modelled after some great work by Andy Lindsay of Parallax,
reduced down to bare minimum and made Analog mode a manual
selection (Press Mode on controller so Red Mode Led is on.
In digital mode the joysticks duplicate the nearby 4-button
keypads.

Hal Albach
*/

#include "simpletools.h"                      // Include simple tools
typedef unsigned char uchar;                  // make a long type shorter

// pin variables
const char cmd =  8;     // command  orange   output
const char data = 7;     // data     brown    input
const char clk  = 6;     // sclock   blue     output
const char att  = 5;     // att      yellow   output

uchar mode, Byte_4, Byte_5, joyRX, joyRY, joyLX, joyLY; // stuff from PS2

/* Byte 4, left to right: left, down, right, up, start, RJB, LJB, select
* Byte 5, left to right: square, X, circle, triangle, R1, L1, R2, L2
* RJB & LJB are joystick buttons, press joystick down to activate, only
* available in Analog mode.
*/

uchar spi(char);
void ps2_cog();

/****************************  MAIN   *************************************/

int main()
{
cog_run(ps2_cog, 28);

while(1)
{
printi("%c", HOME);

printi("mode = %2x

", mode);
printi("Byte_4 = %08b
", (uchar)~Byte_4);
printi("Byte_5 = %08b
", (uchar)~Byte_5);
if(mode == 0x73)                      // skip rest if not in analog mode
{
printi("joyRX = %03d
", joyRX);
printi("joyRY = %03d
", joyRY);
printi("joyLX = %03d
", joyLX);
printi("joyLY = %03d
", joyLY);
}
pause(250);
}
}

/**************************************************************************/
/***********************    CONTROLLER COG    *****************************/
/**************************************************************************/

void ps2_cog()
{
high(att);
high(clk);
low(cmd);

char poll[3] = {0x01, 0x42, 0x0};

while(1)
{
low(att);                   // attention to controller
spi(poll[0]);               // send 0x01 poll code 1, expect 0xFF return
mode = spi(poll[1]);        // send 0x42 poll code 2, expect 0x41 or 0x73 return
//                        save return in mode
spi(poll[2]);               // send dummy, receive 0x5a  (always)
Byte_4 = spi(poll[2]);      //             receive Byte_4
Byte_5 = spi(poll[2]);      //             receive Byte_5
if(mode == 0x73)            // do following only if in analog mode (0x73)
{
joyRX  = spi(poll[2]);    //             receive joyRX anaolog
joyRY  = spi(poll[2]);    //             receive joyRY anaolog
joyLX  = spi(poll[2]);    //             receive joyLX anaolog
joyLY  = spi(poll[2]);    //             receive joyLY anaolog
}
high(att);
pause(25);
}
}

/*************************   Full Duplex SPI routine   *****************************
|
Full Duplex SPI Transfer Routine by Dariusz Caban, of the Silesian University of    |
Technology’s Institute of Informatics (Gliwice, Poland).                            |
|
http://www.rpi.edu/dept/ecse/mps/Coding_SPI_sw.pdf                                  |
|
Modified for Propeller and PS2 by Hal Albach                                        |
|
************************************************************************************/

uchar spi(char io)
{
for(char i = 8; i; i--)
{
if (io & 1) high(cmd);        // if io & 1 is true set cmd pin high
else low(cmd);              //    else set cmd pin low
low(clk);
io >>= 1;                     // shift io right once, msb = 0
if(input(data)) io |= 0x80;   // data to io msb (receive slave bit)
high(clk);                    // master & slave latches input data bit
}
return io;
}

/************************************************************************************/
>/pre>

Comments

Sign In or Register to comment.