Nuvoton
/
NuMaker-mbed-emWin-example
NuMaker emWin HMI
Embed:
(wiki syntax)
Show/hide line numbers
testutils.c
00001 /* 00002 * tslib/src/ts_getxy.c 00003 * 00004 * Copyright (C) 2001 Russell King. 00005 * 00006 * This file is placed under the GPL. Please see the file 00007 * COPYING for more details. 00008 * 00009 * $Id: testutils.c,v 1.2 2004/10/19 22:01:27 dlowder Exp $ 00010 * 00011 * Waits for the screen to be touched, averages x and y sample 00012 * coordinates until the end of contact 00013 */ 00014 #if 0 00015 //#include "config.h" 00016 #include <stdio.h> 00017 #include <stdlib.h> 00018 //#include <unistd.h> 00019 //#include <sys/time.h> 00020 //#include "wbio.h" 00021 //#include "wbtypes.h" 00022 //#include "wblib.h" 00023 #include "tslib.h" 00024 #include "fbutils.h" 00025 #include "TouchPanel.h" 00026 #include "GUI.h" 00027 00028 static int sort_by_x(const void* a, const void *b) 00029 { 00030 return (((struct ts_sample *)a)->x - ((struct ts_sample *)b)->x); 00031 } 00032 00033 static int sort_by_y(const void* a, const void *b) 00034 { 00035 return (((struct ts_sample *)a)->y - ((struct ts_sample *)b)->y); 00036 } 00037 00038 void getxy(int *x, int *y) 00039 { 00040 #define MAX_SAMPLES 128 00041 struct ts_sample samp[MAX_SAMPLES]; 00042 int index, middle; 00043 int sumx, sumy; 00044 00045 printf("getxy\n"); 00046 again: 00047 do 00048 { 00049 if ( Read_TouchPanel(&sumx, &sumy) > 0 ) 00050 { 00051 if ( (sumx < 0) || ( sumy < 0 ) ) 00052 continue; 00053 break; 00054 } 00055 } 00056 while (1); 00057 00058 /* Now collect up to MAX_SAMPLES touches into the samp array. */ 00059 index = 0; 00060 do 00061 { 00062 if (index < MAX_SAMPLES-1) 00063 index++; 00064 if ( Read_TouchPanel(&sumx, &sumy) > 0) 00065 { 00066 samp[index].x = sumx; 00067 samp[index].y = sumy; 00068 samp[index].pressure = 1000; 00069 } 00070 else 00071 { 00072 samp[index].x = samp[index-1].x; 00073 samp[index].y = samp[index-1].y; 00074 samp[index].pressure = 0; 00075 } 00076 00077 // printf("%d %d %d\n", samp[index].x, samp[index].y , samp[index].pressure); 00078 } 00079 while (samp[index].pressure > 0); 00080 //printf("Took %d samples...\n",index); 00081 00082 /* 00083 * At this point, we have samples in indices zero to (index-1) 00084 * which means that we have (index) number of samples. We want 00085 * to calculate the median of the samples so that wild outliers 00086 * don't skew the result. First off, let's assume that arrays 00087 * are one-based instead of zero-based. If this were the case 00088 * and index was odd, we would need sample number ((index+1)/2) 00089 * of a sorted array; if index was even, we would need the 00090 * average of sample number (index/2) and sample number 00091 * ((index/2)+1). To turn this into something useful for the 00092 * real world, we just need to subtract one off of the sample 00093 * numbers. So for when index is odd, we need sample number 00094 * (((index+1)/2)-1). Due to integer division truncation, we 00095 * can simplify this to just (index/2). When index is even, we 00096 * need the average of sample number ((index/2)-1) and sample 00097 * number (index/2). Calculate (index/2) now and we'll handle 00098 * the even odd stuff after we sort. 00099 */ 00100 middle = index/2; 00101 if (x) 00102 { 00103 qsort(samp, index, sizeof(struct ts_sample), sort_by_x); 00104 if (index & 1) 00105 *x = samp[middle].x; 00106 else 00107 *x = (samp[middle-1].x + samp[middle].x) / 2; 00108 } 00109 if (y) 00110 { 00111 qsort(samp, index, sizeof(struct ts_sample), sort_by_y); 00112 if (index & 1) 00113 *y = samp[middle].y; 00114 else 00115 *y = (samp[middle-1].y + samp[middle].y) / 2; 00116 } 00117 if ( (index <= 3) || ( *x < 0) || ( *y < 0 ) ) 00118 goto again; 00119 } 00120 00121 extern int ts_phy2log(int *sumx, int *sumy); 00122 00123 static int palette [] = 00124 { 00125 /*0x000000, 0xffe080, 0xffffff, 0xe0c0a0, 0x304050, 0x80b8c0*/ 00126 GUI_MAKE_COLOR(0x000000), GUI_MAKE_COLOR(0x80e0ff), GUI_MAKE_COLOR(0xffffff), GUI_MAKE_COLOR(0xa0c0e0), GUI_MAKE_COLOR(0x504030), GUI_MAKE_COLOR(0xc0b880), 00127 GUI_MAKE_COLOR(0x7F1F00), GUI_MAKE_COLOR(0x20201F), GUI_MAKE_COLOR(0x5F3F1F), GUI_MAKE_COLOR(0xAFBFCF), GUI_MAKE_COLOR(0xF080D0), GUI_MAKE_COLOR(0x3F477F), 00128 GUI_MAKE_COLOR(0x207820) 00129 }; 00130 #define NR_COLORS (sizeof (palette) / sizeof (palette [0])) 00131 00132 #define NR_BUTTONS 3 00133 struct ts_button 00134 { 00135 int x, y, w, h; 00136 char *text; 00137 int flags; 00138 #define BUTTON_ACTIVE 0x00000001 00139 }; 00140 static struct ts_button buttons [NR_BUTTONS]; 00141 00142 /* [inactive] border fill text [active] border fill text */ 00143 static int button_palette [6] = 00144 { 00145 1, 4, 2, 00146 1, 5, 0 00147 }; 00148 00149 void button_draw (struct ts_button *button) 00150 { 00151 int s = (button->flags & BUTTON_ACTIVE) ? 3 : 0; 00152 00153 rect(button->x, button->y, button->x + button->w, 00154 button->y + button->h, button_palette [s]); 00155 fillrect(button->x + 1, button->y + 1, 00156 button->x + button->w - 2, 00157 button->y + button->h - 2, button_palette [s + 1]); 00158 // put_string_center(button->x + button->w / 2, 00159 // button->y + button->h / 2, 00160 // button->text, button_palette [s + 2]); 00161 00162 GUI_SetColor(palette[button_palette [s + 2]]); 00163 GUI_DispStringHCenterAt(button->text, button->x + button->w / 2, button->y + button->h / 2); 00164 } 00165 00166 int button_handle (struct ts_button *button, int x, int y, unsigned int p) 00167 { 00168 int inside = (x >= button->x) && (y >= button->y) && 00169 (x < button->x + button->w) && 00170 (y < button->y + button->h); 00171 00172 if (p > 0) 00173 { 00174 if (inside) 00175 { 00176 if (!(button->flags & BUTTON_ACTIVE)) 00177 { 00178 button->flags |= BUTTON_ACTIVE; 00179 button_draw (button); 00180 } 00181 } 00182 else if (button->flags & BUTTON_ACTIVE) 00183 { 00184 button->flags &= ~BUTTON_ACTIVE; 00185 button_draw (button); 00186 } 00187 } 00188 else if (button->flags & BUTTON_ACTIVE) 00189 { 00190 button->flags &= ~BUTTON_ACTIVE; 00191 button_draw (button); 00192 return 1; 00193 } 00194 00195 return 0; 00196 } 00197 00198 static void refresh_screen(void) 00199 { 00200 int i; 00201 00202 // fillrect (0, 0, xres - 1, yres - 1, 0); 00203 GUI_Clear(); 00204 // put_string_center (xres/2, yres/4, "Touchscreen test program", 1); 00205 // put_string_center (xres/2, yres/4+20,"Touch screen to move crosshair", 2); 00206 00207 GUI_SetColor(palette[1]); 00208 GUI_DispStringHCenterAt("Touchscreen test program", xres / 2, yres / 4); 00209 00210 GUI_SetColor(palette[2]); 00211 GUI_DispStringHCenterAt("Touch screen to move crosshair", xres / 2, yres / 4 + 20); 00212 00213 for (i = 0; i < NR_BUTTONS; i++) 00214 button_draw (&buttons [i]); 00215 } 00216 00217 int ts_test(int xsize, int ysize) 00218 { 00219 int sumx, sumy; 00220 int x, y; 00221 unsigned int i; 00222 unsigned int mode = 0; 00223 int quit_pressed = 0; 00224 00225 xres = xsize; 00226 yres = ysize; 00227 00228 x = xres/2; 00229 y = yres/2; 00230 00231 for (i = 0; i < NR_COLORS; i++) 00232 setcolor (i, palette [i]); 00233 00234 /* Initialize buttons */ 00235 //memset (&buttons, 0, sizeof (buttons)); 00236 buttons [0].w = buttons [1].w = buttons [2].w = xres / 4; 00237 buttons [0].h = buttons [1].h = buttons [2].h = 20; 00238 buttons [0].x = 0; 00239 buttons [1].x = (3 * xres) / 8; 00240 buttons [2].x = (3 * xres) / 4 - 1; 00241 buttons [0].y = buttons [1].y = buttons [2].y = 10; 00242 buttons [0].text = "Drag"; 00243 buttons [1].text = "Draw"; 00244 buttons [2].text = "Quit"; 00245 00246 refresh_screen (); 00247 00248 while (1) 00249 { 00250 struct ts_sample samp; 00251 00252 /* Show the cross */ 00253 if ((mode & 15) != 1) 00254 put_cross(x, y, 2 | XORMODE); 00255 00256 if ( Read_TouchPanel(&sumx, &sumy) > 0) 00257 { 00258 ts_phy2log(&sumx, &sumy); 00259 samp.x = sumx; 00260 samp.y = sumy; 00261 samp.pressure = 1000; 00262 } 00263 else 00264 { 00265 samp.x = x; 00266 samp.y = y; 00267 samp.pressure = 0; 00268 } 00269 GUI_Delay(30); 00270 00271 /* Hide it */ 00272 if ((mode & 15) != 1) 00273 put_cross(x, y, 2 | XORMODE); 00274 00275 for (i = 0; i < NR_BUTTONS; i++) 00276 if (button_handle(&buttons [i], samp.x, samp.y, samp.pressure)) 00277 switch (i) 00278 { 00279 case 0: 00280 mode = 0; 00281 refresh_screen (); 00282 break; 00283 case 1: 00284 mode = 1; 00285 refresh_screen (); 00286 break; 00287 case 2: 00288 quit_pressed = 1; 00289 } 00290 00291 if (samp.pressure > 0) 00292 { 00293 if (mode == 0x80000001) 00294 line (x, y, samp.x, samp.y, 2); 00295 //pixel(x, y, 2); 00296 x = samp.x; 00297 y = samp.y; 00298 mode |= 0x80000000; 00299 } 00300 else 00301 mode &= ~0x80000000; 00302 if (quit_pressed) 00303 break; 00304 } 00305 // fillrect(0, 0, xres - 1, yres - 1, 0); 00306 GUI_Clear(); 00307 00308 return 0; 00309 } 00310 #endif
Generated on Mon Mar 4 2024 07:48:13 by 1.7.2