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


#ifndef __CLASSAD7843_H__
#define __CLASSAD7843_H__
#include "asf.h"
#include "classPio.h"
#include "classGraphics.h"
#include "sam4e.h"
#include "tipos.h"

#define TC_CR_Y			    0x92 //DFR = 0, PD = 10
#define TC_CR_X			    0xD2 //DFR = 0, PD = 10


#define PIXELS_X        480
#define PIXELS_Y        800

typedef enum DIRECTION
{
	Horizontal,
	Vertical
}DIRECTION;

typedef struct TP_CAL_POINT
{
	int32_t pixel_X;
	int32_t pixel_Y;
	int32_t ad7843_val_X;
	int32_t ad7843_val_Y;
	
}TP_CAL_POINT;



extern classPio Puerto;
extern classGraphics Gui;
extern void save_params_to_flash(void);
class classAD7843
{
//variables
public:
   DIRECTION Orientacion = Horizontal;
   TP_CAL_POINT Points[2];
protected:
private:

//functions
public:
	classAD7843();
	~classAD7843();
	#define TP_CS_LOW()      Puerto.set_pin_low(PIOD, PIN_24_bp)
	#define TP_CS_HIGH()     Puerto.set_pin_high(PIOD, PIN_24_bp)
	#define TP_CLK_LOW()     Puerto.set_pin_low(PIOD, PIN_25_bp)
	#define TP_CLK_HIGH()    Puerto.set_pin_high(PIOD, PIN_25_bp)
	#define TP_SDI_LOW()     Puerto.set_pin_low(PIOD, PIN_26_bp)
	#define TP_SDI_HIGH()    Puerto.set_pin_high(PIOD, PIN_26_bp)
	#define TOUCH_PRESSED()  !Puerto.get_pin_level(PIOD, PIN_29_bp)
	#define getTP_SDO()	     Puerto.get_pin_level(PIOD, PIN_27_bp)
	unsigned char TCIsPenOn(void);
	
	void Set_pins_for_spi(void);
	void Set_pins_for_bit_banged(void);
	uint16_t TCGetX(void); //call this after TCRead() to get X value
	uint16_t TCGetY(void); //call this after TCRead() to get Y value
	uint16_t ad7843_read(uint8_t);
	uint16_t ad7843_spi_read(uint8_t ads_ctl);
	void tp_calibration(void);


protected:
private:
	classAD7843( const classAD7843 &c );
	classAD7843& operator=( const classAD7843 &c );

}; //classAD7843

#endif //__CLASSAD7843_H__
