Category : C Source Code
Archive   : JOYSTICK.ZIP
Filename : CAL_STIC.C
Output of file : CAL_STIC.C contained in archive : JOYSTICK.ZIP
/* CAL_STICK.C */
/* written by Raymond F. Genovese */
/* Turbo-C (V1.5) */
/* unsigned character default */
/***********************************/
/* here are some routines to calibrate and read the joystick on the PC.
after calibration, joystick() returns a value corresponding to the
direction of the joystick....
1
8 2
7 0 3
6 4
5
*/
#include
#include
/* global variables to hold the calibrate values for when the stick is moved */
char maxx,maxy,minx,miny;
/* global variables to hold the state of the two fire buttons */
char button1=0,button2=0;
union REGS r;
void calibrate_joy(),fire();
char joystick(),fire_any();
int long_wait_fire();
main()
{
char ch=0;
calibrate_joy();
printf("\n OK move joystick or press a fire button to end \n");
while((!button1)&&(!button2)){
fire();
ch=joystick();
if(ch)
printf("%d",ch);
}
exit(1);
}
void calibrate_joy()
{
char count_x,count_y,joy;
char x_center,y_center,x_min,y_min,x_max,y_max;
printf("CENTER joystick and press a key ");
(void)getch();
count_x=0;
count_y=0;
outportb(0x201,0);
while((joy=inportb(0x201))&3){
if(joy&1)
count_x++;
if(joy&2)
count_y++;
}
printf("OK-%d %d \n",count_x,count_y);
x_center=count_x;
y_center=count_y;
printf("Position joystick to UPPER RIGHT and press a key ");
(void)getch();
count_x=0;
count_y=0;
outportb(0x201,0);
while((joy=inportb(0x201))&3){
if(joy&1)
count_x++;
if(joy&2)
count_y++;
}
printf("OK-%d %d \n",count_x,count_y);
x_max=count_x;
y_min=count_y;
printf("Position joystick to LOWER LEFT and press a key ");
(void)getch();
count_x=0;
count_y=0;
outportb(0x201,0);
while((joy=inportb(0x201))&3){
if(joy&1)
count_x++;
if(joy&2)
count_y++;
}
printf("OK-%d %d \n",count_x,count_y);
x_min=count_x;
y_max=count_y;
/* this is where the globals get set */
maxx=x_center+((x_max-x_center)/2);
minx=x_center-((x_center-x_min)/2);
maxy=y_center+((y_max-y_center)/2);
miny=y_center-((y_center-y_min)/2);
}
char joystick()
{
/* returns 0-8 for the joystick position
based on maxx and maxy from calibration */
char joy;
unsigned count_x,count_y;
count_x=0; count_y=0;
outportb(0x201,0);
while((joy=inportb(0x201))&3){
if(joy&1)
count_x++;
if(joy&2)
count_y++;
}
if(count_x>=maxx){
if(count_y>=maxy)
joy=4;
else if(count_y<=miny)
joy=2;
else
joy=3;
return(joy);
}
if(count_x<=minx){
if(count_y>=maxy)
joy=6;
else if (count_y<=miny)
joy=8;
else
joy=7;
return(joy);
}
if(count_y<=miny)
return((joy=1));
else if(count_y>=maxy)
return((joy=5));
else
return((joy=0));
}
void fire()
{
/* set the global vars button1 and button2 to 1 if pressed */
char button;
button=inportb(0x201);
if(button&16)
button1=0;
else
button1=1;
if(button&32)
button2=0;
else
button2=1;
}
/* these next to routines are not in the demo but you may find them interesting */
char fire_any()
{
/* returns 1 if either button is pressed */
char button;
button=inportb(0x201);
if((button&16)&&(button&32))
return(0);
else
return(1);
}
int long_wait_fire( unsigned n)
{
/* waits for n centiseconds or until any button is pressed */
/* returns 1 if timeout & 0 if button is pressed */
struct time tod;
unsigned long start_time,timeout_time,current_time;
char button;
start_time=(360000L*tod.ti_hour+6000L*tod.ti_min+
100*tod.ti_sec+tod.ti_hund);
timeout_time=start_time+n;
for( ; ; ){
gettime(&tod);
current_time=(360000L*tod.ti_hour+6000L*tod.ti_min+
100*tod.ti_sec+tod.ti_hund);
if(current_time
if(current_time>=timeout_time)
return(1);
button=inportb(0x201);
if((button&16)&&(button&32))
return(0);
}
}
Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!
This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.
But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/