Skip to content

Commit 664c8c5

Browse files
committed
Added Adafruit Touchscreen Library
Added support for STM32 to the Adafruit Resistive Touchscreen library.
1 parent 2e9b4f3 commit 664c8c5

5 files changed

Lines changed: 428 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This is the 4-wire resistive touch screen firmware for Arduino. Works with all Arduinos and the Mega
2+
3+
4+
To install, click DOWNLOAD SOURCE in the top right corner, and rename the uncompressed folder "TouchScreen". See our tutorial at http://www.ladyada.net/library/arduino/libraries.html on Arduino Library installation
5+
6+
Ported to the STM32 by Jaret Burkett https://github.com/jaretburkett
Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,310 @@
1+
// Touch screen library with X Y and Z (pressure) readings as well
2+
// as oversampling to avoid 'bouncing'
3+
// (c) ladyada / adafruit
4+
// Code under MIT License
5+
6+
// Ported to STM32 by Jaret Burkett https://github.com/jaretburkett
7+
8+
#include "pins_arduino.h"
9+
#include "wiring_private.h"
10+
#ifdef __AVR
11+
#include <avr/pgmspace.h>
12+
#elif defined(ESP8266)
13+
#include <pgmspace.h>
14+
#elif defined (__STM32F1__)
15+
#include <avr/pgmspace.h>
16+
#include <WProgram.h>
17+
#endif
18+
#include "TouchScreen_STM.h"
19+
20+
// increase or decrease the touchscreen oversampling. This is a little different than you make think:
21+
// 1 is no oversampling, whatever data we get is immediately returned
22+
// 2 is double-sampling and we only return valid data if both points are the same
23+
// 3+ uses insert sort to get the median value.
24+
// We found 2 is precise yet not too slow so we suggest sticking with it!
25+
26+
#define NUMSAMPLES 2
27+
28+
TSPoint::TSPoint(void) {
29+
x = y = 0;
30+
}
31+
32+
TSPoint::TSPoint(int16_t x0, int16_t y0, int16_t z0) {
33+
x = x0;
34+
y = y0;
35+
z = z0;
36+
}
37+
38+
bool TSPoint::operator==(TSPoint p1) {
39+
return ((p1.x == x) && (p1.y == y) && (p1.z == z));
40+
}
41+
42+
bool TSPoint::operator!=(TSPoint p1) {
43+
return ((p1.x != x) || (p1.y != y) || (p1.z != z));
44+
}
45+
46+
#if (NUMSAMPLES > 2)
47+
static void insert_sort(int array[], uint8_t size) {
48+
uint8_t j;
49+
int save;
50+
51+
for (int i = 1; i < size; i++) {
52+
save = array[i];
53+
for (j = i; j >= 1 && save < array[j - 1]; j--)
54+
array[j] = array[j - 1];
55+
array[j] = save;
56+
}
57+
}
58+
#endif
59+
60+
TSPoint TouchScreen::getPoint(void) {
61+
int x, y, z;
62+
int samples[NUMSAMPLES];
63+
uint8_t i, valid;
64+
65+
#if !defined (__STM32F1__)
66+
uint8_t xp_port = digitalPinToPort(_xp);
67+
uint8_t yp_port = digitalPinToPort(_yp);
68+
uint8_t xm_port = digitalPinToPort(_xm);
69+
uint8_t ym_port = digitalPinToPort(_ym);
70+
71+
uint8_t xp_pin = digitalPinToBitMask(_xp);
72+
uint8_t yp_pin = digitalPinToBitMask(_yp);
73+
uint8_t xm_pin = digitalPinToBitMask(_xm);
74+
uint8_t ym_pin = digitalPinToBitMask(_ym);
75+
#endif
76+
77+
valid = 1;
78+
79+
pinMode(_yp, INPUT);
80+
pinMode(_ym, INPUT);
81+
82+
#if !defined (__STM32F1__)
83+
*portOutputRegister(yp_port) &= ~yp_pin;
84+
*portOutputRegister(ym_port) &= ~ym_pin;
85+
86+
pinMode(_xp, OUTPUT);
87+
pinMode(_xm, OUTPUT);
88+
89+
*portOutputRegister(xp_port) |= xp_pin;
90+
*portOutputRegister(xm_port) &= ~xm_pin;
91+
#else
92+
digitalWrite(_yp, LOW);
93+
digitalWrite(_ym, LOW);
94+
95+
pinMode(_xp, OUTPUT);
96+
pinMode(_xm, OUTPUT);
97+
98+
digitalWrite(_xp, HIGH);
99+
digitalWrite(_xm, LOW);
100+
#endif
101+
102+
for (i=0; i<NUMSAMPLES; i++) {
103+
samples[i] = analogRead(_yp);
104+
}
105+
#if NUMSAMPLES > 2
106+
insert_sort(samples, NUMSAMPLES);
107+
#endif
108+
#if NUMSAMPLES == 2
109+
if (samples[0] != samples[1]) { valid = 0; }
110+
#endif
111+
112+
#if !defined (__STM32F1__)
113+
x = (1023-samples[NUMSAMPLES/2]);
114+
#else
115+
x = (4095-samples[NUMSAMPLES/2]);
116+
#endif
117+
118+
pinMode(_xp, INPUT);
119+
pinMode(_xm, INPUT);
120+
#if !defined (__STM32F1__)
121+
*portOutputRegister(xp_port) &= ~xp_pin;
122+
#else
123+
digitalWrite(_xp, LOW);
124+
#endif
125+
126+
pinMode(_yp, OUTPUT);
127+
128+
#if !defined (__STM32F1__)
129+
*portOutputRegister(yp_port) |= yp_pin;
130+
#else
131+
digitalWrite(_yp, HIGH);
132+
#endif
133+
134+
pinMode(_ym, OUTPUT);
135+
136+
for (i=0; i<NUMSAMPLES; i++) {
137+
samples[i] = analogRead(_xm);
138+
}
139+
140+
#if NUMSAMPLES > 2
141+
insert_sort(samples, NUMSAMPLES);
142+
#endif
143+
#if NUMSAMPLES == 2
144+
if (samples[0] != samples[1]) { valid = 0; }
145+
#endif
146+
147+
#if !defined (__STM32F1__)
148+
y = (1023-samples[NUMSAMPLES/2]);
149+
#else
150+
y = (4095-samples[NUMSAMPLES/2]);
151+
#endif
152+
153+
pinMode(_xp, OUTPUT);
154+
#if !defined (__STM32F1__)
155+
// Set X+ to ground
156+
*portOutputRegister(xp_port) &= ~xp_pin;
157+
158+
// Set Y- to VCC
159+
*portOutputRegister(ym_port) |= ym_pin;
160+
161+
// Hi-Z X- and Y+
162+
*portOutputRegister(yp_port) &= ~yp_pin;
163+
#else
164+
// Set X+ to ground
165+
digitalWrite(_xp, LOW);
166+
167+
// Set Y- to VCC
168+
digitalWrite(_ym, HIGH);
169+
170+
// Hi-Z X- and Y+
171+
digitalWrite(_yp, LOW);
172+
#endif
173+
174+
//digitalWrite(_yp, LOW);
175+
pinMode(_yp, INPUT);
176+
177+
int z1 = analogRead(_xm);
178+
int z2 = analogRead(_yp);
179+
180+
if (_rxplate != 0) {
181+
// now read the x
182+
float rtouch;
183+
rtouch = z2;
184+
rtouch /= z1;
185+
rtouch -= 1;
186+
rtouch *= x;
187+
rtouch *= _rxplate;
188+
#if !defined (__STM32F1__)
189+
rtouch /= 1024;
190+
#else
191+
rtouch /= 4095;
192+
#endif
193+
194+
z = rtouch;
195+
} else {
196+
#if !defined (__STM32F1__)
197+
z = (1023-(z2-z1));
198+
#else
199+
z = (4095-(z2-z1));
200+
#endif
201+
}
202+
203+
if (! valid) {
204+
z = 0;
205+
}
206+
207+
return TSPoint(x, y, z);
208+
}
209+
210+
TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym) {
211+
_yp = yp;
212+
_xm = xm;
213+
_ym = ym;
214+
_xp = xp;
215+
_rxplate = 0;
216+
pressureThreshhold = 10;
217+
}
218+
219+
220+
TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym,
221+
uint16_t rxplate) {
222+
_yp = yp;
223+
_xm = xm;
224+
_ym = ym;
225+
_xp = xp;
226+
_rxplate = rxplate;
227+
228+
pressureThreshhold = 10;
229+
}
230+
231+
int TouchScreen::readTouchX(void) {
232+
pinMode(_yp, INPUT);
233+
pinMode(_ym, INPUT);
234+
digitalWrite(_yp, LOW);
235+
digitalWrite(_ym, LOW);
236+
237+
pinMode(_xp, OUTPUT);
238+
digitalWrite(_xp, HIGH);
239+
pinMode(_xm, OUTPUT);
240+
digitalWrite(_xm, LOW);
241+
242+
#if !defined (__STM32F1__)
243+
return (1023-analogRead(_yp));
244+
#else
245+
return (4095-analogRead(_yp));
246+
#endif
247+
248+
}
249+
250+
251+
int TouchScreen::readTouchY(void) {
252+
pinMode(_xp, INPUT);
253+
pinMode(_xm, INPUT);
254+
digitalWrite(_xp, LOW);
255+
digitalWrite(_xm, LOW);
256+
257+
pinMode(_yp, OUTPUT);
258+
digitalWrite(_yp, HIGH);
259+
pinMode(_ym, OUTPUT);
260+
digitalWrite(_ym, LOW);
261+
262+
#if !defined (__STM32F1__)
263+
return (1023-analogRead(_xm));
264+
#else
265+
return (4095-analogRead(_xm));
266+
#endif
267+
}
268+
269+
270+
uint16_t TouchScreen::pressure(void) {
271+
// Set X+ to ground
272+
pinMode(_xp, OUTPUT);
273+
digitalWrite(_xp, LOW);
274+
275+
// Set Y- to VCC
276+
pinMode(_ym, OUTPUT);
277+
digitalWrite(_ym, HIGH);
278+
279+
// Hi-Z X- and Y+
280+
digitalWrite(_xm, LOW);
281+
pinMode(_xm, INPUT);
282+
digitalWrite(_yp, LOW);
283+
pinMode(_yp, INPUT);
284+
285+
int z1 = analogRead(_xm);
286+
int z2 = analogRead(_yp);
287+
288+
if (_rxplate != 0) {
289+
// now read the x
290+
float rtouch;
291+
rtouch = z2;
292+
rtouch /= z1;
293+
rtouch -= 1;
294+
rtouch *= readTouchX();
295+
rtouch *= _rxplate;
296+
#if !defined (__STM32F1__)
297+
rtouch /= 1024;
298+
#else
299+
rtouch /= 4095;
300+
#endif
301+
302+
return rtouch;
303+
} else {
304+
#if !defined (__STM32F1__)
305+
return (1023-(z2-z1));
306+
#else
307+
return (4095-(z2-z1));
308+
#endif
309+
}
310+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Touch screen library with X Y and Z (pressure) readings as well
2+
// as oversampling to avoid 'bouncing'
3+
// (c) ladyada / adafruit
4+
// Code under MIT License
5+
6+
// Ported to STM32 by Jaret Burkett https://github.com/jaretburkett
7+
8+
#ifndef _ADAFRUIT_TOUCHSCREEN_STM_H_
9+
#define _ADAFRUIT_TOUCHSCREEN_STM_H_
10+
#include <stdint.h>
11+
12+
class TSPoint {
13+
public:
14+
TSPoint(void);
15+
TSPoint(int16_t x, int16_t y, int16_t z);
16+
17+
bool operator==(TSPoint);
18+
bool operator!=(TSPoint);
19+
20+
int16_t x, y, z;
21+
};
22+
23+
class TouchScreen {
24+
public:
25+
TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym);
26+
TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, uint16_t rx);
27+
28+
bool isTouching(void);
29+
uint16_t pressure(void);
30+
int readTouchY();
31+
int readTouchX();
32+
TSPoint getPoint();
33+
int16_t pressureThreshhold;
34+
35+
private:
36+
uint8_t _yp, _ym, _xm, _xp;
37+
uint16_t _rxplate;
38+
};
39+
40+
#endif
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Touch screen library with X Y and Z (pressure) readings as well
2+
// as oversampling to avoid 'bouncing'
3+
// This demo code returns raw readings, public domain
4+
5+
#include <stdint.h>
6+
#include <TouchScreen_STM.h>
7+
8+
#define YP 8 // must be an analog pin
9+
#define XM 9 // must be an analog pin
10+
#define YM 10 // can be a digital pin
11+
#define XP 11 // can be a digital pin
12+
13+
// For better pressure precision, we need to know the resistance
14+
// between X+ and X- Use any multimeter to read it
15+
// For the one we're using, its 300 ohms across the X plate
16+
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
17+
18+
void setup(void) {
19+
Serial.begin(9600);
20+
}
21+
22+
void loop(void) {
23+
// a point object holds x y and z coordinates
24+
TSPoint p = ts.getPoint();
25+
26+
// we have some minimum pressure we consider 'valid'
27+
// pressure of 0 means no pressing!
28+
if (p.z > ts.pressureThreshhold) {
29+
Serial.print("X = "); Serial.print(p.x);
30+
Serial.print("\tY = "); Serial.print(p.y);
31+
Serial.print("\tPressure = "); Serial.println(p.z);
32+
}
33+
34+
delay(100);
35+
}

0 commit comments

Comments
 (0)