[comp.sys.amiga.tech] Proportional joysticks

bart@amiga.UUCP (Barry A. Whitebook) (11/06/88)

[eat this line -- please]

this is amiga!bart. as this subject keeps resurfacing in various venues i'm
going to post a some code to read prop joysticks using interrupt handlers.

/******************************************************************************
*	$Header: propjoy.c,v 34.1 85/11/24 17:59:44 bart Exp $
******************************************************************************/

#include <exec/types.h>
#include <exec/nodes.h>
#include <exec/execbase.h>
#include <exec/execname.h>
#include <graphics/gfxbase.h>
#include <graphics/graphint.h>
#include <hardware/cia.h>
#include <hardware/custom.h>
#include <hardware/intbits.h>
#include <resources/potgo.h>
extern struct Custom custom;

#define V1_POINT_2	33
#define NUM_SERVERS	2
#define MAX_COUNT	(UWORD)~1		
#define POTGO		&custom.potgo
#define POT0DAT		&custom.pot0dat
#define POT1DAT		&custom.pot1dat
#define HIGHINTPRI 127L 	
#define LOWINTPRI -127L 	
#define START_B		0	
#define DATRX_B		8	
#define DATRY_B		10
#define DATLX_B		12	
#define DATLY_B		14
#define START_F		(1L << START_B)
#define DATRX_F		(1L << DATRX_B)
#define DATRY_F		(1L << DATRY_B)	
#define DATLX_F		(1L << DATLX_B)
#define DATLY_F		(1L << DATLY_B)	
#define RPOTX		(START_F | DATRX_F) 
#define RPOTY		(START_F | DATRY_F) 
#define RPOTXY		(START_F | DATRX_F | DATRY_F) 
#define LPOTX		(START_F | DATLX_F) 
#define LPOTY		(START_F | DATLY_F) 
#define LPOTXY		(START_F | DATLX_F | DATLY_F) 

struct ExecBase *ExecBase = NULL;
struct GfxBase *GfxBase = NULL;
struct PotgoBase *PotgoBase = NULL;

UWORD oldbits = NULL;
UWORD potbits = NULL;
ULONG potdat = NULL;

struct Isrvstr server[NUM_SERVERS] = {NULL};

first_server(i) 
int i; 
{ 
	potdat = *(ULONG *)POT0DAT;
	WritePotgo(oldbits,((~1)<<8)|oldbits);
	return(NULL);
}

second_server(i)
int i;
{
	WritePotgo(potbits,((~1)<<8)|potbits);
	return(NULL);
}

use(data)
ULONG data;
{
	printf("use: data == %08lx\n",data);
}

main()
{
	LONG error = FALSE;
	struct Isrvstr *iserver[NUM_SERVERS];
	LONG i;

	server[0].is_Node.ln_Pri = HIGHINTPRI;	
	server[1].is_Node.ln_Pri = LOWINTPRI;	
	iserver[0] = &server[0];
	iserver[1] = &server[1];

	if((ExecBase = (struct ExecBase *)
	    OpenLibrary(EXECNAME,V1_POINT_2)) != NULL)
	{
		if((GfxBase = (struct GfxBase *)
		    OpenLibrary("graphics.library",V1_POINT_2)) != NULL)
		{
			if((PotgoBase = 
			    OpenResource(POTGONAME,V1_POINT_2)) != NULL)
			{
				oldbits = ~(AllocPotBits(~1));
				FreePotBits(~oldbits);
				potbits = AllocPotBits(START_F);
				Forbid();	
				iserver,first_server,0);
				AddTOF(iserver[0],first_server,0);
				iserver,second_server,1);
				AddTOF(iserver[1],second_server,1);
				Permit();
				for(i=0; i < MAX_COUNT; i++)
				{
					WaitTOF();
					use(propjoy);
				}
				Forbid();	
				for(i=0; i < NUM_SERVERS; i++)
				{
					RemTOF(iserver[i]);
				}
				Permit();
				FreePotBits(potbits);
				CloseLibrary(GfxBase);
			}
			else { error = TRUE; }
		}
		else { error = TRUE; }
		CloseLibrary(ExecBase);
	} else { error = TRUE; }
	exit(error);
}

cs161agc@sdcc10.ucsd.EDU (John Schultz) (11/06/88)

  I just put the necessary code in my vblank server and presto (just
read potx, poty then write potgo).  Works fine, but what we need now
are analog joysticks with either an adapter or correct value pots (~
1 meg)  With 470k ohm pots and following the Hardware Manual docs,
we can only generate values from 0..49; 1 meg pots bring this range
up to 0..255.  Is this an error in the Hardware Manual or are we
doing something wrong?

  John