/* 
* classTFT.h
* NT35510 TFT Cpp library
* Created: 04/02/2020 18:29:31
* Author: Ledo
*/


#ifndef __CLASSTFT_H__
#define __CLASSTFT_H__

#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include "tipos.h"
#include "classPio.h"
#include "asf.h"


#define USE_HORIZONTAL  	1 
#define USE_VERTICAL    	0 

#define LCD_W 480
#define LCD_H 800

/*************************************
* Color Definition
*************************************/
#define WHITE         	 0xFFFF
#define BLACK         	 0x0000
#define BLUE         	 0x001F
#define BRED             0XF81F
#define GRED 			 0XFFE0
#define GBLUE			 0X07FF
#define RED           	 0xF800
#define MAGENTA       	 0xF81F
#define GREEN         	 0x07E0
#define CYAN          	 0x7FFF
#define YELLOW        	 0xFFE0
#define BROWN 			 0XBC40
#define BRRED 			 0XFC07
#define GRAY  			 0X8430
#define DARKBLUE      	 0X01CF
#define LIGHTBLUE      	 0X7D7C
#define GRAYBLUE       	 0X5458
#define LIGHTGREEN     	 0X841F
#define LGRAY 			 0XC618
#define LGRAYBLUE        0XA651
#define LBBLUE           0X2B12

// VGA color palette
#define VGA_BLACK		0x0000
#define VGA_WHITE		0xFFFF
#define VGA_RED			0xF800
#define VGA_GREEN		0x0400
#define VGA_BLUE		0x001F
#define VGA_SILVER		0xC618
#define VGA_GRAY		0x8410
#define VGA_MAROON		0x8000
#define VGA_YELLOW		0xFFE0
#define VGA_OLIVE		0x8400
#define VGA_LIME		0x07E0
#define VGA_AQUA		0x07FF
#define VGA_TEAL		0x0410
#define VGA_NAVY		0x0010
#define VGA_FUCHSIA		0xF81F
#define VGA_PURPLE		0x8010
#define VGA_TRANSPARENT	0xFFFFFFFF


typedef struct LCD_DEV
{
	uint16_t width;			
	uint16_t height;			
	uint16_t id;				
	uint8_t  dir;			
	uint16_t	 wramcmd;		
	uint16_t  rramcmd;  
	uint16_t  setxcmd;		
	uint16_t  setycmd;		
}LCD_DEV;



extern classPio Puerto;
class classTFT
{
//variables
public:
    uint16_t  POINT_COLOR;
    uint16_t  BACK_COLOR;
    LCD_DEV lcddev;	
protected:
private:

//functions
public:
	classTFT();
	~classTFT();
	void Tft_Reset(void);
	void Tft_Init(void);
	void Tft_Clear(uint16_t Color);
	uint16_t Tft_read(void);
	uint16_t Tft_RD_DATA(void);
	void Tft_WR_REG(uint16_t Reg)	;
	void Tft_SetCursor(uint16_t Xpos, uint16_t Ypos);
	void Tft_SetWindows(uint16_t xStar, uint16_t yStar,uint16_t xEnd,uint16_t yEnd);
	void Tft_DrawPoint(uint16_t x,uint16_t y);
	uint16_t Tft_ReadPoint(uint16_t x,uint16_t y);
	void Tft_WriteRAM_Prepare(void);
	void Tft_ReadRAM_Prepare(void);
	void Tft_direction(uint8_t direction );
	void Tft_WR_DATA_16Bit(uint16_t Data);
	uint16_t Tft_RD_DATA_16Bit(void);
	uint16_t Tft_Read_ID(void);
	uint16_t Color_To_565(uint8_t r, uint8_t g, uint8_t b);


protected:
private:
    //===================================================
    // Definicion de macros
    //#define TFT_CS_LOW()   Puerto.set_pin_low(PIOD,TFT_CS_PIN_bp);
    //#define TFT_CS_HIGH()  Puerto.set_pin_high(PIOD,TFT_CS_PIN_bp);
    //#define TFT_DC_LOW()   Puerto.set_pin_low(PIOD,TFT_DC_PIN_bp);
    //#define TFT_DC_HIGH()  Puerto.set_pin_high(PIOD,TFT_DC_PIN_bp);
    #define TFT_BKL_ON()   Puerto.set_pin_high(PIOD,TFT_BKL_PIN_bp);
    #define TFT_BKL_OFF()  Puerto.set_pin_low(PIOD,TFT_BKL_PIN_bp);
    #define TFT_RST_OFF()  Puerto.set_pin_high(PIOD,TFT_RST_PIN_bp);
    #define TFT_RST_ON()   Puerto.set_pin_low(PIOD,TFT_RST_PIN_bp);
    //#define TFT_RD_LOW()   Puerto.set_pin_low(PIOD,TFT_RD_PIN_bp);
    //#define TFT_RD_HIGH()  Puerto.set_pin_high(PIOD,TFT_RD_PIN_bp);
    //#define TFT_WR_LOW()   Puerto.set_pin_low(PIOD,TFT_WR_PIN_bp);
    //#define TFT_WR_HIGH()  Puerto.set_pin_high(PIOD,TFT_WR_PIN_bp);
	
	//Fast Macros
	#define TFT_CS_LOW()   PIOD->PIO_CODR = 1 << (TFT_CS_PIN_bp & 0x1F);
    #define TFT_CS_HIGH()  PIOD->PIO_SODR = 1 << (TFT_CS_PIN_bp & 0x1F);
    #define TFT_DC_LOW()   PIOD->PIO_CODR = 1 << (TFT_DC_PIN_bp & 0x1F);
    #define TFT_DC_HIGH()  PIOD->PIO_SODR = 1 << (TFT_DC_PIN_bp & 0x1F);	
	#define TFT_RD_LOW()   PIOD->PIO_CODR = 1 << (TFT_RD_PIN_bp & 0x1F);
    #define TFT_RD_HIGH()  PIOD->PIO_SODR = 1 << (TFT_RD_PIN_bp & 0x1F);	
	#define TFT_WR_LOW()   PIOD->PIO_CODR = 1 << (TFT_WR_PIN_bp & 0x1F);
	#define TFT_WR_HIGH()  PIOD->PIO_SODR = 1 << (TFT_WR_PIN_bp & 0x1F);
	
	
	
	//void Tft_write(uint8_t HVAL,uint8_t LVAL);
	void Tft_write(uint16_t VAL);

	void Tft_WR_DATA(uint16_t Data);
	//void Tft_WR_REG(uint16_t Reg);
	void Tft_WriteReg(uint16_t LCD_Reg, uint16_t LCD_RegValue);
	void Tft_ReadReg(uint16_t LCD_Reg,uint16_t *Rval,int n);

	classTFT( const classTFT &c );
	classTFT& operator=( const classTFT &c );

}; //classTFT

#endif //__CLASSTFT_H__
