/* -------------------------------------------- */
/* ---------- Included header files ----------- */
/* -------------------------------------------- */
#include <ME218_C32.h>
#include <timers12.h>
#include <stdio.h>
#include <stdlib.h>
#include "ADS12.h"
#include "target.h"
#include "shooter.h"

/* -------------------------------------------- */
/* -------------- Defined values -------------- */
/* -------------------------------------------- */
#define TARGET_SELECT_LSB 0x01
#define TARGET_SELECT_ALL 0x07
#define PHOTOCELL_HIT 175
#define PHOTOCELL1_IN 4
#define PHOTOCELL2_IN 5
#define LED_OUT 0x08
#define TARGET_PERIOD 500
#define PERIOD_DECAY 50
#define PERIOD_MIN 50
#define TARGET_TIMER 4
#define NUM_TARGETS 8

/* -------------------------------------------- */
/* --------- Static module variables ---------- */
/* -------------------------------------------- */

static char activeTarget;
static unsigned char targetsHit1;
static unsigned char targetsHit2;

/* -------------------------------------------- */
/* ---------- Function definitions ------------ */
/* -------------------------------------------- */

void initTargets(void) {
    srand(TMRS12_GetTime());    
    activeTarget=-1;
    targetsHit1=0;
    targetsHit2=0;
    TMRS12_InitTimer(TARGET_TIMER,TARGET_PERIOD);
}

unsigned char checkTarget1(void) {
    unsigned int currLight;

    // Check to see that a target is active
    if(activeTarget == -1)
        return 0;

    currLight = ADS12_ReadADPin(PHOTOCELL1_IN);
    //printf("Light: %d\t%d\r\n",currLight,ADS12_ReadADPin(PHOTOCELL2_IN));
    // Check to see that a gun is being fired and that light is high
    if(isFiring1() && (currLight < PHOTOCELL_HIT)) {
        printf("Player 1 Hit!!! Light level is: %d\r\n", currLight);      
        return 1;
    }
    return 0;
}  

unsigned char checkTarget2(void) {
    unsigned int currLight;

    // Check to see that a target is active
    if(activeTarget == -1)
        return 0;

    currLight = ADS12_ReadADPin(PHOTOCELL2_IN);
    // Check to see that a gun is being fired and that light is high
    if(isFiring2() && (currLight < PHOTOCELL_HIT)) {
        printf("Player 2 Hit!!! Light level is: %d\r\n", currLight);      
        return 1;
    }
    return 0;
}  

void registerHit1(void) {
    // Deactivate LED
    PTAD &= (~LED_OUT);
    activeTarget = -1;

    targetsHit1++;

    if(TARGET_PERIOD - PERIOD_DECAY*(targetsHit1+targetsHit2) < PERIOD_MIN)
        TMRS12_InitTimer(TARGET_TIMER,PERIOD_MIN);
    else
        TMRS12_InitTimer(TARGET_TIMER,TARGET_PERIOD - PERIOD_DECAY*(targetsHit1+targetsHit2));    
    
    vibrate1();
}

void registerHit2(void) {
    // Deactivate LED
    PTAD &= (~LED_OUT);
    activeTarget = -1;

    targetsHit2++;

    if(TARGET_PERIOD - PERIOD_DECAY*(targetsHit1+targetsHit2) < PERIOD_MIN)
        TMRS12_InitTimer(TARGET_TIMER,PERIOD_MIN);
    else
        TMRS12_InitTimer(TARGET_TIMER,TARGET_PERIOD - PERIOD_DECAY*(targetsHit1+targetsHit2));    
    
    vibrate2();
}

unsigned char checkActivateTarget(void) {
   return TMRS12_IsTimerExpired(TARGET_TIMER);
}

void activateTarget(void) {
    TMRS12_ClearTimerExpired(TARGET_TIMER);
    activeTarget = rand()%NUM_TARGETS;
    printf("Selecting target: %d\r\n",activeTarget);

    //Clear select bits
    PTAD &= (~TARGET_SELECT_ALL);

    PTAD |= activeTarget * TARGET_SELECT_LSB;

    PTAD |= LED_OUT;
}

unsigned char score1(void) {
    return targetsHit1;
}

unsigned char score2(void) {
    return targetsHit2;
}

void endTarget(void) {
    PTAD &= (~LED_OUT);
}