-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyTFT.cpp
310 lines (260 loc) · 8.6 KB
/
myTFT.cpp
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
//-------------------------------------------------
// definition and slight extensions to TFT_eSPI
//-------------------------------------------------
#include "myTFT.h"
#define DEBUG_CALIBRATION
#include <SPIFFS.h>
#ifdef DEBUG_CALIBRATION
#include "FluidNC_UI.h"
#endif
#define CALIBRATION_SANITY_VALUE 6000
// calibration data should not be zero or larger than this
const char *my_tft_driver = "ILI9431_32";
int my_tft_rotation = 3;
const uint16_t calibration_data_28_1[5] = {252,3404,308,3417,7};
// ILI9341 2.8" 320x280 in rotation(1?)
const uint16_t calibration_data_32_3[5] = {449,3431,374,3437,1};
// ILI9341 3.2" 320x280 in rotation(3)
const uint16_t calibration_data_32_1[5] = {487,3385,300,3437,7};
// TODO: ILI9341 3.2" 320x280 in rotation(1)
uint16_t calibration_data[5];
TFT_eSPI tft = TFT_eSPI();
#include "fonts/Dialog_plain_14.h"
#include "fonts/Dialog_plain_16.h"
#include "fonts/Dialog_plain_18.h"
#include "fonts/Dialog_plain_22.h"
#include "fonts/FreeMonoBold8pt7b.h" // "fonts/Roboto_Mono_Bold_14.h"
#include "fonts/mySymbolFont.h"
String getCalibrationFilename()
{
String retval = "/tft_";
retval += my_tft_driver;
retval += "_";
retval += my_tft_rotation;
retval += "_data.txt";
return retval;
}
void drawText( // draw clipped and justified text
const char *text,
JustifyType just,
FontType font,
int16_t x, int16_t y, int16_t w, int16_t h,
uint16_t fg,
uint16_t bg)
{
tft.setTextColor(fg,bg);
// setTextPadding() tells TFT to "fill the bounding box with background color"
// otherwise, TFT draws the background color to the end of the screen width (weird)
if (fg != bg)
tft.setTextPadding(w);
// JUSTIFICATION
// weird .. if we want to tell TFT to draw something centered we have to both tell
// it that we want it centered, by setting the "datum" which is the origin point of the text to use,
// and then specify the center of the rectangle ourselves, thus the "x,y" passed in must
// be coupled to the "datum" via these if and switch statements.
//
// NOTE that "top" and "bottom" depend on if the font has "ascenders" or "descenders" and
// does not always line up "baseline" to zero (but at least doesn't overflow "bottom" or "top")
int16_t use_x =
just == JUST_CENTER ||
just == JUST_CENTER_TOP ||
just == JUST_CENTER_BOTTOM ? w / 2 :
just == JUST_RIGHT ||
just == JUST_RIGHT_TOP ||
just == JUST_RIGHT_BOTTOM ? w :
0;
int16_t use_y =
just == JUST_LEFT ||
just == JUST_CENTER ||
just == JUST_RIGHT ? h / 2 :
just == JUST_LEFT_BOTTOM ||
just == JUST_CENTER_BOTTOM ||
just == JUST_RIGHT_BOTTOM ? h :
0;
uint8_t use_datum;
switch (just)
{
case JUST_LEFT:
use_datum = ML_DATUM;
break;
case JUST_RIGHT:
use_datum = MR_DATUM;
break;
case JUST_LEFT_TOP:
use_datum = TL_DATUM;
break;
case JUST_CENTER_TOP:
use_datum = TC_DATUM;
break;
case JUST_RIGHT_TOP:
use_datum = TR_DATUM;
break;
case JUST_LEFT_BOTTOM:
use_datum = BL_DATUM;
break;
case JUST_CENTER_BOTTOM:
use_datum = BC_DATUM;
break;
case JUST_RIGHT_BOTTOM:
use_datum = BR_DATUM;
break;
case JUST_CENTER:
use_datum = MC_DATUM;
break;
}
// FONTS - were also poorly implemented in TFT with a bad API, so I
// #ifdef'd out any fonts included in TFT_eSPI but leave LOAD_GFXFF defined.
// The original GFX fonts will not be linked unless they are referenced.
// Created a new set of fonts in my fonts/ folder, at http://oleddisplay.squix.ch
// and added new ones that look good and make sense.
switch (font)
{
case FONT_TINY:
tft.setFreeFont(&Dialog_plain_14);
break;
case FONT_SMALL:
tft.setFreeFont(&Dialog_plain_16);
break;
case FONT_BIG:
tft.setFreeFont(&Dialog_plain_22);
break;
case FONT_MONO:
tft.setFreeFont(&FreeMonoBold8pt7b);
break;
case FONT_SYMBOL:
tft.setFreeFont(&mySymbolFont);
break;
default:
case FONT_NORMAL:
tft.setFreeFont(&Dialog_plain_18);
break;
}
// So now we can finally draw the text.
// Set the "viewport" to implement clipping
tft.setViewport(x,y,w,h);
tft.setTextDatum(use_datum);
tft.drawString(text, use_x, use_y);
tft.resetViewport();
}
void calibrate_my_tft()
{
#ifdef DEBUG_CALIBRATION
g_debug("Doing TFT Calibration");
#endif
tft.fillScreen(TFT_BLACK);
drawText(
"Touch corners as indicated",
JUST_CENTER,
FONT_NORMAL,
0,0,UI_SCREEN_WIDTH,UI_SCREEN_HEIGHT,
COLOR_WHITE,
COLOR_BLACK);
uint16_t t_caldata[5];
bool ok = tft.calibrateTouch(t_caldata, TFT_MAGENTA, TFT_BLACK, 15, 6000);
if (ok)
{
for (int i=0; i<5; i++) // sanity check
{
if (t_caldata[i] == 0 || t_caldata[i] > CALIBRATION_SANITY_VALUE)
{
g_debug("ABORTING calibration due to bogus calibration data");
return;
}
}
for (int i=0; i<5; i++)
calibration_data[i] = t_caldata[i];
#ifdef DEBUG_CALIBRATION
g_debug("writing TFT Calibration data: %d,%d,%d,%d,%d",
calibration_data[0],
calibration_data[1],
calibration_data[2],
calibration_data[3],
calibration_data[4]);
#endif
String filename = getCalibrationFilename();
File f = SPIFFS.open(filename.c_str(), "w");
if (f)
{
f.write((const unsigned char *)calibration_data, 14);
f.close();
}
else
{
#ifdef DEBUG_CALIBRATION
g_debug("WARNING: Could not open %s for writing. Using default calibration data",filename.c_str());
#endif
}
}
else
{
#ifdef DEBUG_CALIBRATION
g_debug("TFT Calibration Failed");
#endif
}
}
bool getCalibrationData()
{
bool ok = false;
String filename = getCalibrationFilename();
if (SPIFFS.exists(filename.c_str()))
{
File f = SPIFFS.open(filename.c_str(), "r");
if (f)
{
uint16_t t_caldata[5];
if (f.readBytes((char *)t_caldata, 14) == 14)
{
#ifdef DEBUG_CALIBRATION
g_debug("got TFT Calibration data: %d,%d,%d,%d,%d",
t_caldata[0],
t_caldata[1],
t_caldata[2],
t_caldata[3],
t_caldata[4]);
#endif
for (int i=0; i<5; i++) // sanity check
{
if (t_caldata[i] == 0 || t_caldata[i] > CALIBRATION_SANITY_VALUE)
{
g_debug("ABORTING read due to bogus calibration data");
return false;
}
}
for (int i=0; i<5; i++)
calibration_data[i] = t_caldata[i];
ok = 1;
}
f.close();
}
}
return ok;
}
void init_my_tft(
const char *driver,
int rotation )
{
my_tft_driver = driver;
my_tft_rotation = rotation;
tft.init();
tft.setRotation(my_tft_rotation);
if (!strcmp(my_tft_driver,"ILI9431_28") && my_tft_rotation == 2)
{
memcpy(calibration_data,calibration_data_28_1,5 * sizeof(int));
}
else if (!strcmp(my_tft_driver,"ILI9431_32") && my_tft_rotation == 3)
{
memcpy(calibration_data,calibration_data_32_3,5 * sizeof(int));
}
else if (!strcmp(my_tft_driver,"ILI9431_32") && my_tft_rotation == 1)
{
memcpy(calibration_data,calibration_data_32_1,5 * sizeof(int));
}
else
{
g_error("FluidNC_UI/myTFT.cpp - UNKNOWN DRIVER(%s) ROTATION(%d) combination",my_tft_driver,my_tft_rotation);
memcpy(calibration_data,calibration_data_32_3,5 * sizeof(int));
}
if (!getCalibrationData())
calibrate_my_tft();
tft.setTouch(calibration_data);
}