home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of A1200
/
World_Of_A1200.iso
/
datafiles
/
text
/
c_manual
/
system
/
dirtyinput
/
analogue.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-02-27
|
6KB
|
180 lines
/***********************************************************/
/* */
/* Amiga C Encyclopedia (ACE) V3.0 Amiga C Club (ACC) */
/* ------------------------------- ------------------ */
/* */
/* Book: ACM Intuition Amiga C Club */
/* Chapter: DirtyInput Tulevagen 22 */
/* File: Analogue.c 181 41 LIDINGO */
/* Author: Anders Bjerin SWEDEN */
/* Date: 92-05-02 */
/* Version: 1.10 */
/* */
/* Copyright 1992, Anders Bjerin - Amiga C Club (ACC) */
/* */
/* Registered members may use this program freely in their */
/* own commercial/noncommercial programs/articles. */
/* */
/***********************************************************/
/* This is an Analogue Joystick reader. It is handy, easy and fast but */
/* a bit naughty function since it reads directly from the hardware */
/* registers. It can read either port 1 ("the mouse port") or port 2 */
/* ("the joystick port"). Note that an Analogue Joystick can have two */
/* buttons. */
/* */
/* Synopsis: Analogue( port, x, y, button1, button2 ); */
/* */
/* port: (UBYTE) Set the flag PORT1 if you want to check the first */
/* (mouse) port, or set the flag PORT2 if you want to check */
/* the second (joystick) port. */
/* */
/* x: (UBYTE *) Horizontal value. */
/* */
/* y: (UBYTE *) Vertical value. */
/* */
/* button1: (BOOL *) Button nr 1. TRUE=Pressed FALSE=Released. */
/* */
/* button2: (BOOL *) Button nr 2. TRUE=Pressed FALSE=Released. */
/* */
/* ------------------------------------------------------------------- */
/* */
/* HOW TO READ ANALOGUE JOYSTICKS: */
/* 1. Wait until the video beam have reached the top of the display. */
/* Use for example the WaitTOF() function in the Graphics Library. */
/* 2. Activate the potentiometers. Two timing capacitors will slowly */
/* be charged, and the charging speed depends on the position of */
/* the stick. */
/* 3. After a while the Amiga will check how much the capacitors have */
/* been charged. The result is stored in the pot0dat/pot1dat field */
/* in the Custom structure. (Bits 15-8 show the vertical position, */
/* and bits 7-0 the horizontal.) */
/* An analogue joystick can have two buttons, and their status is */
/* stored in the joy0dat/joy1dat field in the Custom structure. */
/* (If button 1 is pressed bit 1 is set, and if button 2 is pressed */
/* bit 9 is set.) */
/* 4. Go back to point 1. */
#include <exec/types.h> /* UBYTE, BOOL etc */
#include <graphics/gfxbase.h> /* struct GfxBase */
#include <graphics/gfxmacros.h> /* CINIT(), CMOVE(), etc */
#include <hardware/custom.h> /* struct Custom */
#define XMASK 0x00FF /* Horizontal: Bits 7 - 0 */
#define YMASK 0xFF00 /* Vertical: Bits 15 - 8 */
#define ANALOGUE 0x0001 /* Activate the potentiometers. */
#define PORT1 1 /* "Mouse port" */
#define PORT2 2 /* "Joystick port" */
/* Pointer to the Graphics Library: */
struct GfxBase *GfxBase;
/* Declare a Custom structure. This structure will */
/* automatically be initialized! (Nice isn't it?) */
extern struct Custom far custom;
/* Declare the functions: */
void main();
void Analogue(
UBYTE port,
UBYTE *x,
UBYTE *y,
BOOL *button1,
BOOL *button2
);
void main()
{
int timer = 0; /* Counter. */
UBYTE x, y; /* Horizontal/Vertical values. */
BOOL fire1, fire2; /* Button 1 and Button 2. */
/* Open the Graphics library: */
GfxBase = (struct GfxBase *)
OpenLibrary( "graphics.library", 0 );
if( !GfxBase )
exit( -1 ); /* ERROR! Could NOT open the Graphics library! */
printf( " X Y Button1 Button2\n---------------------------\n" );
while( timer < 100 )
{
Analogue( PORT2, &x, &y, &fire1, &fire2 );
timer++;
printf( "[%3d %3d] %8s %8s\n", x, y,
fire1 ? "Pressed " : "Released",
fire2 ? "Pressed " : "Released" );
}
/* Close the Graphics Library: */
CloseLibrary( GfxBase );
/* Everything OK, time to leave: */
exit( 0 );
}
void Analogue(
UBYTE port,
UBYTE *x,
UBYTE *y,
BOOL *button1,
BOOL *button2
)
{
UWORD pot; /* The potentiometer values. */
UWORD joy; /* Button 1 and 2. */
if( port == 1 )
{
/* Port 1 */
pot = custom.pot0dat;
joy = custom.joy0dat;
}
else
{
/* Port 2 */
pot = custom.pot1dat;
joy = custom.joy1dat;
}
*x = pot & XMASK; /* Horizontal: Bits 7 - 0 */
*y = (pot & YMASK) >> 8; /* Vertical: Bits 15 - 8 */
*button1 = joy & 0x0002 ? TRUE : 0; /* Left Button (1) */
*button2 = joy & 0x0200 ? TRUE : 0; /* Right Button (2) */
/* Wait until the video beam has reached the top of the display: */
WaitTOF();
/* Activate the potentiometers: */
custom.potgo = ANALOGUE;
}