#include <stdio.h>
#include <stdlib.h>
#include <tslib.h>      //包含tslib.h头文件


int judge_number(const int x, const int y)
{
    if((y>=0) && (y<160)){
        if((x >= 0) && (x < 160)){
            printf("清除\n");
        }
        else if(x < 320) printf("Number:7\n");
        else if(x < 480) printf("Number:4\n");
        else if(x < 640) printf("Number:1\n");
    }
    else if( y<320){
        if((x >= 0) && (x < 160)){
            printf("Number:0\n");
        }
        else if(x < 320) printf("Number:8\n");
        else if(x < 480) printf("Number:5\n");
        else if(x < 640) printf("Number:2\n");
    }
    else if( y<480){
        if((x >= 0) && (x < 160)){
            printf("确定\n");
        }
        else if(x < 320) printf("Number:9\n");
        else if(x < 480) printf("Number:6\n");
        else if(x < 640) printf("Number:3\n");
    }
}

int main(int argc, char *argv[])
{
    struct tsdev *ts = NULL;
    struct ts_sample samp;
    int pressure = 0;//用于保存上一次的按压力,初始为0,表示松开
    int a;

    /* 打开并配置触摸屏设备 */
    ts = ts_setup(NULL, 0);
    if (NULL == ts) {
        fprintf(stderr, "ts_setup error");
        exit(EXIT_FAILURE);
    }

    /* 读数据 */
    for ( ; ; ) {

        if (0 > ts_read(ts, &samp, 1)) {
            fprintf(stderr, "ts_read error");
            ts_close(ts);
            exit(EXIT_FAILURE);
        }

        if (samp.pressure) {//按压力>0
            if (pressure)   //若上一次的按压力>0
                //printf("移动(%d, %d)\n", samp.x, samp.y);
                a = 0;
            else
                judge_number(samp.x,samp.y);
                //printf("按下(%d, %d)\n", samp.x, samp.y);
           while(1)
           {
                ts_read(ts, &samp, 1);
                if(!samp.pressure) break;
           }
        }
        else printf("松开\n");//打印坐标

        pressure = samp.pressure;
    }

    ts_close(ts);
    exit(EXIT_SUCCESS);
}