/**************************************************************************

Lift Motor Module

PWM: U4
Dir: U5

**************************************************************************/

/*---------------------------- Include Files ----------------------------*/
#include <stdio.h>
#include <timers12.h>
#include "PWM.h"
#include "ADS12.h"
#include "game.h"

#include "lift_motor.h"


/*--------------------------- Module Defines ----------------------------*/
// Define Motor Direction Pin
#define	LIFT_UP						PTU &= BIT5LO
#define	LIFT_DOWN					PTU |= BIT5HI

/*---------------------------- Module Types -----------------------------*/
/*-------------------------- Module Variables ---------------------------*/
unsigned char liftMode;

/*-------------------------- Module Functions ---------------------------*/
/*----------------------------- Module Code -----------------------------*/

// InitializeLiftMotor: takes nothing, returns nothing
void InitializeLiftMotor(void) {

	PWMOnOff(1,1,1,1,1);
  	PWMFrequency(0,800); // group 0
	LiftMotorStop();

  	/* Clear IRQEN to make Port E1 available as an input*/
   INTCR = ~_S12_IRQEN;
   DDRE &= (BIT0LO | BIT1LO);
   DDRU |= BIT5HI;

   liftMode = LOWERING; // assume we start with the cage down
}

// LiftMotorStop: takes no arguments, returns nothing
void LiftMotorStop(void){

	PWMDutyCycle(LIFT_MOTOR, 0);
}

// LiftMotorUp: takes no arguments, returns nothing
void LiftMotorUp(void){

	liftMode = LIFTING;
	LIFT_UP;
	PWMDutyCycle(LIFT_MOTOR, 50);
}

// LiftMotorDown: takes no arguments, returns nothing
void LiftMotorDown(void){

	liftMode = LOWERING;
	LIFT_DOWN;
	PWMDutyCycle(LIFT_MOTOR, 25);
}

// CheckLiftLimitSwitch: takes TOP or BOTTOM switch, returns 1 if pressed
unsigned char CheckLiftLimitSwitch(unsigned char liftSwitch) {

	switch(liftSwitch) {
		case TOP:
			if ((PORTE & BIT0HI) == BIT0HI)
				return 1;
			else
				return 0;
		break;

		case BOTTOM:
			if ((PORTE & BIT1HI) == BIT1HI)
				return 1;
			else
				return 0;
		break;
	}
}

// ReturnLiftMode: takes no arguments, returns LIFTING or LOWERING
unsigned char ReturnLiftMode(void) {
	return liftMode;
}