/*****************************************************
*********            Main Program          ***********
*********  ME 218A - Aut 2007 - Project    ***********
******************************************************/

#include <stdio.h>
#include <ME218_C32.h>
#include <timers12.h>
#include "PWMS12.h"
#include "IO_Definitions.h"
#include "Constants.h"
#include "ConfigureInitialize.h"
#include "ADS12.h"
#include "scoreboard.h"

void StartGame(void);
void GameOver(void);
int Perform(char);
void WaitMillis(int);

static int plyr_score, comp_score;


void main(void) 
{
     signed char state;
     int main_loop=1;
     int play_loop=1;
     int differential;
     char event;
     
     printf("Starting... \r\n");
 
     GameConfigure();             
 // This defines the I/O, initializes the outputs, initializes the timer
 // module & PWM module 
     
     
     while(main_loop) 
     {
          
          printf("Ready for penny \r\n");
     			     
          WaitForPenny();
          printf("Thanks for your money!! \r\n ");
          
          plyr_score = 0;
		    comp_score = 0;
		    StartGame();
          
          printf("Let's play.\r\n");
          
          while(play_loop) 
          {
              
              event = CheckEvents();
              play_loop = Perform(event);
          }

// Determine score differential
          if (plyr_score > comp_score) 
            differential = plyr_score - comp_score;
          else differential = 0;

// Turn on Prize Dispenser
          state = PWMS12_SetDuty(PRIZE_DISP_SPEED, PWMS12_CHAN1);
         
          WaitMillis(differential*DISP_TIME);				
		    state = PWMS12_SetDuty(OFF, PWMS12_CHAN1);
          
// Reset Game
          GameOver();
		    play_loop = 1;
		 }
     
}

void StartGame(void) 
{
     unsigned char GoalieSpeed;
     short dif;
     int duty;
     char state;
     
/* Read in difficulty */

     dif = ADS12_ReadADPin(DIFFICULTY);
		 printf("dif = %d \r\n",dif);  

/* Set goalie speed */
     
     if (dif < 250) {
        printf("Difficulty Easy \r\n");
        GoalieSpeed = 75;
        duty = 60;
     }
     else if (dif < 500) {
        printf("Difficulty Medium \r\n");
        GoalieSpeed = 80;
        duty = 70;
     }
     else if (dif < 750) {
        printf("Difficulty Hard \r\n");
        GoalieSpeed = 85;
        duty = 80;
     }
     else {
        printf("Difficulty Extreme \r\n");
        GoalieSpeed = 90;
     		duty = 90;
     }
     
/* Turn on goalie */
     state = PWMS12_SetDuty(GoalieSpeed, PWMS12_CHAN0);
   
/* Turn on scores */
	  InitializeScoreboard();
	   	 
/* Start main program timer */
     TMRS12_InitTimer(GAME_CLOCK, (SECOND*GAME_TIME));
     TMRS12_StartTimer(GAME_CLOCK);
		 
		 TMRS12_InitTimer(SECOND_TMR, SECOND);
		 TMRS12_StartTimer(SECOND_TMR);

}

void GameOver(void) 
{
   char state;
   
// Turn off Goalie  
  state = PWMS12_SetDuty(OFF, PWMS12_CHAN0);

// Turn off Flippers
  TurnOffFlipper(LT_FLIPPER);
  TurnOffFlipper(RT_FLIPPER);

// Re-initialize Scoreboard  
  InitializeScoreboard();
}
   

int Perform(char event) {
  char state;
  
  switch (event){
    
    case PLYR:  
       PTAD |= SOUND;
       WaitMillis(SOUND_TIME);
       PTAD &= SOUND_OFF;
       
       plyr_score++; 
       IncrementPlayerScore();
       printf("Player Goal! The score is now Player %d, Computer %d\r\n", plyr_score,comp_score);
       
       return 1;
       break;
    
    case OPP:
       PTAD |= SOUND;
       WaitMillis(SOUND_TIME);
       PTAD &= SOUND_OFF;
       
       comp_score++; 
       IncrementComputerScore();
       printf("Computer Goal! The score is now Player %d, Computer %d\r\n", plyr_score,comp_score);
       
       return 1;
       break;
    
    case LT_FLIPPER_SWING:
       PushFlipper(LT_FLIPPER);
       printf("left push \r\n");
       return 1;
       break;
    
    case L_FLIPPER_RTN:
       PullFlipper(LT_FLIPPER);
       printf("left pull \r\n");
       return 1;
       break;
    
    case L_FLIPPER_DONE:
       TurnOffFlipper(LT_FLIPPER);  
       return 1;
       break;
    
    case RT_FLIPPER_SWING: 
       PushFlipper(RT_FLIPPER);
       printf("right push \r\n");
       return 1;
       break;
    
    case R_FLIPPER_RTN: 
       PullFlipper(RT_FLIPPER);
       printf("right pull \r\n");
       return 1;
       break;
    
    case R_FLIPPER_DONE:
       TurnOffFlipper(RT_FLIPPER);  
       return 1;
       break;
    
    case TIME_OVER:
       state = PWMS12_SetDuty(OFF, PWMS12_CHAN0);
       printf("Game Over!\r\n");
       return 0;
       break;
    
    case SECOND_ELAPSED:
       printf("Time: -1\r\n");
       DecrementScoreboardTime();
       TMRS12_InitTimer(SECOND_TMR, SECOND);
       TMRS12_StartTimer(SECOND_TMR);
       return 1;
       break;
    
    case GOALIE_CHANGE:
       printf("Goalie switches direction. \r\n");
       if (PTM & GOALIE_DIRECTION) {
        PTM &= BIT3LO;
       }
       else PTM |= GOALIE_DIRECTION;
       return 1;
       break;
    
    default: 
       //printf("Failure to perform \r\n");
  	   return 1;
  	   break;
  }
  updatedisplayfractionally();
}

void WaitMillis(int time) {
  int T_init,T,delta_T=0;
  
  T_init = TMRS12_GetTime();
  while(delta_T<time) {
    T = TMRS12_GetTime();
    delta_T = T-T_init;
	}
}     