/**********************************************************
 * gun.c: Virtual Reality Utility for allow for a 
 * Nintendo gun on /dev/bpp0 parallel port sparc10ZX
 * Written in WorldToolKit 2.1Beta for Sparc10 ZX system under Solaris 2.3
 *    originally written by:
 *    Steve Wayde and
 *    Roger W. Webster, Ph.D.
 *    MILLERSVILLE UNIVERSITY
 *    Intelligent Machines Laboratory
 *    CS373 Graphics & Virtual Reality
 *	o spears to shoot at passing fish
 *	o nintendo GUN recognition (shoots spears)
*********************************************************/
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioccom.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <sys/bpp_io.h>

/* struct bpp_error_status err;
struct bpp_error_status *addr = &err; */
struct bpp_transfer_parms tparms;
struct  bpp_transfer_parms *addr_tparms = &tparms;

struct  bpp_pins pins;
struct  bpp_pins *addr_pins = &pins;
char gunbuffer[10] = "S";
int commongunfile;
/************************************************
 ***		Initialize the Parallel Port  ***
 ************************************************/
int GUN_INIT(void)
{
	int n, r;
	system ("/sparc10/pub/setuppp.out");
        /* Open the file */
        if ((commongunfile = open("/dev/bpp0", 0)) == NULL) 
	{
       		printf ("error open \n");
		perror("/dev/bpp0");
       		exit(1);
        } /* end of if commongunfile */
	printf("commongunfile is %d\n", commongunfile);
	if (commongunfile == -1) 
	     printf("WOOOW--> problems opening /dev/bpp0\n");
        r = ioctl(commongunfile, BPPIOC_GETPARMS, &tparms);
	printf("ioctl r is %d\n", r);
	printf("read handshake: is %x\n", tparms.read_handshake);
 	if (tparms.read_handshake == BPP_ACK_BUSY_HS) 
		printf ("BPP ack + busy hs\n");	
	if (tparms.read_handshake == BPP_NO_HS) 
		printf ("BPP NO hs\n");
	if (tparms.read_handshake == BPP_ACK_HS) 
		printf ("BPP ACK hs\n");
	if (tparms.read_handshake == BPP_BUSY_HS) 
		printf ("BPP busy hs\n");
 	if (tparms.read_handshake == BPP_CLEAR_MEM) 
		printf ("BPP clear mem hs\n");	
 	if (tparms.read_handshake == BPP_XSCAN_HS) 
		printf ("BPP x scan hs\n");	

	printf("read set up time: is %x\n",tparms.read_setup_time);
	printf("read strobe width: is %x\n",tparms.read_strobe_width);
	printf("read timeout: is %d\n",tparms.read_timeout);
	if (r == 0) { return 1; }
	else { return 0; }
} /* end GUN-INIT */

void check_gunfire(int f1[2])
{   
        int count = -1, r;      
	while (1) 
	{ 
            r = ioctl(commongunfile, BPPIOC_TESTIO);
            if (r == -1) {
                count = -1; }
	    else
               {if (count == -1)
		  write(f1[1], gunbuffer, 1);
	        count = 0;
	    }
        }      
} /* end check_gunfire */

int GUN_SHOT(void)
{
    int count, r;

    	r = ioctl(commongunfile, BPPIOC_TESTIO);
	if (r == -1) {
		for (count=0;count<10000;count++){
		        r = ioctl(commongunfile, BPPIOC_TESTIO);
			if (r == 0) return r;
		}
	}
	return r;
}

gun_test(argc,argv)
    int argc;
    char *argv[];
{
    int common, n, count, r;        

     /* Open the file */
    if ((common = open("/dev/bpp0", 0)) == NULL) {
       	printf ("error open \n");
	perror("/dev/bpp0");
       	exit(1);
    } /* end of if common */
	printf("common is %d\n", common);
	r = ioctl(common, BPPIOC_GETPARMS, &tparms);
	printf("ioctl r is %d\n", r);
	printf("read handshake: is %x\n", tparms.read_handshake);
 	if (tparms.read_handshake == BPP_ACK_BUSY_HS) 
		printf ("BPP ack + busy hs\n");	
	if (tparms.read_handshake == BPP_NO_HS) 
		printf ("BPP NO hs\n");
	if (tparms.read_handshake == BPP_ACK_HS) 
		printf ("BPP ACK hs\n");
	if (tparms.read_handshake == BPP_BUSY_HS) 
		printf ("BPP busy hs\n");
 	if (tparms.read_handshake == BPP_CLEAR_MEM) 
		printf ("BPP clear mem hs\n");	
 	if (tparms.read_handshake == BPP_XSCAN_HS) 
		printf ("BPP x scan hs\n");	

	printf("read set up time: is %x\n",tparms.read_setup_time);
	printf("read strobe width: is %x\n",tparms.read_strobe_width);
	printf("read timeout: is %d\n",tparms.read_timeout);

	while (1)
	{
    	r = ioctl(common, BPPIOC_TESTIO); 
	if (r == 0)
	{  if (count == 0)  printf ("gun shot\n");
	   count = 1;	
	}
	if (r == -1)
	{ 
	   count = 0;
	}

	}
} /* end of test */
 
#ifdef ROGER
	/*
    	r = ioctl(common, BPPIOC_GETERR, &err);
	printf("ioctl r is %d\n", r);
	printf("timeout: is %d\n", err.timeout_occurred);
	printf("pin: is %d\n", err.pin_status);
	if (err.pin_status == BPP_INIT_PIN)
		printf ("INIT pin\n");
	printf("bus: is %d\n", err.bus_error);
	n = 1;
    	r = read(common, buff, n);
	printf("chars read is %d\n", r);
*/
	printf("ioctl r is %d\n", r);
r = ioctl(common, BPPIOC_GETOUTPINS, &pins);
	printf("ioctl r is %d\n", r);
	printf("output pins: is %x\n", pins.output_reg_pins);
	printf("input pins: is %x\n", pins.input_reg_pins);

#endif

-----------------------------------------------------------------------