/*
 * utft.c
 *
 * Created: 04/06/2018 16:50:26
 *  Author: Ledo
 */ 

#include "utft.h"


//===============================================================
// all display one colour
void ClearScreen(unsigned int bColor)
{
	unsigned int i,j;
	LCD_SetPos(0,319,0,479);
	for (i=0;i<320;i++)
	{
		
		for (j=0;j<480;j++)
		Write_Data_U16(bColor);
	}
}


void fillScr(uint16_t color)
{
	char ch, cl;
	int i,j;
	ch=(uint8_t)(color>>8);
	cl=(uint8_t)(color & 0xFF);

	clrXY();
	
	
	for (i=0;i<disp_y_size;i++)
	{
		
		for (j=0;j<disp_x_size;j++)
		{
          Write_Data_U16(i);
		}
	}

}



/***************************************************************************************
** Function name:           color565
** Description:             convert three 8 bit RGB levels to a 16 bit colour value
***************************************************************************************/
uint16_t color565(uint8_t r, uint8_t g, uint8_t b)
{
	return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
}

/***************************************************************************************
** Function name:           Write_Cmd
** Description:             Send an 8 bit command to the TFT
***************************************************************************************/
void Write_Cmd(uint8_t cmd)
{
	TFT_DC_LOW(); //Modo comando
	TFT_CS_LOW(); //chip select
	TFT_WR_LOW(); //prepara el bus
    //Ponemos el comando en el bus de datos
    PIOD->PIO_ODSR = cmd;
	TFT_WR_HIGH(); //escritura
	TFT_CS_HIGH();
	TFT_DC_HIGH();
}


/***************************************************************************************
** Function name:           Write_Cmd_Data
** Description:             Send a 8 bit data value to the TFT
***************************************************************************************/
void Write_Cmd_Data(uint8_t cmdd)
{
	TFT_CS_LOW(); //chip select
	TFT_WR_LOW(); //prepara el bus
    //Ponemos el comando en el bus de datos
    PIOD->PIO_ODSR = cmdd;	
	TFT_WR_HIGH(); //escritura
	TFT_CS_HIGH();
}


void Write_Data_U16(uint16_t data16)
{
	
	TFT_CS_LOW(); //chip select
	TFT_WR_LOW();
    //Ponemos el comando en el bus de datos
    PIOD->PIO_ODSR = data16;	
	TFT_WR_HIGH();
	TFT_CS_HIGH();
}





//============================================================
//show Box

void LCD_FULL(unsigned int i)
{
	unsigned int w,u;
	LCD_SetPos(0,319,0,479);

	for(w=0;w<320;w++)
	{
		Write_Data_U16(~i);

	}

	for(w=0;w<478;w++)
	{  	Write_Data_U16(~i);
		
		for(u=0;u<318;u++)
		Write_Data_U16(i);

		Write_Data_U16(~i);
	}

	for(w=0;w<320;w++)
	{
		Write_Data_U16(~i);

	}
	
}




int16_t absolute(int16_t value)
{
	if (value < 0) {
		return -value;
	}
	else {
		return value;
	}
}

void swap_word(int16_t a,int16_t b)
{
	int16_t temp = a;
	a = b;
	b = temp;
}

void LCD_SetPos(unsigned int xs,unsigned int xe,unsigned int ys,unsigned int ye)
{

	Write_Cmd(0x2A);
	
	Write_Cmd_Data(xs>>8);
	Write_Cmd_Data(xs);
	Write_Cmd_Data(xe>>8);
	Write_Cmd_Data(xe);
	Write_Cmd(0x2B);
	Write_Cmd_Data(ys>>8);
	Write_Cmd_Data(ys);
	Write_Cmd_Data(ye>>8);
	Write_Cmd_Data(ye);
	
	Write_Cmd(0x2C);
}


//=============================================================
//lcd initial

void ili9488_Initial(void)
{
	TFT_RST_OFF();
	delay_ms(5);
	TFT_RST_ON();
	delay_ms(10);
	TFT_RST_OFF();
	delay_ms(120);
    TFT_BKL_ON();
	  Write_Cmd(0x11); // Sleep out, also SW reset
	  delay_ms(120);

	  Write_Cmd(0x3A);
	  Write_Cmd_Data(0x55);
	  
	  Write_Cmd(0xC2);
	  Write_Cmd_Data(0x44);

	  Write_Cmd(0xC5);
	  Write_Cmd_Data(0x00);
	  Write_Cmd_Data(0x00);
	  Write_Cmd_Data(0x00);
	  Write_Cmd_Data(0x00);

	  Write_Cmd(0xE0);
	  Write_Cmd_Data(0x0F);
	  Write_Cmd_Data(0x1F);
	  Write_Cmd_Data(0x1C);
	  Write_Cmd_Data(0x0C);
	  Write_Cmd_Data(0x0F);
	  Write_Cmd_Data(0x08);
	  Write_Cmd_Data(0x48);
	  Write_Cmd_Data(0x98);
	  Write_Cmd_Data(0x37);
	  Write_Cmd_Data(0x0A);
	  Write_Cmd_Data(0x13);
	  Write_Cmd_Data(0x04);
	  Write_Cmd_Data(0x11);
	  Write_Cmd_Data(0x0D);
	  Write_Cmd_Data(0x00);
	  
	  Write_Cmd(0xE1);
	  Write_Cmd_Data(0x0F);
	  Write_Cmd_Data(0x32);
	  Write_Cmd_Data(0x2E);
	  Write_Cmd_Data(0x0B);
	  Write_Cmd_Data(0x0D);
	  Write_Cmd_Data(0x05);
	  Write_Cmd_Data(0x47);
	  Write_Cmd_Data(0x75);
	  Write_Cmd_Data(0x37);
	  Write_Cmd_Data(0x06);
	  Write_Cmd_Data(0x10);
	  Write_Cmd_Data(0x03);
	  Write_Cmd_Data(0x24);
	  Write_Cmd_Data(0x20);
	  Write_Cmd_Data(0x00);
	  
	  Write_Cmd(0x20);                     // display inversion OFF
	  
	  Write_Cmd(0x36);
	  Write_Cmd_Data(0x48);

	  Write_Cmd(0x29);                     // display on
	  delay_ms(120);
	  //Write_Cmd(0x23);   
}

void ILI9488_EnterSleep_Code(void)
{
	Write_Cmd(0x28); //Display off
	delay_ms(10);
	Write_Cmd(0x10); // Internal oscillator will be stopped
	delay_ms(120);
}


void ILI9488_ExitSleep_Code(void)
{
	Write_Cmd(0x11); // Sleep out
	delay_ms(120);
	Write_Cmd(0x29); //Display on
}


//============================================================
//show gray

void DispRGBGray(void)
{


	unsigned int COL=320;
	unsigned char i,j,k,dbl,dbh;
	
	LCD_SetPos(0,319,0,479);

	// balck -> red
	for(k=0;k<80;k++)
	{
		for(i=0;i<32;i++)
		{
			for(j=0;j<10;j++)
			{
				dbh=i<<3;
				dbl=0;
				Write_Data_U16(color);
			}
		}
	}

	// red -> black
	for(k=0;k<80;k++)
	{

		for(i=31;i>0;i--)
		{
			for(j=0;j<10;j++)
			{
				dbh=i<<3;
				dbl=0;
				Write_Data_U16(color);
			}
		}
		
		dbh=0x00;dbl=0x00;
		for(i=0;i<10;i++)
		Write_Data_U16(color);
		

	}
	// balck -> green
	for(k=0;k<80;k++)
	{

		for(i=0;i<64;i+=2)
		{
			for(j=0;j<10;j++)
			{
				dbh=i>>3;
				dbl=i<<5;
				Write_Data_U16(color);
			}
		}
	}

	// green -> black
	for(k=0;k<80;k++)
	{
		for(i=63;i!=1;i-=2)
		{
			for(j=0;j<10;j++)
			{
				dbh=i>>3;
				dbl=i<<5;
				Write_Data_U16(color);
			}
		}
		dbh=0x00;dbl=0x00;
		for(i=0;i<10;i++)
		Write_Data_U16(color);
	}


	// balck -> blue
	for(k=0;k<80;k++)
	{
		for(i=0;i<32;i++)
		{
			for(j=0;j<10;j++)
			{
				dbh=0;
				dbl=i;
				Write_Data_U16(color);
			}
		}
	}

	// blue -> black
	for(k=0;k<80;k++)
	{

		for(i=31;i>0;i--)
		{
			for(j=0;j<10;j++)
			{
				dbh=0;
				dbl=i;
				Write_Data_U16(color);
			}
		}
		dbh=0x00;dbl=0x00;
		for(i=0;i<10;i++)
		Write_Data_U16(color);
	}


}







void clrXY()
{
	if (orient==PORTRAIT)
		LCD_SetPos(0,disp_x_size-1,0,disp_y_size-1);
	else
		LCD_SetPos(0,disp_y_size-1,0, disp_x_size-1);
}






void setColor(uint16_t col)
{
	color = col;
}

uint16_t getColor()
{
	return color;
}



void setBackColor(uint16_t col)
{
	if (color==VGA_TRANSPARENT)
		_transparent=true;
	else
	{
		backcolor = col;
		_transparent=false;
	}
}

uint16_t getBackColor()
{
	return backcolor;
}

void setPixel(uint16_t color)
{
	//Write_Data((color>>8),(color&0xFF));
	Write_Data_U16(color);	// rrrrrggggggbbbbb
}

void drawPixel(int x, int y)
{
	LCD_SetPos(x, y, x, y);
	setPixel(color);
	clrXY();
}




void drawLine(int x1, int y1, int x2, int y2)
{
	if (y1==y2)
	drawHLine(x1, y1, x2-x1);
	else if (x1==x2)
	drawVLine(x1, y1, y2-y1);
	else
	{
		unsigned int	dx = (x2 > x1 ? x2 - x1 : x1 - x2);
		int16_t			xstep =  x2 > x1 ? 1 : -1;
		unsigned int	dy = (y2 > y1 ? y2 - y1 : y1 - y2);
		int16_t			ystep =  y2 > y1 ? 1 : -1;
		int				col = x1, row = y1;

		if (dx < dy)
		{
			int t = - (dy >> 1);
			while (true)
			{
				LCD_SetPos(col, row, col, row);
				Write_Data_U16(color);
				if (row == y2)
				return;
				row += ystep;
				t += dx;
				if (t >= 0)
				{
					col += xstep;
					t   -= dy;
				}
			}
		}
		else
		{
			int t = - (dx >> 1);
			while (true)
			{
				LCD_SetPos(col, row, col, row);
				Write_Data_U16(color);
				if (col == x2)
				return;
				col += xstep;
				t += dy;
				if (t >= 0)
				{
					row += ystep;
					t   -= dx;
				}
			}
		}
	}
	clrXY();
}

void drawHLine(int16_t x, int16_t y, int16_t l)
{
	
	if (l<0)
	{
		l = -l;
		x -= l;
	}
	
	LCD_SetPos(x,  x+l,y, y);
	{
		for (int16_t i=0; i<l+1; i++)
		{
			Write_Data_U16(color);
		}
	}
	clrXY();
}

void drawVLine(int x, int y, int l)
{
	if (l<0)
	{
		l = -l;
		y -= l;
	}
	//cbi(P_CS, B_CS);
	LCD_SetPos(x, x, y, y+l);
	{
		for (int i=0; i<l+1; i++)
		{
			Write_Data_U16(color);
		}
	}
	//sbi(P_CS, B_CS);
	clrXY();
}



void drawRect(int x1, int y1, int x2, int y2)
{
	if (x1>x2)
	{
		swap_word(x1, x2);
	}
	if (y1>y2)
	{
		swap_word(y1, y2);
	}

	drawHLine(x1, y1, x2-x1);
	drawHLine(x1, y2, x2-x1);
	drawVLine(x1, y1, y2-y1);
	drawVLine(x2, y1, y2-y1);
}

void drawRoundRect(int x1, int y1, int x2, int y2)
{
	if (x1>x2)
	{
		swap_word(x1, x2);
	}
	if (y1>y2)
	{
		swap_word(y1, y2);
	}
	if ((x2-x1)>4 && (y2-y1)>4)
	{
		drawPixel(x1+1,y1+1);
		drawPixel(x2-1,y1+1);
		drawPixel(x1+1,y2-1);
		drawPixel(x2-1,y2-1);
		drawHLine(x1+2, y1, x2-x1-4);
		drawHLine(x1+2, y2, x2-x1-4);
		drawVLine(x1, y1+2, y2-y1-4);
		drawVLine(x2, y1+2, y2-y1-4);
	}
}

void fillRect(int x1, int y1, int x2, int y2)
{
	if (x1>x2)
	{
		swap_word(x1, x2);
	}
	if (y1>y2)
	{
		swap_word(y1, y2);
	}
	if (orient==PORTRAIT)
	{
		for (int i=0; i<((y2-y1)/2)+1; i++)
		{
			drawHLine(x1, y1+i, x2-x1);
			drawHLine(x1, y2-i, x2-x1);
		}
	}
	else
	{
		for (int i=0; i<((x2-x1)/2)+1; i++)
		{
			drawVLine(x1+i, y1, y2-y1);
			drawVLine(x2-i, y1, y2-y1);
		}
	}
}

void fillRoundRect(int x1, int y1, int x2, int y2)
{
	if (x1>x2)
	{
		swap_word(x1, x2);
	}
	if (y1>y2)
	{
		swap_word(y1, y2);
	}

	if ((x2-x1)>4 && (y2-y1)>4)
	{
		for (int i=0; i<((y2-y1)/2)+1; i++)
		{
			switch(i)
			{
				case 0:
				drawHLine(x1+2, y1+i, x2-x1-4);
				drawHLine(x1+2, y2-i, x2-x1-4);
				break;
				case 1:
				drawHLine(x1+1, y1+i, x2-x1-2);
				drawHLine(x1+1, y2-i, x2-x1-2);
				break;
				default:
				drawHLine(x1, y1+i, x2-x1);
				drawHLine(x1, y2-i, x2-x1);
			}
		}
	}
}









void drawString(char *string,int16_t poX, int16_t poY, int16_t size,uint16_t fgcolor)
{
	while(*string)
	{
		///drawChar(*string, poX, poY, size, fgcolor);
		*string++;

		if(poX < disp_x_size)
		{
			poX += FONT_SPACE*size;                                     /* Move cursor right            */
		}
	}
}


void printChar(uint8_t c, int x, int y)
{
	uint8_t i,ch;
	uint16_t j;
	uint16_t temp; 

  
	if (!_transparent)
	{
		if (orient==PORTRAIT)
		{
			LCD_SetPos(x,x+cfont.x_size-1,y,y+cfont.y_size-1);
			temp=((c-cfont.offset)*((cfont.x_size/8)*cfont.y_size))+4;
			for(j=0;j<((cfont.x_size/8)*cfont.y_size);j++)
			{
				ch=cfont.font[temp];//&cfont.font[temp]
				for(i=0;i<8;i++)
				{  
					if((ch&(1<<(7-i)))!=0)   
					{
						setPixel(color);
					} 
					else
					{
						setPixel(backcolor);
					} 
				}
				temp++;
			}
		}
		else
		{
			temp=((c-cfont.offset)*((cfont.x_size/8)*cfont.y_size))+4;

			for(j=0;j<((cfont.x_size/8)*cfont.y_size);j+=(cfont.x_size/8))
			{
				LCD_SetPos(x,x+cfont.x_size-1,y+(j/(cfont.x_size/8)),y+(j/(cfont.x_size/8)));
				for (int zz=(cfont.x_size/8)-1; zz>=0; zz--)
				{
					ch=&cfont.font[temp+zz];//&cfont.font[temp+zz]
					for(i=0;i<8;i++)
					{   
						if((ch&(1<<i))!=0)   
						{
							setPixel(color);
						} 
						else
						{
							setPixel(backcolor);
						}   
					}
				}
				temp+=(cfont.x_size/8);
			}
		}
	}
	else
	{
		temp=((c-cfont.offset)*((cfont.x_size/8)*cfont.y_size))+4;
		for(j=0;j<cfont.y_size;j++) 
		{
			for (int zz=0; zz<(cfont.x_size/8); zz++)
			{
				ch=&cfont.font[temp+zz]; 
				for(i=0;i<8;i++)
				{   
					LCD_SetPos(x+i+(zz*8),x+i+(zz*8)+1,y+j,y+j+1);
				
					if((ch&(1<<(7-i)))!=0)   
					{
						setPixel(color);
					} 
				}
			}
			temp+=(cfont.x_size/8);
		}
	}

	//clrXY();
}

void rotateChar(uint8_t c, int x, int y, int pos, int deg)
{
	uint8_t i,j,ch;
	uint16_t temp; 
	int newx,newy;
	double radian;
	radian=deg*0.0175;  

	//cbi(P_CS, B_CS);

	temp=((c-cfont.offset)*((cfont.x_size/8)*cfont.y_size))+4;
	for(j=0;j<cfont.y_size;j++) 
	{
		for (int zz=0; zz<(cfont.x_size/8); zz++)
		{
			ch=&cfont.font[temp+zz]; 
			for(i=0;i<8;i++)
			{   
				newx=x+(((i+(zz*8)+(pos*cfont.x_size))*cos(radian))-((j)*sin(radian)));
				newy=y+(((j)*cos(radian))+((i+(zz*8)+(pos*cfont.x_size))*sin(radian)));

				LCD_SetPos(newx,newx+1,newy,newy+1);
				
				if((ch&(1<<(7-i)))!=0)   
				{
					setPixel(color);
				} 
				else  
				{
					if (!_transparent)
						setPixel(backcolor);
				}   
			}
		}
		temp+=(cfont.x_size/8);
	}
	//sbi(P_CS, B_CS);
	clrXY();
}

void print(char *st, int x, int y, int deg)
{
	int stl, i;
	stl = strlen(st);
	if (orient==PORTRAIT)
	{
	if (x==RIGHT)
		x=(disp_x_size+1)-(stl*cfont.x_size);
	if (x==CENTER)
		x=((disp_x_size+1)-(stl*cfont.x_size))/2;
	}
	else
	{
	if (x==RIGHT)
		x=(disp_y_size+1)-(stl*cfont.x_size);
	if (x==CENTER)
		x=((disp_y_size+1)-(stl*cfont.x_size))/2;
	}

	for (i=0; i<stl; i++)
		if (deg==0)
			printChar(*st++, x + (i*(cfont.x_size)), y);
		else
			rotateChar(*st++, x, y, i, deg);
}



void printNumI(long num, int x, int y, int length, char filler)
{
	char buf[25];
	char st[27];
	bool neg=false;
	int c=0, f=0;
  
	if (num==0)
	{
		if (length!=0)
		{
			for (c=0; c<(length-1); c++)
				st[c]=filler;
			st[c]=48;
			st[c+1]=0;
		}
		else
		{
			st[0]=48;
			st[1]=0;
		}
	}
	else
	{
		if (num<0)
		{
			neg=true;
			num=-num;
		}
	  
		while (num>0)
		{
			buf[c]=48+(num % 10);
			c++;
			num=(num-(num % 10))/10;
		}
		buf[c]=0;
	  
		if (neg)
		{
			st[0]=45;
		}
	  
		if (length>(c+neg))
		{
			for (int i=0; i<(length-c-neg); i++)
			{
				st[i+neg]=filler;
				f++;
			}
		}

		for (int i=0; i<c; i++)
		{
			st[i+neg+f]=buf[c-i-1];
		}
		st[c+neg+f]=0;

	}

	print(st,x,y,0);
}

void printNumF(double num, uint8_t dec, int x, int y, char divider, int length, char filler)
{
	char st[27];
	bool neg=false;

	if (dec<1)
		dec=1;
	else if (dec>5)
		dec=5;

	if (num<0)
		neg = true;

	_convert_float(st, num, length, dec);

	if (divider != '.')
	{
		for (int i=0; i<sizeof(st); i++)
			if (st[i]=='.')
				st[i]=divider;
	}

	if (filler != ' ')
	{
		if (neg)
		{
			st[0]='-';
			for (int i=1; i<sizeof(st); i++)
				if ((st[i]==' ') || (st[i]=='-'))
					st[i]=filler;
		}
		else
		{
			for (int i=0; i<sizeof(st); i++)
				if (st[i]==' ')
					st[i]=filler;
		}
	}

	//print(st,x,y);
}




void setFont(FONTYPE fontype)
{

	
	switch(fontype)
	{
		case font8x12:
		cfont.font =(unsigned char*) SmallFont;
	    cfont.x_size=SmallFont[0];
	    cfont.y_size=SmallFont[1];
	    cfont.offset=SmallFont[2];
	    cfont.numchars=SmallFont[3];
		break;
		case font16x16:
		cfont.font =(unsigned char*) BigFont;
		cfont.x_size=BigFont[0];
		cfont.y_size=BigFont[1];
		cfont.offset=BigFont[2];
		cfont.numchars=BigFont[3];
		break;
		case font7seg:
		cfont.font = (unsigned char*) SevenSegNumFont;
		cfont.x_size=SevenSegNumFont[0];
		cfont.y_size=SevenSegNumFont[1];
		cfont.offset=SevenSegNumFont[2];
		cfont.numchars=SevenSegNumFont[3];
		break;
		case font48:
		cfont.font = (unsigned char*) GroteskBold24x48;
		cfont.x_size=GroteskBold24x48[0];
		cfont.y_size=GroteskBold24x48[1];
		cfont.offset=GroteskBold24x48[2];
		cfont.numchars=GroteskBold24x48[3];
		break;
		case font7_96_144:
		cfont.font = (unsigned char*) SevenSegment96x144Num;
		cfont.x_size=SevenSegment96x144Num[0];
		cfont.y_size=SevenSegment96x144Num[1];
		cfont.offset=SevenSegment96x144Num[2];
		cfont.numchars=SevenSegment96x144Num[3];
		break;
		
	}
	
}

uint8_t* getFont()
{
	return cfont.font;
}

uint8_t getFontXsize()
{
	return cfont.x_size;
}

uint8_t getFontYsize()
{
	return cfont.y_size;
}



//Representa un bitmap almacenado en la flash externa 25Q128
//x coordenada izquierda del bitmap
//y coordenada superior del bitmap
//sx ancho del bitmap
//sy alto del bitmap
//p apunta al inicio de datos den la flash externa
void drawBitmap(int x, int y, int sx, int sy,uint32_t p, int scale)
{
	unsigned int col;
	int tx, ty, tc, tsx, tsy;
    uint32_t pointer=0;
	if (scale==1)
	{
		for (ty=sy-1; ty>=0; ty--) //for (ty=0; ty<sy; ty++) espejo vertical
		{
			LCD_SetPos(x,x+sx, y+ty, y+sy);
			//for (tx=sx-1; tx>=0; tx--)//espejo for (tx=0; tx<sx; tx++)
			
			for (tx=0; tx<sx; tx++)
			{
				
				col=(mainScreen[pointer+1]<<8) |mainScreen[pointer];
				Write_Data_U16(col);
				pointer +=2;
				
			}
			

		}
	}
	else
	{
		for (ty=0; ty<sy; ty++)
		{
			for (tsy=0; tsy<scale; tsy++)
			{
				LCD_SetPos(x,x+((sx*scale)-1), y+(ty*scale)+tsy,  y+(ty*scale)+tsy);
				for (tx=0; tx<sx; tx++)
				{
					///col=wflash.IO_Read_word(p + pointer);//p +pointer
					for (tsx=0; tsx<scale; tsx++)
					Write_Data_U16(col);
					pointer +=2;
				}
			}
		}
	}
	clrXY();
}


/*

void UTFT::drawBitmap(int x, int y, int sx, int sy, uint32_t p, int scale)
{
	unsigned int col;
	//int tx, ty, tc, tsx, tsy;
    int32_t tx, ty, tc, tsx, tsy;
	if (scale==1)
	{
		if (orient==PORTRAIT)
		{
			setXY(x, y, x+sx-1, y+sy-1);
			for (tc=0; tc<(sx*sy); tc++)
			{
				col=wflash.IO_Read_word(p+2*tc);
				Write_Data(col>>8,col & 0xff);
			}
		}
		else
		{
			for (ty=0; ty<sy; ty++)
			{
				setXY(x, y+ty, x+sx-1, y+ty);
				for (tx=sx-1; tx>=0; tx--)
				{
					col=wflash.IO_Read_word(p+(2*ty*sx)+2*tx);
					Write_Data(col>>8,col & 0xff);
				}
			}
		}
	}
	else
	{
		if (orient==PORTRAIT)
		{
			for (ty=0; ty<sy; ty++)
			{
				setXY(x, y+(ty*scale), x+((sx*scale)-1), y+(ty*scale)+scale);
				for (tsy=0; tsy<scale; tsy++)
				for (tx=0; tx<sx; tx++)
				{
					col=wflash.IO_Read_word(p+(2*ty*sx)+2*tx);
					for (tsx=0; tsx<scale; tsx++)
					Write_Data(col>>8,col & 0xff);
				}
			}
		}
		else
		{
			for (ty=0; ty<sy; ty++)
			{
				for (tsy=0; tsy<scale; tsy++)
				{
					setXY(x, y+(ty*scale)+tsy, x+((sx*scale)-1), y+(ty*scale)+tsy);
					for (tx=sx-1; tx>=0; tx--)
					{
						col=wflash.IO_Read_word(p+(2*ty*sx)+2*tx);
						for (tsx=0; tsx<scale; tsx++)
						Write_Data(col>>8,col & 0xff);
					}
				}
			}
		}
	}
	clrXY();
}

*/


/*

void UTFT::drawBitmap(int x, int y, int sx, int sy, bitmapdatatype data, int deg, int rox, int roy)
{
	unsigned int col;
	int tx, ty, newx, newy;
	double radian;
	radian=deg*0.0175;  

	if (deg==0)
		drawBitmap(x, y, sx, sy, data);
	else
	{
		//cbi(P_CS, B_CS);
		for (ty=0; ty<sy; ty++)
			for (tx=0; tx<sx; tx++)
			{
				col=pgm_read_word(&data[(ty*sx)+tx]);

				newx=x+rox+(((tx-rox)*cos(radian))-((ty-roy)*sin(radian)));
				newy=y+roy+(((ty-roy)*cos(radian))+((tx-rox)*sin(radian)));

				setXY(newx, newy, newx, newy);
				Write_Data(col>>8,col & 0xff);
			}
		//sbi(P_CS, B_CS);
	}
	clrXY();
}

*/










int getDisplayXSize()
{
	if (orient==PORTRAIT)
		return disp_x_size+1;
	else
		return disp_y_size+1;
}

int getDisplayYSize()
{
	if (orient==PORTRAIT)
		return disp_y_size+1;
	else
		return disp_x_size+1;
}



void setDisplayPage(uint8_t page)
{
	if (page>7) page=7;
	
	Write_Cmd(0x0F); 
	Write_Cmd_Data(page);  
}

void setWritePage(uint8_t page)
{
	if (page>7) page=7;
	Write_Cmd(0x0F); 
	Write_Cmd_Data(page);
}

 //Conversión a bcd para visualización


void char_to_bcd(uint8_t c)
{

	uint8_t resto;
	digitos.centenas = 48 +  c / 100;    //centenas
	resto = c % 100;
	digitos.decenas = 48 +  resto / 10; //decenas
	resto = resto % 10;
	digitos.unidades = 48 +  resto;    //unidades

}

 void int_to_bcd(unsigned int value)
 {
	 unsigned int resto;
	 digitos.dec_miles = 48 +  value / 10000;
	 resto = value % 10000;
	 digitos.miles = 48 + resto / 1000;
	 resto=resto % 1000;
	 digitos.centenas = 48 + resto / 100;
	 resto=resto % 100;
	 digitos.decenas = 48 + resto / 10;
	 resto=resto % 10;
	 digitos.unidades = 48 + resto;
 }
 
 
 
 
 
 