-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNT_Touchscreen_Cal_6Pts_BLA.ino
701 lines (563 loc) · 25.7 KB
/
RNT_Touchscreen_Cal_6Pts_BLA.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
/* Copyright (c) 2024 Rui Santos & Sara Santos
Random Nerd Tutorials - https://RandomNerdTutorials.com/esp32-lvgl-ebook/
Original program: 1_9_Touchscreen_Test.ino
THIS EXAMPLE WAS TESTED WITH THE FOLLOWING HARDWARE:
1) ESP32-2432S028R 2.8 inch 240×320 also known as the Cheap Yellow Display (CYD):
https://makeradvisor.com/tools/cyd-cheap-yellow-display-esp32-2432s028r/
SET UP INSTRUCTIONS: https://RandomNerdTutorials.com/cyd-lvgl/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
Modified by Robert (Chip) Fleming on 9 July 2024 to serve as a prototype for touchscreen calibration
Changes from Random Nerds Tutorials' 1_9_Touchscreen_Test.ino are Copyright (c) 2024, Robert F. Fleming III
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the “Software”), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following conditions:
You must include the above copyright notice and this permission notice in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This software is based in part on the Texas Instruments, Inc. application note titled:
"Calibration in touch-screen systems," available at https://www.ti.com/lit/an/slyt277/slyt277.pdf .
*/
/* Install the "lvgl" library version 9.X by kisvegabor to interface with the TFT Display - https://lvgl.io/
*** IMPORTANT: lv_conf.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials ***
*** Ensure the lv_conf.h file is in the ..\library folder, NOT in the ..\library\lvgl folder or a subfolder thereof ***
*** YOU MUST USE THE lv_conf.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM RANDOM NERD TUTORIALS ***
FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https://RandomNerdTutorials.com/cyd-lvgl/ or
https://RandomNerdTutorials.com/esp32-tft-lvgl/ */
#include <lvgl.h>
/* Install the "TFT_eSPI" library by Bodmer to interface with the TFT Display - https://github.com/Bodmer/TFT_eSPI
*** IMPORTANT: User_Setup.h available on the internet will probably NOT work with the examples available at Random Nerd Tutorials ***
*** YOU MUST USE THE User_Setup.h FILE PROVIDED IN THE LINK BELOW IN ORDER TO USE THE EXAMPLES FROM RANDOM NERD TUTORIALS ***
FULL INSTRUCTIONS AVAILABLE ON HOW CONFIGURE THE LIBRARY: https://RandomNerdTutorials.com/cyd-lvgl/ or
https://RandomNerdTutorials.com/esp32-tft-lvgl/ */
#include <TFT_eSPI.h>
// Install the "XPT2046_Touchscreen" library by Paul Stoffregen to use the Touchscreen -
// https://github.com/PaulStoffregen/XPT2046_Touchscreen - Note: this library doesn't require further configuration
#include <XPT2046_Touchscreen.h>
/* Use the Preferences library to store the touchscreen calibration coefficients */
#include <Preferences.h>
Preferences ts_cal_coeffs;
/* Note: Basic Linear Algebra library version 5.1 or above is required for ESP32
Define either USE_BLA or USE_MATRIX_MATH but not both. */
#define USE_BLA
#undef USE_MATRIX_MATH
#ifdef USE_BLA
/* use the Basic Linear Algebra library for TI appnote Equation 7 calculations.
Version 5.1 or above of the Basic Linear Algebra library is required. */
#include <BasicLinearAlgebra.h>
#endif
#ifdef USE_MATRIX_MATH
/* use the Matrix Math library for TI appnote Equation 7 calculations. */
#include <MatrixMath.h>
#endif
// Touchscreen pins
#define XPT2046_IRQ 36 // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25 // T_CLK
#define XPT2046_CS 33 // T_CS
unsigned long delay_start = 0;
#define DELAY_1S 1000
#define DELAY_5S 5000
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
constexpr int SCREEN_WIDTH = 320;
constexpr int SCREEN_HEIGHT = 240;
constexpr int SAMPLES_PER_POINT = 128;
String s;
int ts_points[6][2];
/* define the screen points where touch samples will be taken */
const int scr_points[6][2] = { {13, 11}, {20,220}, {167, 60}, {155, 180}, {300, 13}, {295, 225} };
struct point {
int x;
int y;
};
/* pS is a screen point; pT is a resistive touchscreen point */
struct point aS = {scr_points[0][0], scr_points[0][1] };
struct point bS = {scr_points[1][0], scr_points[1][1] };
struct point cS = {scr_points[2][0], scr_points[2][1] };
struct point dS = {scr_points[3][0], scr_points[3][1] };
struct point eS = {scr_points[4][0], scr_points[4][1] };
struct point fS = {scr_points[5][0], scr_points[5][1] };
struct point aT;
struct point bT;
struct point cT;
struct point dT;
struct point eT;
struct point fT;
/* coefficients for transforming the X and Y coordinates of the resistive touchscreen
to display coordinates... the ones with the "pref_" prefix are retrieved from NVS */
float alphaX, betaX, deltaX, alphaY, betaY, deltaY;
float pref_alphaX, pref_betaX, pref_deltaX, pref_alphaY, pref_betaY, pref_deltaY;
#define DRAW_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 10 * (LV_COLOR_DEPTH / 8))
uint32_t draw_buf[DRAW_BUF_SIZE / 4];
// If logging is enabled, it will inform the user about what is happening in the library
void log_print(lv_log_level_t level, const char * buf) {
LV_UNUSED(level);
Serial.println(buf);
Serial.flush();
}
// Declare function to get the Raw Touchscreen data
void touchscreen_read_pts(bool, bool, int, int);
/* Declare function to display a user instruction upon startup */
void lv_display_instruction(void);
/* Declare function to display crosshairs at given coordinates */
void display_crosshair(int, int);
/* Declare function to display 'X's at given coordinates */
void display_xs(int, int);
/* Declare function to compute the resistive touchscreen coordinates to display coordinates conversion coefficients */
void ts_calibration (
const point, const point,
const point, const point,
const point, const point,
const point, const point,
const point, const point,
const point, const point);
/* Declare function to save the resistive touchscreen coordinate frame to display frame conversion coefficients
in non-volatile storage */
void save_ts_cal_coefficients(void);
/* Declare function to retrieve the touchscreen to display equation coefficients from NVS */
void get_ts_cal_coefficients(void);
void gather_cal_data(void) {
//Function to draw the crosshairs and collect data
bool reset, finished;
int x_avg, y_avg;
for (int i = 0; i < 6; i++) {
lv_obj_clean(lv_scr_act());
//lv_draw_cross(i);
display_crosshair(scr_points[i][0], scr_points[i][1]);
reset = true;
x_avg = 0;
y_avg = 0;
touchscreen_read_pts(reset, &finished, &x_avg, &y_avg);
reset = false;
while (!finished) {
touchscreen_read_pts(reset, &finished, &x_avg, &y_avg);
/* found out the hard way that if I don't do this, the screen doesn't update */
lv_task_handler();
lv_tick_inc(10);
delay(10);
}
lv_obj_clean(lv_scr_act());
lv_task_handler();
lv_tick_inc(10);
delay(10);
ts_points[i][0] = x_avg;
ts_points[i][1] = y_avg;
String s = String("x_avg = " + String(x_avg) + " y_avg = " + String(y_avg) );
Serial.println(s);
delay(1500);
}
}
void compute_transformation_coefficients(void) {
aT = { ts_points[0][0], ts_points[0][1] };
bT = { ts_points[1][0], ts_points[1][1] };
cT = { ts_points[2][0], ts_points[2][1] };
dT = { ts_points[3][0], ts_points[3][1] };
eT = { ts_points[4][0], ts_points[4][1] };
fT = { ts_points[5][0], ts_points[5][1] };
/* compute the resisitve touchscreen to display coordinates conversion coefficients */
ts_calibration(aS, aT, bS, bT, cS, cT, dS, dT, eS, eT, fS, fT);
}
void check_calibration_results(void) {
int x_touch, y_touch, x_scr, y_scr, error;
/* print the stored coefficients to make sure they were actually stored */
get_ts_cal_coefficients();
s = String("Stored X: alpha = " + String(pref_alphaX, 3) + ", beta = " + String(pref_betaX, 3) + ", delta = " +
String(pref_deltaX, 3) );
Serial.println(s);
s = String("Stored Y: alpha = " + String(pref_alphaY, 3) + ", beta = " + String(pref_betaY, 3) + ", delta = " +
String(pref_deltaY, 3) );
Serial.println(s);
Serial.println();
/* if we did our job well, the screen points computed below should match
aS, bS, and cS defined near the top */
for (int i = 0; i < 6; i++) {
/* define some touch points and translate them to screen points */
x_touch = ts_points[i][0];
y_touch = ts_points[i][1];
/* here's the magic equation that uses the magic coefficients */
/* use it to convert resistive touchscreen points to display points */
x_scr = alphaX * x_touch + betaX * y_touch + deltaX;
y_scr = alphaY * x_touch + betaY * y_touch + deltaY;
display_crosshair(scr_points[i][0], scr_points[i][1]);
display_xs(x_scr, y_scr);
s = String("x_touch = " + String(x_touch) + " y_touch = " + String(y_touch) );
Serial.println(s);
s = String("x_scr = " + String(x_scr) + " y_scr = " + String(y_scr) );
Serial.println(s);
error = (int) sqrt( sq(x_scr - scr_points[i][0]) + sq(y_scr - scr_points[i][1]) );
s = String("error = " + String(error) + " pixel(s)");
Serial.println(s);
Serial.println();
}
}
void setup() {
String LVGL_Arduino = String("LVGL Library Version: ") + lv_version_major() + "." + lv_version_minor() +
"." + lv_version_patch();
Serial.begin(115200);
Serial.println(LVGL_Arduino);
// Start LVGL
lv_init();
// Register print function for debugging
lv_log_register_print_cb(log_print);
// Start the SPI for the touchscreen and init the touchscreen
touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
touchscreen.begin(touchscreenSPI);
// Set the Touchscreen rotation in landscape mode
// Note: in some displays, the touchscreen might be upside down, so you might need to set the rotation to 1:
touchscreen.setRotation(1);
touchscreen.setRotation(3);
// Create a display object
lv_display_t * disp;
// Initialize the TFT display using the TFT_eSPI library
disp = lv_tft_espi_create(SCREEN_WIDTH, SCREEN_HEIGHT, draw_buf, sizeof(draw_buf));
lv_display_instruction();
delay_start = millis();
while ((millis() - delay_start) < DELAY_5S) {
lv_task_handler();
lv_tick_inc(10);
delay(10);
}
/* display crosshairs and have the user tap on them until enough samples are gathered */;
gather_cal_data();
/* crunch the numbers to compute the touchscreen to display coordinate transformation equation coefficients */
compute_transformation_coefficients();
/* save the resisitve touchscreen to display coordinates conversion coefficients in NVS */
save_ts_cal_coefficients();
/* display stored correction data and display generated vs measured screen points */
check_calibration_results();
}
void loop() {
lv_task_handler(); // let the GUI do its work
lv_tick_inc(5); // tell LVGL how much time has passed
delay(5); // let this time pass
}
/* function to read a number of points from the resistive touchscreen as the user taps
a stylus on displayed crosshairs. Once the samples have been collected, they are
filtered to remove outliers and then averaged and the average x & y are returned to the caller. */
void touchscreen_read_pts(bool reset, bool *finished, int *x_avg, int *y_avg) {
/* nr_samples = samples taken; good_samples = samples used, samples = array of 100 samples */
static int i, nr_samples, good_samples;
static uint32_t samples[SAMPLES_PER_POINT][2];
/* coordinates to shift and rotate touch screen coordinates to display coordinates */
static float mean_x, mean_y, filt_mean_x, filt_mean_y, stdev_x, stdev_y;
/* caller resets the sample run at each new displayed crosshair */
if (reset) {
nr_samples = 0;
*x_avg = 0;
*y_avg = 0;
*finished = false;
}
// Checks if Touchscreen was touched, and prints X, Y
if(touchscreen.tirqTouched() && touchscreen.touched()) {
// Get Touchscreen points
TS_Point p = touchscreen.getPoint();
samples[nr_samples][0] = p.x;
samples[nr_samples][1] = p.y;
s = String("x, y = " + String(samples[nr_samples][0]) + ", " + String(samples[nr_samples][1]) );
Serial.println(s);
nr_samples++;
/* first compute the x & y averages of all the samples */
if (nr_samples >= SAMPLES_PER_POINT) {
mean_x = 0;
mean_y = 0;
for (i = 0; i < SAMPLES_PER_POINT; i++) {
mean_x += (float)samples[i][0];
mean_y += (float)samples[i][1];
}
mean_x = mean_x / (float)nr_samples;
mean_y = mean_y / (float)nr_samples;
s = String("Unfiltered values: mean_x = " + String(mean_x) + ", mean_y = " + String(mean_y));
Serial.println(s);
/* now compute the x & y standard deviations of all the samples */
stdev_x = 0;
stdev_y = 0;
for (i = 0; i < SAMPLES_PER_POINT; i++) {
stdev_x += sq((float)samples[i][0] - mean_x);
stdev_y += sq((float)samples[i][1] - mean_y);
}
stdev_x = sqrt(stdev_x / (float)nr_samples);
stdev_y = sqrt(stdev_y / (float)nr_samples);
s = String("stdev_x = " + String(stdev_x) + ", stdev_y = " + String(stdev_y));
Serial.println(s);
/* now average the samples that are less than one standard deviation from the mean */
/* this filtering is called "outlier rejection," and is included because outliers were observed in testing */
good_samples = 0;
filt_mean_x = 0;
filt_mean_y = 0;
for (i = 0; i < SAMPLES_PER_POINT; i++) {
if ((abs((float)samples[i][0] - mean_x) < stdev_x) && (abs((float)samples[i][1] - mean_y) < stdev_y)) {
filt_mean_x += (float)samples[i][0];
filt_mean_y += (float)samples[i][1];
good_samples++;
}
}
s = String("Good samples = " + String(good_samples));
Serial.println(s);
filt_mean_x = filt_mean_x / (float)good_samples;
filt_mean_y = filt_mean_y / (float)good_samples;
s = String("Filtered values: filt_mean_x = " + String(filt_mean_x) + ", filt_mean_y = " + String(filt_mean_y));
Serial.println(s);
Serial.println();
*x_avg = (int)mean_x;
*y_avg = (int)mean_y;
*finished = true;
}
}
else {
// nada
}
}
/* Function to display a user instruction on startup */
void lv_display_instruction(void) {
// Create a text label aligned center: https://docs.lvgl.io/master/widgets/label.html
lv_obj_t * text_label = lv_label_create(lv_screen_active());
lv_label_set_text(text_label, "Tap each crosshair until it disappears.");
lv_obj_align(text_label, LV_ALIGN_CENTER, 0, 0);
// Set font type and font size. More information: https://docs.lvgl.io/master/overview/font.html
static lv_style_t style_text_label;
lv_style_init(&style_text_label);
lv_style_set_text_font(&style_text_label, &lv_font_montserrat_14);
lv_obj_add_style(text_label, &style_text_label, 0);
}
/* function to display crosshairs at given coordinates */
void display_crosshair(int x, int y) {
static lv_point_precise_t h_line_points[] = { {0, 0}, {10, 0} };
static lv_point_precise_t v_line_points[] = { {0, 0}, {0, 10} };
static lv_style_t style_line;
lv_style_init(&style_line);
lv_style_set_line_width(&style_line, 2);
lv_style_set_line_color(&style_line, lv_palette_main(LV_PALETTE_LIGHT_BLUE));
lv_style_set_line_rounded(&style_line, true);
// Create crosshair lines
lv_obj_t* crosshair_h = lv_line_create(lv_screen_active());
lv_obj_t* crosshair_v = lv_line_create(lv_screen_active());
lv_line_set_points(crosshair_h, h_line_points, 2); // Set the coordinates for the crosshair_h line
lv_obj_add_style(crosshair_h, &style_line, 0);
lv_line_set_points(crosshair_v, v_line_points, 2); // Set the coordinates for the crosshair_h line
lv_obj_add_style(crosshair_v, &style_line, 0);
lv_obj_set_pos(crosshair_h, x - 5, y);
lv_obj_set_pos(crosshair_v, x, y - 5);
}
/* function to display 'X's at given coordinates */
void display_xs(int x, int y) {
static lv_point_precise_t u_line_points[] = { {0, 0}, {10, 10} }; //upsloping
static lv_point_precise_t d_line_points[] = { {0, 10}, {10, 0} }; //downsloping
static lv_style_t style_line;
lv_style_init(&style_line);
lv_style_set_line_width(&style_line, 2);
lv_style_set_line_color(&style_line, lv_palette_main(LV_PALETTE_YELLOW));
lv_style_set_line_rounded(&style_line, true);
// Create crosshair lines
lv_obj_t* x_u = lv_line_create(lv_screen_active());
lv_obj_t* x_d = lv_line_create(lv_screen_active());
lv_line_set_points(x_u, u_line_points, 2); // Set the coordinates for the upsloping line
lv_obj_add_style(x_u, &style_line, 0);
lv_line_set_points(x_d, d_line_points, 2); // Set the coordinates for the downsloping line
lv_obj_add_style(x_d, &style_line, 0);
lv_obj_set_pos(x_u, x - 5, y - 5);
lv_obj_set_pos(x_d, x - 5, y - 5);
}
/* function to compute the transformation equation coefficients from resistive touchscreen
coordinates to display coordinates...
This was based on the Texas Instruments appnote at:
https://www.ti.com/lit/an/slyt277/slyt277.pdf
It implements Equation 7 of that appnote, which computes a least-squares set of coefficients. */
void ts_calibration (
const point aS, const point aT,
const point bS, const point bT,
const point cS, const point cT,
const point dS, const point dT,
const point eS, const point eT,
const point fS, const point fT) {
bool defined;
uint16_t screenWidth, screenHeight;
s = String("aS = " + String(aS.x) + ", " + String(aS.y));
Serial.println(s);
s = String("bS = " + String(bS.x) + ", " + String(bS.y));
Serial.println(s);
s = String("cS = " + String(cS.x) + ", " + String(cS.y));
Serial.println(s);
s = String("dS = " + String(dS.x) + ", " + String(dS.y));
Serial.println(s);
s = String("eS = " + String(eS.x) + ", " + String(eS.y));
Serial.println(s);
s = String("fS = " + String(fS.x) + ", " + String(fS.y));
Serial.println(s);
s = String("aT = " + String(aT.x) + ", " + String(aT.y));
Serial.println(s);
s = String("bT = " + String(bT.x) + ", " + String(bT.y));
Serial.println(s);
s = String("cT = " + String(cT.x) + ", " + String(cT.y));
Serial.println(s);
s = String("eT = " + String(dT.x) + ", " + String(dT.y));
Serial.println(s);
s = String("eT = " + String(eT.x) + ", " + String(eT.y));
Serial.println(s);
s = String("fT = " + String(fT.x) + ", " + String(fT.y));
Serial.println(s);
Serial.println();
struct f_point {
float x;
float y;
};
struct f_point faS, fbS, fcS, fdS, feS, ffS, faT, fbT, fcT, fdT, feT, ffT;
faS.x = (float)aS.x; fbS.x = (float)bS.x; fcS.x = (float)cS.x, fdS.x = (float)dS.x; feS.x = (float)eS.x; ffS.x = (float)fS.x;
faS.y = (float)aS.y; fbS.y = (float)bS.y; fcS.y = (float)cS.y; fdS.y = (float)dS.y; feS.y = (float)eS.y; ffS.y = (float)fS.y;
faT.x = (float)aT.x; fbT.x = (float)bT.x; fcT.x = (float)cT.x; fdT.x = (float)dT.x; feT.x = (float)eT.x; ffT.x = (float)fT.x;
faT.y = (float)aT.y; fbT.y = (float)bT.y; fcT.y = (float)cT.y; fdT.y = (float)dT.y; feT.y = (float)eT.y; ffT.y = (float)fT.y;
#ifdef USE_BLA
BLA::Matrix<6, 3> A;
BLA::Matrix<3, 6> transA;
BLA::Matrix<6> X;
BLA::Matrix<6> Y;
BLA::Matrix<3, 3> B;
BLA::Matrix<3, 6> C;
BLA::Matrix<3> X_coeff;
BLA::Matrix<3> Y_coeff;
A = { faT.x, faT.y, 1,
fbT.x, fbT.y, 1,
fcT.x, fcT.y, 1,
fdT.x, fdT.y, 1,
feT.x, feT.y, 1,
ffT.x, ffT.y, 1 };
X = { faS.x,
fbS.x,
fcS.x,
fdS.x,
feS.x,
ffS.x };
Y = { faS.y,
fbS.y,
fcS.y,
fdS.y,
feS.y,
ffS.y };
/* Now compute [AtA]^-1 * AtA * X and [AtA]^-1 * AtA * Y */
Serial.print ("A = ");
Serial.println(A);
transA = ~A;
Serial.print ("transA = ");
Serial.println(transA);
B = transA * A;
Serial.print ("Before inversion, B = ");
Serial.println(B);
if (!Invert(B) ) {
Serial.println("Singular matrix in computation of inverse of B = transA*A!");
}
Serial.print ("After inversion, B = ");
Serial.println(B);
C = B * transA;
Serial.print ("C = ");
Serial.println(C);
X_coeff = C * X;
Y_coeff = C * Y;
/* transfer the X and Y coefficients to the Greek-letter variables
Note that BLA requires round brackets while MatrixMath requires square ones */
alphaX = X_coeff(0); betaX = X_coeff(1); deltaX = X_coeff(2);
alphaY = Y_coeff(0); betaY = Y_coeff(1); deltaY = Y_coeff(2);
#endif
#ifdef USE_MATRIX_MATH
mtx_type A[6][3];
mtx_type A_t[3][6];
mtx_type A_tA[3][3];
mtx_type A_tA_save[3][3];
mtx_type Inv_Test[3][3];
mtx_type At_A_inv_A_t[3][3];
mtx_type X[6];
mtx_type Y[6];
mtx_type X_coeff[3];
mtx_type Y_coeff[3];
/* set up the A matrix and the X and Y vectors per Eq. 6 of the TI appnote */
A[0][0] = faT.x; A[0][1] = faT.y; A[0][2] = 1;
A[1][0] = fbT.x; A[1][1] = fbT.y; A[1][2] = 1;
A[2][0] = fcT.x; A[2][1] = fcT.y; A[2][2] = 1;
A[3][0] = fdT.x; A[3][1] = fdT.y; A[3][2] = 1;
A[4][0] = feT.x; A[4][1] = feT.y; A[4][2] = 1;
A[5][0] = ffT.x; A[5][1] = ffT.y; A[5][2] = 1;
X[0] = faS.x; Y[0] = faS.y;
X[1] = fbS.x; Y[1] = fbS.y;
X[2] = fcS.x; Y[2] = fcS.y;
X[3] = fdS.x; Y[3] = fdS.y;
X[4] = feS.x; Y[4] = feS.y;
X[5] = ffS.x; Y[5] = ffS.y;
Matrix.Print((mtx_type*)A, 6, 3, "A");
Matrix.Print((mtx_type*)X, 6, 1, "X");
Matrix.Print((mtx_type*)Y, 6, 1, "Y");
/* Now compute [AtA]^-1 * AtA * X and [AtA]^-1 * AtA * Y */
Matrix.Transpose((mtx_type*)A, 6, 3, (mtx_type*)A_t);
Matrix.Print((mtx_type*)A_t, 3, 6, "A_t");
Matrix.Multiply((mtx_type*)A_t, (mtx_type*)A, 3, 6, 3, (mtx_type*)A_tA);
Matrix.Print((mtx_type*)A_tA, 3, 3, "A_tA");
Matrix.Copy((mtx_type*)A_tA, 3, 3, (mtx_type*)A_tA_save);
Matrix.Print((mtx_type*)A_tA_save, 3, 3, "A_tA_save");
Matrix.Invert((mtx_type*)A_tA, 3);
Matrix.Print((mtx_type*)A_tA, 3, 3, "A_tA_inv");
Serial.println("\nIf the inverted matrix times the original matrix = the identity matrix, all is well...");
Matrix.Multiply((mtx_type*)A_tA, (mtx_type*)A_tA_save, 3, 3, 3, (mtx_type*)Inv_Test);
Matrix.Print((mtx_type*)Inv_Test, 3, 3, "Inv_Test");
Matrix.Multiply((mtx_type*)A_tA, (mtx_type*)A_t, 3, 3, 6, (mtx_type*)At_A_inv_A_t);
Matrix.Print((mtx_type*)At_A_inv_A_t, 3, 6, "At_A_inv_A_t");
Matrix.Multiply((mtx_type*)At_A_inv_A_t, (mtx_type*)X, 3, 6, 1, (mtx_type*)X_coeff);
Matrix.Multiply((mtx_type*)At_A_inv_A_t, (mtx_type*)Y, 3, 6, 1, (mtx_type*)Y_coeff);
Matrix.Print((mtx_type*)X_coeff, 3, 1, "X_coeff");
Matrix.Print((mtx_type*)Y_coeff, 3, 1, "Y_coeff");
/* transfer the X and Y coefficients to the Greek-letter variables
Note that MatrixMath requires square brackets while BLA requires round brackets*/
alphaX = X_coeff[0]; betaX = X_coeff[1]; deltaX = X_coeff[2];
alphaY = Y_coeff[0]; betaY = Y_coeff[1]; deltaY = Y_coeff[2];
#endif
Serial.println();
s = String("Computed X: alpha = " + String(alphaX, 3) + ", beta = " + String(betaX, 3) + ", delta = " + String(deltaX, 3) );
Serial.println(s);
s = String("Computed Y: alpha = " + String(alphaY, 3) + ", beta = " + String(betaY, 3) + ", delta = " + String(deltaY, 3) );
Serial.println(s);
}
/* save the resistive touchscreen coordinate frame to display frame conversion coefficients in non-volatile storage */
void save_ts_cal_coefficients(void) {
/* start the NVS storage functionality in read/write mode */
if ( !ts_cal_coeffs.begin("ts_cal_coeff_ns", false) ) {
Serial.println("ts_cal_coeffs initialization failed in save_ts_cal_coeffs!");
}
if ( !ts_cal_coeffs.clear() ) {
Serial.println("ts_cal_coeffs erasure failed!");
}
int byte_count = 0;
byte_count += ts_cal_coeffs.putFloat("alphaX", alphaX);
byte_count += ts_cal_coeffs.putFloat("betaX", betaX);
byte_count += ts_cal_coeffs.putFloat("deltaX", deltaX);
byte_count += ts_cal_coeffs.putFloat("alphaY", alphaY);
byte_count += ts_cal_coeffs.putFloat("betaY", betaY);
byte_count += ts_cal_coeffs.putFloat("deltaY", deltaY);
/* note that the espressif documentation says eight bytes are stored for floats,
but it's actually only four, corresponding to the size of the variable in bytes...
see https://docs.espressif.com/projects/arduino-esp32/en/latest/tutorials/preferences.html */
if (byte_count < 24) {
Serial.println("Problem saving touchscreen calibration coefficients!");
Serial.println(byte_count);
}
ts_cal_coeffs.end();
}
/* function to retrieve the touchscreen to display equation coefficients from NVS */
void get_ts_cal_coefficients(void) {
/* start the NVS storage functionality in read-only mode */
if ( !ts_cal_coeffs.begin("ts_cal_coeff_ns", true) ) {
Serial.println("ts_cal_coeffs initialization failed in get_ts_cal_coeffs!");
}
pref_alphaX = ts_cal_coeffs.getFloat("alphaX");
pref_betaX = ts_cal_coeffs.getFloat("betaX");
pref_deltaX = ts_cal_coeffs.getFloat("deltaX");
pref_alphaY = ts_cal_coeffs.getFloat("alphaY");
pref_betaY = ts_cal_coeffs.getFloat("betaY");
pref_deltaY = ts_cal_coeffs.getFloat("deltaY");
ts_cal_coeffs.end();
}