/*
 * APP_KEYBOARD.c
 *
 *  Created on: Jan 1, 2024
 *      Author: wuwen
 */

#include "APP_keyboard.h"

window *keyboard_window;

void APP_KEYBOARD_init(window *a_window)
{
	keyboard_window=a_window;
	keyboard_window->high=146;
	keyboard_window->width=180;
}


void down_one_key(int x,int y,char v)
{
	LCD_Fill(x,y,x+30,y+30,GRAY);
	LCD_ShowChar(x+8,y+8,v,16,GRAY,WHITE);
}

char getkeyboardv(int x,int y)
{

	if(x>keyboard_window->x+2&&x<keyboard_window->x+2+32&&y>keyboard_window->y+17&&y<keyboard_window->y+17+32)
	{
		return '0';
	}

	if(x>keyboard_window->x+34&&x<keyboard_window->x+34+32&&y>keyboard_window->y+17&&y<keyboard_window->y+17+32)
	{
		return '1';
	}


	if(x>keyboard_window->x+66&&x<keyboard_window->x+66+32&&y>keyboard_window->y+17&&y<keyboard_window->y+17+32)
	{
		return '2';
	}


	if(x>keyboard_window->x+98&&x<keyboard_window->x+98+32&&y>keyboard_window->y+17&&y<keyboard_window->y+17+32)
	{
		return '3';
	}



	if(x>keyboard_window->x+2&&x<keyboard_window->x+2+32&&y>keyboard_window->y+49&&y<keyboard_window->y+49+32)
	{
		return '4';
	}

	if(x>keyboard_window->x+34&&x<keyboard_window->x+34+32&&y>keyboard_window->y+49&&y<keyboard_window->y+49+32)
	{
		return '5';
	}


	if(x>keyboard_window->x+66&&x<keyboard_window->x+66+32&&y>keyboard_window->y+49&&y<keyboard_window->y+49+32)
	{
		return '6';
	}


	if(x>keyboard_window->x+98&&x<keyboard_window->x+98+32&&y>keyboard_window->y+49&&y<keyboard_window->y+49+32)
	{
		return '7';
	}



	if(x>keyboard_window->x+2&&x<keyboard_window->x+2+32&&y>keyboard_window->y+81&&y<keyboard_window->y+81+32)
	{
		return '8';
	}

	if(x>keyboard_window->x+34&&x<keyboard_window->x+34+32&&y>keyboard_window->y+81&&y<keyboard_window->y+81+32)
	{
		return '9';
	}


	if(x>keyboard_window->x+66&&x<keyboard_window->x+66+32&&y>keyboard_window->y+81&&y<keyboard_window->y+81+32)
	{
		return 'A';
	}


	if(x>keyboard_window->x+98&&x<keyboard_window->x+98+32&&y>keyboard_window->y+81&&y<keyboard_window->y+81+32)
	{
		return 'B';
	}



	if(x>keyboard_window->x+2&&x<keyboard_window->x+2+32&&y>keyboard_window->y+113&&y<keyboard_window->y+113+32)
	{
		return 'C';
	}

	if(x>keyboard_window->x+34&&x<keyboard_window->x+34+32&&y>keyboard_window->y+113&&y<keyboard_window->y+113+32)
	{
		return 'D';
	}


	if(x>keyboard_window->x+66&&x<keyboard_window->x+66+32&&y>keyboard_window->y+113&&y<keyboard_window->y+113+32)
	{
		return 'E';
	}


	if(x>keyboard_window->x+98&&x<keyboard_window->x+98+32&&y>keyboard_window->y+113&&y<keyboard_window->y+113+32)
	{
		return 'F';
	}

	if(x>keyboard_window->x+140&&x<keyboard_window->x+140+32&&y>keyboard_window->y+81&&y<keyboard_window->y+81+32)
	{
		return '<';
	}
	if(x>keyboard_window->x+140&&x<keyboard_window->x+140+32&&y>keyboard_window->y+113&&y<keyboard_window->y+113+32)
	{
		return 'X';
	}
	return 0;
}

char keyboard_char[16]={0};
char keyboard_char_int=0;
void APP_KEYBOARD_loop()
{


	//sprintf(keyboard_char,"%d",keyboard_window->touch_flag);
	LCD_ShowString(keyboard_window->x+130,keyboard_window->y+17,keyboard_char,16,GRAY,WHITE);
	if(keyboard_window->touch_flag==1)
	{
		keyboard_window->touch_flag=0;
		char tempv=getkeyboardv(keyboard_window->touch_x,keyboard_window->touch_y);

		switch(tempv)
		{
		case '<':
			if(keyboard_char_int>0)
			{
				keyboard_char[keyboard_char_int]='\0';
				keyboard_char_int-=1;
				keyboard_char[keyboard_char_int]='\0';
			}
			break;
		case 'X':
			keyboard_char_int=0;
			keyboard_char[keyboard_char_int]='\0';
			break;
		default :
			keyboard_char[keyboard_char_int]=tempv;
			keyboard_char_int+=1;
			keyboard_char[keyboard_char_int]='\0';
			break;

		}





	}
	if(keyboard_window->refresh_windows_flag)
	{
		keyboard_window->refresh_windows_flag--;
		//refresh_windows
		//LCD_ShowString(keyboard_window->x,keyboard_window->y+16,"test",16,CYAN,RED);
		down_one_key(keyboard_window->x+2,keyboard_window->y+17,'0'); down_one_key(keyboard_window->x+34,keyboard_window->y+17,'1'); down_one_key(keyboard_window->x+66,keyboard_window->y+17,'2'); down_one_key(keyboard_window->x+98,keyboard_window->y+17,'3');
		down_one_key(keyboard_window->x+2,keyboard_window->y+49,'4'); down_one_key(keyboard_window->x+34,keyboard_window->y+49,'5'); down_one_key(keyboard_window->x+66,keyboard_window->y+49,'6'); down_one_key(keyboard_window->x+98,keyboard_window->y+49,'7');
		down_one_key(keyboard_window->x+2,keyboard_window->y+81,'8'); down_one_key(keyboard_window->x+34,keyboard_window->y+81,'9'); down_one_key(keyboard_window->x+66,keyboard_window->y+81,'A'); down_one_key(keyboard_window->x+98,keyboard_window->y+81,'B');
		down_one_key(keyboard_window->x+2,keyboard_window->y+113,'C'); down_one_key(keyboard_window->x+34,keyboard_window->y+113,'D'); down_one_key(keyboard_window->x+66,keyboard_window->y+113,'E'); down_one_key(keyboard_window->x+98,keyboard_window->y+113,'F');

		down_one_key(keyboard_window->x+140,keyboard_window->y+81,'<');
		down_one_key(keyboard_window->x+140,keyboard_window->y+113,'X');
	}
}
