📄 calibration.c~
字号:
/* * calibrate.c -- Primary C Source file of * Touchscreen calibration of IntelPXA255 * * (C)opyright 2004 Bit 920 Labs * Written by: Ye Nan <bye200009123@163.com> * Created on: Sat. Apr. 3 10:46:54 GMT +8:00 2004 */#ifndef __INTELPXA255_TS_CAL_C#define __INTELPXA255_TS_CAL_C#include <stdio.h>#include <stdlib.h>#include <linux/fb.h>#include <asm/fcntl.h>#include <asm/mman.h>#include "ts.h"#include "calibration.h"//int ts_handler = 0; TS_CONFIG tsConfig; /* touch screen configuration data */ts_event_t ts_CalPoints[CALIBRATE_POINT_NUM]; /* touch screen calibration pBuffer */ts_event_t current_data;S16 _ConvertX(U16 x);S16 _ConvertY(U16 y);// Configure touch screenint _TouchScreenConfigure(P_RECT pScreen, P_RECT pPan){ S32 tmp,w; RECT screen; RECT pan; tsConfig.scale = 10; /* scale factor */ screen = *pScreen; pan = *pPan; // Calculate x-axis factors tmp = fabs(pan.right-pan.left); if(tmp == 0) return -1; w = fabs(screen.right - screen.left);//*****************************// tsConfig.xFactor = (w << tsConfig.scale) / tmp; printf("x width = %u\n", w); // Calculate y-axis factors tmp = fabs(pan.bottom-pan.top); if(tmp == 0) return -1; w = fabs(screen.bottom - screen.top); tsConfig.yFactor = (w << tsConfig.scale) / tmp; printf("tmp = %u\n", tmp); printf("y width = %u\n", w); // Calculate offsets if ((tsConfig.xFactor == 0) || (tsConfig.yFactor == 0)) { return -1; } tsConfig.xOffset = pan.left - screen.left*fabs(pan.left - pan.right) / fabs(screen.left - screen.right);//*****************************// tsConfig.yOffset = pan.top - screen.top*fabs(pan.bottom - pan.top) / fabs(screen.bottom - screen.top); return 0;}// Retrieve an average data from touch screen input deviceint _GetPenDataAvg(int ts_handler, ts_event_t * ts_avg, unsigned short max_num){ ts_event_t *tmpDataArr; ts_event_t tmpData; unsigned short bp, i; unsigned short count; long tempX, tempY; int ret; // Allocate a buffer for input data tmpDataArr = (ts_event_t *)malloc(max_num * sizeof(ts_event_t)); if (tmpDataArr == NULL) { printf("Error: malloc failed.\n"); return -1; } for (i = 0; i < max_num; i++) { tmpDataArr[i].x = 0; tmpDataArr[i].y = 0; tmpDataArr[i].pressure = 0; tmpDataArr[i].pad = -1; } // Read a set of data into buffer count = 0; do { bp = 0; while (1) { //printf("before read, ts_handler is %d\n",ts_handler); ret = read(ts_handler, &tmpDataArr[bp], sizeof(ts_event_t)); printf("ret =%d, sizeof(ts_event_t)=%d\n",ret,sizeof(ts_event_t)); if (ret != sizeof(ts_event_t) || tmpDataArr[bp].pressure == 0) break; // tmpDataArr[bp].x += 30;//********************************// // tmpDataArr[bp].y += 50; //#ifdef ASP_TEST printf("x = 0x%4x, y = 0x%4x, Press = 0x%4x\n", tmpDataArr[bp].x, tmpDataArr[bp].y, tmpDataArr[bp].pressure); //#endif if (bp < max_num) { bp++; }//if }//while printf("bp= %d\n",bp); count = bp; } while (count == 0); tempX = tempY = 0; for (bp = 0; bp < count; bp++) { tempX += tmpDataArr[bp].x; tempY += tmpDataArr[bp].y; } // Calculate an average value ts_avg->x = tempX / count; ts_avg->y = tempY / count; ts_avg->pressure = 0xffff; printf("ts_avg->x=%d\n",ts_avg->x); printf("ts_avg->y=%d\n",ts_avg->y); // Release free(tmpDataArr);}// Check calibrate pBuffer are measured up or notint _CheckCalibratePoint(void){ unsigned short vl, vr, vt, vb; unsigned short diff, avg; vl = abs(ts_CalPoints[0].y - ts_CalPoints[1].y); vr = abs(ts_CalPoints[2].y - ts_CalPoints[3].y); vt = abs(ts_CalPoints[0].x - ts_CalPoints[3].x); vb = abs(ts_CalPoints[1].x - ts_CalPoints[2].x); diff = abs(vl - vr); avg = ((long)vl + (long)vr) / 2; if (diff > avg /20) //chang from 20 to 10 return -1; diff = abs(vt - vb); avg = ((long)vt + (long)vb) / 2; if (diff > avg /20) return -1; return 0; }// Do calibration of current touch screenint DoCalibrate(int ts_handler){ Point Screen_Point[CALIBRATE_POINT_NUM]; unsigned short x_max, y_max; unsigned short i; int tempx, tempy; RECT rt,st; FILE * fp; // Draw a cross on the screen void _DrawCross(Point p, unsigned long color) { fb_DrawLine_H(p.x - 5, p.x - 1, p.y, color); fb_DrawLine_H(p.x + 1, p.x + 5, p.y, color); fb_DrawLine_V(p.x, p.y - 5, p.y - 1, color); fb_DrawLine_V(p.x, p.y + 1, p.y + 5, color); } fb_Init(); x_max = fb_GetScreenWidth();// - 1;//*************************************// y_max = fb_GetScreenHeight();// - 1; fb_Clear(0x0); // Set screen position of calibration pBuffer Screen_Point[0].x = 60, Screen_Point[0].y = 60; Screen_Point[1].x = 60, Screen_Point[1].y = y_max - 60; Screen_Point[2].x = x_max - 60, Screen_Point[2].y = y_max - 60; Screen_Point[3].x = x_max - 60, Screen_Point[3].y = 60; Screen_Point[4].x = x_max / 2, Screen_Point[4].y = y_max / 2; // Do the calibration i = 0; while (1) { if (i == CALIBRATE_POINT_NUM) { if (_CheckCalibratePoint() < 0) { i = 0; } else break; } _DrawCross(Screen_Point[i], 0xffffffff); printf("before of function _GetPenDataAvg\n"); _GetPenDataAvg(ts_handler, &ts_CalPoints[i], 10);//max_num=10 printf("out of function _GetPenDataAvg\n"); printf("ts_CalPoints[%d].x = %d\n", i, ts_CalPoints[i].x); printf("ts_CalPoints[%d].y = %d\n", i, ts_CalPoints[i].y); _DrawCross(Screen_Point[i], 0x00000000); i++; } st.top = 60; st.left = 60; st.right = fb_GetScreenWidth() - 60; st.bottom = fb_GetScreenHeight() - 60; // Configure touch screen rt.bottom = (ts_CalPoints[0].y + ts_CalPoints[3].y) / 2; rt.left = (ts_CalPoints[0].x + ts_CalPoints[1].x) / 2; rt.right = (ts_CalPoints[2].x + ts_CalPoints[3].x) / 2; rt.top = (ts_CalPoints[1].y + ts_CalPoints[2].y) / 2; _TouchScreenConfigure(&st, &rt); for (i = 0; i < CALIBRATE_POINT_NUM; i++) { // printf("Point[%d]: x = 0x%4x, y = 0x%4x\n", i, ts_CalPoints[i].x, ts_CalPoints[i].y); tempx = ts_CalPoints[i].x; tempy = ts_CalPoints[i].y; printf("Point[%u]: x = %d, y = %d\n", i, tempx,tempy); tempx = _ConvertX(ts_CalPoints[i].x); tempy = y_max-_ConvertY(ts_CalPoints[i].y); printf("RealPoint[%u]: x = %d, y = %d\n", i, tempx,tempy); } tempx = _ConvertX(ts_CalPoints[4].x); tempy = y_max-_ConvertY(ts_CalPoints[4].y); printf("Center Point:x = %u, y = %d\n", tempx, tempy); fb_Release(); return;}// Convert touch screen point to Screen PointS16 _TouchpadConvertLCD(U16 value, U32 factor, U32 offset){ S32 temp; temp = value; temp -= offset; if ( temp < 0) { /* outside LCD area ... */ temp = -temp; return (S16)(((temp * factor) >> tsConfig.scale));//*******************************// } else { /* within LCD area */ return ((S16)((temp * factor) >> tsConfig.scale)); }}// Convert X-coordinateS16 _ConvertX(U16 x){ return _TouchpadConvertLCD(x, tsConfig.xFactor, tsConfig.xOffset); }// Convert Y-coordinateS16 _ConvertY(U16 y)//**************************************//{ // y = 4240 - y; return _TouchpadConvertLCD(y, tsConfig.yFactor, tsConfig.yOffset);// + 16;}#endif // __INTELPXA255_TS_CAL_C
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -