eGUI alias D4D  Release 3.0
Reference Manual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
d4dtch_resistive.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 #include "d4d.h" // include of all public items (types, function etc) of D4D driver
47 #include "common_files/d4d_lldapi.h" // include non public low level driver interface header file (types, function prototypes, enums etc. )
48 #include "common_files/d4d_private.h" // include the private header file that contains perprocessor macros as D4D_MK_STR
49 
50 
51 // identification string of driver - must be same as name D4DTCH_FUNCTIONS structure + "_ID"
52 // it is used for enable the code for compilation
53 #define d4dtch_resistive_ID 1
54 
55 
56 // copilation enable preprocessor condition
57 // the string d4dtch_resistive_ID must be replaced by define created one line up
58 #if (D4D_MK_STR(D4D_LLD_TCH) == d4dtch_resistive_ID)
59 
60  // include of low level driver heaser file
61  // it will be included into wole project only in case that this driver is selected in main D4D configuration file
63  /******************************************************************************
64  * Macros
65  ******************************************************************************/
66 
67  /******************************************************************************
68  * Internal function prototypes
69  ******************************************************************************/
70 
71  static unsigned char D4DTCH_Init_Resistive(void);
72  static unsigned char D4DTCH_DeInit_Resistive(void);
73  static D4D_TOUCHSCREEN_LIMITS* D4DTCH_GetRawLimits_Resistive(void);
74  static unsigned char D4DTCH_GetPositionRaw_Resistive(unsigned short *TouchPositionX,
75  unsigned short *TouchPositionY);
76 
77  /**************************************************************/
83  // the main structure that contains low level driver api functions
84  // the name fo this structure is used for recognizing of configured low level driver of whole D4D
85  // so this name has to be used in main configuration header file of D4D driver to enable this driver
86  const D4DTCH_FUNCTIONS d4dtch_resistive =
87  {
88  D4DTCH_Init_Resistive,
89  D4DTCH_GetPositionRaw_Resistive,
90  D4DTCH_GetRawLimits_Resistive,
91  D4DTCH_DeInit_Resistive
92  };
93 
94  /**************************************************************/
100  /**************************************************************/
105  static unsigned char TchScr_GetFilteredAxis(D4DTCHHW_PINS pinId, unsigned short *Res)
106  {
107  unsigned short wSample[AVERAGE_BUFF_LEN];
108  unsigned short wRes, wSum;
109  unsigned short wADCReading;
110  unsigned short wMaxLimit;
111  unsigned char tmp, tmp_res;
112  int cnt;
113  unsigned char sample_margin;
114  D4D_TOUCHSCREEN_LIMITS* p_limits = D4D_LLD_TCH_HW.D4DTCHHW_GetRawLimits();
115 
116 
117  sample_margin = (unsigned char)(p_limits->rawDataScale / 256);
118  wMaxLimit = (unsigned short)(p_limits->rawDataScale - sample_margin);
119 
120  // clear all local variables and buffer
121  for(tmp = 0; tmp < AVERAGE_BUFF_LEN;tmp++)
122  wSample[tmp] = 0;
123  wSum = 0;
124  cnt = 0;
125 
126 
127  while (cnt < 0x50)
128  {
129  cnt++;
130  // Read ADC value
131  wADCReading = D4D_LLD_TCH_HW.D4DTCHHW_ReadTouchAxis(pinId);
132 
133  // check if input value is under minimal value
134  if(wADCReading < p_limits->touchMinX)
135  break;
136 
137  // check if input value is above maximal value
138  if(wADCReading > wMaxLimit)
139  {
140  #if(D4D_MCU_TYPE != D4D_MK)
141  asm (nop);
142  #endif
143  break;
144  }
145  // Compute the current output value
146  wSum += wADCReading;
147  wSum -= wSample[AVERAGE_BUFF_LEN - 1];
148 
149  // shift all values in input buffer
150  for(tmp = (AVERAGE_BUFF_LEN - 1); tmp > 0 ;tmp--)
151  {
152  wSample[tmp] = wSample[tmp - 1];
153  }
154 
155  // put new value into buffer
156  wSample[0] = wADCReading;
157 
158  // compute current result from all bufer values
159  wRes = (unsigned short)(wSum / AVERAGE_BUFF_LEN);
160 
161  // if buffer is full
162  if(cnt > AVERAGE_BUFF_LEN)
163  {
164  // Check all input samples if are in allowed range
165  tmp_res = 0;
166  for(tmp = 0; tmp < AVERAGE_BUFF_LEN ;tmp++)
167  {
168  if((wRes > (wSample[tmp] - sample_margin)) && (wRes < (wSample[tmp] + sample_margin)))
169  {
170  tmp_res++;
171  }
172  }
173 
174  // If most of sample are in allowed range with output value, assert this sample as result
175  if(tmp_res >= (AVERAGE_BUFF_LEN - 2)) {
176  *Res = wRes;
177  return 1;
178  }
179  }
180  }
181 
182  return 0;
183  }
184 
185 
186  //-----------------------------------------------------------------------------
187  // FUNCTION: D4DTCH_Init_Resistive
188  // SCOPE: Low Level Driver API function
189  // DESCRIPTION: The function is used for initialization of this low level driver
190  //
191  // PARAMETERS: none
192  //
193  // RETURNS: result: 1 - Success
194  // 0 - Failed
195  //-----------------------------------------------------------------------------
196  static unsigned char D4DTCH_Init_Resistive(void)
197  {
198  if(D4D_LLD_TCH_HW.D4DTCHHW_Init == NULL)
199  return 0;
200  if(D4D_LLD_TCH_HW.D4DTCHHW_ReadTouchAxis == NULL)
201  return 0;
202  if(D4D_LLD_TCH_HW.D4DTCHHW_GetRawLimits == NULL)
203  return 0;
204  if(D4D_LLD_TCH_HW.D4DTCHHW_PinCtl == NULL)
205  return 0;
206  if(D4D_LLD_TCH_HW.D4DTCHHW_DeInit == NULL)
207  return 0;
208 
209  return D4D_LLD_TCH_HW.D4DTCHHW_Init();
210  }
211 
212  //-----------------------------------------------------------------------------
213  // FUNCTION: D4DTCH_DeInit_Resistive
214  // SCOPE: Low Level Driver API function
215  // DESCRIPTION: The function is used for deinitialization of this low level driver
216  //
217  // PARAMETERS: none
218  //
219  // RETURNS: result: 1 - Success
220  // 0 - Failed
221  //-----------------------------------------------------------------------------
222  static unsigned char D4DTCH_DeInit_Resistive(void)
223  {
224  return 0;
225  }
226 
227  //-----------------------------------------------------------------------------
228  // FUNCTION: D4DTCH_GetRawLimits_Resistive
229  // SCOPE: Low Level Driver API function
230  // DESCRIPTION: The function returns pointer on touch screen raw limits
231  // structure.
232  // PARAMETERS: none
233  //
234  // RETURNS: pointer on touch screen raw limit structure
235  //
236  //-----------------------------------------------------------------------------
237  static D4D_TOUCHSCREEN_LIMITS* D4DTCH_GetRawLimits_Resistive(void)
238  {
239  return D4D_LLD_TCH_HW.D4DTCHHW_GetRawLimits();
240  }
241 
242  //-----------------------------------------------------------------------------
243  // FUNCTION: D4DTCH_GetPositionRaw_Resistive
244  // SCOPE: Low Level Driver API function
245  // DESCRIPTION: Reads touch screen and returns raw uncompensated X, Y
246  // coordinates if screen touched
247  // PARAMETERS: unsigned short *TouchPositionX Pointer to X coordinate
248  // unsigned short *TouchPositionY Pointer to Y ccordinate
249  // RETURNS: 0 no screen touch
250  // 1 screen touch,
251  //-----------------------------------------------------------------------------
252  static unsigned char D4DTCH_GetPositionRaw_Resistive (unsigned short *TouchPositionX,
253  unsigned short *TouchPositionY)
254  {
255  // Declare and initialise local variables
256  unsigned short tmpRes;
257  unsigned char tmpCnt;
258  unsigned char bSampleComplete = 0;
259  unsigned short tmp_delay;
260 
261  // Switch on ADC channel on Y+ wire
262  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_PLUS_PIN, D4DHW_PIN_ADC_ON);
263  tmpCnt = 0;
264 
265  // to check that the touch screen surface is without any voltage and prepared
266  while((D4D_LLD_TCH_HW.D4DTCHHW_ReadTouchAxis(D4DTCH_Y_PLUS_PIN) > D4D_LLD_TCH_HW.D4DTCHHW_GetRawLimits()->touchMinY) && (++tmpCnt))
267  {
268  ;
269  }
270 
271  // Disable ADC function on Y+
272  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_PLUS_PIN, D4DHW_PIN_ADC_OFF);
273 
274  // when the touch screen surface is prepared
275  // switch on on X+ wire high level
276  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_PLUS_PIN, D4DHW_PIN_SET_1);
277  // wait a moment
278 
279  tmp_delay = DELAY_NOP_CNT;
280  while(tmp_delay--)
281  #if(D4D_MCU_TYPE != D4D_MK)
282  asm (nop);
283  #else
284  asm("NOP");
285  #endif
286 
287  // and Y- pin put into high Z mode to keep measurent non affected
288 
289  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_MINUS_PIN, D4DHW_PIN_IN);
290  // Switch on ADC channel on Y+ wire
291  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_PLUS_PIN, D4DHW_PIN_ADC_ON);
292 
293  tmp_delay = DELAY_NOP_CNT;
294  while(tmp_delay--)
295  #if(D4D_MCU_TYPE != D4D_MK)
296  asm (nop);
297  #else
298  asm("NOP");
299  #endif
300 
301 
302  // read value of Y axis and check if touch screen is touched
303  if(TchScr_GetFilteredAxis(D4DTCH_Y_PLUS_PIN, &tmpRes))
304  {
305  // if it's touched save the result of Y measurement
306  *TouchPositionY = tmpRes;
307 
308  // Disable ADC function on Y+
309  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_PLUS_PIN, D4DHW_PIN_ADC_OFF);
310  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_MINUS_PIN, D4DHW_PIN_OUT);
311 
312  // Set pins to default values
313  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_PLUS_PIN, D4DHW_PIN_SET_0);
314  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_MINUS_PIN, D4DHW_PIN_SET_0);
315  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_PLUS_PIN, D4DHW_PIN_SET_0);
316  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_MINUS_PIN, D4DHW_PIN_SET_0);
317 
318 
319 
320  // wait a moment
321  tmp_delay = DELAY_NOP_CNT;
322  while(tmp_delay--)
323  #if(D4D_MCU_TYPE != D4D_MK)
324  asm (nop);
325  #else
326  asm("NOP");
327  #endif
328 
329  // Switch on ADC channel on X+ wire
330  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_PLUS_PIN, D4DHW_PIN_ADC_ON);
331  // to check that the touch screen surface is without any voltage and prepared
332  tmpCnt = 0;
333  while((D4D_LLD_TCH_HW.D4DTCHHW_ReadTouchAxis(D4DTCH_X_PLUS_PIN) > D4D_LLD_TCH_HW.D4DTCHHW_GetRawLimits()->touchMinX) && (++tmpCnt))
334  {
335  ;
336  }
337  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_PLUS_PIN, D4DHW_PIN_ADC_OFF);
338 
339  // when the touch screen surface is prepared
340  // switch on on Y+ wire high level
341  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_PLUS_PIN, D4DHW_PIN_SET_1);
342 
343  // wait a moment
344  tmp_delay = DELAY_NOP_CNT;
345  while(tmp_delay--)
346  #if(D4D_MCU_TYPE != D4D_MK)
347  asm (nop);
348  #else
349  asm("NOP");
350  #endif
351 
352  // and X- pin put into high Z mode to keep measurent non affected
353  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_MINUS_PIN, D4DHW_PIN_IN);
354  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_PLUS_PIN, D4DHW_PIN_ADC_ON);
355 
356  // wait a moment
357  tmp_delay = DELAY_NOP_CNT;
358  while(tmp_delay--)
359  #if(D4D_MCU_TYPE != D4D_MK)
360  asm (nop);
361  #else
362  asm("NOP");
363  #endif
364 
365  // read value of X axis and check if touch screen is touched
366  if(TchScr_GetFilteredAxis(D4DTCH_X_PLUS_PIN, &tmpRes))
367  {
368  // if it's touched save the result of X measurement
369  *TouchPositionX = tmpRes;
370  bSampleComplete = 1;
371  }
372  }
373  // Disable ADC function on X+ and Y+
374  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_PLUS_PIN, D4DHW_PIN_ADC_OFF);
375  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_MINUS_PIN, D4DHW_PIN_OUT);
376  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_PLUS_PIN, D4DHW_PIN_ADC_OFF);
377  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_MINUS_PIN, D4DHW_PIN_OUT);
378 
379  // Put on touch screen pins into default mode
380  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_PLUS_PIN, D4DHW_PIN_SET_0);
381  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_X_MINUS_PIN, D4DHW_PIN_SET_0);
382  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_PLUS_PIN, D4DHW_PIN_SET_0);
383  (void)D4D_LLD_TCH_HW.D4DTCHHW_PinCtl(D4DTCH_Y_MINUS_PIN, D4DHW_PIN_SET_0);
384 
385  // return back result - if screen is touched or not
386  return bSampleComplete;
387  }
388 
389 #endif //(D4D_MK_STR(D4D_LLD_TCH) == d4dtch_resistive_ID)
Switch on the pin for read by ADC.
Definition: d4d_lldapi.h:82
Analog touch screen X- signal.
Definition: d4d_lldapi.h:90
D4D driver - resistive touch screen driver function header file.
D4DTCHHW_PINS
D4D low level MCU types definition for analog resistive touch screen signals.
Definition: d4d_lldapi.h:87
D4D low level touch screen interface API structure.
Definition: d4d_lldapi.h:195
D4D Driver main header file.
D4D Driver private header file.
#define D4D_LLD_TCH_HW
Definition: d4d_types.h:82
Switch pin to input mode (equivalent to high-Z)
Definition: d4d_lldapi.h:78
D4D low level touch screen limitation structure.
Definition: d4d_lldapi.h:105
Switch pin to output mode.
Definition: d4d_lldapi.h:77
#define NULL
Type definition of null pointer.
Definition: d4d_types.h:184
Set pin output register to logic 1.
Definition: d4d_lldapi.h:80
Switch off the pin for read by ADC.
Definition: d4d_lldapi.h:83
unsigned short rawDataScale
The scale mask of ADC convertor.
Definition: d4d_lldapi.h:107
Analog touch screen X+ signal.
Definition: d4d_lldapi.h:89
Set pin output register to logic 0.
Definition: d4d_lldapi.h:79
D4D driver - resistive touch screen driver function header file.
#define AVERAGE_BUFF_LEN
#define DELAY_NOP_CNT
Analog touch screen Y- signal.
Definition: d4d_lldapi.h:92
Analog touch screen Y+ signal.
Definition: d4d_lldapi.h:91