OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [mw/] [src/] [contrib/] [GPL/] [tpcal/] [tpcal.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Touch-panel calibration program
3
 * Copyright (C) 1999 Bradley D. LaRonde <brad@ltc.com>
4
 *
5
 * This program is free software; you may redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include <stdio.h>
20
#include <errno.h>
21
#include <stdlib.h>
22
#include <time.h>
23
#include <string.h>
24
#include "windows.h"
25
#include "mou_tp.h"
26
#include "transform.h"
27
 
28
static int xext, yext;
29
static CALIBRATION_PAIRS cps;
30
static CALIBRATION_PAIR* pcp = 0;
31
 
32
void DrawAbout(HDC hdc, RECT r)
33
{
34
        const int ver_major = 0;
35
        const int ver_minor = 5;
36
        const char app_name[] = "Touch Panel Calibrator";
37
        const char title[] = "%s, version %d.%d";
38
        const char copyright[] = "(C) 1999 Bradley D. LaRonde <brad@ltc.com>";
39
        const char warranty[] = "ABSOLUTELY NO WARRANTY";
40
        const char license1[] = "This is free software, and you are welcome to";
41
        const char license2[] = "redistribute it under certain conditions.";
42
 
43
        const int leading = 15;
44
 
45
        char s[1024];
46
 
47
        sprintf(s, title, app_name, ver_major, ver_minor);
48
        SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
49
        DrawText(hdc, s, -1, &r, DT_CENTER);
50
 
51
        r.top += leading;
52
        DrawText(hdc, copyright, -1, &r, DT_CENTER);
53
 
54
        r.top += leading;
55
        DrawText(hdc, warranty, -1, &r, DT_CENTER);
56
 
57
        r.top += leading;
58
        DrawText(hdc, license1, -1, &r, DT_CENTER);
59
        r.top += leading;
60
        DrawText(hdc, license2, -1, &r, DT_CENTER);
61
}
62
 
63
void DrawDone(HDC hdc, RECT r)
64
{
65
        const char donemsg[] = "Calibration is done!";
66
        SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
67
        DrawText(hdc, donemsg, -1, &r, DT_CENTER);
68
}
69
 
70
 
71
void DrawTarget(HDC hdc, POINT p)
72
{
73
        const int scale = 9;
74
        const int center = 3;
75
 
76
        // I never knew that Windows GDI always leaves off
77
        // the last point in a line, and the right and bottom side of a rectangle.
78
        // Why would they make use always figure +1?  It isn't always obvious where
79
        // +1 is (i.e. a line going at a 10 degree angle).  Blech.
80
        // I can only hope that there is some overwhelmingly compelling reason why.
81
 
82
        Rectangle(hdc, p.x - center, p.y - center, p.x + center + 1, p.y + center + 1);
83
 
84
        // scale
85
        MoveToEx(hdc, p.x - scale, p.y, NULL);
86
        LineTo(hdc, p.x + scale + 1, p.y);
87
 
88
        MoveToEx(hdc, p.x, p.y - scale, NULL);
89
        LineTo(hdc, p.x, p.y + scale + 1);
90
}
91
 
92
void DrawLabelAlign(HDC hdc, POINT p, LPCSTR psz, int align)
93
{
94
        RECT r;
95
        const int w = 180;
96
        const int h = 14;
97
        const int m = 15;
98
 
99
        switch(align)
100
        {
101
                case 1:
102
                        // right
103
                        r.left = p.x + m;
104
                        r.right = p.x + w + m;
105
                        r.top =  p.y - (h/2);
106
                        r.bottom = p.y + (h/2);
107
                        break;
108
 
109
                case 2:
110
                        // left
111
                        r.left = p.x - (w + m);
112
                        r.right = p.x - m;
113
                        r.top =  p.y - (h/2);
114
                        r.bottom = p.y + (h/2);
115
                        break;
116
 
117
                case 3:
118
                        // below
119
                        r.left = p.x - (w/2);
120
                        r.right = p.x + (w/2);
121
                        r.top =  p.y + m;
122
                        r.bottom = p.y + m + h;
123
                        break;
124
 
125
                default:
126
                        // at
127
                        r.left = p.x;
128
                        r.right = p.x + w;
129
                        r.top =  p.y;
130
                        r.bottom = p.y + h;
131
                        break;
132
        }
133
 
134
        SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
135
        DrawText(hdc, psz, -1, &r, DT_LEFT);
136
}
137
 
138
void DrawLabel(HDC hdc, POINT p, LPCSTR psz)
139
{
140
        if ( p.x < (xext * 1 / 4) )
141
                return DrawLabelAlign(hdc, p, psz, 1);
142
 
143
        if ( p.x > (xext * 3 / 4) )
144
                return DrawLabelAlign(hdc, p, psz, 2);
145
 
146
        return DrawLabelAlign(hdc, p, psz, 3);
147
}
148
 
149
POINT GetTarget(int n)
150
{
151
        const int inset = 10;
152
        POINT p;
153
 
154
        switch (n)
155
        {
156
                case 0:
157
                        p.x = xext / 2; p.y = yext / 2;
158
                        pcp = &cps.center;
159
                        break;
160
 
161
                case 1:
162
                        p.x = inset; p.y = inset;
163
                        pcp = &cps.ul;
164
                        break;
165
 
166
                case 2:
167
                        p.x = xext - inset; p.y = inset;
168
                        pcp = &cps.ur;
169
                        break;
170
 
171
                case 3:
172
                        p.x = xext - inset; p.y = yext - inset;
173
                        pcp = &cps.lr;
174
                        break;
175
 
176
                case 4:
177
                        p.x = inset; p.y = yext - inset;
178
                        pcp = &cps.ll;
179
                        break;
180
 
181
                default:
182
                        // return a random target
183
                        p.x = random() / (RAND_MAX / xext);
184
                        p.y = random() / (RAND_MAX / yext);
185
                        pcp = 0;
186
                        break;
187
        }
188
 
189
        return p;
190
}
191
 
192
static int total_targets = 5;
193
static int current_target = 0;
194
static POINT current_target_location;
195
 
196
void DoPaint(HDC hdc)
197
{
198
        const char szInstructions[] = "Please touch the center of the target.";
199
 
200
        POINT p;
201
        int i, n;
202
        int old_rop;
203
        POINT last = current_target_location;
204
        HPEN hOldPen;
205
 
206
        if (current_target == total_targets) {
207
                RECT r = {10, yext/2,  xext - 10, yext/2 + 40};
208
                DrawDone(hdc, r);
209
                return;
210
        }
211
 
212
        if (current_target == 0)
213
        {
214
                RECT r = {10, yext - 85, xext - 10, yext - 10};
215
                DrawAbout(hdc, r);
216
        }
217
 
218
        current_target_location = GetTarget(current_target);
219
 
220
        old_rop = SetROP2(hdc, R2_XORPEN);
221
        hOldPen = SelectObject(hdc, GetStockObject(WHITE_PEN));
222
 
223
        n = 20;
224
        for (i=0; i < n; i++)
225
        {
226
                p.x = last.x + ((current_target_location.x - last.x) * i / n);
227
                p.y = last.y + ((current_target_location.y - last.y) * i / n);
228
                DrawTarget(hdc, p);
229
                Sleep(60);
230
                DrawTarget(hdc, p);
231
        }
232
 
233
        // final position
234
        SetROP2(hdc, R2_COPYPEN);
235
        SelectObject(hdc, GetStockObject(BLACK_PEN));
236
 
237
        DrawTarget(hdc, current_target_location);
238
        DrawLabel(hdc, current_target_location, szInstructions);
239
 
240
        // put things back
241
        SetROP2(hdc, old_rop);
242
        SelectObject(hdc, hOldPen);
243
}
244
 
245
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
246
#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
247
 
248
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
249
{
250
        PAINTSTRUCT ps;
251
        HDC hdc;
252
 
253
        switch(msg) {
254
                case WM_PAINT:
255
                        hdc = BeginPaint(hwnd, &ps);
256
                        DoPaint(hdc);
257
                        EndPaint(hwnd, &ps);
258
                        break;
259
 
260
                case WM_NCHITTEST:
261
                        return HTCLIENT;
262
 
263
                case WM_LBUTTONUP:
264
                        if ( pcp != 0 )
265
                        {
266
                                pcp->screen.x = current_target_location.x * TRANSFORMATION_UNITS_PER_PIXEL;
267
                                pcp->screen.y = current_target_location.y * TRANSFORMATION_UNITS_PER_PIXEL;
268
                                pcp->device.x = GET_X_LPARAM(lp);
269
                                pcp->device.y = GET_Y_LPARAM(lp);
270
                        }
271
 
272
                        if ( ++current_target == total_targets )
273
                        {
274
                                TRANSFORMATION_COEFFICIENTS tc;
275
#if 0
276
                                CalcTransformationCoefficientsSimple(&cps, &tc);
277
                                printf("%d %d %d %d %d %d %d\n",
278
                                        tc.a, tc.b, tc.c, tc.d, tc.e, tc.f, tc.s);
279
                                CalcTransformationCoefficientsBetter(&cps, &tc);
280
                                printf("%d %d %d %d %d %d %d\n",
281
                                        tc.a, tc.b, tc.c, tc.d, tc.e, tc.f, tc.s);
282
                                CalcTransformationCoefficientsEvenBetter(&cps, &tc);
283
                                printf("%d %d %d %d %d %d %d\n",
284
                                        tc.a, tc.b, tc.c, tc.d, tc.e, tc.f, tc.s);
285
#endif
286
                                CalcTransformationCoefficientsBest(&cps, &tc);
287
                                printf("%d %d %d %d %d %d %d\n",
288
                                        tc.a, tc.b, tc.c, tc.d, tc.e, tc.f, tc.s);
289
                                InvalidateRect(hwnd, NULL, TRUE);
290
                                UpdateWindow(hwnd);
291
                                PostQuitMessage(0);
292
                                break;
293
                        }
294
                        InvalidateRect(hwnd, NULL, TRUE);
295
                        UpdateWindow(hwnd);
296
                        break;
297
 
298
                default:
299
                        return DefWindowProc(hwnd, msg, wp, lp);
300
        }
301
 
302
        return 0;
303
}
304
 
305
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
306
{
307
        WNDCLASS wc;
308
        RECT r;
309
        HWND hwnd;
310
        MSG msg;
311
        MWCOORD big;
312
 
313
        srandom(time(NULL));
314
 
315
        /* WndButtonRegister(NULL); */
316
 
317
        wc.style = CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW;
318
        wc.lpfnWndProc = (WNDPROC)WndProc;
319
        wc.cbClsExtra = 0;
320
        wc.cbWndExtra = 0;
321
        wc.hInstance = 0;
322
        wc.hIcon = 0;
323
        wc.hCursor = 0;
324
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
325
        wc.lpszMenuName = NULL;
326
        wc.lpszClassName = "tpcal";
327
        RegisterClass(&wc);
328
 
329
        GetWindowRect(GetDesktopWindow(), &r);
330
        xext = r.right;
331
        yext = r.bottom;
332
 
333
        hwnd = CreateWindowEx(0L, "tpcal", "Touch Panel Calibration",
334
                WS_VISIBLE, 0, 0, xext, yext,
335
                NULL, (HMENU)1, NULL, NULL);
336
 
337
        // Don't restrict mouse much in order to handle uncalibrated points.
338
        big = 1000000;
339
        GdRestrictMouse(-big, -big, big, big);
340
 
341
        // We want all mouse events - even ones outside our window.
342
        SetCapture(hwnd);
343
 
344
        while(GetMessage(&msg, NULL, 0, 0)) {
345
                TranslateMessage(&msg);
346
                DispatchMessage(&msg);
347
        }
348
 
349
        return 0;
350
}
351
 
352
int enable_pointing_coordinate_transform;
353
 
354
int MwUserInit(int ac, char** av)
355
{
356
        enable_pointing_coordinate_transform = 0;
357
 
358
        if ( ac > 1 )
359
                total_targets = atol(av[1]);
360
 
361
        return 0;
362
}
363
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.