 /*
  RTH 12.2007
  lisy80 first version for GTB Spiderman
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include "hw_lib.h"
#include "coils.h"
#include "displays.h"
#include "switches.h"
#include "utils.h"

#define VERSION "0.12"
#define PIN "Spiderman"
int gtb_sys80_type = 0;  //0==SYS80, 1=SYS80A, 2==SYS80B

#define	SW_1HOLE 2
#define	SW_2HOLE 12
#define	SW_3HOLE 22
#define	SW_REPLAY 47
#define	SW_OUTHOLE 67

#define CL_OFF 0
#define CL_ON	1
#define CL_PULSE 2
#define CL_QRELAY 1
#define CL_PLAYER1 5
#define CL_GAMEOVER 12
#define CL_1a3HOLEKICKER 58
#define	CL_OUTHOLEKICKER 59
#define CL_2HOLEKICKER 60



int main( int argc, char **argv )
{
	int s,count;
	char str[80];
	char str2[80];


	printf("\n LISY80 Version %s\n",VERSION);
	printf("for %s\n",PIN);

	// INIT the Hardware
	iow_init_warrior();

	// coils
	init_coils();

	// INIT displays
	// TODO

	printf("wait for play button\n");
	while ( get_switch_change() != SW_REPLAY );
	
	printf("activate Q Relay\n");
	coil_set( CL_QRELAY, CL_ON);

	printf("kick out ball\n");
	coil_set( CL_OUTHOLEKICKER, CL_PULSE);

	printf("set player 1 to 00\n");
	strcpy(str,"s,1,    00");
	set_display_str(str);
	
	printf("activate player 1 lamp\n");
	coil_set( CL_PLAYER1, CL_ON);

	printf("erase display player 1 to 4 \\n");
	strcpy(str,"s,2,      ");
	set_display_str(str);
	strcpy(str,"s,3,      ");
	set_display_str(str);
	strcpy(str,"s,4,      ");
	set_display_str(str);

	printf("set status display to first ball\\n");
	strcpy(str,"s,0,  01");
	set_display_str(str);

	// wait for outhole
	// meanwhile count points and show switch stati
	count=0;
	while ( (s=get_switch_change()) != SW_OUTHOLE )
	  {
      		if ( s > 0 )
        	{
          	  printf("Switches No: %d\n",s);
		  count = count + 10;
		  strcpy(str,"s,1,");
		  sprintf(str2,"%6d",count);
		  strcat(str,str2);
		  set_display_str(str);
		  if ( s == SW_1HOLE ) coil_set( CL_1a3HOLEKICKER, CL_PULSE);
		  if ( s == SW_2HOLE ) coil_set( CL_2HOLEKICKER, CL_PULSE);
		  if ( s == SW_3HOLE ) coil_set( CL_1a3HOLEKICKER, CL_PULSE);
        	}
	  }

	// GAME OVER

	printf("deactivate Q Relay\n");
	coil_set( CL_QRELAY, CL_OFF);

	printf("deactivate player 1 lamp\\n");
	coil_set( CL_PLAYER1, CL_OFF);

	printf("activate Game Over lamp\\n");
	coil_set( CL_GAMEOVER, CL_ON);

	exit(0);
}
