Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
SeeedTouchScreen.cpp
Go to the documentation of this file.
1 /*
2  SeeedTouchScreen.cpp - Library for 4-line resistance touch screen.
3  Modified by loovee Aug 12, 2012.
4  (c) ladyada / adafruit
5  Code under MIT License.
6 */
7 
8 #include "pins_arduino.h"
9 #include "wiring_private.h"
10 #include <avr/pgmspace.h>
11 #include "SeeedTouchScreen.h"
12 
13 // increase or decrease the touchscreen oversampling. This is a little different than you make think:
14 // 1 is no oversampling, whatever data we get is immediately returned
15 // 2 is double-sampling and we only return valid data if both points are the same
16 // 3+ uses insert sort to get the median value.
17 // We found 2 is precise yet not too slow so we suggest sticking with it!
18 
19 #define NUMSAMPLES 2 // sample number
20 #define COMP 2
21 #define AVERAGE 1
22 #define RXPLATE 300
23 #define TSDEBUG 0 // if print the debug information
25 {
26  x = y = 0;
27 }
28 
29 Point::Point(int x0, int y0, int z0)
30 {
31  x = x0;
32  y = y0;
33  z = z0;
34 }
35 
37 {
38  return ((p1.x == x) && (p1.y == y) && (p1.z == z));
39 }
40 
42 {
43  return ((p1.x != x) || (p1.y != y) || (p1.z != z));
44 }
45 
46 TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym)
47 {
48  _yp = yp;
49  _xm = xm;
50  _ym = ym;
51  _xp = xp;
52 }
53 
54 #if AVERAGE
55 #define AVERAGETIME 4
56 int avr_analog(int adpin)
57 {
58  int sum = 0;
59  int max = 0;
60  int min = 1024;
61  for (int i = 0; i<AVERAGETIME; i++)
62  {
63  int tmp = analogRead(adpin);
64  if (tmp > max)max = tmp;
65  if (tmp < min)min = tmp;
66  sum += tmp;
67  // sum+=analogRead(adpin);
68  }
69  return (sum-min-max)/(AVERAGETIME-2);
70 
71 }
72 #endif
73 
75 {
76  int x, y, z = 1;
77  int samples[NUMSAMPLES];
78 #if TSDEBUG
79  int xx[2] = {0, 0};
80  int yy[2] = {0, 0};
81 #endif
82  uint8_t i, valid;
83 
84  uint8_t xp_port = digitalPinToPort(_xp);
85  unsigned char yp_port = digitalPinToPort(_yp);
86  unsigned char xm_port = digitalPinToPort(_xm);
87  unsigned char ym_port = digitalPinToPort(_ym);
88 
89  unsigned char xp_pin = digitalPinToBitMask(_xp);
90  unsigned char yp_pin = digitalPinToBitMask(_yp);
91  unsigned char xm_pin = digitalPinToBitMask(_xm);
92  unsigned char ym_pin = digitalPinToBitMask(_ym);
93  valid = 1;
94  pinMode(_yp, INPUT);
95  pinMode(_ym, INPUT);
96 
97  *portOutputRegister(yp_port) &= ~yp_pin;
98  *portOutputRegister(ym_port) &= ~ym_pin;
99 
100  pinMode(_xp, OUTPUT);
101  pinMode(_xm, OUTPUT);
102 
103  *portOutputRegister(xp_port) |= xp_pin;
104  *portOutputRegister(xm_port) &= ~xm_pin;
105 
106  for (i=0; i<NUMSAMPLES; i++)
107  {
108 #if AVERAGE
109  samples[i] = avr_analog(_yp);
110 #else
111  samples[i] = analogRead(_yp);
112 #endif
113 
114 #if TSDEBUG
115  xx[i] = samples[i];
116 #endif
117  }
118 
119 #if !COMP
120  if (samples[0] != samples[1])
121  {
122  valid = 0;
123  }
124 #else
125  int icomp = samples[0]>samples[1]?samples[0]-samples[1]:samples[1] - samples[0];
126  if (icomp > COMP)valid = 0;
127 #endif
128 
129  x = (samples[0] + samples[1]);
130 
131  pinMode(_xp, INPUT);
132  pinMode(_xm, INPUT);
133  *portOutputRegister(xp_port) &= ~xp_pin;
134 
135  pinMode(_yp, OUTPUT);
136  *portOutputRegister(yp_port) |= yp_pin;
137  pinMode(_ym, OUTPUT);
138 
139  for (i=0; i<NUMSAMPLES; i++)
140  {
141 #if AVERAGE
142  samples[i] = avr_analog(_xm);
143 #else
144  samples[i] = analogRead(_xm);
145 #endif
146 #if TSDEBUG
147  yy[i] = samples[i];
148 #endif
149  }
150 
151 #if !COMP
152  if (samples[0] != samples[1])
153  {
154  valid = 0;
155  }
156 #else
157  icomp = samples[0]>samples[1]?samples[0]-samples[1]:samples[1] - samples[0];
158  if (icomp>COMP)valid = 0;
159 #endif
160  y = (samples[0]+samples[0]);
161 
162  pinMode(_xp, OUTPUT);
163  *portOutputRegister(xp_port) &= ~xp_pin; // Set X+ to ground
164  *portOutputRegister(ym_port) |= ym_pin; // Set Y- to VCC
165  *portOutputRegister(yp_port) &= ~yp_pin; // Hi-Z X- and Y+
166  pinMode(_yp, INPUT);
167 
168  int z1 = analogRead(_xm);
169  int z2 = analogRead(_yp);
170  float rtouch = 0;
171 
172  rtouch = z2;
173  rtouch /= z1;
174  rtouch -= 1;
175  rtouch *= (2046-x)/2;
176  rtouch *= RXPLATE;
177  rtouch /= 1024;
178  z = rtouch;
179  if (! valid)
180  {
181  z = 0;
182  }
183 
184 #if TSDEBUG
185  if (z > __PRESURE)
186  {
187  Serial.print("x1 = ");
188  Serial.print(xx[0]);
189  Serial.print("\tx2 = ");
190  Serial.print(xx[1]);
191  Serial.print("\ty2 = ");
192  Serial.print(yy[0]);
193  Serial.print("\ty2 = ");
194  Serial.println(yy[1]);
195  }
196 #endif
197 
198  return Point(x, y, z);
199 }
200 
202 {
203  Point p = getPoint();
204  if (p.z > __PRESURE)return 1;
205  else return 0;
206 }
#define NUMSAMPLES
bool operator!=(Point)
#define COMP
#define __PRESURE
#define min(a, b)
int avr_analog(int adpin)
bool isTouching(void)
TouchScreen(unsigned char xp, unsigned char yp, unsigned char xm, unsigned char ym)
#define RXPLATE
#define AVERAGETIME
static int i
Definition: DC2430A.ino:184
bool operator==(Point)