/* 
* classAD7843.cpp
*
* Created: 06/02/2020 21:37:44
* Author: Ledo
*/


#include "classAD7843.h"

// default constructor
classAD7843::classAD7843()
{
} //classAD7843

// default destructor
classAD7843::~classAD7843()
{
} //~classAD7843

void classAD7843::Set_pins_for_spi(void)
{
	//cs pin
	Puerto.pin_set_output(PIOA,AD7843_CS_PIN_MASK,1,0,0);
	//miso Pin
	Puerto.pin_set_peripheral(PIOA,PIO_PERIPH_A,PIO_PA12A_MISO);
	//mosi Pin
	Puerto.pin_set_peripheral(PIOA,PIO_PERIPH_A,PIO_PA13A_MOSI);
	//spck Pin Pin
	Puerto.pin_set_peripheral(PIOA,PIO_PERIPH_A,PIO_PA14A_SPCK);
	//AD7843 TP_PEN PIN
	Puerto.pin_set_input(PIOA,AD7843_TP_PEN_PIN_MASK,PIO_PULLUP);
	TP_CS_HIGH();
}

void classAD7843::Set_pins_for_bit_banged(void)
{
	//cs pin
	Puerto.pin_set_output(PIOA,AD7843_CS_PIN_MASK,1,0,0);
	//miso Pin
	Puerto.pin_set_input(PIOA,(1<<12),PIO_INPUT);
	//mosi Pin
	Puerto.pin_set_output(PIOA,(1<<13),1,0,0);
	//spck Pin Pin
	Puerto.pin_set_output(PIOA,(1<<14),1,0,0);
	//AD7843 TP_PEN PIN
	Puerto.pin_set_input(PIOA,AD7843_TP_PEN_PIN_MASK,PIO_PULLUP);
	TP_CS_HIGH();
}

// ads_rd():
// A bit-banged implementation of the SPI access for AD7843...
// Slow and steady gets the job done!
//
uint16_t classAD7843::ad7843_read(uint8_t ads_ctl)
{
	uint16_t bit, val;
	Set_pins_for_bit_banged();
	TP_CLK_LOW();
	TP_CS_LOW();

	for(bit = 0x80;bit != 0;bit >>= 1)
	{
		if (ads_ctl & bit)
		{
			TP_SDI_HIGH();
		}
		else
		{
			TP_SDI_LOW();
			
		}

		TP_CLK_HIGH();
		delay_us(1);
		TP_CLK_LOW();
		delay_us(1);//clk a 500 kHz
	}

	val = 0;
	for(bit = 0x8000;bit != 0;bit >>= 1)
	{
		TP_CLK_HIGH();
		if (getTP_SDO())
		val |= bit;
		delay_us(1);
		TP_CLK_LOW();
		delay_us(1);
	}

	TP_CLK_LOW();
	delay_us(1);
	TP_CS_HIGH();
	Set_pins_for_spi();
	return(val);
}



uint16_t classAD7843::TCGetX(void) //call this after TCRead() to get X value
{
	int32_t temp;
	if(Orientacion == Vertical)
	temp = ad7843_read(TC_CR_X);
	else
	temp = ad7843_read(TC_CR_Y);

	
	return ((Points[1].pixel_X - Points[0].pixel_X) * (temp - Points[0].ad7843_val_X) /
	(Points[1].ad7843_val_X - Points[0].ad7843_val_X))
	+ Points[0].pixel_X;
}

uint16_t classAD7843::TCGetY(void) //call this after TCRead() to get Y value
{
	int32_t temp;
	if(Orientacion == Vertical)
	temp = ad7843_read(TC_CR_Y);
	else
	temp = ad7843_read(TC_CR_X);
	return ((Points[1].pixel_Y - Points[0].pixel_Y) * (temp - Points[0].ad7843_val_Y) /
	(Points[1].ad7843_val_Y - Points[0].ad7843_val_Y))
	+ Points[0].pixel_Y;

}

unsigned char classAD7843::TCIsPenOn(void) //TC_PEN_PIN is 0 when the screen is pressed
{
	//return ((PORTF.IN & TP_PEN)>0) ? 0 : 1;
	return Puerto.get_pin_level(PIOA,AD7843_TP_PEN_PIN) ? 0 : 1;
}


void classAD7843::tp_calibration(void)
{
	Display.Tft_Clear(WHITE);
	Gui.drawCircle(5,5,VGA_BLUE,4,0);
	Gui.print("Touch Left Point", 20, 200, 0, VGA_WHITE, 0);
	delay_s(4);
	while(!TOUCH_PRESSED());
	Points[1].pixel_X = 5;
	Points[1].pixel_Y = 5;
	Points[1].ad7843_val_X = ad7843_read(TC_CR_Y);//Invertido en horizontal
	Points[1].ad7843_val_Y = ad7843_read(TC_CR_X);

	Gui.drawCircle(795,475,VGA_BLUE,4,0);
	Gui.print("Touch Right Point", 20, 200, 0, 0xffff, 0);
	delay_s(4);
	while(!TOUCH_PRESSED());
	Points[0].pixel_X = 795;
	Points[0].pixel_Y = 475;
	Points[0].ad7843_val_X = ad7843_read(TC_CR_Y);//Invertido en horizontal
	Points[0].ad7843_val_Y = ad7843_read(TC_CR_X);
	save_params_to_flash();
}





