/**************************************************************************************************/
/* LOW VOLTAGE LOAD SWITCHER BOARD																  */
/* Copyright 2012 MBARI.                                                    					  */
/* MBARI Proprietary Information. All rights reserved.                      					  */
/* FILENAME:		potentiometer.c																  */
/* DESCRIPTION:		This file contains functions relating to the operation of the				  */
/*					board's digital potentiometers.												  */
/* FUNCTION LIST:	potReset(int device_select)													  */
/*					potReadWrite(int pot_select, int cmd, int wiper, int value)					  */
/*					potIncrDecr(int pot_select, int cmd, int wiper, int cycles)					  */
/**************************************************************************************************/
#include <p24fxxxx.h>
#include <stdio.h>

#include "potentiometer.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"

////////////////////////////////////////////////////////////////////////////////////////////////////
//	NAME:			PotReset(int pot_select)
//	DESCRIPTION:	Resets the digital potentiometers. 
//	NOTES:			The argument pot_select will take one of two possible defs, POT_01_RST, or 
//					POT_02_RST, as set up in sys_defs.h.  More pot defs may be added in later 
//					iterations of board with more than two digital potentiometers.
////////////////////////////////////////////////////////////////////////////////////////////////////
void potReset(int pot_select)
{
	expSetLow(DIO_05_CS, GPIOB, pot_select);		
	expRead(DIO_05_CS, GPIOB);
	tmrDelayTicks(1);
	expSetHigh(DIO_05_CS, GPIOB, pot_select);
	expRead(DIO_05_CS, GPIOB);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//	NAME:			potReadWrite(int pot_select, int cmd, int wiper, int value)
//	DESCRIPTION:	Formats a command using bit shifts and a logical "or" into two 8-bit pieces,  
//					then writes them to one of the board's digital potentiometers.
//	NOTES:			When using this function to read, just set the argument 'value' equal to 0x00.
////////////////////////////////////////////////////////////////////////////////////////////////////
int potReadWrite(int pot_select, int cmd, int wiper, int value)
{
	int dummy, data;
	char commandMSB, commandLSB, valMSB;

	//SHIFT BYTES TO FIT IN MSB
	wiper = wiper << 4;		 
	cmd = cmd << 2;
	valMSB = value >> 8;

	//SET VALUES
	commandMSB = wiper|cmd|valMSB;
	commandLSB = (char)value;

	//SEND COMMAND TO POT
	CSAddSet(pot_select);
	dummy = ReadWriteSPI2(commandMSB);
	data = ReadWriteSPI2(commandLSB);
	CSAddSet(DESELECT);

	//RESET
	if(pot_select == POT_01_CS)
	{
		potReset(POT_01_RST);
	}
	else
	{
		potReset(POT_02_RST);
	}

	return data;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//	NAME:			potIncrDecr(int pot_select, int cmd, int wiper, int cycles)
//	DESCRIPTION:	This function increments or decrements the wiper using an 8-bit command.
//	NOTES:			Does not work with non-volatile memory locations. Not used.
////////////////////////////////////////////////////////////////////////////////////////////////////
void potIncrDecr(int pot_select, int cmd, int wiper, int cycles)
{
	int command, c;

	//SHIFT BYTES
	wiper = wiper << 4;		 
	cmd = cmd << 2;
		
	//SET VALUES
	command = wiper|cmd;	

	for(c = 0; c <= cycles; c++)
	{
		//SEND COMMAND TO POT
		CSAddSet(pot_select);
		spiReadWrite2(command);
		CSAddSet(DESELECT);
	}
}
