forked from mjculross/Enigma_Visual_Simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFTcal-Adafruit.ino
214 lines (180 loc) · 4.97 KB
/
TFTcal-Adafruit.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// Touchpad calibration program for Adafruit Touch Shield V2
//
// - determines touchpad constants TS_MINX, TS_MINY, TS_MAXX, & TS_MAXY on-the-fly
// - use something small but not too sharp (e.g. toothpick) to poke the touchscreen
//
// V1.0 by MJCulross (KD5RXT) - 20141231-1100
//
// Uses the Adafruit Touch Shield V2 library
// for its display output & touchpad input
//
//
// The following pins are used in this project:
//
// PIN D2 = (not used)
// PIN D3 = TFT backlight control (optional - solder jumper to activate)
// PIN D4 = microSD chip select
// PIN D5 = (not used)
// PIN D6 = (not used)
// PIN D7 = (not used)
// PIN D8 = Resistive TouchScreen chip select
// PIN D9 = TFT data/command select
// PIN D10 = TFT chip select
// PIN D11 = SPI data input pin - used for TFT, microSD, & Resistive TouchScreen data/command
// PIN D12 = SPI data output pin - used for TFT, microSD, & Resistive TouchScreen data/command
// PIN D13 = SPI serial clock pin - used for TFT, microSD, & Resistive TouchScreen clock
// PIN A0 = (not used)
// PIN A1 = (not used)
// PIN A2 = (not used)
// PIN A3 = (not used)
// PIN A4 = (not used)
// PIN A5 = (not used)
#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h>
#include <Wire.h> // this is needed even tho we aren't using it
#include <Adafruit_ILI9341.h>
#include <Adafruit_STMPE610.h>
// This is rough calibration data for the raw touch data to the screen coordinates
#define TS_MINX 210
#define TS_MINY 200
#define TS_MAXX 3860
#define TS_MAXY 3760
// The STMPE610 uses hardware SPI on the shield, and #8
#define STMPE_CS 8
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
// The display uses hardware SPI, plus #9 & #10
#define TFT_CS 10
#define TFT_DC 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
const int speaker=2; // define the speaker pin
uint16_t sampleX[10];
uint16_t sampleY[10];
int indexX = 0;
int indexY = 0;
void setup()
{
tft.begin(); //init TFT library
ts.begin(); //init TouchScreen library
// blank the whole display
tft.fillRect(0, 0, 240, 320, ILI9341_WHITE);
}
void loop()
{
uint16_t rawX;
uint16_t avgX = 0;
uint16_t rawY;
uint16_t avgY = 0;
uint8_t rawZ;
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(1);
for (int i = 0; i < 9; i++)
{
sampleX[i] = 200;
sampleY[i] = 200;
}
tft.fillRect(0, 200, 240, 40, ILI9341_WHITE);
tft.setCursor(20, 20);
tft.println("Touch the top-left corner...");
// wait for touch
while (! ts.touched()) {}
// wait for touch to go away
while (ts.touched())
{
ts.readData(&rawX, &rawY, &rawZ);
sampleX[indexX] = rawX;
if (++indexX > 9)
{
indexX = 0;
}
for (int i = 0; i < 9; i++)
{
avgX += sampleX[i];
}
avgX /= 10;
sampleY[indexY] = rawY;
if (++indexY > 9)
{
indexY = 0;
}
for (int i = 0; i < 9; i++)
{
avgY += sampleY[i];
}
avgY /= 10;
tft.fillRect(0, 40, 240, 50, ILI9341_WHITE);
tft.setCursor(20, 40);
tft.println("rawX: ");
tft.setCursor(50, 40);
tft.println(rawX);
tft.setCursor(20, 50);
tft.println("rawY: ");
tft.setCursor(50, 50);
tft.println(rawY);
tft.setCursor(20, 70);
tft.println("TS_MINX: ");
tft.setCursor(70, 70);
tft.println(avgX);
tft.setCursor(20, 80);
tft.println("TS_MINY: ");
tft.setCursor(70, 80);
tft.println(avgY);
// now, empty any buffered data
while (! ts.bufferEmpty())
{
ts.readData(&rawX, &rawY, &rawZ);
}
}
tft.fillRect(0, 20, 240, 40, ILI9341_WHITE);
tft.setCursor(20, 200);
tft.println("Touch the bottom-right corner...");
// wait for touch
while (! ts.touched()) {}
// wait for touch to go away
while (ts.touched())
{
ts.readData(&rawX, &rawY, &rawZ);
sampleX[indexX] = rawX;
if (++indexX > 9)
{
indexX = 0;
}
for (int i = 0; i < 9; i++)
{
avgX += sampleX[i];
}
avgX /= 10;
sampleY[indexY] = rawY;
if (++indexY > 9)
{
indexY = 0;
}
for (int i = 0; i < 9; i++)
{
avgY += sampleY[i];
}
avgY /= 10;
tft.fillRect(0, 120, 240, 50, ILI9341_WHITE);
tft.setCursor(20, 120);
tft.println("rawX: ");
tft.setCursor(50, 120);
tft.println(rawX);
tft.setCursor(20, 130);
tft.println("rawY: ");
tft.setCursor(50, 130);
tft.println(rawY);
tft.setCursor(20, 150);
tft.println("TS_MAXX: ");
tft.setCursor(70, 150);
tft.println(avgX);
tft.setCursor(20, 160);
tft.println("TS_MAXY: ");
tft.setCursor(70, 160);
tft.println(avgY);
// now, empty any buffered data
while (! ts.bufferEmpty())
{
ts.readData(&rawX, &rawY, &rawZ);
}
}
}