-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLGFXTouchPanel.cpp
More file actions
222 lines (207 loc) · 7.39 KB
/
Copy pathLGFXTouchPanel.cpp
File metadata and controls
222 lines (207 loc) · 7.39 KB
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
#include "LGFXTouchPanel.h"
#include "DekiLogSystem.h"
#include "DekiEngine.h"
#include "SceneSystem.h"
#include "DekiInput.h" // from deki-input
#include "DekiI2C.h" // from deki-i2c
#include "IDekiI2C.h" // from deki-i2c
#if defined(ESP32)
#include <LovyanGFX.hpp>
#include "LGFXDisplayPanel.h"
#include "LovyanGFXTouch.h"
#include "esp_log.h"
static const char* TAG = "LGFXTouch";
void LGFXTouchPanel::Setup(SetupCallback onComplete)
{
ESP_LOGI(TAG, "Setting up touch (driver=%d)", static_cast<int>(driverType));
DEKI_LOG_INFO("LGFXTouchPanel: Setting up touch panel (driver=%d)", static_cast<int>(driverType));
lgfx::LGFX_Device* gfxDevice = LGFXDisplayPanel::GetLGFXDevice();
if (!gfxDevice)
{
DEKI_LOG_ERROR("LGFXTouchPanel: No LGFX device available (LGFXDisplayPanel not setup yet?)");
onComplete(false);
return;
}
// Resolve shared I2C bus (only for capacitive drivers; XPT2046 is SPI).
int bus_sda = -1;
int bus_scl = -1;
int bus_freq = 0;
const bool is_i2c_driver =
driverType == TouchDriverType::FT5x06 ||
driverType == TouchDriverType::GT911 ||
driverType == TouchDriverType::CST816S;
if (is_i2c_driver)
{
IDekiI2C* bus = DekiI2C::GetBus(i2c_port);
if (!bus)
{
DEKI_LOG_ERROR("LGFXTouchPanel: no I2C bus on port %d — add I2CBusComponent before LGFXTouchPanel in boot scene", (int)i2c_port);
onComplete(false);
return;
}
bus_sda = bus->GetSdaPin();
bus_scl = bus->GetSclPin();
bus_freq = bus->GetFrequencyHz();
}
lgfx::ITouch* touch = nullptr;
// Create the appropriate touch driver
switch (driverType)
{
case TouchDriverType::FT5x06:
{
auto* t = new lgfx::Touch_FT5x06();
auto cfg = t->config();
cfg.x_min = x_min;
cfg.x_max = x_max;
cfg.y_min = y_min;
cfg.y_max = y_max;
cfg.pin_int = pin_int;
cfg.pin_rst = pin_rst;
cfg.pin_sda = bus_sda;
cfg.pin_scl = bus_scl;
cfg.i2c_addr = 0x38;
cfg.i2c_port = i2c_port;
cfg.freq = bus_freq;
cfg.bus_shared = true;
cfg.offset_rotation = static_cast<uint8_t>(offset_rotation);
t->config(cfg);
touch = t;
DEKI_LOG_INFO("LGFXTouchPanel: FT5x06 on I2C port %d (SDA=%d SCL=%d freq=%d)", (int)i2c_port, bus_sda, bus_scl, bus_freq);
break;
}
case TouchDriverType::GT911:
{
auto* t = new lgfx::Touch_GT911();
auto cfg = t->config();
cfg.x_min = x_min;
cfg.x_max = x_max;
cfg.y_min = y_min;
cfg.y_max = y_max;
cfg.pin_int = pin_int;
cfg.pin_rst = pin_rst;
cfg.pin_sda = bus_sda;
cfg.pin_scl = bus_scl;
cfg.i2c_addr = 0x5D;
cfg.i2c_port = i2c_port;
cfg.freq = bus_freq;
cfg.bus_shared = true;
cfg.offset_rotation = static_cast<uint8_t>(offset_rotation);
t->config(cfg);
touch = t;
DEKI_LOG_INFO("LGFXTouchPanel: GT911 on I2C port %d (SDA=%d SCL=%d freq=%d)", (int)i2c_port, bus_sda, bus_scl, bus_freq);
break;
}
case TouchDriverType::CST816S:
{
auto* t = new lgfx::Touch_CST816S();
auto cfg = t->config();
cfg.x_min = x_min;
cfg.x_max = x_max;
cfg.y_min = y_min;
cfg.y_max = y_max;
cfg.pin_int = pin_int;
cfg.pin_rst = pin_rst;
cfg.pin_sda = bus_sda;
cfg.pin_scl = bus_scl;
cfg.i2c_addr = 0x15;
cfg.i2c_port = i2c_port;
cfg.freq = bus_freq;
cfg.bus_shared = true;
cfg.offset_rotation = static_cast<uint8_t>(offset_rotation);
t->config(cfg);
touch = t;
DEKI_LOG_INFO("LGFXTouchPanel: CST816S on I2C port %d (SDA=%d SCL=%d freq=%d)", (int)i2c_port, bus_sda, bus_scl, bus_freq);
break;
}
case TouchDriverType::XPT2046:
{
auto* t = new lgfx::Touch_XPT2046();
auto cfg = t->config();
cfg.x_min = x_min;
cfg.x_max = x_max;
cfg.y_min = y_min;
cfg.y_max = y_max;
cfg.pin_int = pin_int;
cfg.pin_rst = pin_rst;
cfg.pin_cs = spi_cs;
cfg.pin_mosi = spi_mosi;
cfg.pin_miso = spi_miso;
cfg.pin_sclk = spi_clk;
cfg.bus_shared = bus_shared;
cfg.offset_rotation = static_cast<uint8_t>(offset_rotation);
t->config(cfg);
touch = t;
DEKI_LOG_INFO("LGFXTouchPanel: Using XPT2046 driver (SPI)");
break;
}
default:
DEKI_LOG_ERROR("LGFXTouchPanel: Unknown driver type %d", static_cast<int>(driverType));
onComplete(false);
return;
}
// Attach touch controller to the display panel
auto* panel = gfxDevice->getPanel();
if (panel)
{
panel->setTouch(touch);
ESP_LOGI(TAG, "Touch panel attached OK");
// Explicitly initialize touch hardware. device->init() called
// panel->initTouch() earlier, but touch was null at that point.
// We must call it now to perform I2C bus init, hardware reset,
// and touch controller register validation.
// The FT5x06 may need time after I2C bus init to respond,
// so retry with delays if the first attempt fails.
bool touchOk = false;
for (int attempt = 0; attempt < 5; attempt++)
{
if (panel->initTouch())
{
ESP_LOGI(TAG, "Touch controller initialized (attempt %d)", attempt + 1);
touchOk = true;
break;
}
ESP_LOGW(TAG, "initTouch() attempt %d failed, retrying...", attempt + 1);
lgfx::delay(50);
}
if (!touchOk)
{
ESP_LOGE(TAG, "Touch controller failed to initialize after all retries");
// Don't register touch input — polling a non-responsive I2C device
// causes ~7ms timeout per frame, killing performance
}
else
{
// Create and register LovyanGFXTouch with input backend
auto input = std::make_unique<LovyanGFXTouch>();
input->SetPinInt(pin_int);
if (input->Initialize())
{
DekiInput::SetInput(std::move(input), "LovyanGFXTouch");
DEKI_LOG_INFO("LGFXTouchPanel: Touch input registered with DekiInput");
}
else
{
DEKI_LOG_WARNING("LGFXTouchPanel: Failed to initialize LovyanGFXTouch");
}
}
// Mark owner as Persistent so touch persists across scene changes
if (GetOwner())
{
DekiEngine::GetInstance().GetSceneSystem().MarkPersistent(GetOwner());
}
onComplete(true);
}
else
{
DEKI_LOG_ERROR("LGFXTouchPanel: No display panel available");
delete touch;
onComplete(false);
}
}
#else // !ESP32 (Editor build)
void LGFXTouchPanel::Setup(SetupCallback onComplete)
{
// Touch panel is hardware-only; in editor, just report success
onComplete(true);
}
#endif