/*****************************************************
*********       Penny Checker Module       ***********
*********  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 "ADS12.h"

void PushFlipper(char);
void PullFlipper(char);
void TurnOffFlipper(char);

void PushFlipper(char flipper) 
{
    char direction_pin, actuation_pin, timer; 
    
/* Identify the flipper to actuate and assign pointers to those pins */    
    if (flipper == LT_FLIPPER) 
    {
        direction_pin = LF_DIR;
        actuation_pin = SWING_LEFT;
        timer = LEFT_FLPR_TMR; 
    } 
    else if (flipper == RT_FLIPPER) 
    {
        direction_pin = RF_DIR;
        actuation_pin = SWING_RIGHT;
        timer = RIGHT_FLPR_TMR;
    } 
    else printf("Error passing in flipper ID\r\n");

/* Start push timer */        
    TMRS12_InitTimer(timer, FLIPPER_PUSH);
    TMRS12_StartTimer(timer);

/* Turn on motor */    
    if (flipper == LT_FLIPPER) PTAD |= direction_pin; // Actuate direction forward
    else PTM |= direction_pin;
    
    PTAD |= actuation_pin;	    // Turn on motor (full power)
}

void PullFlipper(char flipper) 
{
    char direction_pin, actuation_pin, timer; 
    
/* Identify the flipper to actuate and assign pointers to those pins */    
    if (flipper == LT_FLIPPER) 
    {
        direction_pin = LF_REV;
        actuation_pin = SWING_LEFT;
        timer = LEFT_FLPR_TMR;
    } 
    else if (flipper == RT_FLIPPER) 
    {
        direction_pin = RF_REV;
        actuation_pin = SWING_RIGHT;
        timer = RIGHT_FLPR_TMR;
    } 
    else printf("Error passing in flipper ID\r\n");

/* Start Pull timer */
    TMRS12_InitTimer(timer, FLIPPER_PULL);
    TMRS12_StartTimer(timer);

/* Turn on motor */    
    if (flipper == LT_FLIPPER) PTAD &= direction_pin; // Actuate direction reverse
    else PTM &= direction_pin;
    
    PTAD |= actuation_pin;	   // Turn on motor (full power)
}

void TurnOffFlipper(char flipper) 
{
    char actuation_pin;
    
    /* Identify the flipper to actuate and assign pointers to those pins */    
    if (flipper == LT_FLIPPER) 
    {
        PTAD &= TURN_OFF_LEFT;
    } 
    else if (flipper == RT_FLIPPER) 
    {
        PTAD &= TURN_OFF_RIGHT;
    } 
    else printf("Error passing in flipper ID\r\n");
    
}
