@geastack/targets 0.1.78 → 0.1.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,141 @@
1
+ // ESP-IDF binding for the CH422G I/O expander: adapts the new i2c_master API
2
+ // to gea::chips::ch422g::Bus. The chip answers at four fixed addresses (one per
3
+ // function), so the binding holds one device handle per address on the primary
4
+ // bus. Boot-time levels and the pins' roles come from board.h.
5
+
6
+ #include "chip_bindings/expanders/io_expander.h"
7
+
8
+ #include "board.h"
9
+ #include "expanders/ch422g/ch422g.h"
10
+ #include "i2c.h"
11
+
12
+ #include "driver/i2c_master.h"
13
+ #include "esp_log.h"
14
+ #include "freertos/FreeRTOS.h"
15
+ #include "freertos/semphr.h"
16
+
17
+ namespace {
18
+
19
+ constexpr char kTag[] = "ch422g";
20
+ constexpr TickType_t kI2cTimeoutTicks = pdMS_TO_TICKS(100);
21
+
22
+ #if GEA_BOARD_HAS_EXPANDER
23
+ constexpr std::uint8_t kInitialOutputs = static_cast<std::uint8_t>(gea::platform::board::expander.initialOutputs);
24
+ #else
25
+ constexpr std::uint8_t kInitialOutputs = gea::chips::ch422g::kDefaultIoLevels;
26
+ #endif
27
+
28
+ class EspI2cCommandBus final : public gea::chips::ch422g::Bus {
29
+ public:
30
+ bool attach(i2c_master_bus_handle_t bus) {
31
+ return attachAddress(bus, gea::chips::ch422g::kAddressWriteSystem, system_) &&
32
+ attachAddress(bus, gea::chips::ch422g::kAddressWriteOpenDrain, openDrain_) &&
33
+ attachAddress(bus, gea::chips::ch422g::kAddressWriteIo, io_) &&
34
+ attachAddress(bus, gea::chips::ch422g::kAddressReadIo, read_);
35
+ }
36
+
37
+ bool write(std::uint8_t address, std::uint8_t value) override {
38
+ i2c_master_dev_handle_t device = handleFor(address);
39
+ return device && i2c_master_transmit(device, &value, 1, kI2cTimeoutTicks) == ESP_OK;
40
+ }
41
+
42
+ bool read(std::uint8_t address, std::uint8_t &value) override {
43
+ i2c_master_dev_handle_t device = handleFor(address);
44
+ return device && i2c_master_receive(device, &value, 1, kI2cTimeoutTicks) == ESP_OK;
45
+ }
46
+
47
+ private:
48
+ static bool attachAddress(i2c_master_bus_handle_t bus, std::uint8_t address, i2c_master_dev_handle_t &out) {
49
+ if (out) return true;
50
+ i2c_device_config_t config = {};
51
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
52
+ config.device_address = address;
53
+ config.scl_speed_hz = gea::chips::ch422g::kI2cFrequencyHz;
54
+ return i2c_master_bus_add_device(bus, &config, &out) == ESP_OK;
55
+ }
56
+
57
+ i2c_master_dev_handle_t handleFor(std::uint8_t address) const {
58
+ if (address == gea::chips::ch422g::kAddressWriteSystem) return system_;
59
+ if (address == gea::chips::ch422g::kAddressWriteOpenDrain) return openDrain_;
60
+ if (address == gea::chips::ch422g::kAddressWriteIo) return io_;
61
+ if (address == gea::chips::ch422g::kAddressReadIo) return read_;
62
+ return nullptr;
63
+ }
64
+
65
+ i2c_master_dev_handle_t system_ = nullptr;
66
+ i2c_master_dev_handle_t openDrain_ = nullptr;
67
+ i2c_master_dev_handle_t io_ = nullptr;
68
+ i2c_master_dev_handle_t read_ = nullptr;
69
+ };
70
+
71
+ class Ch422gExpander final : public gea::platform::esp32::chip_bindings::expanders::IoExpander {
72
+ public:
73
+ bool init() override {
74
+ if (attempted_) return ready_;
75
+ attempted_ = true;
76
+ lock_ = xSemaphoreCreateMutex();
77
+
78
+ auto bus = gea::platform::i2c::Bus::primary();
79
+ if (!bus.available()) {
80
+ ESP_LOGE(kTag, "I2C bus not ready");
81
+ return false;
82
+ }
83
+ if (!bus_.attach(static_cast<i2c_master_bus_handle_t>(bus.nativeHandle()))) {
84
+ ESP_LOGE(kTag, "failed to add CH422G devices");
85
+ return false;
86
+ }
87
+ if (!driver_.configure(bus_, kInitialOutputs)) {
88
+ ESP_LOGE(kTag, "CH422G did not acknowledge");
89
+ return false;
90
+ }
91
+ ready_ = true;
92
+ ESP_LOGI(kTag, "CH422G ready (IO0-7 outputs=0x%02x)", static_cast<unsigned>(kInitialOutputs));
93
+ return true;
94
+ }
95
+
96
+ bool writePin(int pin, bool high) override {
97
+ if (!init()) return false;
98
+ Guard guard(lock_);
99
+ return driver_.setPin(bus_, pin, high);
100
+ }
101
+
102
+ bool readPin(int pin, bool &high) override {
103
+ if (!init()) return false;
104
+ if (pin < 0 || pin >= gea::chips::ch422g::kIoPinCount) return false;
105
+ Guard guard(lock_);
106
+ std::uint8_t levels = 0;
107
+ if (!driver_.readInputs(bus_, levels)) return false;
108
+ high = (levels >> pin) & 1u;
109
+ return true;
110
+ }
111
+
112
+ private:
113
+ struct Guard {
114
+ explicit Guard(SemaphoreHandle_t lock) : lock_(lock) {
115
+ if (lock_) xSemaphoreTake(lock_, portMAX_DELAY);
116
+ }
117
+ ~Guard() {
118
+ if (lock_) xSemaphoreGive(lock_);
119
+ }
120
+ SemaphoreHandle_t lock_;
121
+ };
122
+
123
+ EspI2cCommandBus bus_;
124
+ gea::chips::ch422g::Driver driver_;
125
+ SemaphoreHandle_t lock_ = nullptr;
126
+ bool attempted_ = false;
127
+ bool ready_ = false;
128
+ };
129
+
130
+ } // namespace
131
+
132
+ namespace gea::platform::esp32::chip_bindings::expanders {
133
+
134
+ IoExpander &ioExpander() {
135
+ static Ch422gExpander expander;
136
+ return expander;
137
+ }
138
+
139
+ const char *ioExpanderDriverName() { return "ch422g"; }
140
+
141
+ } // namespace gea::platform::esp32::chip_bindings::expanders
@@ -0,0 +1,29 @@
1
+ #pragma once
2
+
3
+ // The I/O expander a board routes slow control lines through: a backlight
4
+ // enable, a touch or panel reset, a card-select. Bindings that need one of
5
+ // those lines (rgb_panel.cpp, gt911.cpp) ask board.h which expander pin carries
6
+ // it and drive it through this interface; a board with no expander compiles
7
+ // them with GEA_BOARD_HAS_EXPANDER=0 and the calls fall away.
8
+ //
9
+ // Each board target links exactly one expander binding (ch422g, …) that
10
+ // provides these symbols. Pin numbers are the expander's own (0-based), not
11
+ // MCU GPIOs.
12
+ namespace gea::platform::esp32::chip_bindings::expanders
13
+ {
14
+
15
+ class IoExpander
16
+ {
17
+ public:
18
+ virtual ~IoExpander() = default;
19
+ // Idempotent: the first caller brings the chip up on the primary I2C bus;
20
+ // later callers see the cached result.
21
+ virtual bool init() = 0;
22
+ virtual bool writePin(int pin, bool high) = 0;
23
+ virtual bool readPin(int pin, bool &high) = 0;
24
+ };
25
+
26
+ IoExpander &ioExpander();
27
+ const char *ioExpanderDriverName();
28
+
29
+ } // namespace gea::platform::esp32::chip_bindings::expanders
@@ -0,0 +1,355 @@
1
+ // ESP-IDF binding for the GT911 touch controller: I2C transport, reset (over a
2
+ // GPIO or through the board's I/O expander), the INT line, and the task that
3
+ // turns reports into Touchscreen events. Unlike the FT3168 binding this file
4
+ // also answers the Touchscreen surface itself, so a composed board links it
5
+ // with no board-local glue.
6
+
7
+ #include "gt911_controller.h"
8
+
9
+ #include "board.h"
10
+ #include "i2c.h"
11
+ #if GEA_BOARD_HAS_EXPANDER
12
+ #include "chip_bindings/expanders/io_expander.h"
13
+ #endif
14
+
15
+ #include "driver/gpio.h"
16
+ #include "esp_attr.h"
17
+ #include "esp_log.h"
18
+ #include "esp_timer.h"
19
+ #include "freertos/FreeRTOS.h"
20
+ #include "freertos/task.h"
21
+
22
+ namespace {
23
+
24
+ constexpr char kTag[] = "gt911";
25
+ constexpr gpio_num_t kResetPin = gea::platform::board::touch.reset;
26
+ constexpr gpio_num_t kInterruptPin = gea::platform::board::touch.interrupt;
27
+ #if GEA_BOARD_HAS_EXPANDER
28
+ constexpr int kExpanderResetPin = gea::platform::board::expander.touchReset;
29
+ #else
30
+ constexpr int kExpanderResetPin = -1;
31
+ #endif
32
+ constexpr int kPollIntervalMs = 10;
33
+ // Same reasoning as the FT3168 binding: sample in the render's idle windows on
34
+ // CPU1 rather than preempting the raster worker; a board with real-time work
35
+ // on that core raises it.
36
+ #ifndef GEA_EMBEDDED_TOUCH_TASK_PRIORITY
37
+ #define GEA_EMBEDDED_TOUCH_TASK_PRIORITY 4
38
+ #endif
39
+ constexpr int kTaskPriority = GEA_EMBEDDED_TOUCH_TASK_PRIORITY;
40
+ constexpr TickType_t kI2cTimeoutTicks = pdMS_TO_TICKS(100);
41
+
42
+ volatile std::int64_t g_touchIntUs = 0;
43
+
44
+ } // namespace
45
+
46
+ namespace gea::platform::esp32::chip_bindings::gt911 {
47
+
48
+ bool EspI2cRegisterBus::attach(i2c_master_bus_handle_t bus, std::uint8_t address) {
49
+ if (device_) return true;
50
+ i2c_device_config_t config = {};
51
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
52
+ config.device_address = address;
53
+ config.scl_speed_hz = gea::chips::gt911::kI2cFrequencyHz;
54
+ return i2c_master_bus_add_device(bus, &config, &device_) == ESP_OK;
55
+ }
56
+
57
+ void EspI2cRegisterBus::detach() {
58
+ if (!device_) return;
59
+ i2c_master_bus_rm_device(device_);
60
+ device_ = nullptr;
61
+ }
62
+
63
+ bool EspI2cRegisterBus::writeRegisters(std::uint16_t reg, const std::uint8_t *data, std::size_t length) {
64
+ if (!device_ || length > 30) return false;
65
+ std::uint8_t frame[32];
66
+ frame[0] = static_cast<std::uint8_t>(reg >> 8);
67
+ frame[1] = static_cast<std::uint8_t>(reg & 0xff);
68
+ for (std::size_t i = 0; i < length; i++) frame[2 + i] = data[i];
69
+ return i2c_master_transmit(device_, frame, 2 + length, kI2cTimeoutTicks) == ESP_OK;
70
+ }
71
+
72
+ bool EspI2cRegisterBus::readRegisters(std::uint16_t reg, std::uint8_t *data, std::size_t length) {
73
+ if (!device_) return false;
74
+ const std::uint8_t address[2] = {static_cast<std::uint8_t>(reg >> 8), static_cast<std::uint8_t>(reg & 0xff)};
75
+ return i2c_master_transmit_receive(device_, address, sizeof(address), data, length, kI2cTimeoutTicks) == ESP_OK;
76
+ }
77
+
78
+ TouchController &TouchController::instance() {
79
+ static TouchController controller;
80
+ return controller;
81
+ }
82
+
83
+ void TouchController::setObserver(Observer observer) { observer_ = observer; }
84
+ void TouchController::setPointerObserver(PointerObserver observer) { pointerObserver_ = observer; }
85
+
86
+ esp_err_t TouchController::begin() {
87
+ if (initialized_) return ESP_OK;
88
+
89
+ ESP_LOGI(kTag, "Initializing GT911 touch (RST=%d, expander RST=%d, INT=%d)", static_cast<int>(kResetPin),
90
+ kExpanderResetPin, static_cast<int>(kInterruptPin));
91
+
92
+ auto bus = gea::platform::i2c::Bus::primary();
93
+ if (!bus.available()) return ESP_ERR_INVALID_STATE;
94
+ bus_ = static_cast<i2c_master_bus_handle_t>(bus.nativeHandle());
95
+
96
+ esp_err_t err = resetChip();
97
+ if (err != ESP_OK) return err;
98
+
99
+ err = attachI2cDevice();
100
+ if (err != ESP_OK) return err;
101
+
102
+ err = startTask();
103
+ if (err != ESP_OK) return err;
104
+
105
+ err = attachInterrupt();
106
+ if (err != ESP_OK) return err;
107
+
108
+ initialized_ = true;
109
+ ESP_LOGI(kTag, "Touch controller ready (%s)", kInterruptPin >= 0 ? "interrupt-driven" : "polled");
110
+ return ESP_OK;
111
+ }
112
+
113
+ bool TouchController::read(TouchSample &sample) {
114
+ const bool touching = core_.read(device_, sample);
115
+ current_ = sample;
116
+ return touching;
117
+ }
118
+
119
+ TouchSample TouchController::cached() const { return current_; }
120
+
121
+ void TouchController::consumeLatestMove(int *x, int *y) {
122
+ latestMoveQueued_.store(false, std::memory_order_release);
123
+ if (x) *x = latestMove_.x;
124
+ if (y) *y = latestMove_.y;
125
+ }
126
+
127
+ // The GT911 latches its I2C address from the INT line as reset is released:
128
+ // low picks 0x5d, high picks 0x14. Driving INT low across the reset makes the
129
+ // address deterministic whichever way the board's pull goes; a board without
130
+ // INT wired just gets both addresses probed in attachI2cDevice.
131
+ esp_err_t TouchController::resetChip() {
132
+ if constexpr (kInterruptPin >= 0) {
133
+ gpio_config_t config = {};
134
+ config.pin_bit_mask = 1ULL << static_cast<unsigned>(kInterruptPin);
135
+ config.mode = GPIO_MODE_OUTPUT;
136
+ esp_err_t err = gpio_config(&config);
137
+ if (err != ESP_OK) return err;
138
+ gpio_set_level(kInterruptPin, 0);
139
+ }
140
+
141
+ bool pulsed = false;
142
+ if constexpr (kResetPin >= 0) {
143
+ gpio_config_t config = {};
144
+ config.pin_bit_mask = 1ULL << static_cast<unsigned>(kResetPin);
145
+ config.mode = GPIO_MODE_OUTPUT;
146
+ esp_err_t err = gpio_config(&config);
147
+ if (err != ESP_OK) return err;
148
+ gpio_set_level(kResetPin, 0);
149
+ vTaskDelay(pdMS_TO_TICKS(10));
150
+ gpio_set_level(kResetPin, 1);
151
+ pulsed = true;
152
+ }
153
+ #if GEA_BOARD_HAS_EXPANDER
154
+ if (!pulsed && kExpanderResetPin >= 0) {
155
+ auto &expander = gea::platform::esp32::chip_bindings::expanders::ioExpander();
156
+ if (!expander.writePin(kExpanderResetPin, false)) return ESP_FAIL;
157
+ vTaskDelay(pdMS_TO_TICKS(10));
158
+ if (!expander.writePin(kExpanderResetPin, true)) return ESP_FAIL;
159
+ pulsed = true;
160
+ }
161
+ #endif
162
+ if (!pulsed) ESP_LOGI(kTag, "no reset line for touch; relying on power-on reset");
163
+ // The controller needs ~50ms after reset before it answers; give it more
164
+ // on a cold boot where the panel power is still settling.
165
+ vTaskDelay(pdMS_TO_TICKS(pulsed ? 100 : 50));
166
+
167
+ if constexpr (kInterruptPin >= 0) {
168
+ // Hand INT back to the controller: input, no pull (the panel drives it).
169
+ gpio_config_t config = {};
170
+ config.pin_bit_mask = 1ULL << static_cast<unsigned>(kInterruptPin);
171
+ config.mode = GPIO_MODE_INPUT;
172
+ config.intr_type = GPIO_INTR_DISABLE;
173
+ esp_err_t err = gpio_config(&config);
174
+ if (err != ESP_OK) return err;
175
+ vTaskDelay(pdMS_TO_TICKS(10));
176
+ }
177
+ return ESP_OK;
178
+ }
179
+
180
+ esp_err_t TouchController::attachI2cDevice() {
181
+ const std::uint8_t addresses[] = {gea::chips::gt911::kI2cAddressPrimary, gea::chips::gt911::kI2cAddressSecondary};
182
+ for (std::uint8_t address : addresses) {
183
+ if (!device_.attach(bus_, address)) continue;
184
+ if (core_.configure(device_)) {
185
+ ESP_LOGI(kTag, "GT911 found at 0x%02x", address);
186
+ return ESP_OK;
187
+ }
188
+ device_.detach();
189
+ }
190
+ ESP_LOGE(kTag, "GT911 not found at 0x%02x or 0x%02x", gea::chips::gt911::kI2cAddressPrimary,
191
+ gea::chips::gt911::kI2cAddressSecondary);
192
+ return ESP_ERR_NOT_FOUND;
193
+ }
194
+
195
+ esp_err_t TouchController::startTask() {
196
+ task_ = xTaskCreateStaticPinnedToCore(taskEntry, "gt911-touch",
197
+ static_cast<std::uint32_t>(sizeof(taskStack_) / sizeof(taskStack_[0])), this,
198
+ kTaskPriority, taskStack_, &taskControlBlock_, 1);
199
+ if (task_) return ESP_OK;
200
+ ESP_LOGE(kTag, "Failed to start touch task");
201
+ return ESP_ERR_NO_MEM;
202
+ }
203
+
204
+ esp_err_t TouchController::attachInterrupt() {
205
+ if constexpr (kInterruptPin < 0) {
206
+ ESP_LOGI(kTag, "no interrupt GPIO for touch; polling instead");
207
+ return ESP_OK;
208
+ } else {
209
+ gpio_config_t config = {};
210
+ config.pin_bit_mask = 1ULL << static_cast<unsigned>(kInterruptPin);
211
+ config.mode = GPIO_MODE_INPUT;
212
+ // The GT911 pulses INT high for every report by default (the polarity
213
+ // is a config-register bit); waking on either edge covers both.
214
+ config.intr_type = GPIO_INTR_ANYEDGE;
215
+ esp_err_t err = gpio_config(&config);
216
+ if (err != ESP_OK) return err;
217
+
218
+ err = gpio_install_isr_service(0);
219
+ if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) return err;
220
+
221
+ return gpio_isr_handler_add(kInterruptPin, interruptEntry, this);
222
+ }
223
+ }
224
+
225
+ void IRAM_ATTR TouchController::interruptEntry(void *arg) {
226
+ auto *self = static_cast<TouchController *>(arg);
227
+ if (self) self->notifyTaskFromInterrupt();
228
+ }
229
+
230
+ void IRAM_ATTR TouchController::notifyTaskFromInterrupt() {
231
+ g_touchIntUs = esp_timer_get_time();
232
+ BaseType_t woken = pdFALSE;
233
+ if (task_) vTaskNotifyGiveFromISR(task_, &woken);
234
+ portYIELD_FROM_ISR(woken);
235
+ }
236
+
237
+ void TouchController::taskEntry(void *arg) { static_cast<TouchController *>(arg)->pollLoop(); }
238
+
239
+ void TouchController::pollLoop() {
240
+ // Per-finger slot tracking (the same shape as the FT3168 and CST9217
241
+ // bindings): each slot emits its own Down/Move/Up tagged with its pointerId,
242
+ // so a second finger reaches pinch-zoom. Slot 0 is the primary pointer.
243
+ bool slotActive[kMaxFingers] = {false, false};
244
+ int slotX[kMaxFingers] = {0, 0};
245
+ int slotY[kMaxFingers] = {0, 0};
246
+ bool anyActive = false;
247
+
248
+ while (true) {
249
+ constexpr bool kInterruptDriven = kInterruptPin >= 0;
250
+ const TickType_t wait = (anyActive || !kInterruptDriven) ? pdMS_TO_TICKS(kPollIntervalMs) : portMAX_DELAY;
251
+ ulTaskNotifyTake(pdTRUE, wait);
252
+
253
+ gea::chips::gt911::MultiTouchSample multi;
254
+ core_.readMulti(device_, multi);
255
+
256
+ current_ = multi.count > 0 ? TouchSample{true, multi.x[0], multi.y[0]}
257
+ : TouchSample{false, current_.x, current_.y};
258
+
259
+ for (int slot = 0; slot < kMaxFingers; slot++) {
260
+ const bool nowActive = slot < multi.count;
261
+ const int x = nowActive ? multi.x[slot] : slotX[slot];
262
+ const int y = nowActive ? multi.y[slot] : slotY[slot];
263
+
264
+ if (nowActive && !slotActive[slot]) {
265
+ if (slot == 0) latestMove_ = TouchSample{true, x, y};
266
+ notifyPointer(Phase::Down, true, x, y, slot);
267
+ } else if (nowActive && slotActive[slot] && (x != slotX[slot] || y != slotY[slot])) {
268
+ if (slot == 0) {
269
+ latestMove_ = TouchSample{true, x, y};
270
+ if (!latestMoveQueued_.exchange(true, std::memory_order_acq_rel)) {
271
+ notifyPointer(Phase::Move, true, x, y, slot);
272
+ }
273
+ } else {
274
+ notifyPointer(Phase::Move, true, x, y, slot);
275
+ }
276
+ } else if (!nowActive && slotActive[slot]) {
277
+ notifyPointer(Phase::Up, false, x, y, slot);
278
+ }
279
+
280
+ if (nowActive) {
281
+ slotX[slot] = x;
282
+ slotY[slot] = y;
283
+ }
284
+ slotActive[slot] = nowActive;
285
+ }
286
+ anyActive = multi.count > 0;
287
+ }
288
+ }
289
+
290
+ void TouchController::notifyPointer(Phase phase, bool touching, int x, int y, int pointerId) {
291
+ if (pointerObserver_) {
292
+ pointerObserver_(phase, touching, x, y, pointerId);
293
+ } else if (observer_ && pointerId == 0) {
294
+ observer_(phase, touching, x, y);
295
+ }
296
+ }
297
+
298
+ void TouchController::notifyMove(const TouchSample &sample) {
299
+ latestMove_ = sample;
300
+ if (latestMoveQueued_.exchange(true, std::memory_order_acq_rel)) return;
301
+ notifyPointer(Phase::Move, true, sample.x, sample.y, 0);
302
+ }
303
+
304
+ void TouchController::injectEvent(Phase phase, bool touching, int x, int y) {
305
+ TouchSample sample;
306
+ sample.x = x;
307
+ sample.y = y;
308
+ sample.touching = touching;
309
+ current_ = sample;
310
+ if (phase == Phase::Move) notifyMove(sample);
311
+ else notifyPointer(phase, touching, x, y, 0);
312
+ }
313
+
314
+ } // namespace gea::platform::esp32::chip_bindings::gt911
315
+
316
+ // The Touchscreen surface, answered here so a composed board links the GT911
317
+ // without a board-local touch.cpp.
318
+ namespace {
319
+
320
+ gea::platform::esp32::chip_bindings::gt911::TouchController &touchController() {
321
+ return gea::platform::esp32::chip_bindings::gt911::TouchController::instance();
322
+ }
323
+
324
+ } // namespace
325
+
326
+ void gea::platform::touch::Touchscreen::setObserver(Observer observer) { touchController().setObserver(observer); }
327
+
328
+ // Strong override of the runtime's weak no-op: the GT911 reports several
329
+ // fingers, so every contact reaches the app.
330
+ void gea::platform::touch::Touchscreen::setPointerObserver(PointerObserver observer) {
331
+ touchController().setPointerObserver(observer);
332
+ }
333
+
334
+ bool gea::platform::touch::Touchscreen::init() { return touchController().begin() == ESP_OK; }
335
+
336
+ int gea::platform::touch::Touchscreen::read(int *x, int *y) {
337
+ gea::chips::gt911::TouchSample sample;
338
+ const bool touching = touchController().read(sample);
339
+ if (x) *x = sample.x;
340
+ if (y) *y = sample.y;
341
+ return touching ? 1 : 0;
342
+ }
343
+
344
+ int gea::platform::touch::Touchscreen::readCached(int *x, int *y) {
345
+ const auto sample = touchController().cached();
346
+ if (x) *x = sample.x;
347
+ if (y) *y = sample.y;
348
+ return sample.touching ? 1 : 0;
349
+ }
350
+
351
+ void gea::platform::touch::Touchscreen::consumeLatestMove(int *x, int *y) { touchController().consumeLatestMove(x, y); }
352
+
353
+ void gea::platform::touch::Touchscreen::injectEvent(Phase phase, bool touching, int x, int y) {
354
+ touchController().injectEvent(phase, touching, x, y);
355
+ }
@@ -0,0 +1,84 @@
1
+ #pragma once
2
+
3
+ #include "touch/gt911/gt911.h"
4
+ #include "touch.h"
5
+
6
+ #include <cstddef>
7
+ #include <cstdint>
8
+ #include <atomic>
9
+
10
+ #include "driver/gpio.h"
11
+ #include "driver/i2c_master.h"
12
+ #include "esp_err.h"
13
+ #include "freertos/FreeRTOS.h"
14
+ #include "freertos/task.h"
15
+
16
+ namespace gea::platform::esp32::chip_bindings::gt911 {
17
+
18
+ using TouchSample = gea::chips::gt911::TouchSample;
19
+
20
+ // GT911 registers are 16 bits wide; the address goes out big-endian ahead of
21
+ // the data in one transaction.
22
+ class EspI2cRegisterBus final : public gea::chips::gt911::RegisterBus {
23
+ public:
24
+ bool attach(i2c_master_bus_handle_t bus, std::uint8_t address);
25
+ void detach();
26
+ bool attached() const { return device_ != nullptr; }
27
+ bool writeRegisters(std::uint16_t reg, const std::uint8_t *data, std::size_t length) override;
28
+ bool readRegisters(std::uint16_t reg, std::uint8_t *data, std::size_t length) override;
29
+
30
+ private:
31
+ i2c_master_dev_handle_t device_ = nullptr;
32
+ };
33
+
34
+ class TouchController {
35
+ public:
36
+ using Observer = gea::platform::touch::Touchscreen::Observer;
37
+ using PointerObserver = gea::platform::touch::Touchscreen::PointerObserver;
38
+ using Phase = gea::platform::touch::Phase;
39
+
40
+ TouchController(const TouchController &) = delete;
41
+ TouchController &operator=(const TouchController &) = delete;
42
+
43
+ static TouchController &instance();
44
+
45
+ void setObserver(Observer observer);
46
+ void setPointerObserver(PointerObserver observer);
47
+ esp_err_t begin();
48
+ bool read(TouchSample &sample);
49
+ TouchSample cached() const;
50
+ void consumeLatestMove(int *x, int *y);
51
+ void injectEvent(Phase phase, bool touching, int x, int y);
52
+
53
+ private:
54
+ TouchController() = default;
55
+
56
+ esp_err_t resetChip();
57
+ esp_err_t attachI2cDevice();
58
+ esp_err_t startTask();
59
+ esp_err_t attachInterrupt();
60
+ static void interruptEntry(void *arg);
61
+ void notifyTaskFromInterrupt();
62
+ static void taskEntry(void *arg);
63
+ void pollLoop();
64
+ void notifyPointer(Phase phase, bool touching, int x, int y, int pointerId);
65
+ void notifyMove(const TouchSample &sample);
66
+
67
+ static constexpr std::size_t kTaskStackWords = 4096;
68
+ static constexpr int kMaxFingers = gea::chips::gt911::kMaxTouchPoints;
69
+
70
+ bool initialized_ = false;
71
+ i2c_master_bus_handle_t bus_ = nullptr;
72
+ EspI2cRegisterBus device_;
73
+ gea::chips::gt911::ControllerCore core_;
74
+ TaskHandle_t task_ = nullptr;
75
+ StaticTask_t taskControlBlock_ = {};
76
+ StackType_t taskStack_[kTaskStackWords] = {};
77
+ TouchSample current_ = {};
78
+ TouchSample latestMove_ = {};
79
+ std::atomic<bool> latestMoveQueued_{false};
80
+ Observer observer_ = nullptr;
81
+ PointerObserver pointerObserver_ = nullptr;
82
+ };
83
+
84
+ } // namespace gea::platform::esp32::chip_bindings::gt911
@@ -235,8 +235,36 @@ endif()
235
235
  set(GEA_EMBEDDED_HTTPS_REQUIRES "")
236
236
  set(GEA_EMBEDDED_HTTPS_COMPILE_DEFINITIONS "")
237
237
  set(GEA_EMBEDDED_BLE_COMPILE_DEFINITIONS "")
238
- if("${_GEA_EMBEDDED_HOST_BINDINGS}" MATCHES "(^|;)ble($|;)")
238
+
239
+ # IDF evaluates this component once in script mode with the broad placeholder
240
+ # app and again for the real configure. A normal variable from the requirements
241
+ # pass can therefore say BLE is enabled even when the selected app does not use
242
+ # it. Only an explicit -D cache value is an override; otherwise resolve the
243
+ # capability again from the real app analysis before choosing sources/REQUIRES.
244
+ # The gea CLI turns the Bluetooth controller off in sdkconfig for apps without
245
+ # the capability, so a build that still compiled ble_hid.cpp would fail on the
246
+ # missing esp_bt.h.
247
+ if(DEFINED ENV{GEA_EMBEDDED_BLE_OTA} AND "$ENV{GEA_EMBEDDED_BLE_OTA}" STREQUAL "1")
248
+ set(GEA_EMBEDDED_CAPABILITY_BLE 1)
249
+ elseif(DEFINED ENV{GEA_EMBEDDED_CAPABILITY_BLE}
250
+ AND NOT "$ENV{GEA_EMBEDDED_CAPABILITY_BLE}" STREQUAL "")
251
+ set(GEA_EMBEDDED_CAPABILITY_BLE "$ENV{GEA_EMBEDDED_CAPABILITY_BLE}")
252
+ elseif(DEFINED CACHE{GEA_EMBEDDED_CAPABILITY_BLE}
253
+ AND NOT "$CACHE{GEA_EMBEDDED_CAPABILITY_BLE}" STREQUAL "")
254
+ set(GEA_EMBEDDED_CAPABILITY_BLE "$CACHE{GEA_EMBEDDED_CAPABILITY_BLE}")
255
+ else()
256
+ set(GEA_EMBEDDED_CAPABILITY_BLE 0)
257
+ if("${_GEA_EMBEDDED_HOST_BINDINGS}" MATCHES "(^|;)ble($|;)")
258
+ set(GEA_EMBEDDED_CAPABILITY_BLE 1)
259
+ endif()
260
+ endif()
261
+ set(GEA_EMBEDDED_BLE_SOURCES "")
262
+ set(GEA_EMBEDDED_BLE_REQUIRES "")
263
+ if(GEA_EMBEDDED_CAPABILITY_BLE)
239
264
  list(APPEND GEA_EMBEDDED_BLE_COMPILE_DEFINITIONS GEA_EMBEDDED_APP_USES_BLE=1)
265
+ list(APPEND GEA_EMBEDDED_BLE_SOURCES
266
+ "${GEA_EMBEDDED_ROOT}/targets/esp32/connectivity/ble_hid.cpp")
267
+ list(APPEND GEA_EMBEDDED_BLE_REQUIRES bt)
240
268
  endif()
241
269
  # Note: BLE driver registration + BLE controller preinit are forced from
242
270
  # `app_main.cpp` and `bluetooth_service.cpp` regardless of the macro above,
@@ -311,8 +339,9 @@ idf_component_register(
311
339
  "${GEA_EMBEDDED_ROOT}/targets/esp32/services/frame_scheduler.cpp"
312
340
  "${GEA_EMBEDDED_ROOT}/targets/esp32/services/memory.cpp"
313
341
  "${GEA_EMBEDDED_ROOT}/targets/esp32/services/ota.cpp"
342
+ "${GEA_EMBEDDED_ROOT}/targets/esp32/services/ota_image.cpp"
314
343
  "${GEA_EMBEDDED_ROOT}/targets/esp32/services/storage_service.cpp"
315
- "${GEA_EMBEDDED_ROOT}/targets/esp32/connectivity/ble_hid.cpp"
344
+ ${GEA_EMBEDDED_BLE_SOURCES}
316
345
  "${GEA_EMBEDDED_ROOT}/targets/esp32/services/heap.cpp"
317
346
  ${APP_GENERATED_SOURCES}
318
347
  INCLUDE_DIRS
@@ -333,6 +362,10 @@ idf_component_register(
333
362
  esp_http_client
334
363
  ${GEA_EMBEDDED_HTTPS_REQUIRES}
335
364
  app_update
365
+ esp_app_format
366
+ # esp_flash_read_id: the OTA status endpoint reports the flash JEDEC id,
367
+ # which decides whether CONFIG_SPI_FLASH_AUTO_SUSPEND is safe on a board.
368
+ spi_flash
336
369
  esp_mm
337
370
  esp_netif
338
371
  esp_event
@@ -343,7 +376,7 @@ idf_component_register(
343
376
  esp_driver_ledc
344
377
  esp_driver_usb_serial_jtag
345
378
  esp_psram
346
- bt
379
+ ${GEA_EMBEDDED_BLE_REQUIRES}
347
380
  LDFRAGMENTS
348
381
  )
349
382
 
@@ -420,6 +453,11 @@ target_compile_definitions(${COMPONENT_LIB} PRIVATE
420
453
  # then-broken queueFrameEvent catch-up. =0 re-enables catch-up, now driven by
421
454
  # the work-based, event-loop graceful-overrun path (frame_scheduler/runtime).
422
455
  GEA_EMBEDDED_FRAME_SCHEDULER_DROP_CATCHUP_FRAMES=0
456
+ # A heavy canvas app overruns every frame on this panel (bouncing-balls-jsx
457
+ # sits at ~48ms against an 8ms target), so the graceful-overrun catch-up loop
458
+ # never yields and IDLE0 starves: the task watchdog fires every 5s. Cap the
459
+ # burst like the P4 boards do so idle runs at least every 60 frames.
460
+ GEA_EMBEDDED_FRAME_SCHEDULER_MAX_CATCHUP_FRAMES_BEFORE_YIELD=60
423
461
  GEA_EMBEDDED_APP_FRAME_TASK_PRIORITY=23
424
462
  ${GEA_EMBEDDED_BLE_COMPILE_DEFINITIONS}
425
463
  ${GEA_EMBEDDED_HTTPS_COMPILE_DEFINITIONS}