The Sony PlayStation Joystick Controller

==============================================================================

Useful information about the Sony joystick and driving software.
 
						Mark Harris May '98

==============================================================================

The Sony PlayStation controller has two small joysticks and 14 buttons,
and Alwyn thought this would be great to use for controlling O.
Christer built some electronics to produce RS232-type serial 
signals from it, and I have written a driving routine that generates
X events that can be interpreted by other X programs, including O.
The joysticks use conventional potentiometers instead of the
terrible optical devices on the Nintendo, so the circuitry and code needed
are much simpler, and it seems to work much better. Note that we have
stripped out all the old electronics because we didn't understand the 
signals, but I was recently pointed at Vojtech Pavlik's site which has code 
for a Linux parallel port driver, which could possibly be adapted for 
our use, when I have time to explore it.

Christer's box transmits a sequence of 20 8-bit bytes at 9600 baud, 
in the order left_x, left_y, right_x, right_y, etc
with the data arranged as follows :

8 \
7  |
6    data
5  |
4 /
3--  1=x_data, 0=y_data 
2--  status of button corresponding to byte number
1--  1=byte number zero, 0=byte number incrementing

Any non-resting state causes data to be transmitted regularly.

The X routine is called joystick.c, and compiles on DEC Alpha and SGIs.
The name of the the terminal port is read from the env variable JOYSTICK_PORT,
and is probably "/dev/tty00", at least on the DEC Alphas.

Ideally the driver should be called from the application program, 
but for testing, you can run /home/markh/joy/joystick in any window,
and the X events will be picked up by whichever window the mouse is in.
There is a link to the latest version in /usr/local/bin on arcturus,
so you should be able to simply type 'joystick' on this machine.

Useful runtime options include :
'c'  to see confirmation of data being sent
'tN' to throw away all but every Nth data point
'h'  to get help on the less useful runtime options

You will even find an O emulator in the same directory, called simply 'joy'.

There is something a little odd with O's treatment of rotation angles,
in that it seems to ignore numbers lower than 10. At first I thought
this was because it divided by 10, so I multiplied by 10. 
But I now realise it subtracts 10, so I can now do slow rotations by 
sending values between 10 and 20. This will probably cause non-linear
behaviour in other programs.

Incorporating into existing code
================================

You need to call the driving program something like :

--------------------------------------------
fork_pid = fork();
if (fork_pid == 0) {
 execlp("joystick","joystick",0,0); 
 exit();
}
--------------------------------------------

Then it can be killed later with :

--------------------------------------------
kill (fork_pid,SIGKILL);
--------------------------------------------

The X event is dealt with as follows :

--------------------------------------------
while (1) {

  XNextEvent(display,&my_event);

  switch (my_event.type) {

  case ClientMessage:
    x = my_event.xclient.data.b[1];
    y = my_event.xclient.data.b[2];
    break;

  }
}	
--------------------------------------------

The new data structures 
=======================

The data arrives in 'my_event' as follows :

my_event.xclient.data.b[byte]

byte	contents
----	--------

0	Internal use
1	The X coordinate of the left joystick position (-80 to +80)
2	The Y coordinate of the left joystick position (-80 to +80)
3	The X coordinate of the right joystick position (-80 to +80)
4	The Y coordinate of the right joystick position (-80 to +80)
5	The X coordinate of the right joystick position (-80 to +80) (same as 3)
6	unused
7	/\ right (0 or 1)
8	[] right (0 or 1)
9	X  right (0 or 1)
10	O  right (0 or 1)
11	"1" right (0 or 1)
12	"1" left  (0 or 1)
13	-> left (0 or 1)
14	"^  left (0 or 1)
15	"<-" left (0 or 1)
16	"V"  left (0 or 1)
17	right joystick push (0 or 1)
18	left  joystick push (0 or 1)
19	88  (identifies client message as coming from Sony joystick)

Mark Harris 1998

(Back to MRH notes)