//drv_touch_generic.h
//ys_drv_touch_generic_int035.h
//INT035 Touch Driver (touch controller: MAX11802 SPI interface)
// 2017.08.15 by YSDK Design
//////////////////////////////////////////////////////////////////



#ifndef _DRV_TOUCH_GENERIC_H
#define _DRV_TOUCH_GENERIC_H

#include "driver/touch/drv_touch.h"

#ifdef __cplusplus
extern "C" {
#endif



// Port definition and Port control command definition
//TP_DIN (RA6: SDO mode)
#define TPMOSIConfig() (TRISAbits.TRISA6 = 0) //RA6:out
#define TPMOSIHigh() (LATAbits.LATA6 = 1)
#define TPMOSILow() (LATAbits.LATA6 = 0)

//TP_DOUT (RE8: SDI mode)
#define TPMISOConfig() (TRISEbits.TRISE8 = 1) //RE8: in mode
#define TPMISOConfig2() (ANSELEbits.ANSE8 = 0) //RE8: Digital mode
#define TPMISOHigh() (LATEbits.LATE8 = 1)
#define TPMISOLow() (LATEbits.LATE8 = 0)
#define TPMISORead() (PORTEbits.RE8)

//TP_CLK (RA7: SCK mode)
#define TPSCLConfig() (TRISAbits.TRISA7 = 0) //RA7: out mode
#define TPSCLHigh() (LATAbits.LATA7 = 1)
#define TPSCLLow() (LATAbits.LATA7 = 0)

//TP_CS (RE9: Chip Select mode)
#define TPCSConfig() (TRISEbits.TRISE9 = 0) //RE9: out mode
#define TPCSConfig2() (ANSELEbits.ANSE9 = 0) //RE9: Digital mode
#define TPCSHigh() (LATEbits.LATE9 = 1)
#define TPCSLow() (LATEbits.LATE9 = 0)


//TP_TIRQ //Pulse out when touch on INT035 display
#define TPIRQConfig()
#define TPIRQRead()


//Port Initialize function
#define InitBitBangedIO(){ \
TPMOSIHigh(); \
TPMOSIConfig(); \
TPMISOConfig(); \
TPMISOConfig2(); \
TPSCLLow(); \
TPSCLConfig(); \
TPCSHigh(); \
TPCSConfig(); \
TPCSConfig2(); \
}


SYS_MODULE_OBJ DRV_TOUCH_GENERIC_Initialize( const SYS_MODULE_INDEX index,
const SYS_MODULE_INIT * const init );
void DRV_TOUCH_GENERIC_Deinitialize ( SYS_MODULE_OBJ object );
SYS_STATUS DRV_TOUCH_GENERIC_Status ( SYS_MODULE_OBJ object );
void DRV_TOUCH_GENERIC_Tasks ( SYS_MODULE_OBJ object );
DRV_HANDLE DRV_TOUCH_GENERIC_Open ( const SYS_MODULE_INDEX drvIndex,
const DRV_IO_INTENT intent );
void DRV_TOUCH_GENERIC_Close ( DRV_HANDLE handle );
DRV_TOUCH_POSITION_STATUS DRV_TOUCH_GENERIC_TouchStatus( const SYS_MODULE_INDEX index );
void DRV_TOUCH_GENERIC_TouchDataRead( const SYS_MODULE_INDEX index );
short DRV_TOUCH_GENERIC_TouchGetX( uint8_t touchNumber );
short DRV_TOUCH_GENERIC_TouchGetRawX(void);
short DRV_TOUCH_GENERIC_TouchGetY( uint8_t touchNumber );
short DRV_TOUCH_GENERIC_TouchGetRawY( void );
void DRV_TOUCH_GENERIC_TouchStoreCalibration( void );
void DRV_TOUCH_GENERIC_CalibrationSet(DRV_TOUCH_SAMPLE_POINTS * samplePoints);
short DRV_TOUCH_GENERIC_PositionDetect(void);



void ys_TouchHardwareInit(void);
short ys_TouchDetectPosition(void);
static void ys_TPWriteCommand(unsigned char cmd, unsigned char dat);
static void ys_TPWriteByte(unsigned char value);
static unsigned char ys_TPReadCommand(unsigned char cmd);
static unsigned char ys_TPReadByte(void);
short ys_TouchDetectPosition(void);
short ys_TouchGetX(void);
short ys_TouchGetRawX(void);
short ys_TouchGetY(void);
short ys_TouchGetRawY(void);







typedef uintptr_t DRV_TOUCH_GENERIC_HANDLE;


#define DRV_TOUCH_GENERIC_HANDLE_INVALID ((DRV_TOUCH_GENERIC_HANDLE)(-1))


#define DRV_TOUCH_GENERIC_INDEX_0 0
#define DRV_TOUCH_GENERIC_INDEX_1 1


#define DRV_TOUCH_GENERIC_INDEX_COUNT 2


typedef enum {

TOUCH_GENERIC_ID_1 = 0,
TOUCH_GENERIC_NUMBER_OF_MODULES

} DRV_TOUCH_GENERIC_MODULE_ID;


typedef enum
{
/* Task initialize state */
DRV_TOUCH_GENERIC_TASK_STATE_INIT = 0,

/* Task read touch input request state */
DRV_TOUCH_GENERIC_TASK_STATE_READ_INPUT,

/* Task touch input decode state */
DRV_TOUCH_GENERIC_TASK_STATE_DECODE_INPUT,

/* Task complete state */
DRV_TOUCH_GENERIC_TASK_STATE_DONE,

} DRV_TOUCH_GENERIC_TASK_STATE;


typedef struct
{
/* The status of the driver */
SYS_STATUS status;

// /* The peripheral Id associated with the object */
int touchId;

/* Save the index of the driver. Important to know this
as we are using reference based accessing */
SYS_MODULE_INDEX drvIndex;

/* Flag to indicate instance in use */
bool inUse;

/* Flag to indicate module used in exclusive access mode */
bool isExclusive;

/* Number of clients possible with the hardware instance */
uint8_t numClients;

/* Orientation of the display (given in degrees of 0,90,180,270) */
uint16_t orientation;

/* Horizontal Resolution of the displayed orientation in Pixels */
uint16_t horizontalResolution;

/* Vertical Resolution of the displayed orientaion in Pixels */
uint16_t verticalResolution;


/* Touch status */
DRV_TOUCH_POSITION_STATUS touchStatus;


} DRV_TOUCH_GENERIC_OBJECT;


typedef struct _DRV_TOUCH_GENERIC_CLIENT_OBJECT
{
/* Driver Object associated with the client */
DRV_TOUCH_GENERIC_OBJECT* driverObject;

/* The intent with which the client was opened */
DRV_IO_INTENT intent;


} DRV_TOUCH_GENERIC_CLIENT_OBJECT;

typedef struct
{
/* System module initialization */
SYS_MODULE_INIT moduleInit;

/* initialize function for module (normally called statically */
SYS_MODULE_OBJ (*drvInitialize) (const SYS_MODULE_INDEX index,
const SYS_MODULE_INIT * const init);

/* index for the maxtouch driver instance used by this driver */
int touchID;

/* */
uint16_t orientation; // Orientation of the display (given in degrees of 0,90,180,270)

/* */
uint16_t horizontalResolution; // Horizontal Resolution of the displayed orientation in Pixels

/* */
uint16_t verticalResolution;

} DRV_TOUCH_GENERIC_INIT;




#ifdef __cplusplus
}
#endif

#endif //_DRV_TOUCH_GENERIC_H