eGUI alias D4D  Release 3.0
Reference Manual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
d4d_low.c
Go to the documentation of this file.
1 /**************************************************************************
2 *
3 * Copyright 2014 by Petr Gargulak. eGUI Community.
4 * Copyright 2009-2013 by Petr Gargulak. Freescale Semiconductor, Inc.
5 *
6 ***************************************************************************
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License Version 3
9 * or later (the "LGPL").
10 *
11 * As a special exception, the copyright holders of the eGUI project give you
12 * permission to link the eGUI sources with independent modules to produce an
13 * executable, regardless of the license terms of these independent modules,
14 * and to copy and distribute the resulting executable under terms of your
15 * choice, provided that you also meet, for each linked independent module,
16 * the terms and conditions of the license of that module.
17 * An independent module is a module which is not derived from or based
18 * on this library.
19 * If you modify the eGUI sources, you may extend this exception
20 * to your version of the eGUI sources, but you are not obligated
21 * to do so. If you do not wish to do so, delete this
22 * exception statement from your version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 *
28 * You should have received a copy of the GNU General Public License
29 * and the GNU Lesser General Public License along with this program.
30 * If not, see <http://www.gnu.org/licenses/>.
31 *
32 ***************************************************************************/
46 /**************************************************************/
52 #include "d4d.h"
55 
56 
57 /**************************************************************/
63 /**************************************************************/
69 
70 #ifdef D4D_LLD_TCH
71  static D4D_TOUCHSCREEN_CALIB d4d_tchCalib = {0, 0, 0, 0, 0};
72 #endif
73 
74 #if D4D_ROUND_CORNER_ENABLE == D4D_TRUE
75  static void D4D_LCD_QPlot4points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_LINETYPE line_type, D4D_COLOR color, D4D_QUADRANT quadrant);
76  static void D4D_LCD_QPlot4pointsfill(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_COLOR color,D4D_QUADRANT quadrant);
77  static void D4D_LCD_QPlot8points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color, D4D_QUADRANT quadrant);
78 #endif
79 
80 /**************************************************************/
87 {
89  return D4D_FALSE;
91  return D4D_FALSE;
93  return D4D_FALSE;
95  return D4D_FALSE;
97  return D4D_FALSE;
99  return D4D_FALSE;
101  return D4D_FALSE;
103  return D4D_FALSE;
104 
105 
106  if(!D4D_LLD_LCD.D4DLCD_Init())
107  return D4D_FALSE;
108 
109  #ifdef D4D_LLD_TCH
110  if(D4D_LLD_TCH.D4DTCH_Init == NULL)
111  return D4D_FALSE;
112  if(D4D_LLD_TCH.D4DTCH_GetPositionRaw == NULL)
113  return D4D_FALSE;
114  if(D4D_LLD_TCH.D4DTCH_GetRawLimits == NULL)
115  return D4D_FALSE;
116  if(D4D_LLD_TCH.D4DTCH_DeInit == NULL)
117  return D4D_FALSE;
118 
119  if(!D4D_LLD_TCH.D4DTCH_Init())
120  return D4D_FALSE;
121  #endif
122 
123  #ifdef D4D_LLD_MOUSE
124  if(D4D_LLD_MOUSE.D4DMOUSE_Init == NULL)
125  return D4D_FALSE;
126  if(D4D_LLD_MOUSE.D4DMOUSE_SetCoor == NULL)
127  return D4D_FALSE;
128  if(D4D_LLD_MOUSE.D4DMOUSE_SetPointer == NULL)
129  return D4D_FALSE;
130  if(D4D_LLD_MOUSE.D4DMOUSE_DeInit == NULL)
131  return D4D_FALSE;
132 
133  if(!D4D_LLD_MOUSE.D4DMOUSE_Init())
134  return D4D_FALSE;
135  #endif
136 
138 
139  #ifdef D4D_LLD_TCH
140  // Clear flag to indicate touch screen not calibrated
141  d4d_tchCalib.ScreenCalibrated = 0;
142  #endif
143  return D4D_TRUE;
144 }
145 
146 
148 {
149  return d4d_orientation;
150 }
151 
153 {
154  d4d_orientation = new_orientation;
155 
157 }
158 
160 {
163  else
165 }
166 
168 {
171  else
173 }
174 
175 
177 {
178  D4D_COOR tmp;
179 
180  tmp = *c1;
181  *c1 = *c2;
182  *c2 = tmp;
183 }
184 
185 #ifdef __CWCC__
186  #pragma mark -
187  #pragma mark Draving Functions
188  #pragma mark -
189 #endif
190 
191 
193 {
194  if (line_type == D4D_LINE_THICK)
195  {
196  (void)D4D_LLD_LCD.D4DLCD_SetWindow(x1, y1, (unsigned short)(x1+1), (unsigned short)(y1+1));
201  }
202  else
203  {
204  (void)D4D_LLD_LCD.D4DLCD_SetWindow(x1, y1, x1, y1);
206  }
207 #if D4D_LLD_FLUSH_ELEMENT != D4D_FALSE
209 #endif
210 }
211 
213 {
214  LWord i, i_max;
215 
216  //Check the coordination if are in right order
217  if(x1 > x2)
218  D4D_LCD_SwapCoor(&x1, &x2);
219 
220  if(y1 > y2)
221  D4D_LCD_SwapCoor(&y1, &y2);
222 
223  // Set the logic window on LCD
224  (void)D4D_LLD_LCD.D4DLCD_SetWindow(x1, y1, x2, y2);
225 
226  // Count the pixel count
227  i_max = (LWord)((LWord)(x2-x1+1)*(LWord)(y2-y1+1));
228 
229  // send all pixels into LCD
230  for (i=0; i < i_max ; ++i)
232 #if D4D_LLD_FLUSH_ELEMENT != D4D_FALSE
234 #endif
235 }
236 
237 void D4D_LCD_Rect(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_LINETYPE line_type, D4D_COLOR color) //draw rectangle
238 {
239  D4D_LCD_Line(x1, y1, x2, y1, line_type, color);
240  D4D_LCD_Line(x2, y1, x2, y2, line_type, color);
241  D4D_LCD_Line(x1, y2, x2, y2, line_type, color);
242  D4D_LCD_Line(x1, y1, x1, y2, line_type, color);
243 }
244 
245 void D4D_LCD_Line (D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_LINETYPE line_type, D4D_COLOR color) //draw line - bresenham algorithm
246 {
247  sWord delta_x, delta_y;
248  sByte ix, iy;
249 
250  if((x1 == x2) || (y1 == y2)) // if the line is vertical or horizontal then draw box function is faster than general bresenham algorithm
251  {
252  if(line_type == D4D_LINE_THICK)
253  {
254  if(x1 > x2)
255  {
256  D4D_LCD_SwapCoor(&x1, &x2);
257  }
258 
259  if(y1 > y2)
260  {
261  D4D_LCD_SwapCoor(&y1, &y2);
262  }
263 
264  x2++;
265  y2++;
266  }
267 
268  D4D_LCD_Box(x1 ,y1 ,x2 , y2, color);
269 
270  return;
271  }
272 
273  delta_x = (sWord)(D4D_Abs16((sWord)(x2 - x1)) << 1);
274  delta_y = (sWord)(D4D_Abs16((sWord)(y2 - y1)) << 1);
275 
276  // if x1 == x2 or y1 == y2, then it does not matter what we set here
277  ix = (sByte)((x2 > x1)?1:-1);
278  iy = (sByte)((y2 > y1)?1:-1);
279 
280  D4D_LCD_PutPixel((D4D_COOR) x1, (D4D_COOR) y1, line_type, color);
281 
282  if (delta_x >= delta_y)
283  {
284  // error may go below zero
285  sLWord error = delta_y - (delta_x >> 1);
286 
287  while (x1 != x2)
288  {
289  if (error >= 0)
290  {
291  if (error || (ix > 0))
292  {
293  y1 += iy;
294  error -= delta_x;
295  }
296  // else do nothing
297  }
298  // else do nothing
299 
300  x1 += ix;
301  error += delta_y;
302 
303  D4D_LCD_PutPixel((D4D_COOR) x1, (D4D_COOR) y1, line_type, color);
304  }
305  }
306  else
307  {
308  // error may go below zero
309  sLWord error = delta_x - (delta_y >> 1);
310 
311  while (y1 != y2)
312  {
313  if (error >= 0)
314  {
315  if (error || (iy > 0))
316  {
317  x1 += ix;
318  error -= delta_y;
319  }
320  // else do nothing
321  }
322  // else do nothing
323 
324  y1 += iy;
325  error += delta_x;
326 
327  D4D_LCD_PutPixel((D4D_COOR) x1, (D4D_COOR) y1, line_type, color);
328  }
329  }
330  }
331 
332 // The '(x != 0 && y != 0)' test in the last line of this function
333 // may be omitted for a performance benefit if the radius of the
334 // circle is known to be non-zero.
335 static void D4D_LCD_Plot4points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_LINETYPE line_type, D4D_COLOR color)
336 {
337  D4D_LCD_PutPixel((D4D_COOR)(cx + x), (D4D_COOR)(cy + y), line_type, color);
338  if (x != 0) D4D_LCD_PutPixel((D4D_COOR)(cx - x), (D4D_COOR)(cy + y), line_type, color);
339  if (y != 0) D4D_LCD_PutPixel((D4D_COOR)(cx + x), (D4D_COOR)(cy - y), line_type, color);
340  if (x != 0 && y != 0) D4D_LCD_PutPixel((D4D_COOR)(cx - x), (D4D_COOR)(cy - y), line_type, color);
341 }
342 
344 {
345  D4D_LCD_Box((D4D_COOR)(cx-x), (D4D_COOR)(cy+y), (D4D_COOR)(cx+x), (D4D_COOR)(cy+y), color);
346  D4D_LCD_Box((D4D_COOR)(cx-x), (D4D_COOR)(cy-y), (D4D_COOR)(cx+x), (D4D_COOR)(cy-y), color);
347 }
348 
349 
350 static void D4D_LCD_Plot8points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color)
351 {
352  if(fill) {
353 
354  D4D_LCD_Plot4pointsfill(cx, cy, x, y, color);
355  if (x != y) D4D_LCD_Plot4pointsfill(cx, cy, y, x, color);
356  }
357  else {
358 
359  D4D_LCD_Plot4points(cx, cy, x, y, line_type, color);
360  if (x != y) D4D_LCD_Plot4points(cx, cy, y, x, line_type, color);
361  }
362 }
363 
364 
365 
366 // 'cx' and 'cy' denote the offset of the circle centre from the origin.
368  D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color) //draw circle
369 {
370  sWord e = (sWord)(-1 * (sWord)radius);
371  D4D_COOR x = radius;
372  D4D_COOR y = 0;
373 
374  while (x >= y)
375  {
376  D4D_LCD_Plot8points(cx, cy, x, y, fill, line_type, color);
377 
378  e += 2*y + 1;
379  y++;
380 
381  if (e >= 0)
382  {
383  --x;
384  e -= 2*x;
385  }
386  }
387 }
388 
389 #ifdef __CWCC__
390  #pragma mark -
391  #pragma mark Draving Round Corner Functions
392  #pragma mark -
393 #endif
394 
395 #if D4D_ROUND_CORNER_ENABLE == D4D_TRUE
396 
397 void D4D_LCD_RBox(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_COLOR color, D4D_COOR radius)
398 {
399  //Check the coordination if are in right order
400  if(x1 > x2)
401  D4D_LCD_SwapCoor(&x1, &x2);
402 
403  if(y1 > y2)
404  D4D_LCD_SwapCoor(&y1, &y2);
405 
406 
407  if(radius && (x1 != x2) && (y1 != y2))
408  {
409 
410  D4D_LCD_Box(x1, (unsigned short)(y1+radius), x2, (unsigned short)(y2-radius), color);
411  D4D_LCD_Box((unsigned short)(x1+radius),y1,(unsigned short)(x2-radius),(unsigned short)(y1+radius), color);
412  D4D_LCD_Box((unsigned short)(x1+radius),(unsigned short)(y2-radius),(unsigned short)(x2-radius),y2, color);
413 
414 
415  D4D_LCD_QCircle((unsigned short)(x1+radius), (unsigned short)(y1+radius), radius, D4D_TRUE, D4D_LINE_THIN, color, D4D_QUAD_4);
416  D4D_LCD_QCircle((unsigned short)(x2-radius), (unsigned short)(y1+radius), radius, D4D_TRUE, D4D_LINE_THIN, color, D4D_QUAD_1);
417  D4D_LCD_QCircle((unsigned short)(x2-radius), (unsigned short)(y2-radius), radius, D4D_TRUE, D4D_LINE_THIN, color, D4D_QUAD_2);
418  D4D_LCD_QCircle((unsigned short)(x1+radius), (unsigned short)(y2-radius), radius, D4D_TRUE, D4D_LINE_THIN, color, D4D_QUAD_3);
419  }else
420  {
421  if(x1 == x2)
422  {
423  if((y2 - y1) - (2 * radius) > 1)
424  {
425  y2 -= radius;
426  y1 += radius;
427  }else
428  {
429  int k = (y2 - y1) / 2;
430  y2 -= k;
431  y1 += k;
432  }
433  }
434 
435  if(y1 == y2)
436  {
437  if((x2 - x1) - (2 * radius) > 1)
438  {
439  x2 -= radius;
440  x1 += radius;
441  }else
442  {
443  int k = (x2 - x1) / 2;
444  x2 -= k;
445  x1 += k;
446  }
447  }
448 
449  D4D_LCD_Box(x1, y1, x2, y2, color);
450  }
451 }
452 
453 void D4D_LCD_RRect(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_LINETYPE line_type, D4D_COLOR color, D4D_COOR radius) //draw rectangle
454 {
455  //Check the coordination if are in right order
456  if(x1 > x2)
457  D4D_LCD_SwapCoor(&x1, &x2);
458 
459  if(y1 > y2)
460  D4D_LCD_SwapCoor(&y1, &y2);
461 
462  if(radius && (x1 != x2) && (y1 != y2))
463  {
464  D4D_LCD_Line((unsigned short)(x1+radius), y1, (unsigned short)(x2-radius), y1, line_type, color);
465  D4D_LCD_Line(x2, (unsigned short)(y1+radius), x2, (unsigned short)(y2-radius), line_type, color);
466  D4D_LCD_Line((unsigned short)(x1+radius), y2, (unsigned short)(x2-radius), y2, line_type, color);
467  D4D_LCD_Line(x1, (unsigned short)(y1+radius), x1, (unsigned short)(y2-radius), line_type, color);
468 
469  D4D_LCD_QCircle((unsigned short)(x1+radius), (unsigned short)(y1+radius), radius, D4D_FALSE, line_type, color, D4D_QUAD_4);
470  D4D_LCD_QCircle((unsigned short)(x2-radius), (unsigned short)(y1+radius), radius, D4D_FALSE, line_type, color, D4D_QUAD_1);
471  D4D_LCD_QCircle((unsigned short)(x2-radius), (unsigned short)(y2-radius), radius, D4D_FALSE, line_type, color, D4D_QUAD_2);
472  D4D_LCD_QCircle((unsigned short)(x1+radius), (unsigned short)(y2-radius), radius, D4D_FALSE, line_type, color, D4D_QUAD_3);
473  }else
474  {
475  if(x1 == x2)
476  {
477  if((y2 - y1) - (2 * radius) > 1)
478  {
479  y2 -= radius;
480  y1 += radius;
481  }else
482  {
483  int k = (y2 - y1) / 2;
484  y2 -= k;
485  y1 += k;
486  }
487  }
488 
489  if(y1 == y2)
490  {
491  if((x2 - x1) - (2 * radius) > 1)
492  {
493  x2 -= radius;
494  x1 += radius;
495  }else
496  {
497  int k = (x2 - x1) / 2;
498  x2 -= k;
499  x1 += k;
500  }
501  }
502 
503  D4D_LCD_Rect(x1, y1, x2, y2, line_type, color);
504  }
505 }
506 
507 
508 static void D4D_LCD_QPlot4points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_LINETYPE line_type, D4D_COLOR color, D4D_QUADRANT quadrant)
509 {
510  switch(quadrant)
511  {
512  case D4D_QUAD_1:
513  if (y != 0) D4D_LCD_PutPixel((D4D_COOR)(cx + x), (D4D_COOR)(cy - y), line_type, color);
514  break;
515  case D4D_QUAD_2:
516  D4D_LCD_PutPixel((D4D_COOR)(cx + x), (D4D_COOR)(cy + y), line_type, color);
517  break;
518  case D4D_QUAD_3:
519  if (x != 0) D4D_LCD_PutPixel((D4D_COOR)(cx - x), (D4D_COOR)(cy + y), line_type, color);
520  break;
521  case D4D_QUAD_4:
522  if (x != 0 && y != 0) D4D_LCD_PutPixel((D4D_COOR)(cx - x), (D4D_COOR)(cy - y), line_type, color);
523  break;
524  }
525 }
526 
528 {
529  switch(quadrant)
530  {
531  case D4D_QUAD_1:
532  D4D_LCD_Box((D4D_COOR)(cx), (D4D_COOR)(cy-y), (D4D_COOR)(cx+x), (D4D_COOR)(cy-y), color);
533  break;
534  case D4D_QUAD_2:
535  D4D_LCD_Box((D4D_COOR)(cx), (D4D_COOR)(cy+y), (D4D_COOR)(cx+x), (D4D_COOR)(cy+y), color);
536  break;
537  case D4D_QUAD_3:
538  D4D_LCD_Box((D4D_COOR)(cx-x), (D4D_COOR)(cy+y), (D4D_COOR)(cx), (D4D_COOR)(cy+y), color);
539  break;
540  case D4D_QUAD_4:
541  D4D_LCD_Box((D4D_COOR)(cx-x), (D4D_COOR)(cy-y), (D4D_COOR)(cx), (D4D_COOR)(cy-y), color);
542  break;
543  }
544 }
545 
546 
547 static void D4D_LCD_QPlot8points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color, D4D_QUADRANT quadrant)
548 {
549  if(fill) {
550 
551  D4D_LCD_QPlot4pointsfill(cx, cy, x, y, color, quadrant);
552  if (x != y) D4D_LCD_QPlot4pointsfill(cx, cy, y, x, color, quadrant);
553  }
554  else {
555 
556  D4D_LCD_QPlot4points(cx, cy, x, y, line_type, color,quadrant);
557  if (x != y) D4D_LCD_QPlot4points(cx, cy, y, x, line_type, color, quadrant);
558  }
559 }
560 
562  D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color, D4D_QUADRANT quadrant) //draw circle
563 {
564  sWord error = (sWord)(-1 * (sWord)radius);
565  D4D_COOR x = radius;
566  D4D_COOR y = 0;
567 
568  if(!radius)
569  {
570  D4D_LCD_PutPixel(cx, cy, line_type, color);
571  return;
572  }
573 
574  // The following while loop may altered to 'while (x > y)' for a
575  // performance benefit, as long as a call to 'plot4points' follows
576  // the body of the loop. This allows for the elimination of the
577  // '(x != y') test in 'plot8points', providing a further benefit.
578  //
579  // For the sake of clarity, this is not shown here.
580  while (x >= y)
581  {
582  D4D_LCD_QPlot8points(cx, cy, x, y, fill, line_type, color, quadrant);
583 
584  error += y;
585  ++y;
586  error += y;
587 
588  // The following test may be implemented in assembly language in
589  // most machines by testing the carry flag after adding 'y' to
590  // the value of 'error' in the previous step, since 'error'
591  // nominally has a negative value.
592  if (error >= 0)
593  {
594  --x;
595  error -= x;
596  error -= x;
597  }
598  }
599 }
600 
601 
602 
603 #endif
604 
605 #ifdef __CWCC__
606  #pragma mark -
607  #pragma mark Touch Screen Functions
608  #pragma mark -
609 #endif
610 
611 #ifdef D4D_LLD_TCH
612 
614 {
615  return d4d_tchCalib.ScreenCalibrated;
616 }
617 
618 
620 {
621  return d4d_tchCalib;
622 }
623 
625 {
626  d4d_tchCalib = newCalib;
627  d4d_tchCalib.ScreenCalibrated = 1;
628 }
629 
630 
631 void D4D_TCH_GetCalibratedPosition(D4D_COOR *TouchPositionX, D4D_COOR *TouchPositionY)
632 {
633  D4D_COOR Temp;
634 
635  if (d4d_tchCalib.ScreenCalibrated)
636  {
637  // Screen has been touched and calibrated. Is X offset > touch position?
638  if (d4d_tchCalib.TouchScreenXoffset > *TouchPositionX)
639  {
640  // Offset > touch postion. Force Touch position = offset
641  *TouchPositionX = 0;
642  }else
643  {
644  // Adjust coordinates using screen calibration data
645  *TouchPositionX = (D4D_COOR)(((*TouchPositionX - d4d_tchCalib.TouchScreenXoffset) * 16) /
646  d4d_tchCalib.TouchScreenXBitsPerPixelx16);
647  // Transform touch coordinates to display position
648  if (*TouchPositionX > D4D_SCREEN_SIZE_LONGER_SIDE)
649  {
650  *TouchPositionX = D4D_SCREEN_SIZE_LONGER_SIDE;
651  }
652  }
653 
654 
655  // Is Y offset > touch position?
656  if (d4d_tchCalib.TouchScreenYoffset > *TouchPositionY)
657  {
658  // Offset > touch postion. Force Touch position = offset
659  *TouchPositionY = 0;
660  }else
661  {
662  // Adjust coordinates using screen calibration data
663  *TouchPositionY = (D4D_COOR)(((*TouchPositionY - d4d_tchCalib.TouchScreenYoffset) * 16) /
664  d4d_tchCalib.TouchScreenYBitsPerPixelx16);
665  // Transform touch coordinates to display position
666  if (*TouchPositionY > D4D_SCREEN_SIZE_SHORTER_SIDE)
667  {
668  *TouchPositionY = D4D_SCREEN_SIZE_SHORTER_SIDE;
669  }
670  }
671 
672  // convert coordinations to right orientation of LCD
673  switch(d4d_orientation)
674  {
675  case D4D_ORIENT_PORTRAIT:
676  Temp = *TouchPositionX;
677  *TouchPositionX = *TouchPositionY;
678  *TouchPositionY = (D4D_COOR)(D4D_SCREEN_SIZE_LONGER_SIDE - Temp);
679  break;
680 
682  Temp = *TouchPositionX;
683  *TouchPositionX = (D4D_COOR)(D4D_SCREEN_SIZE_SHORTER_SIDE - *TouchPositionY);
684  *TouchPositionY = Temp;
685  break;
686 
688  *TouchPositionX = (D4D_COOR)(D4D_SCREEN_SIZE_LONGER_SIDE - *TouchPositionX);
689  *TouchPositionY = (D4D_COOR)(D4D_SCREEN_SIZE_SHORTER_SIDE - *TouchPositionY);
690  break;
691  }
692  }
693 }
694 
695 //
696 //-----------------------------------------------------------------------------
697 // FUNCTION: LCD_GetTouchScreenPosition
698 // SCOPE: Global
699 // DESCRIPTION: Reads touch screen and returns X, Y coorinates if screen
700 // touched
701 // PARAMETERS: LWord *TouchPositionX Pointer to X coordinate
702 // LWord *TouchPositionY Pointer to Y ccordinate
703 // RETURNS: 0 no screen touch
704 // 1 screen touch,
705 //-----------------------------------------------------------------------------
706 //
707 Byte D4D_TCH_GetRawPosition(D4D_COOR *TouchPositionX, D4D_COOR *TouchPositionY)
708 {
709  // Read raw touch position into *TouchPositionX and *TouchPositionY
710  return D4D_LLD_TCH.D4DTCH_GetPositionRaw (TouchPositionX, TouchPositionY);
711 }
712 
713 static void D4D_GetCalibrationPoint(Byte ix, Word* X, Word* Y, D4D_COLOR fore, D4D_COLOR bckg )
714 {
715  D4D_COOR tmp_x, tmp_y;
716 
717  tmp_x = D4D_DisplayWidth();
718  tmp_y = D4D_DisplayHeight();
719 
720 
721  switch(ix)
722  {
723  case 0:
726  break;
727  case 1:
728  tmp_x -= D4DTCH_CALIB_CROSS_OFFSET;
729  tmp_y -= D4DTCH_CALIB_CROSS_OFFSET;
730  break;
731  }
732 
733  D4D_TCH_DrawCalibrationPoint(tmp_x, tmp_y, fore);
735 
736  while (D4D_LLD_TCH.D4DTCH_GetPositionRaw ((unsigned short*)X, (unsigned short*)Y) == 0)
737  {
739  }
740 
741  D4D_TCH_DrawCalibrationPoint(tmp_x, tmp_y, bckg);
742 }
743 
744 //
745 //-----------------------------------------------------------------------------
746 // FUNCTION: LCD_CalibrateTouchScreen
747 // SCOPE: Global
748 // DESCRIPTION: Reads touchscreen positions in relation to screen positions
749 // and calculates calibration constants. These constants are used
750 // to calulate 'real' screen positions
751 // PARAMETERS: none
752 // RETURNS: VOID
753 //-----------------------------------------------------------------------------
754 //
755 void D4D_TCH_Calibrate(D4D_COLOR fore, D4D_COLOR bckg)
756 {
757  // Declare and initialise local variables
758  D4D_STRING tmp_txtbuff;
759  D4D_STR_PROPERTIES tmp_str_prty;
760  D4D_TOUCHSCREEN_LIMITS* p_tchRawLimits = D4D_LLD_TCH.D4DTCH_GetRawLimits();
761  Word X[2];
762  Word Y[2];
763  D4D_TCHAR str_touch[10] = D4D_DEFSTR("X1:00000");
764 
765  Byte tmp_i;
766 
767  tmp_txtbuff.fontId = D4D_FONT_SYSTEM_DEFAULT;
768  tmp_str_prty.font_properties = 0;
769  tmp_txtbuff.str_properties = &tmp_str_prty;
770 
772 
773  // Initialise display orientation to LANDSCAPE
774  D4D_ClearScreen(bckg);
776 
778  {
779  tmp_txtbuff.buffSize = 0;
780  tmp_txtbuff.printLen = 0;
781  tmp_txtbuff.printOff = 0;
782  tmp_txtbuff.pText = D4D_DEFSTR("Touch the cross by stylus.");
783  D4D_DrawTextXY(10, (D4D_COOR)(D4D_DisplayHeight() - 3 * (D4D_GetFontHeight(D4D_FONT_SYSTEM_DEFAULT)+1) - 10), &tmp_txtbuff, fore, bckg);
784  }
785 
786  // Set touchscreen calibrated flag to FALSE
787  d4d_tchCalib.ScreenCalibrated = 0;
788 
789  while (d4d_tchCalib.ScreenCalibrated == 0)
790  {
791  // Capture input calibration points
792  for(tmp_i=0;tmp_i<2;tmp_i++)
793  {
794 
795  D4D_GetCalibrationPoint(tmp_i, &X[tmp_i], &Y[tmp_i], fore, bckg);
796 
798  {
799  D4D_COOR tmp_y_text = (D4D_COOR)(D4D_DisplayHeight() - (2 - tmp_i) * (D4D_GetFontHeight(D4D_FONT_SYSTEM_DEFAULT) + 1) - 5);
800 
801  tmp_txtbuff.pText = str_touch;
802  str_touch[1] = (D4D_TCHAR)('1' + tmp_i);
803  str_touch[0] = 'X';
804  str_touch[D4D_SprintDecU16((Word)X[tmp_i],&str_touch[3],' ') + 3] = 0;
805 
807 
808  D4D_DrawTextXY(10, tmp_y_text, &tmp_txtbuff, fore, bckg);
809  str_touch[0] = 'Y';
810  str_touch[D4D_SprintDecU16((Word)Y[tmp_i],&str_touch[3],' ') + 3] = 0;
811  D4D_DrawTextXY((D4D_COOR)(10 + D4D_GetFontWidth(D4D_FONT_SYSTEM_DEFAULT) * 9), tmp_y_text, &tmp_txtbuff, fore, bckg);
812  }
813 
816  }
817 
818  // convert captured values into Landscape orientation
819  switch(d4d_orientation)
820  {
821  Word tmp_coor;
822 
824  tmp_coor = X[0];
825  X[0] = X[1];
826  X[1] = tmp_coor;
827  tmp_coor = Y[0];
828  Y[0] = Y[1];
829  Y[1] = tmp_coor;
830  break;
831  case D4D_ORIENT_PORTRAIT:
832  tmp_coor = Y[0];
833  Y[0] = Y[1];
834  Y[1] = tmp_coor;
835  break;
837  tmp_coor = X[0];
838  X[0] = X[1];
839  X[1] = tmp_coor;
840  break;
842  default:
843  break;
844  }
845 
846  // Check X1 > X2 and Y1 > Y2
847  if ((X[0] > X[1]) && (Y[0] > Y[1]))
848  {
849  Word dX, dY;
850 
851  // compute difference in axis X
852  dX = (Word)(X[0] - X[1]);
853 
854  // compute difference in axis Y
855  dY = (Word)(Y[0] - Y[1]);
856 
857  // Calculate number of (bits / pixel) * 16 for X
859 
860  // Does number of ADC bits per pixel in X seem reasonable?
861  if (d4d_tchCalib.TouchScreenXBitsPerPixelx16 < ((16 * p_tchRawLimits->rawDataScale) / D4D_SCREEN_SIZE_LONGER_SIDE))
862  {
863  // Calculate number of (bits / pixel) * 16 for Y
865 
866  // Does number of ADC bits per pixel in Y seem reasonable?
867  if (d4d_tchCalib.TouchScreenYBitsPerPixelx16 < ((16 * p_tchRawLimits->rawDataScale) / D4D_SCREEN_SIZE_SHORTER_SIDE))
868  {
869  // Calculate LCD Y offset
870  d4d_tchCalib.TouchScreenYoffset = (Word)((Y[1] * 16 - D4DTCH_CALIB_CROSS_OFFSET * d4d_tchCalib.TouchScreenYBitsPerPixelx16) / 16);
871 
872  // Calculate LCD X offset
873  d4d_tchCalib.TouchScreenXoffset = (Word)((X[1] * 16 - D4DTCH_CALIB_CROSS_OFFSET * d4d_tchCalib.TouchScreenXBitsPerPixelx16) / 16);
874 
875  // Is screen offset in X and Y reasonable?
876  if (d4d_tchCalib.TouchScreenYoffset < p_tchRawLimits->touchOffMaxY && d4d_tchCalib.TouchScreenXoffset < p_tchRawLimits->touchOffMaxX)
877  {
878  // Set flag to indicate touch screen calibrated
879  d4d_tchCalib.ScreenCalibrated = 1;
880  }
881  }
882  }
883  }
884  }
885 
886 }
887 
888 //
889 //-----------------------------------------------------------------------------
890 // FUNCTION: LCD_DrawCalibrationPoint
891 // SCOPE: Global
892 // DESCRIPTION: Draws a crosshair on screen
893 // PARAMETERS: D4D_COOR X - x coord of crosshair
894 // D4D_COOR Y - y coord of crosshair
895 // D4D_COLOR Color - crosshair color
896 // RETURNS: VOID
897 //-----------------------------------------------------------------------------
898 //
900 {
901  D4D_LCD_Line((D4D_COOR)(X - 10), Y, (D4D_COOR)(X + 10), Y, D4D_LINE_THIN, Color);
902  D4D_LCD_Line(X, (D4D_COOR)(Y - 10), X, (D4D_COOR)(Y + 10), D4D_LINE_THIN, Color);
903 }
904 
905 #else
906 
907 #ifdef __CWCC__
908  #pragma mark -
909  #pragma mark Dummy Touch Screen Functions
910  #pragma mark -
911 #endif
912 
913 
915 {
916  D4D_UNUSED(fore);
917  D4D_UNUSED(bckg);
918 }
919 
921 {
922  D4D_UNUSED(X);
923  D4D_UNUSED(Y);
924  D4D_UNUSED(Color);
925 }
926 
927 #endif
#define D4D_LLD_TCH
Definition: d4d_types.h:79
void D4D_LCD_RBox(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_COLOR color, D4D_COOR radius)
Definition: d4d_low.c:397
D4D_LINETYPE
Type definition of eGUI line type.
Definition: d4d_types.h:291
D4D_INDEX buffSize
size of text buffer array
Definition: d4d_string.h:103
D4D_WCHAR D4D_TCHAR
Type definition of eGUI character (it depends on UNICODE setting if this is D4D_CHAR or D4D_WCHAR)...
Definition: d4d_types.h:284
unsigned short TouchScreenYBitsPerPixelx16
Input value gain per 16 pixels in axis Y.
Definition: d4d_types.h:356
D4D_FONT_TYPE * D4D_GetFont(D4D_FONT ix)
Definition: d4d_font.c:340
D4D_INDEX printLen
Length of string that should be used (printed).
Definition: d4d_string.h:106
signed long sLWord
Type definition of sLWord (signed 32-bit).
Definition: d4d_types.h:171
Type definition of eGUI touch screen calibration structure.
Definition: d4d_types.h:350
#define D4D_LLD_MOUSE
Definition: d4d_types.h:85
D4D_COLOR(* D4DLCD_Read_PixelColor)(void)
The LCD driver read pixel function. The pixels will be read on position (automaticaly incremented) se...
Definition: d4d_lldapi.h:153
void(* D4DLCD_FlushBuffer)(D4DLCD_FLUSH_MODE mode)
The LCD driver flush function.
Definition: d4d_lldapi.h:154
void D4D_FillRectXY(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_COLOR color)
Function draw filled rectangle on the screen.
First quadrant &lt;0-90)
Definition: d4d_types.h:325
void D4D_TCH_Calibrate(D4D_COLOR fore, D4D_COLOR bckg)
Definition: d4d_low.c:914
The notification that the end of screen is draw.
Definition: d4d_lldapi.h:140
unsigned short touchOffMaxX
Maximal valid raw value of axis X.
Definition: d4d_lldapi.h:110
#define D4D_FALSE
This is definition of boolean operation value in eGUI - FALSE.
Definition: d4d_types.h:104
The string type. This structure contains all properties about string in eGUI.
Definition: d4d_string.h:100
#define D4D_SCREEN_SIZE_LONGER_SIDE
This macro is used to define the size of longer side of LCD. If not defined, the longer side is set t...
Definition: d4d_base.h:101
D4D_COOR D4D_DisplayHeight(void)
Definition: d4d_low.c:167
void D4D_LCD_Line(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_LINETYPE line_type, D4D_COLOR color)
Definition: d4d_low.c:245
void D4D_LCD_Rect(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_LINETYPE line_type, D4D_COLOR color)
Definition: d4d_low.c:237
D4D_FONT fontId
index of used font
Definition: d4d_string.h:104
The flush after one basic element is draw.
Definition: d4d_lldapi.h:138
void D4D_LCD_Box(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_COLOR color)
Definition: d4d_low.c:212
Landscape up side down orientation.
Definition: d4d_types.h:346
unsigned char(* D4DLCD_DeInit)(void)
The LCD driver deinicialization function.
Definition: d4d_lldapi.h:156
Byte D4D_TCH_GetCalibrationStatus(void)
#define D4D_TRUE
This is definition of boolean operation value in eGUI - TRUE.
Definition: d4d_types.h:106
void D4D_TCH_GetCalibratedPosition(D4D_COOR *TouchPositionX, D4D_COOR *TouchPositionY)
Word D4D_Abs16(sWord num)
Calculation of absolute value - 16 bit.
Definition: d4d_math.c:325
void D4D_ClearScreen(D4D_COLOR color)
Function readraw complete screen by one color.
Third quadrant &lt;180-270)
Definition: d4d_types.h:327
D4D Driver main header file.
void D4D_LCD_SetOrientation(D4D_ORIENTATION new_orientation)
Definition: d4d_low.c:152
The string properties type. This structure contains as Font as Text properties.
Definition: d4d_string.h:93
void D4D_TCH_DrawCalibrationPoint(D4D_COOR X, D4D_COOR Y, D4D_COLOR Color)
Definition: d4d_low.c:920
void D4D_LCD_SwapCoor(D4D_COOR *c1, D4D_COOR *c2)
Definition: d4d_low.c:176
static void D4D_LCD_Plot4pointsfill(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_COLOR color)
Definition: d4d_low.c:343
D4D Driver private header file.
#define D4D_DEFSTR(str)
Macro that helps declare the strings in eGUI.
Definition: d4d_string.h:246
D4D_ORIENTATION
Type definition of eGUI display orientation.
Definition: d4d_types.h:341
Portrait up side down orientation.
Definition: d4d_types.h:344
D4DLCD_ORIENTATION
D4D low level screen orientation enumeration type.
Definition: d4d_lldapi.h:64
Byte D4D_COOR
Type definition of eGUI coordination variables.
Definition: d4d_types.h:219
unsigned short TouchScreenXBitsPerPixelx16
Input value gain per 16 pixels in axis X.
Definition: d4d_types.h:355
unsigned char Byte
Type definition of Byte (unsigned 8-bit).
Definition: d4d_types.h:151
D4D low level touch screen limitation structure.
Definition: d4d_lldapi.h:105
#define D4D_DrawTextXY(x, y, buffText, colorText, colorBkgd)
Function that draw text on the screen the coordination is sets directly XY.
D4D_ORIENTATION D4D_LCD_GetOrientation(void)
Definition: d4d_low.c:147
void D4D_LCD_PutPixel(D4D_COOR x1, D4D_COOR y1, D4D_LINETYPE line_type, D4D_COLOR color)
Definition: d4d_low.c:192
D4D_TOUCHSCREEN_CALIB D4D_TCH_GetCalibration(void)
Line type thick.
Definition: d4d_types.h:294
#define NULL
Type definition of null pointer.
Definition: d4d_types.h:184
Landscape orientation.
Definition: d4d_types.h:345
D4D_TCHAR * pText
pointer to text array
Definition: d4d_string.h:102
unsigned long LWord
Type definition of LWord (unsigned 32-bit).
Definition: d4d_types.h:167
D4D_STR_PROPERTIES * str_properties
pointer to string properties
Definition: d4d_string.h:105
Byte D4D_TCH_GetRawPosition(D4D_COOR *TouchPositionX, D4D_COOR *TouchPositionY)
signed short sWord
Type definition of sWord (signed 16-bit).
Definition: d4d_types.h:163
static void D4D_LCD_Plot8points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color)
Definition: d4d_low.c:350
#define D4D_SCREEN_SIZE_SHORTER_SIDE
This macro is used to define the size of shorter side of LCD. If not defined, the shorter side is set...
Definition: d4d_base.h:108
unsigned short rawDataScale
The scale mask of ADC convertor.
Definition: d4d_lldapi.h:107
void D4D_TCH_SetCalibration(D4D_TOUCHSCREEN_CALIB newCalib)
D4D_QUADRANT
Type definition of eGUI circle quadrant.
Definition: d4d_types.h:323
signed char sByte
Type definition of sByte (signed 8-bit).
Definition: d4d_types.h:155
Second quadrant &lt;90-180)
Definition: d4d_types.h:326
void D4D_LCD_QCircle(D4D_COOR cx, D4D_COOR cy, D4D_COOR radius, D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color, D4D_QUADRANT quadrant)
Definition: d4d_low.c:561
Fourth quadrant &lt;270-360)
Definition: d4d_types.h:328
D4D_BOOL D4D_LCD_Init(void)
Definition: d4d_low.c:86
unsigned char(* D4DLCD_SetOrientation)(D4DLCD_ORIENTATION new_orientation)
The LCD driver set orientation function.
Definition: d4d_lldapi.h:151
const D4DLCD_FUNCTIONS D4D_LLD_LCD
LWord D4D_BOOL
Type definition of eGUI boolean.
Definition: d4d_types.h:204
The notification that the start of screen is done.
Definition: d4d_lldapi.h:139
#define D4D_UNUSED(x)
Macro used just for notify compiler that the input parameter is not used.
Definition: d4d_base.h:504
static void D4D_LCD_QPlot4points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_LINETYPE line_type, D4D_COLOR color, D4D_QUADRANT quadrant)
Definition: d4d_low.c:508
D4D_FONT_SIZE D4D_GetFontWidth(D4D_FONT ix)
Definition: d4d_font.c:398
void D4D_LCD_Circle(D4D_COOR cx, D4D_COOR cy, D4D_COOR radius, D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color)
Definition: d4d_low.c:367
Portrait orientation.
Definition: d4d_types.h:343
static void D4D_LCD_Plot4points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_LINETYPE line_type, D4D_COLOR color)
Definition: d4d_low.c:335
Line type thin.
Definition: d4d_types.h:293
static void D4D_LCD_QPlot8points(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_BOOL fill, D4D_LINETYPE line_type, D4D_COLOR color, D4D_QUADRANT quadrant)
Definition: d4d_low.c:547
unsigned short TouchScreenXoffset
Input value offset in axis X.
Definition: d4d_types.h:353
static D4D_ORIENTATION d4d_orientation
Definition: d4d_low.c:68
unsigned short touchOffMaxY
Maximal valid raw value of axis Y.
Definition: d4d_lldapi.h:111
unsigned short TouchScreenYoffset
Input value offset in axis Y.
Definition: d4d_types.h:354
D4D driver - resistive touch screen driver function header file.
unsigned char ScreenCalibrated
Boolean if the values are valid.
Definition: d4d_types.h:352
#define D4DTCH_CALIB_CROSS_OFFSET
This macro is used in touch screen calibration to define offset of calibration points from edge of LC...
Definition: d4d_base.h:340
void(* D4DLCD_Send_PixelColor)(D4D_COLOR value)
The LCD driver send pixel function. The pixels will be draw on position (automaticaly incremented) se...
Definition: d4d_lldapi.h:152
unsigned char(* D4DLCD_Init)(void)
The LCD driver initialization function.
Definition: d4d_lldapi.h:149
LWord D4D_COLOR
Type definition of eGUI color variables.
Definition: d4d_types.h:262
Byte D4D_SprintDecU16(Word val, D4D_TCHAR *pText, D4D_TCHAR fill)
The function convert decimal unsigned 16 bit number to string.
Definition: d4d_string.c:201
D4D_FONT_PROPERTIES font_properties
Font properties structure.
Definition: d4d_string.h:95
D4D_FONT_SIZE D4D_GetFontHeight(D4D_FONT ix)
Definition: d4d_font.c:381
static void D4D_LCD_QPlot4pointsfill(D4D_COOR cx, D4D_COOR cy, D4D_COOR x, D4D_COOR y, D4D_COLOR color, D4D_QUADRANT quadrant)
Definition: d4d_low.c:527
#define D4D_FONT_SYSTEM_DEFAULT
This macro is used to define the system font index (For example used in touch screen calibration)...
Definition: d4d_base.h:133
unsigned short Word
Type definition of Word (unsigned 16-bit).
Definition: d4d_types.h:159
void D4D_LCD_RRect(D4D_COOR x1, D4D_COOR y1, D4D_COOR x2, D4D_COOR y2, D4D_LINETYPE line_type, D4D_COLOR color, D4D_COOR radius)
Definition: d4d_low.c:453
D4D_INDEX printOff
Offset of string that should be used (printed).
Definition: d4d_string.h:107
The notification of forcing flush by user code.
Definition: d4d_lldapi.h:141
D4D_COOR D4D_DisplayWidth(void)
Definition: d4d_low.c:159
unsigned char(* D4DLCD_SetWindow)(unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2)
The LCD driver Set logic window function. Into this logic window will be draw pixels.
Definition: d4d_lldapi.h:150
void(* D4DLCD_Delay_ms)(unsigned short period)
The LCD driver delay function.
Definition: d4d_lldapi.h:155