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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [mw/] [src/] [drivers/] [dynacal.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/* Calibration program for the Dynapro S3 controller */
2
/* Written by Jordan Crouse, September 5, 2001       */
3
 
4
/* Copyright, 2001, Century Embedded Technologies    */
5
 
6
 
7
#include <stdio.h>
8
#include <unistd.h>
9
#include <fcntl.h>
10
#include <errno.h>
11
 
12
#include <sys/termios.h>
13
#include <nano-X.h>
14
 
15
struct {
16
  int scrX;
17
  int scrY;
18
  int rawX;
19
  int rawY;
20
} dataPoints[4];
21
 
22
/* The current and saved termios */
23
static struct termios ios_saved;
24
static struct termios ios_current;
25
 
26
#define BGCOLOR GR_RGB(0,0,0)
27
#define FGCOLOR GR_RGB(255,255,255)
28
 
29
char *instructions[4] = {
30
  "Sony CIS Calibration",
31
  " ",
32
  "Please press on each target",
33
  "as they appear"
34
};
35
 
36
int calInitSerial(char *dev) {
37
 
38
  /* Open up the serial port */
39
  int fd = open(dev, O_NONBLOCK);
40
 
41
  if (fd <= 0) {
42
    perror("open serial");
43
    return(-1);
44
  }
45
 
46
  tcgetattr(fd, &ios_saved);
47
  ios_current = ios_saved;
48
 
49
  cfmakeraw(&ios_current);
50
 
51
  /* Set the baud rate */
52
 
53
  cfsetispeed(&ios_current, B2400);
54
  cfsetospeed(&ios_current, B2400);
55
 
56
  /* Set the data bits and remove the parity */
57
  ios_current.c_cflag &= ~(CSIZE | PARENB);
58
  ios_current.c_cflag |= CS8;
59
 
60
  ios_current.c_cc[VMIN] = 3;
61
  ios_current.c_cc[VTIME] = 1;
62
 
63
  tcsetattr(fd, TCSANOW, &ios_current);
64
  tcflush(fd, TCIOFLUSH);
65
 
66
  return(fd);
67
}
68
 
69
void calCloseSerial(int fd) {
70
  tcsetattr(fd, TCSANOW, &ios_saved);
71
  tcflush(fd, TCIOFLUSH);
72
 
73
  close(fd);
74
}
75
 
76
int calReadSerial(int fd) {
77
 
78
  unsigned char f;
79
  int val = read(fd, &f, sizeof(f));
80
 
81
  if (val <= 0) return(val);
82
 
83
  return((int) f);
84
}
85
 
86
/* Fd is the port to watch, data is the data that we are getting */
87
 
88
int calGetInput(int fd, int *data) {
89
 
90
  int count = 0;
91
  int state = 0;
92
 
93
  /* Read the data coming in off the line */
94
 
95
  while(1) {
96
 
97
    int c = calReadSerial(fd);
98
 
99
    if (c < 0 && errno != EAGAIN) return(-1);
100
 
101
    if (count++ > 500) return(0);
102
    if (c <= 0) continue;
103
 
104
    switch(state) {
105
 
106
    case 0:
107
      if (c & 0x80) {
108
        data[0] = (unsigned char) c;
109
        state = 1;
110
      }
111
      else
112
        fprintf(stderr, "Non start byte recieved (%2.2x)\n", c);
113
 
114
      break;
115
 
116
    case 1:
117
      if (!(c & 0x80)) {
118
        data[1] = (unsigned char) c;
119
        state = 2;
120
      }
121
      else {
122
        fprintf(stderr, "Got a start byte in the middle of the packet\n");
123
        data[0] = (unsigned char) c;
124
 
125
        state = 0;
126
      }
127
 
128
      break;
129
 
130
    case 2:
131
 
132
      if (!(c & 0x80)) {
133
        data[2] = (unsigned char) c;
134
        return(1);
135
      }
136
      else {
137
        fprintf(stderr, "Got a start byte in the middle of the packet\n");
138
        data[0] = (unsigned char) c;
139
 
140
        state = 0;
141
      }
142
 
143
      break;
144
    }
145
  }
146
 
147
  return(1);
148
}
149
 
150
 
151
void drawText(GR_WINDOW_ID id, char **text, int count) {
152
 
153
  int tw, th, tb;
154
  int xpos, ypos;
155
  int i;
156
 
157
  GR_GC_ID gc = GrNewGC();
158
  GR_FONT_ID font = GrCreateFont(GR_FONT_GUI_VAR, 12, 0);
159
  GR_WINDOW_INFO info;
160
 
161
  GrGetWindowInfo(id, &info);
162
 
163
  GrSetGCFont(gc, font);
164
  GrSetGCForeground(gc, FGCOLOR);
165
  GrSetGCBackground(gc, BGCOLOR);
166
 
167
  /* Get the first line of text from the array, and check the size */
168
  GrGetGCTextSize(gc, text[0], -1, GR_TFTOP, &tw, &th, &tb);
169
 
170
  ypos = (info.height - ((count * th)+ 3)) / 2;
171
 
172
  /* Draw each line of the instructions */
173
 
174
  for(i = 0; i < count; i++) {
175
    GrGetGCTextSize(gc, text[i], -1, GR_TFTOP, &tw, &th, &tb);
176
    xpos = (info.width - tw) / 2;
177
    GrText(id, gc, xpos, ypos, text[i], -1, GR_TFTOP);
178
 
179
    ypos += th + 3;
180
  }
181
 
182
  GrDestroyGC(gc);
183
  GrDestroyFont(font);
184
}
185
 
186
int doPoints(GR_WINDOW_ID id, int fd) {
187
 
188
  int data[4];
189
  int err = 0, i;
190
  fd_set fdset;
191
 
192
  GR_GC_ID gc = GrNewGC();
193
 
194
  for(i = 0; i < 4; i++) {
195
 
196
    int totalx = 0, totaly = 0;
197
    int p;
198
 
199
    /* Clear the previous point */
200
 
201
    if (i - 1 >= 0) {
202
      GrSetGCForeground(gc, BGCOLOR);
203
      GrFillRect(id, gc, dataPoints[i - 1].scrX - 10,
204
                 dataPoints[i - 1].scrY - 10, 20, 20);
205
    }
206
 
207
    /* Now draw the new point */
208
    GrSetGCForeground(gc, GR_RGB(255,0,0));
209
 
210
    GrFillRect(id, gc, dataPoints[i].scrX - 10, dataPoints[i].scrY - 1,
211
               20, 2);
212
 
213
    GrFillRect(id, gc, dataPoints[i].scrX - 1, dataPoints[i].scrY - 10,
214
               2, 20);
215
 
216
    GrFlush();
217
 
218
    /* Wait until we get a button click */
219
 
220
    FD_SET(fd, &fdset);
221
 
222
    if (select(fd + 1, &fdset, 0, 0, 0) != -1) {
223
      int val;
224
      int index = 0;
225
 
226
      while(1) {
227
        val = calGetInput(fd, data);
228
        if (val < 0) break;
229
        if (val == 0) continue;
230
 
231
        totaly += (data[2] | ((data[0] & 0x07) << 7));
232
        totalx += (data[1] | ((data[0] & 0x38) << 4));
233
 
234
        index++;
235
        if (!(data[0] & 0x40)) break;
236
      }
237
 
238
      if (index > 0) {
239
        dataPoints[i].rawX = totalx / index;
240
        dataPoints[i].rawY = totaly / index;
241
      }
242
    }
243
  }
244
 
245
  GrDestroyGC(gc);
246
  return(err);
247
}
248
 
249
 
250
int main(int argc, char **argv) {
251
 
252
  int serialFd;
253
  int outFd;
254
 
255
  GR_SCREEN_INFO info;
256
  GR_WINDOW_ID calWindow;
257
  GR_EVENT event;
258
 
259
  /* Open up the graphics */
260
 
261
  if (GrOpen() == -1) {
262
    fprintf(stderr, "Error!  Unable to open the graphics engine\n");
263
    return(-1);
264
  }
265
 
266
  /* Now open the serial port */
267
 
268
  serialFd = calInitSerial("/dev/ttyS1");
269
  if (serialFd == -1) {
270
    fprintf(stderr, "Error!  Unable to open the touchscreen device\n");
271
    return(-1);
272
  }
273
 
274
  GrGetScreenInfo(&info);
275
 
276
  /* Decide which points we are going to touch */
277
 
278
  dataPoints[0].scrX = 10;
279
  dataPoints[0].scrY = 10;
280
 
281
  dataPoints[1].scrX = 10;
282
  dataPoints[1].scrY = info.rows - 10;
283
 
284
  dataPoints[2].scrX = info.cols - 10;
285
  dataPoints[2].scrY = info.rows - 10;
286
 
287
  dataPoints[3].scrX = info.cols - 10;
288
  dataPoints[3].scrY = 10;
289
 
290
  /* Now, create a window that spans the entire size of the screen */
291
  calWindow = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, info.cols, info.rows, 0, BGCOLOR, FGCOLOR);
292
  GrSelectEvents(calWindow, GR_EVENT_MASK_EXPOSURE);
293
  GrMapWindow(calWindow);
294
  /* Wait for exposure */
295
  while(GrPeekEvent(&event) != GR_EVENT_TYPE_EXPOSURE);
296
 
297
  /* Ok, now that we have been exposed, draw the instructions */
298
  drawText(calWindow, instructions, 4);
299
 
300
  if (!doPoints(calWindow, serialFd)) {
301
 
302
    double scrXDelta, rawXDelta;
303
    double scrYDelta, rawYDelta;
304
 
305
    double deltaX, deltaY;
306
 
307
    scrXDelta = (double) dataPoints[2].scrX - dataPoints[0].scrX;
308
    rawXDelta = (double) dataPoints[2].rawX - dataPoints[0].rawX;
309
 
310
    scrYDelta = (double) dataPoints[1].scrY - dataPoints[0].scrY;
311
    rawYDelta = (double) dataPoints[1].rawY - dataPoints[0].rawY;
312
 
313
    /* We can now extrapolate and discover the extreme edges of the screen */
314
 
315
    /* First, the low values */
316
 
317
    deltaX = abs( (rawXDelta / scrXDelta) * ((double) dataPoints[0].scrX));
318
    deltaY = abs( (rawYDelta / scrYDelta) * ((double) dataPoints[0].scrY));
319
 
320
    /*
321
    deltaX = abs((double) dataPoints[0].scrX * rawXDelta) / scrXDelta);
322
    deltaY = abs((double) (dataPoints[0].scrY * rawYDelta) / scrYDelta);
323
    */
324
 
325
    /* Print out the raw values, accounting for possible inversion */
326
 
327
    if (dataPoints[0].rawX > dataPoints[2].rawX) {
328
      printf("%d ", (int) (dataPoints[0].rawX + deltaX));
329
      printf("%d ", (int) (dataPoints[2].rawX - deltaX));
330
    }
331
    else {
332
      printf("%d ", (int) (dataPoints[0].rawX - deltaX));
333
      printf("%d ", (int) (dataPoints[2].rawX + deltaX));
334
    }
335
 
336
    if (dataPoints[0].rawY >dataPoints[1].rawY) {
337
      printf("%d ", (int) (dataPoints[0].rawY + deltaY));
338
      printf("%d\n", (int) (dataPoints[1].rawY - deltaY));
339
    }
340
    else {
341
      printf("%d ", (int) (dataPoints[0].rawY - deltaY));
342
      printf("%d\n", (int) (dataPoints[1].rawY + deltaY));
343
    }
344
  }
345
  else {
346
    fprintf(stderr, "Error - Unable to read the touchscreen\n");
347
  }
348
 
349
  /* Close everything down */
350
  calCloseSerial(serialFd);
351
  GrClose();
352
 
353
  /* Byebye! */
354
  return(0);
355
}
356
 
357
 
358
 
359
 
360
 

powered by: WebSVN 2.1.0

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