/*
 * MD_POT.c
 *
 * Created: 13.1.2013 16:50:36
 *  Author: MichalM11x
 */ 
#include <avr/io.h>
#include <util/delay.h>
#include "MD_POT.h"

//uint8_t  iPot_Des;
//uint8_t  iPot_Act = 0;
//uint8_t  iCS_Port;
void fPot_null(digpot*data){
	unsigned char chip = 0;
	unsigned char incr = 0;
	chip|=POT_CS1|POT_CS2|POT_CS3;
	PORTC&=~(POT_UD);
	PORTC&=~(chip);
	while((incr)<=100){
	PORTC^=(POT_INC);				//0 Wiper position is incremented from high to low
	incr++;;//I really wanted to use ternary operator at least once
	PORTC^=(POT_INC);				//1	in order to finish the pulse
}
	PORTC&=~(POT_INC);				//0- Final de/incrementation
	
	PORTC|=(chip);				//1- To disable modifications to wiper
	PORTC|=(POT_INC);
	for(int i = 0; i<3;i++){
	data->previous[i] = 0;
	data->new[i] = 0;
	}
	data->newvalue = 0;	
}

void fPot_ChangeTemp(unsigned char new, unsigned char previous, unsigned char index){
	unsigned char iPot_Act = previous;
	unsigned char final_val = new;
	uint8_t chip=0;
	
	if (index == 0)	//Pot is chosen
		chip|=POT_CS1;
	if (index == 1)
		chip|=POT_CS2;
	if (index == 2)
		chip|=POT_CS3;
		
	if(iPot_Act<final_val){
		PORTC|=(POT_UD);		//1 to UD to increment
		final_val--;			//Final incrementation has to be done outside of loop
	}
	
	if(iPot_Act>final_val){
		PORTC&=(~POT_UD);			//0 to UD to decrement
		final_val++;			//Final decrement has to be done outside of loop
	}

	PORTC&=(~chip);			//Assert CS (activate low), wiper responds to INC
		
	while(iPot_Act!=final_val){			//change wiper position by |final_val-iPot_Act| to get it to desired position
		PORTC^=(POT_INC);				//0 Wiper position is incremented on high to low
		((iPot_Act>final_val)?(iPot_Act--):(iPot_Act++));//I really wanted to use ternary operator at least once
		PORTC^=(POT_INC);				//1	in order to finish the pulse		
		} 
							
		PORTC&=(~POT_INC);				//0- Final de/incrementation 
		//data->previous[index] = data->new[index];
		
		PORTC|=(chip);				//1- To disable modifications to wiper
		PORTC|=(POT_INC);				//1- Last value was not saved to EEPROM
		//data->newvalue = 0;
}

void fPot_ChangePerm(digpot*data, unsigned char index)
{
	unsigned char iPot_Act = data->previous[index];
	unsigned char final_val = data->new[index];
	uint8_t chip=0;
	
	if (index == 0)
		chip=POT_CS1;
	if (index == 1)
		chip=POT_CS2;
	if (index == 2)
		chip=POT_CS3;
	
	if(iPot_Act<final_val){
		PORTC|=(POT_UD);		//1 to UD to increment
	}
	
	if(iPot_Act>final_val){
		PORTC&=~(POT_UD);		//0 to UD to decrement
	}

	PORTC&=~(chip);			//Assert CS (activate low) wiper now responds to INC
		
	while(iPot_Act!=final_val){			//change wiper position by |final_val-iPot_Act| to get it to desired position
		PORTC&=~(POT_INC);				//0 Wiper position is incremented on high to low
		PORTC|=(POT_INC);				//1	in order to finish the pulse
		((iPot_Act>final_val)?(iPot_Act--):(iPot_Act++));} //I really wanted to use ternary operator at least once
						
		PORTC|=(chip);				//1- To disable modifications to wiper
		//_delay_ms(20);					//wait for last value to be saved to NV register
		data->previous[index] = iPot_Act;
}		