2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 * @brief Touchpanel(eGalax) Calibration Tool
15 #include "ico_ictl-touch_egalax.h"
17 /* Number of times to repeat a touch about each point */
18 #define CALIBCONF_READ_NUM (5)
19 #define XY_COORDNATE_DELTA (50)
21 /* Macro for adjust coordinate */
22 #define delta_add(x) \
23 (x) > 256 ? (x) + XY_COORDNATE_DELTA : ((x) > XY_COORDNATE_DELTA ? (x) - XY_COORDNATE_DELTA : 0);
25 static void print_usage(const char *pName);
26 static char *find_event_device(void);
27 static FILE *open_conffile(void);
28 static void get_coordinates(int evfd);
29 static void read_event(int evfd, int *x, int *y);
30 static void sort_data(int buff[], int left, int right);
32 int mDispWidth = CALIBRATION_DISP_WIDTH;
33 int mDispHeight = CALIBRATION_DISP_HEIGHT;
39 /*--------------------------------------------------------------------------*/
41 * @brief Touchpanel(eGalax) Calibration Tool main routine
43 * @param arguments of standard main routeins(argc, argv)
46 * @retval 8 configuration file Error
47 * @retval 9 can not open event device(touchpanel)
49 /*--------------------------------------------------------------------------*/
51 main(int argc, char *argv[])
54 int evfd; /* event device fd */
55 char *eventDeviceName = NULL; /* event device name to hook */
60 for (ii = 1; ii < argc; ii++) {
61 if (strcasecmp(argv[ii], "-h") == 0) {
66 else if (strcasecmp(argv[ii], "-width") == 0) {
73 mDispWidth = strtol(argv[ii], (char **)0, 0);
74 if ((mDispWidth <= 0) || (mDispWidth > 8192)) {
79 else if (strcasecmp(argv[ii], "-height") == 0) {
86 mDispHeight = strtol(argv[ii], (char **)0, 0);
87 if ((mDispHeight <= 0) || (mDispHeight > 8192)) {
93 /* Input event device name */
94 eventDeviceName = argv[ii];
98 if (eventDeviceName == NULL) {
99 /* If event device not present, get default device */
100 eventDeviceName = find_event_device();
101 if (eventDeviceName == NULL) {
102 /* System has no touchpanel, Error */
107 evfd = open(eventDeviceName, O_RDONLY);
109 perror("Open event device");
113 /* Open configuration file for output */
114 fp = open_conffile();
116 fprintf(stderr, "%s: Can not open config file\n", argv[0]);
121 CALIBRATION_PRINT("\n");
122 CALIBRATION_PRINT("+================================================+\n");
123 CALIBRATION_PRINT("| Configration Tool for Calibration Touch ver0.1 |\n");
124 CALIBRATION_PRINT("+------------------------------------------------+\n");
125 CALIBRATION_PRINT("| use display width = %d\n", CALIBRATION_DISP_WIDTH);
126 CALIBRATION_PRINT("| use display height = %d\n", CALIBRATION_DISP_HEIGHT);
127 CALIBRATION_PRINT("+------------------------------------------------+\n");
129 get_coordinates(evfd);
131 CALIBRATION_PRINT("+------------------------------------------------+\n");
132 CALIBRATION_PRINT("| save config |\n");
133 CALIBRATION_PRINT("+------------------------------------------------+\n");
135 cnt = sprintf(buff, "%s=%d\n", CALIBRATOIN_STR_DISP_W, mDispWidth);
136 fwrite(buff, sizeof(char), cnt, fp);
137 CALIBRATION_PRINT("| %s", buff);
139 cnt = sprintf(buff, "%s=%d\n", CALIBRATOIN_STR_DISP_H, mDispHeight);
140 fwrite(buff, sizeof(char), cnt, fp);
141 CALIBRATION_PRINT("| %s", buff);
143 cnt = sprintf(buff, "%s=%d*%d\n", CALIBRATOIN_STR_POS1, mPosX[0], mPosY[0]);
144 fwrite(buff, sizeof(char), cnt, fp);
145 CALIBRATION_PRINT("| %s", buff);
147 cnt = sprintf(buff, "%s=%d*%d\n", CALIBRATOIN_STR_POS2, mPosX[1], mPosY[1]);
148 fwrite(buff, sizeof(char), cnt, fp);
149 CALIBRATION_PRINT("| %s", buff);
151 cnt = sprintf(buff, "%s=%d*%d\n", CALIBRATOIN_STR_POS3, mPosX[2], mPosY[2]);
152 fwrite(buff, sizeof(char), cnt, fp);
153 CALIBRATION_PRINT("| %s", buff);
155 cnt = sprintf(buff, "%s=%d*%d\n", CALIBRATOIN_STR_POS4, mPosX[3], mPosY[3]);
156 fwrite(buff, sizeof(char), cnt, fp);
157 CALIBRATION_PRINT("| %s", buff);
163 /* Close outputed configuration file */
166 CALIBRATION_PRINT("| |\n");
167 CALIBRATION_PRINT("| finish Tools |\n");
172 /*--------------------------------------------------------------------------*/
174 * @brief find_event_device: Find eGalax touchpanel device
177 * @return device name
178 * @retval != NULL device name string
179 * @retvel == NULL system has no eGalax touchpanel
181 /*--------------------------------------------------------------------------*/
183 find_event_device(void)
189 static char edevice[64];
191 /* Search input device */
192 fp = fopen("/proc/bus/input/devices", "r");
194 CALIBRATION_INFO("find_event_device: /proc/bus/input/devices Open Error\n");
198 while (fgets(buf, sizeof(buf), fp)) {
200 for (i = 0; buf[i]; i++) {
201 if (strncmp(&buf[i], "eGalax", 6) == 0) {
202 /* Find eGalax touchpanel device */
209 for (i = 0; buf[i]; i++) {
210 if (strncmp(&buf[i], "Handlers", 8) == 0) {
211 /* Find eGalax device and Handlers */
212 for (j = i+8; buf[j]; j++) {
213 if (buf[j] != ' ') continue;
214 strcpy(edevice, "/dev/input/");
215 for (k = strlen(edevice); k < (int)sizeof(edevice); k++) {
217 if ((buf[j] == 0) || (buf[j] == ' ') ||
218 (buf[j] == '\t') || (buf[j] == '\n') ||
219 (buf[j] == '\r')) break;
223 CALIBRATION_INFO("Event device of eGalax=<%s>\n", edevice);
234 CALIBRATION_PRINT("System has no eGalax Touchpanel\n");
238 /*--------------------------------------------------------------------------*/
240 * @brief read coordinates form touchpanel
242 * @param[in] evfd touchpanel event device
245 /*--------------------------------------------------------------------------*/
247 get_coordinates(int evfd)
249 int bufX[CALIBCONF_READ_NUM];
250 int bufY[CALIBCONF_READ_NUM];
254 CALIBRATION_PRINT("| Touch the Top-Left corner of the screen %d times\n",
256 for (ii = 0; ii < CALIBCONF_READ_NUM; ii++) {
257 read_event(evfd, &x, &y);
260 CALIBRATION_PRINT("| # %d: %dx%d \n", ii, x, y);
262 sort_data(bufX, 0, CALIBCONF_READ_NUM - 1);
263 sort_data(bufY, 0, CALIBCONF_READ_NUM - 1);
264 mPosX[0] = delta_add(bufX[CALIBCONF_READ_NUM / 2]);
265 mPosY[0] = delta_add(bufY[CALIBCONF_READ_NUM / 2]);
267 CALIBRATION_PRINT("+------------------------------------------------+\n");
268 CALIBRATION_PRINT("| Touch the Top-Right corner of the screen %d times\n",
270 for (ii = 0; ii < CALIBCONF_READ_NUM; ii++) {
271 read_event(evfd, &x, &y);
274 CALIBRATION_PRINT("| # %d: %dx%d \n", ii, x, y);
276 sort_data(bufX, 0, CALIBCONF_READ_NUM - 1);
277 sort_data(bufY, 0, CALIBCONF_READ_NUM - 1);
278 mPosX[1] = delta_add(bufX[CALIBCONF_READ_NUM / 2]);
279 mPosY[1] = delta_add(bufY[CALIBCONF_READ_NUM / 2]);
281 CALIBRATION_PRINT("+------------------------------------------------+\n");
282 CALIBRATION_PRINT("| Touch the Bottom-Left corner of the screen %d times\n",
284 for (ii = 0; ii < CALIBCONF_READ_NUM; ii++) {
285 read_event(evfd, &x, &y);
288 CALIBRATION_PRINT("| # %d: %dx%d \n", ii, x, y);
290 sort_data(bufX, 0, CALIBCONF_READ_NUM - 1);
291 sort_data(bufY, 0, CALIBCONF_READ_NUM - 1);
292 mPosX[2] = delta_add(bufX[CALIBCONF_READ_NUM / 2]);
293 mPosY[2] = delta_add(bufY[CALIBCONF_READ_NUM / 2]);
295 CALIBRATION_PRINT("+------------------------------------------------+\n");
296 CALIBRATION_PRINT("| Touch the Bottom-Right corner of the screen %d times\n",
298 for (ii = 0; ii < CALIBCONF_READ_NUM; ii++) {
299 read_event(evfd, &x, &y);
302 CALIBRATION_PRINT("| # %d: %dx%d \n", ii, x, y);
304 sort_data(bufX, 0, CALIBCONF_READ_NUM - 1);
305 sort_data(bufY, 0, CALIBCONF_READ_NUM - 1);
306 mPosX[3] = delta_add(bufX[CALIBCONF_READ_NUM / 2]);
307 mPosY[3] = delta_add(bufY[CALIBCONF_READ_NUM / 2]);
310 /*--------------------------------------------------------------------------*/
312 * @brief sort integer dates
314 * @param[in] buff array of integer datas
315 * @param[in] left array start index
316 * @param[in] right array end index
319 /*--------------------------------------------------------------------------*/
321 sort_data(int buff[], int left, int right)
329 pivot = buff[(left + right) / 2];
331 while (buff[ii] < pivot) {
334 while (pivot < buff[jj]) {
347 if (left < (ii - 1)) {
348 sort_data(buff, left, ii - 1);
350 if ((jj + 1) < right) {
351 sort_data(buff, jj + 1, right);
355 /*--------------------------------------------------------------------------*/
357 * @brief event read from touchpanel
359 * @param[in] evfd touchpanel event device
362 /*--------------------------------------------------------------------------*/
364 read_event(int evfd, int *x, int *y)
372 struct timeval delay;
373 struct input_event events[64];
376 /* select timeoutt value */
378 delay.tv_usec = 100*1000;
383 /* wait for event read or timedout */
384 ret = select(evfd + 1, &fds, NULL, NULL, &delay);
388 if (FD_ISSET(evfd, &fds)) {
390 rsize = read(evfd, events, sizeof(events) );
391 for (ii = 0; ii < (int)(rsize/sizeof(struct input_event)); ii++) {
392 if ((events[ii].type == EV_ABS) && (events[ii].code == ABS_X)) {
395 *x = events[ii].value;
397 else if ((events[ii].type == EV_ABS) && (events[ii].code == ABS_Y)) {
400 *y = events[ii].value;
404 /* Input 2 times (Touch On and Off) */
405 if ((flagX >= 2) && (flagY >= 2)) {
412 delay.tv_usec = 200*1000;
416 /* wait for input (or timedout) */
417 ret = select(evfd + 1, &fds, NULL, NULL, &delay);
424 rsize = read(evfd, events, sizeof(events));
428 /*--------------------------------------------------------------------------*/
430 * @brief open configuration file
433 * @return file descriptor
434 * @retval != NULL file descriptor
435 * @retval NULL open error
437 /*--------------------------------------------------------------------------*/
444 /* Get configuration file path */
445 confp = getenv(CALIBRATOIN_CONF_ENV);
447 confp = CALIBRATOIN_CONF_FILE;
450 /* Open configuration file */
451 fp = fopen(confp, "w");
459 /*--------------------------------------------------------------------------*/
463 * @param[in] pName program name
466 /*--------------------------------------------------------------------------*/
468 print_usage(const char *pName)
470 fprintf(stderr, "Usage: %s [-h][-width width][-height height] [device]\n", pName);