Skip to content

Commit a7e909a

Browse files
Merge branch 'master' into development
2 parents 7ee66cc + 0469149 commit a7e909a

6 files changed

Lines changed: 438 additions & 10 deletions

File tree

STM32F1/libraries/Adafruit_ILI9341_STM/examples/breakouttouchpaint/breakouttouchpaint.ino

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@
1515

1616
/** NOT FOR USE WITH THE TOUCH SHIELD, ONLY FOR THE BREAKOUT! **/
1717

18-
#include <Adafruit_GFX.h> // Core graphics library
18+
#include <Adafruit_GFX_AS.h> // Core graphics library
1919
#include <SPI.h>
2020
#include <Adafruit_ILI9341_STM.h>
21-
#include "TouchScreen.h"
21+
#include <TouchScreen_STM.h>
2222

2323
// These are the four touchscreen analog pins
24-
#define YP A2 // must be an analog pin, use "An" notation!
25-
#define XM A3 // must be an analog pin, use "An" notation!
26-
#define YM 5 // can be a digital pin
27-
#define XP 4 // can be a digital pin
24+
#define YP PB0 // must be an analog pin
25+
#define XM PA3 // must be an analog pin
26+
#define YM PB7 // can be a digital pin
27+
#define XP PC13 // can be a digital pin
2828

2929
// This is calibration data for the raw touch data to the screen coordinates
30-
#define TS_MINX 150
31-
#define TS_MINY 120
32-
#define TS_MAXX 920
33-
#define TS_MAXY 940
30+
#define TS_MINX 3670
31+
#define TS_MINY 3790
32+
#define TS_MAXX 640
33+
#define TS_MAXY 300
3434

3535
#define MINPRESSURE 10
3636
#define MAXPRESSURE 1000
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

0 commit comments

Comments
 (0)