/**************************************************
******* Lee la coordenada y ********************
**************************************************/
int16 y_pos(void)
{
int16 result;
set_tris_a(0x04);
output_a(0x0A); //Energize X plate, RA1 and RA3
set_adc_channel(2); //Set AN2 as input
delay_us(10);
result = read_adc(); //get Y axis value
if (result != 0)
{
set_tris_a(0x01);
set_adc_channel(0);
delay_us(10);
tmp = (1023 - read_adc());
result = (result + tmp) >> 1;
}
Return(result);
}
/**************************************************
******* Lee la coordenada x ********************
**************************************************/
int16 x_pos(void)
{
int16 result;
set_tris_a(0x02);
output_a(0x05); //Energize X plate, RA1 and RA3
set_adc_channel(1); //Set AN2 as input
delay_us(10);
result = read_adc(); //get Y axis value
if (result != 0)
{
set_tris_a(0x08);
set_adc_channel(3);
delay_us(10);
tmp = (1023 - read_adc());
result = (result + tmp) >> 1;
}
Return(result);
}
/**************************************************
*** Funcion para determinar si la pantalla ***
*** ha sudo pulsada o no... ***
**************************************************/
int1 touch(void)
{
int1 resp;
int16 tmp,ty,tx;
resp=false;
ty = y_pos();
if (ty > 0)
{
delay_ms(30);
ty=y_pos();
tx=x_pos();
if (ty > y_min)
{
if(ty < y_max)
{
resp = true;
tmp=(tx - x_min) << 7;
x_loc =(int16)(tmp/x_rng);
if(x_loc > 127) x_loc = 127;
tmp = (ty - y_min) << 6;
y_loc = (int16)(tmp/y_rng);
if(y_loc > 63) y_loc = 63;
}
}
}
return(resp);
}
/*************************************************************
***** Rutina para calibrar la pantalla *******
*************************************************************/
void calibracion()
{
int1 sw;
glcd_fillScreen(OFF);
glcd_update();
sw=1;
while(x_pos()==0)
{
glcd_rect(0,0,2,2,YES,SW);
glcd_update();
delay_ms(100);
sw=!sw;
}
delay_ms(30);
y_min = y_pos();
x_min = x_pos();
glcd_rect(0,0,2,2,YES,OFF);
glcd_update();
while(x_pos() > 0);
delay_ms(30);
while(x_pos()==0)
{
glcd_rect(125,61,127,63,YES,SW);
glcd_update();
delay_ms(100);
sw=!sw;
}
delay_ms(30);
y_max = y_pos();
x_max = x_pos();
y_rng = y_max - y_min;
x_rng = x_max - x_min;
while(x_pos() > 0);
glcd_fillScreen(OFF);
glcd_update();
}
/**********************************************************
**** muestra las coordenadas ***************************
**********************************************************/
void print_xy_val()
{
glcd_rect(13,40,29,57,YES,OFF);
if(Touch()==true)
{
touch_flag = true;
sprintf(valor_x,"%lu",x_loc);
valor_x[4]='\0';
glcd_text57(13,40,valor_x,1,ON);
sprintf(valor_y,"%lu",y_loc);
valor_y[3]='\0';
glcd_text57(13,50,valor_y,1,ON);
}
else
{
touch_flag = false;
glcd_rect(13,40,29,57,YES,OFF);
}
//glcd_text57(1,40,x1,1,ON);
//glcd_text57(1,50,x2,1,ON);
glcd_update();
}
/*****************************************************************
****** etermina sobre que boton se ha pulsado ********
*****************************************************************/
int8 boton_act(void)
{
int8 button;
if(((x_loc>3) &&(x_loc<17)) &((y_loc>31) &&(y_loc<44))) button = '1';
if(((x_loc>22) &&(x_loc<37))&((y_loc>31) &&(y_loc<44))) button = '2';
if(((x_loc>49)&&(x_loc<58))&((y_loc>31) &&(y_loc<44))) button = '3';
if(((x_loc>65) &&(x_loc<78)) &((y_loc>31)&&(y_loc<44))) button = '4';
if(((x_loc>85) &&(x_loc<100))&((y_loc>31)&&(y_loc<44))) button = '5';
if(((x_loc>107)&&(x_loc<121))&((y_loc>31)&&(y_loc<44))) button = '6';
if(((x_loc>1) &&(x_loc<15)) &((y_loc>49)&&(y_loc<62))) button = '7';
if(((x_loc>21) &&(x_loc<37))&((y_loc>49)&&(y_loc<62))) button = '8';
if(((x_loc>42)&&(x_loc<58))&((y_loc>49)&&(y_loc<62))) button = '9';
if(((x_loc>64) &&(x_loc<78)) &((y_loc>49)&&(y_loc<62))) button = '0';
if(((x_loc>84) &&(x_loc<101))&((y_loc>49)&&(y_loc<62))) button = 'X';
if(((x_loc>105) &&(x_loc<124))&((y_loc>49)&&(y_loc<62))) button = 'O';
return(button);
}