/* 
* classKeyboard.cpp
*
* Created: 08/02/2020 10:16:32
* Author: Ledo
*/


#include "classKeyboard.h"

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

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


void classKeyboard::drawKeys(void)
{
	int padding = 10;
	int ancho = 160;
	int alto = 100;
	int x = 25;
	int y = 25;
	Display.Tft_Clear(KEY_BACKCOLOR);
	Gui.drawRectangle(4, 4, 796, 476,VGA_WHITE);
	
	drawButton("1",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("4",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("7",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("DEL",x,y,ancho,alto);
	
	x = x + ancho + padding;
	y = 25;
	drawButton("2",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("5",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("8",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("0",x,y,ancho,alto);
	
	x = x + ancho + padding;
	y = 25;
	drawButton("3",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("6",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("9",x,y,ancho,alto);
	y = y + padding + alto;
	drawButton("ESC",x,y,ancho,alto);
	
	x = x + ancho + padding + 24;
	y = 252;
	drawButton("ENTER",x,y,ancho + 40,alto * 2);
	
	Display.POINT_COLOR = VGA_WHITE;
	Display.BACK_COLOR = KEY_BACKCOLOR;
	Gui.drawFillRectangle(x,25,x+200,25+alto,VGA_WHITE);
	Display.POINT_COLOR = VGA_BLACK;
	Gui.drawRectangle(x+1,26,x+199,25+alto-1,VGA_BLACK);
	Gui.drawRectangle(x,25,x+200,25+alto,VGA_BLACK);
	
	keyValue = 0;
	Gui.setFont(font7seg);

}

void classKeyboard::drawButton(char * string, uint16_t x1, uint16_t y1,uint16_t ancho, uint16_t alto)
{
	Gui.setFont(font48);
	Display.POINT_COLOR = KEY_COLOR;
	Display.BACK_COLOR = KEY_BACKCOLOR;
	Gui.drawFillRectangle(x1,y1,x1+ancho,y1+alto,KEY_COLOR);
	Display.POINT_COLOR = VGA_BLACK;
	Display.BACK_COLOR = KEY_BACKCOLOR;
	Gui.drawRectangle(x1+1,y1+1,x1+ancho-1,y1+alto-1,KEY_COLOR);
	Gui.drawRectangle(x1,y1,x1+ancho,y1+alto,KEY_BACKCOLOR);

	switch(strlen(string))
	{
		case 1:
		Gui.print(string, x1 + ancho / 3 + 20, y1 + alto / 3, 0, KEY_FORECOLOR, KEY_COLOR);
		break;
		case 3:
		Gui.print(string, x1 + ancho / 3, y1 + alto / 3, 0, KEY_FORECOLOR, KEY_COLOR);
		break;
		case 5:
		Gui.print(string, x1 + ancho / 4 - 10, y1 + alto / 3 + 10, 0, KEY_FORECOLOR, KEY_COLOR);
		break;
		default:
		break;
		
	}
}

uint8_t classKeyboard::presetKey(void)
{
	int16_t x,y;
	x = Touch.TCGetX();
	y = Touch.TCGetY();
	if(x < 170)
	return coll1_decode();
	if((x > 200) & (x < 350))
	return coll2_decode();
	if((x > 370) & (x < 520))
	return coll3_decode();
	if((x > 550) & (y > 300))
	return 13; //Enter Key
	return 0;
}

uint8_t classKeyboard::coll1_decode(void)
{
	int16_t y = Touch.TCGetY();
	if(y<120)
	return 1;
	
	if((y > 130) & (y < 220))
	return 4;
	
	if((y > 250) & (y < 340))
	return 7;

	if(y > 360)
	return 10;
	return 0;
}

uint8_t classKeyboard::coll2_decode(void)
{
	int16_t y = Touch.TCGetY();
	if(y < 120)
	return 2;
	
	if((y > 130) & (y < 220))
	return 5;
	
	if((y > 250) & (y < 340))
	return 8;

	if(y > 360)
	return 0;
	return 0;
}

uint8_t classKeyboard::coll3_decode(void)
{
	int16_t y = Touch.TCGetY();
	if(y < 120)
	return 3;
	
	if((y > 130) & ( y < 220))
	return 6;
	
	if((y > 250) & (y < 340))
	return 9;

	if(y > 360)
	return 12;
	return 0;
	
}

