/**************************************************************************************************/
/* LOW VOLTAGE LOAD SWITCHER BOARD																  */
/* Copyright 2012 MBARI.                                                    					  */
/* MBARI Proprietary Information. All rights reserved.                      					  */
/* FILENAME:		conditions.c																  */
/* DESCRIPTION:		This file contains functions relating to the retrieval of over current,		  */
/*					over voltage, and under voltage conditions on the scientific instruments.	  */
/* FUNCTION LIST:	condOverCurr(int channel)													  */
/*					condOverVolt(int channel)													  */
/*					condUnderVolt(int channel)													  */
/**************************************************************************************************/
#include <p24fxxxx.h>
#include <stdio.h>

#include "dig_io.h"
#include "sys_defs.h"
#include "spi2.h"
#include "expander.h"
#include "actions.h"
#include "serial.h"
#include "sys_timer.h"
#include "breaker.h"
#include "relay.h"
#include "conditions.h"

////////////////////////////////////////////////////////////////////////////////////////////////////
//	NAME:			condOverCurr(int channel)
//	DESCRIPTION:	This function checks for the presence of an over current condition on the
//					scientific instrument.
//	NOTES:			This function's logic will have to change in order to handle more than two 
//					instrument channels in future versions of the board.  Expansion capabilities 
//					are built into most other functions.  Today, the programmer was lazy.
////////////////////////////////////////////////////////////////////////////////////////////////////
int condOverCurr(int channel)
{
	int state = 0;
	
	if(channel == 1)
	{
		state = expReadBit(DIO_01_CS, GPIOB, GP5);
	}
	else
	{
		state = expReadBit(DIO_01_CS, GPIOA, GP5);
	}

	if(state == 1)
	{
//		printf("over current condition\r\n");
	}

	return state;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//	NAME:			condOverVolt(int channel)
//	DESCRIPTION:	This function checks for the presence of an over voltage condition on the
//					scientific instrument.
//	NOTES:			This function's logic will have to change in order to handle more than two 
//					instrument channels in future versions of the board.  Expansion capabilities 
//					are built into most other functions.  Today, the programmer was lazy.
////////////////////////////////////////////////////////////////////////////////////////////////////
int condOverVolt(int channel)
{
	int state = 0;
	
	if(channel == 1)
	{
		state = expReadBit(DIO_01_CS, GPIOB, GP6);
	}
	else
	{
		state = expReadBit(DIO_01_CS, GPIOA, GP6);
	}

	if(state == 1)
	{
//		printf("over voltage condition\r\n");
	}

	return state;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//	NAME:			condUnderVolt(int channel)
//	DESCRIPTION:	This function checks for the presence of an under voltage condition on the
//					scientific instrument.
//	NOTES:			This function's logic will have to change in order to handle more than two 
//					instrument channels in future versions of the board.  Expansion capabilities 
//					are built into most other functions.  Today, the programmer was lazy.
////////////////////////////////////////////////////////////////////////////////////////////////////
int condUnderVolt(int channel)
{
	int state = 0;
	
	if(channel == 1)
	{
		state = expReadBit(DIO_01_CS, GPIOB, GP7);
	}
	else
	{
		state = expReadBit(DIO_01_CS, GPIOA, GP7);
	}

	if(state == 1)
	{
//		printf("under voltage condition\r\n");
	}

	return state;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
//	NAME:			INT1 Interrupt Service Routine (ISR)
//	DESCRIPTION:	This ISR is triggered when an over current, over voltage, or under voltage
//					condition is present on the board.  This routine handles the response to 
//					each of those conditions.
//	NOTES:			Will need to handle more channels in future version of the board.
////////////////////////////////////////////////////////////////////////////////////////////////////
void __attribute__((interrupt, no_auto_psv)) _INT1Interrupt(void)
{
	printf("Interrupt!\r\n\n");
	
	IFS1bits.INT1IF = 0;    		//Clear the INT1 interrupt flag or else

}