-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouch.hpp
393 lines (320 loc) · 11.7 KB
/
touch.hpp
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
/*================================================================================
* Touch event manager
*================================================================================*/
/*--------------------------------------------------------------------------------
* Configuration data for calibration of touch panel
*--------------------------------------------------------------------------------*/
typedef struct TouchConfig {
// Member variables
uint16_t cal[8];
int8_t offset[2];
// Comparison operator
bool operator >= (TouchConfig &RHS) {
return !bcmp(cal, RHS.cal, sizeof(cal));
}
bool operator <= (TouchConfig &RHS) {
return (offset[0] != RHS.offset[0]) || (offset[1] != RHS.offset[1]);
}
bool operator != (TouchConfig &RHS) {
return bcmp(cal, RHS.cal, sizeof(cal)) || (offset[0] != RHS.offset[0]) || (offset[1] != RHS.offset[1]);
}
} TouchConfig_t;
/*--------------------------------------------------------------------------------
* Touch panel calibration parameters
* When 'USE_PREFERENCES' is 'false', the calibration result must be embedded.
*--------------------------------------------------------------------------------*/
TouchConfig_t tch_cnf = {
#if defined (LOVYANGFX_HPP_)
// LovyanGFX
.cal = { 0, 0, 0, 0, 0, 0, 0, 0 },
#elif defined (_XPT2046_SCREENPOINT_H_)
// XPT2046_Touchscreen
.cal = { 0, 0, 0, 0, 0, },
#else
// TFT_eSPI
.cal = { 0, 0, 0, 0, 0, },
#endif
.offset = { 0, },
};
/*--------------------------------------------------------------------------------
* Event definition
*--------------------------------------------------------------------------------*/
#define PERIOD_DEBOUNCE 25 // [msec]
#define PERIOD_TOUCHED 50 // [msec]
#define PERIOD_TAP2 200 // [msec]
#define PERIOD_CLEAR_EVENT 100 // [msec]
typedef enum {
EVENT_NONE = (0x00), // considered as 'HIGH'
EVENT_RISING = (0x01), // touch --> untouch
EVENT_FALLING = (0x02), // untouch --> touch
EVENT_TOUCHED = (0x04), // touch --> touch
EVENT_TAP2 = (0x08), // double tap
EVENT_EXPAND = (0x10), // not a touch event, but define for convenience
// alias
EVENT_INIT = (EVENT_NONE),
EVENT_UP = (EVENT_RISING),
EVENT_DOWN = (EVENT_FALLING),
EVENT_DRAG = (EVENT_FALLING | EVENT_TOUCHED),
EVENT_TAP = (EVENT_FALLING | EVENT_RISING),
EVENT_CLICK = (EVENT_FALLING | EVENT_RISING),
EVENT_CHANGE = (EVENT_FALLING | EVENT_RISING),
EVENT_SELECT = (EVENT_FALLING | EVENT_TAP2),
EVENT_ALL = (EVENT_FALLING | EVENT_RISING | EVENT_TOUCHED),
EVENT_WATCH = (EVENT_EXPAND), // watch events on a specific screen
EVENT_SHOW = (EVENT_EXPAND), // show something on a specific screen
} Event_t;
typedef struct Touch {
Event_t event; // Detected event
uint16_t x, y; // The coordinates where the event fired
} Touch_t;
/*--------------------------------------------------------------------------------
* Function prototyping
*--------------------------------------------------------------------------------*/
bool touch_setup(void);
bool touch_event(Touch_t &touch);
void touch_clear(void);
void touch_calibrate(TouchConfig_t *config);
bool touch_save(TouchConfig_t *config);
bool touch_load(TouchConfig_t *config);
/*--------------------------------------------------------------------------------
* Simple touch point correction
*--------------------------------------------------------------------------------*/
extern uint16_t lcd_width;
extern uint16_t lcd_height;
/*--------------------------------------------------------------------------------
* Instance of XPT2046_Touchscreen library
*--------------------------------------------------------------------------------*/
#if defined (_XPT2046_Touchscreen_h_)
#include "boards/XPT2046_ScreenPoint.h"
static SPIClass sp_spi = SPIClass(TOUCH_SPI_BUS);
static XPT2046_ScreenPoint sp(TOUCH_CS, TOUCH_IRQ);
#endif
#if defined (XPT2046_Bitbang_h)
#include "boards/XPT2046_ScreenPoint.h"
static XPT2046_ScreenPoint sp(TOUCH_MOSI, TOUCH_MISO, TOUCH_CLK, TOUCH_CS);
#endif
/*--------------------------------------------------------------------------------
* Setup touch manager
*--------------------------------------------------------------------------------*/
bool touch_setup(void) {
#if defined (_XPT2046_Touchscreen_h_)
sp_spi.begin(TOUCH_CLK, TOUCH_MISO, TOUCH_MOSI, TOUCH_CS);
sp.begin(sp_spi, lcd_width, lcd_height, SCREEN_ROTATION);
#endif
#if defined (XPT2046_Bitbang_h)
sp.begin(lcd_width, lcd_height, SCREEN_ROTATION);
#endif
#if USE_PREFERENCES
// Load calibration parameters from FLASH
if (touch_load(&tch_cnf) == false) {
touch_calibrate(&tch_cnf);
touch_save(&tch_cnf);
}
#else
if (tch_cnf.cal[0] == 0) {
touch_calibrate(&tch_cnf);
}
#endif
#if defined (LOVYANGFX_HPP_)
// https://github.com/lovyan03/LovyanGFX/discussions/539
GFX_EXEC(setTouchCalibrate(tch_cnf.cal));
return true;
#elif defined (_XPT2046_SCREENPOINT_H_)
sp.setTouch(static_cast<const uint16_t*>(tch_cnf.cal));
return true;
#else // defined (_TFT_eSPIH_)
// https://github.com/Bodmer/TFT_eSPI/tree/master/examples/Generic/Touch_calibrate
GFX_EXEC(setTouch(tch_cnf.cal));
return true;
#endif
}
/*--------------------------------------------------------------------------------
* Touch event manager
*--------------------------------------------------------------------------------*/
bool touch_event(Touch_t &touch) {
uint32_t time = millis();
static uint32_t prev_time;
static uint16_t x, y;
static bool prev_stat;
static uint8_t count;
Event_t event = EVENT_NONE;
#if defined (_XPT2046_SCREENPOINT_H_)
bool stat = sp.getTouch(&x, &y);
#else // LovyanGFX || TFT_eSPI
bool stat = GFX_EXEC(getTouch(&x, &y));
#endif // _XPT2046_SCREENPOINT_H_
// when state changes, check again after a certain period of time
uint32_t dt = time - prev_time;
if (stat != prev_stat) {
if (dt < PERIOD_DEBOUNCE) {
return false;
} else {
// update the time when state changes
prev_time = time;
// reset double tap counter
if (dt > PERIOD_TAP2 || count >= 4) {
count = 0;
}
}
}
// untouch --> touch
if (prev_stat == false && stat == true) {
event = EVENT_FALLING;
count = (count == 0 ? count + 1 : (dt <= PERIOD_TAP2 ? count + 1 : 0));
} else
// touch --> untouch
if (prev_stat == true && stat == false) {
event = EVENT_RISING;
count = dt <= PERIOD_TAP2 ? count + 1 : 0;
} else
// touch --> touch
if (prev_stat == true && stat == true) {
event = EVENT_TOUCHED;
}
// update state
prev_stat = stat;
if (event != EVENT_NONE) {
if (stat) {
x += tch_cnf.offset[0];
y += tch_cnf.offset[1];
x = constrain(x, 0, lcd_width - 1);
y = constrain(y, 0, lcd_height - 1);
}
// TAP2 = FALLING --> RISING --> FALLING --> RISING
touch.event = (Event_t)(event | (count >= 4 ? EVENT_TAP2 : EVENT_NONE));
touch.x = x;
touch.y = y;
// DBG_EXEC(printf("event: %d, x: %d, y: %d, dt: %d, count: %d\n", touch.event, x, y, dt, count));
return true;
}
return false;
}
/*--------------------------------------------------------------------------------
* Wait until there are no more touch events
*--------------------------------------------------------------------------------*/
void touch_clear(void) {
Touch_t touch;
delay(PERIOD_CLEAR_EVENT);
while(touch_event(touch));
}
/*--------------------------------------------------------------------------------
* Calibrating the touch panel
*--------------------------------------------------------------------------------*/
void touch_calibrate(TouchConfig_t *config) {
if (!Serial) {
Serial.begin(115200);
}
#if defined (_XPT2046_SCREENPOINT_H_)
extern TFT_eSPI tft;
sp.calibrateTouch(config->cal, &tft, TFT_WHITE, TFT_BLACK);
printf("\n// XPT2046_Touchscreen\n");
printf(".cal = { ");
for (int i = 0; i < 5; ++i) {
printf("%d", config->cal[i]);
printf(i < 4 ? ", " : ", 0, },\n");
}
#elif defined (LOVYANGFX_HPP_)
// https://github.com/lovyan03/LovyanGFX/tree/master/examples/HowToUse/2_user_setting
GFX_EXEC(clear(0));
GFX_EXEC(setTextSize(2));
GFX_EXEC(setTextDatum(textdatum_t::middle_center));
GFX_EXEC(drawString("touch the arrow marker.", lcd_width >> 1, lcd_height >> 1));
GFX_EXEC(setTextDatum(textdatum_t::top_left));
GFX_EXEC(calibrateTouch(config->cal, TFT_WHITE, TFT_BLACK, std::max(lcd_width, lcd_height) >> 3));
printf("\n// LovyanGFX\n");
printf(".cal = { ");
for (int i = 0; i < 8; ++i) {
printf("%d", config->cal[i]);
printf(i < 7 ? ", " : " },\n");
}
#elif defined (_TFT_eSPIH_)
// https://github.com/Bodmer/TFT_eSPI/tree/master/examples/Generic/Touch_calibrate
GFX_EXEC(fillScreen(TFT_BLACK));
GFX_EXEC(setCursor(20, 20));
GFX_EXEC(setTextSize(2));
GFX_EXEC(setTextColor(TFT_WHITE, TFT_BLACK));
GFX_EXEC(println("Touch corners in order"));
GFX_EXEC(calibrateTouch(config->cal, TFT_MAGENTA, TFT_BLACK, 15));
printf("\n// TFT_eSPI\n");
printf(".cal = { ");
for (int i = 0; i < 5; ++i) {
printf("%d", config->cal[i]);
printf(i < 4 ? ", " : ", 0, },\n");
}
#endif
// clear offset
config->offset[0] = config->offset[1] = 0;
}
/*--------------------------------------------------------------------------------
* Saving and loading calibration configurations using preferences.h
* https://docs.espressif.com/projects/arduino-esp32/en/latest/tutorials/preferences.html
* https://github.com/espressif/arduino-esp32/tree/master/libraries/Preferences
*--------------------------------------------------------------------------------*/
#include <Preferences.h>
#define RW_MODE false
#define RO_MODE true
#define INIT_KEY "initialized"
#if defined (_XPT2046_SCREENPOINT_H_)
#define PREF_KEY "XPT2046"
#elif defined (LOVYANGFX_HPP_)
#define PREF_KEY "LovyanGFX"
#elif defined (_TFT_eSPIH_)
#define PREF_KEY "TFT_eSPI"
#endif
bool touch_save(TouchConfig_t *config) {
#if USE_PREFERENCES
Preferences touchPref;
if (touchPref.begin(PREF_KEY, RW_MODE) == false) {
DBG_EXEC(printf("Preferences: begin(%s) failed.\n", PREF_KEY));
return false;
} else {
DBG_EXEC(printf("Preferences: found %s.\n", PREF_KEY));
}
if (touchPref.putBytes("cal", static_cast<const void*>(config->cal), sizeof(config->cal)) != sizeof(config->cal)) {
DBG_EXEC(printf("Preferences: putBytes(cal) failed.\n"));
touchPref.end();
return false;
}
if (touchPref.putBytes("offset", static_cast<const void*>(config->offset), sizeof(config->offset)) != sizeof(config->offset)) {
DBG_EXEC(printf("Preferences: putBytes(offset) failed.\n"));
touchPref.end();
return false;
}
if (touchPref.putBool(INIT_KEY, true) != sizeof(true)) {
DBG_EXEC(printf("Preferences: putBool(%s) failed.\n", INIT_KEY));
touchPref.end();
return false;
}
touchPref.end();
#endif
return true;
}
bool touch_load(TouchConfig_t *config) {
#if USE_PREFERENCES
Preferences touchPref;
TouchConfig_t c;
if (touchPref.begin(PREF_KEY, RO_MODE) == false) {
DBG_EXEC(printf("Preferences: %s does not exist.\n", PREF_KEY));
return false;
}
// Check if it already exists
if (touchPref.isKey(INIT_KEY) == false) {
DBG_EXEC(printf("Preferences: %s does not exist.\n", INIT_KEY));
touchPref.end();
return false;
}
if (touchPref.getBytes("cal", static_cast<void*>(&c.cal), sizeof(c.cal)) != sizeof(c.cal)) {
DBG_EXEC(printf("Preferences: getBytes(cal) failed.\n"));
touchPref.end();
return false;
}
if (touchPref.getBytes("offset", static_cast<void*>(&c.offset), sizeof(c.offset)) != sizeof(c.offset)) {
DBG_EXEC(printf("Preferences: getBytes(offset) failed.\n"));
touchPref.end();
return false;
}
*config = c;
touchPref.end();
#endif
return true;
}