@geastack/targets 0.1.107 → 0.1.109

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.
Files changed (47) hide show
  1. package/build-config.json +120 -1
  2. package/package.json +6 -6
  3. package/targets/esp32/chip_bindings/audio/ns4168.cpp +326 -0
  4. package/targets/esp32/chip_bindings/displays/rgb_panel.cpp +2485 -105
  5. package/targets/esp32/chip_bindings/displays/sh8601.cpp +12 -2
  6. package/targets/esp32/chip_bindings/expanders/elecrow_board_control.cpp +169 -0
  7. package/targets/esp32/chip_bindings/expanders/io_expander.h +4 -0
  8. package/targets/esp32/chip_bindings/imu/qmi8658.cpp +10 -3
  9. package/targets/esp32/chip_bindings/power/axp2101.cpp +58 -0
  10. package/targets/esp32/chip_bindings/rtc/bm8563.cpp +129 -0
  11. package/targets/esp32/chip_bindings/touch/cst9217.cpp +26 -23
  12. package/targets/esp32/connectivity/wifi.cpp +30 -2
  13. package/targets/esp32/gea_board_buttons.cmake +13 -0
  14. package/targets/esp32/gpio.cpp +124 -1
  15. package/targets/esp32/gpio_pins.h +17 -0
  16. package/targets/esp32/services/buttons.cpp +185 -0
  17. package/targets/esp32/services/buttons.h +46 -0
  18. package/targets/esp32/services/device_control.cpp +98 -6
  19. package/targets/esp32/services/heap.cpp +25 -1
  20. package/targets/esp32-s3-elecrow-crowpanel-advance-7.0.json +182 -0
  21. package/targets/esp32-s3-touch-amoled-1.75c/CMakeLists.txt +11 -0
  22. package/targets/esp32-s3-touch-amoled-1.75c/main/CMakeLists.txt +54 -0
  23. package/targets/esp32-s3-touch-amoled-1.75c/main/Kconfig.projbuild +12 -0
  24. package/targets/esp32-s3-touch-amoled-1.75c/main/gea-dependencies.json +42 -0
  25. package/targets/esp32-s3-touch-amoled-1.75c/main/idf_component.yml +10 -0
  26. package/targets/esp32-s3-touch-amoled-1.75c/main/include/board.h +113 -0
  27. package/targets/esp32-s3-touch-amoled-1.75c/partitions.csv +9 -0
  28. package/targets/esp32-s3-touch-amoled-1.75c/sdkconfig.defaults +279 -0
  29. package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +13 -1
  30. package/targets/esp32-s3-touch-amoled-1.8/main/app_main.cpp +2 -0
  31. package/targets/esp32-s3-touch-amoled-2.06/main/CMakeLists.txt +44 -2
  32. package/targets/esp32-s3-touch-amoled-2.06/main/app_main.cpp +13 -0
  33. package/targets/esp32-s3-touch-amoled-2.06/main/sdcard_mount.cpp +56 -0
  34. package/targets/esp32-s3-touch-amoled-2.06/sdkconfig.defaults +5 -3
  35. package/targets/esp32-s3-touch-lcd-3.49/CMakeLists.txt +11 -0
  36. package/targets/esp32-s3-touch-lcd-3.49/main/CMakeLists.txt +82 -0
  37. package/targets/esp32-s3-touch-lcd-3.49/main/Kconfig.projbuild +12 -0
  38. package/targets/esp32-s3-touch-lcd-3.49/main/gea-dependencies.json +42 -0
  39. package/targets/esp32-s3-touch-lcd-3.49/main/idf_component.yml +8 -0
  40. package/targets/esp32-s3-touch-lcd-3.49/main/include/axs15231b_panel_init.h +9 -0
  41. package/targets/esp32-s3-touch-lcd-3.49/main/include/board.h +160 -0
  42. package/targets/esp32-s3-touch-lcd-3.49/main/panel_reset.cpp +93 -0
  43. package/targets/esp32-s3-touch-lcd-3.49/main/power.cpp +60 -0
  44. package/targets/esp32-s3-touch-lcd-3.49/main/touch.cpp +327 -0
  45. package/targets/esp32-s3-touch-lcd-3.49/partitions.csv +7 -0
  46. package/targets/esp32-s3-touch-lcd-3.49/sdkconfig.defaults +279 -0
  47. package/targets.json +70 -1
@@ -6,11 +6,14 @@
6
6
  // of them to light one diode on the boards that have one.
7
7
 
8
8
  #include "gpio.h"
9
+ #include "gpio_pins.h"
9
10
 
11
+ #include <atomic>
10
12
  #include <cstdint>
11
13
  #include <cstring>
12
14
 
13
15
  #include "driver/gpio.h"
16
+ #include "esp_attr.h"
14
17
  #include "driver/rmt_tx.h"
15
18
  #include "esp_err.h"
16
19
  #include "esp_log.h"
@@ -25,6 +28,19 @@ bool validPin(int pin)
25
28
  return pin >= 0 && pin < GPIO_NUM_MAX && GPIO_IS_VALID_GPIO(static_cast<gpio_num_t>(pin));
26
29
  }
27
30
 
31
+ // One bit per open Gpio slot, set from an edge interrupt and taken by the app
32
+ // thread. The interrupt does nothing else: the callback the edge stands for
33
+ // runs on the app thread, where TS code may run.
34
+ std::atomic<unsigned> gEdges{0};
35
+
36
+ // gpio_pins.h: the pins another driver owns, whether it takes their edge
37
+ // interrupts, and the Gpio slot an open owned pin reports to (-1 for none).
38
+ std::atomic<bool> gClaimed[GPIO_NUM_MAX];
39
+ std::atomic<bool> gClaimedInterrupt[GPIO_NUM_MAX];
40
+ std::atomic<std::int8_t> gOwnedSlot[GPIO_NUM_MAX];
41
+
42
+ bool claimed(int pin) { return gClaimed[pin].load(std::memory_order_acquire); }
43
+
28
44
  // WS2812 is a single-wire protocol where a bit is a fixed-length pulse and only
29
45
  // the high/low split says whether it is a 0 or a 1. At the 10 MHz resolution
30
46
  // below one tick is 100 ns, so these are the datasheet's T0H/T0L/T1H/T1L.
@@ -78,7 +94,7 @@ namespace gea::platform::gpio {
78
94
 
79
95
  bool Gpio::configureOutput(int pin)
80
96
  {
81
- if (!validPin(pin)) return false;
97
+ if (!validPin(pin) || claimed(pin)) return false;
82
98
  const gpio_num_t num = static_cast<gpio_num_t>(pin);
83
99
  gpio_reset_pin(num);
84
100
  gpio_config_t config = {};
@@ -93,6 +109,8 @@ bool Gpio::configureOutput(int pin)
93
109
  bool Gpio::configureInput(int pin, bool pull_up)
94
110
  {
95
111
  if (!validPin(pin)) return false;
112
+ // An owned pin is already an input, set up by its owner.
113
+ if (claimed(pin)) return true;
96
114
  const gpio_num_t num = static_cast<gpio_num_t>(pin);
97
115
  // A strapping pin (GPIO0 among them) can still be muxed to a boot function;
98
116
  // resetting forces it back to a plain GPIO matrix entry, which is why
@@ -119,6 +137,111 @@ bool Gpio::read(int pin)
119
137
  return gpio_get_level(static_cast<gpio_num_t>(pin)) != 0;
120
138
  }
121
139
 
140
+ namespace {
141
+
142
+ void IRAM_ATTR edgeInterrupt(void *arg)
143
+ {
144
+ gEdges.fetch_or(1u << reinterpret_cast<std::uintptr_t>(arg), std::memory_order_relaxed);
145
+ }
146
+
147
+ } // namespace
148
+
149
+ bool Gpio::configure(int pin, int mode)
150
+ {
151
+ if (!validPin(pin)) return false;
152
+ if (claimed(pin)) return mode == Input || mode == InputPullUp || mode == InputPullDown || mode == InputPullUpDown;
153
+ const gpio_num_t num = static_cast<gpio_num_t>(pin);
154
+ gpio_config_t config = {};
155
+ config.pin_bit_mask = 1ULL << pin;
156
+ config.intr_type = GPIO_INTR_DISABLE;
157
+ switch (mode) {
158
+ case Input:
159
+ case InputPullUp:
160
+ case InputPullDown:
161
+ case InputPullUpDown:
162
+ config.mode = GPIO_MODE_INPUT;
163
+ config.pull_up_en = (mode & InputPullUp) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE;
164
+ config.pull_down_en = (mode & InputPullDown) ? GPIO_PULLDOWN_ENABLE : GPIO_PULLDOWN_DISABLE;
165
+ break;
166
+ case Output:
167
+ if (!GPIO_IS_VALID_OUTPUT_GPIO(num)) return false;
168
+ config.mode = GPIO_MODE_OUTPUT;
169
+ break;
170
+ case OutputOpenDrain:
171
+ if (!GPIO_IS_VALID_OUTPUT_GPIO(num)) return false;
172
+ config.mode = GPIO_MODE_INPUT_OUTPUT_OD;
173
+ break;
174
+ default:
175
+ return false;
176
+ }
177
+ // Strapping pins (GPIO0 among them) can be left muxed to a boot function;
178
+ // resetting returns the pin to a plain GPIO matrix entry.
179
+ gpio_reset_pin(num);
180
+ return gpio_config(&config) == ESP_OK;
181
+ }
182
+
183
+ bool Gpio::watch(int pin, int edges, int slot)
184
+ {
185
+ if (!validPin(pin) || slot < 0 || slot > 31) return false;
186
+ if (claimed(pin)) {
187
+ if (!gClaimedInterrupt[pin].load(std::memory_order_acquire)) return false;
188
+ gOwnedSlot[pin].store(static_cast<std::int8_t>(slot), std::memory_order_release);
189
+ return true;
190
+ }
191
+ gpio_int_type_t type = GPIO_INTR_ANYEDGE;
192
+ if (edges == kRising) type = GPIO_INTR_POSEDGE;
193
+ else if (edges == kFalling) type = GPIO_INTR_NEGEDGE;
194
+ else if (edges != (kRising | kFalling)) return false;
195
+ // Shared with the board's buttons; whichever starts first installs it.
196
+ const esp_err_t service = gpio_install_isr_service(0);
197
+ if (service != ESP_OK && service != ESP_ERR_INVALID_STATE) {
198
+ ESP_LOGE(kTag, "GPIO interrupt service unavailable: %s", esp_err_to_name(service));
199
+ return false;
200
+ }
201
+ const gpio_num_t num = static_cast<gpio_num_t>(pin);
202
+ if (gpio_set_intr_type(num, type) != ESP_OK) return false;
203
+ if (gpio_isr_handler_add(num, edgeInterrupt, reinterpret_cast<void *>(static_cast<std::uintptr_t>(slot))) != ESP_OK)
204
+ return false;
205
+ return gpio_intr_enable(num) == ESP_OK;
206
+ }
207
+
208
+ void Gpio::unwatch(int pin)
209
+ {
210
+ if (!validPin(pin)) return;
211
+ if (claimed(pin)) {
212
+ gOwnedSlot[pin].store(-1, std::memory_order_release);
213
+ return;
214
+ }
215
+ const gpio_num_t num = static_cast<gpio_num_t>(pin);
216
+ gpio_intr_disable(num);
217
+ gpio_isr_handler_remove(num);
218
+ gpio_set_intr_type(num, GPIO_INTR_DISABLE);
219
+ }
220
+
221
+ unsigned Gpio::takeEdges() { return gEdges.exchange(0, std::memory_order_acquire); }
222
+
223
+ } // namespace gea::platform::gpio
224
+
225
+ namespace gea::platform::esp32::gpio {
226
+
227
+ void claimPin(int pin, bool interrupt)
228
+ {
229
+ if (!validPin(pin)) return;
230
+ gOwnedSlot[pin].store(-1, std::memory_order_relaxed);
231
+ gClaimedInterrupt[pin].store(interrupt, std::memory_order_relaxed);
232
+ gClaimed[pin].store(true, std::memory_order_release);
233
+ }
234
+
235
+ void IRAM_ATTR ownedPinEdge(int pin)
236
+ {
237
+ const int slot = gOwnedSlot[pin].load(std::memory_order_acquire);
238
+ if (slot >= 0) gEdges.fetch_or(1u << slot, std::memory_order_relaxed);
239
+ }
240
+
241
+ } // namespace gea::platform::esp32::gpio
242
+
243
+ namespace gea::platform::gpio {
244
+
122
245
  bool AddressableLed::attach(int pin, int count)
123
246
  {
124
247
  if (!validPin(pin) || count <= 0 || count > kMaxPixels) return false;
@@ -0,0 +1,17 @@
1
+ #pragma once
2
+
3
+ // Pins whose interrupt another ESP32 driver owns: the board's buttons
4
+ // (services/buttons.cpp). The Gpio and Digital classes can still open such a
5
+ // pin as an input. Gpio::configure() then leaves the owner's setup alone, and
6
+ // Gpio::watch() rides on the owner's interrupt, which reports each edge
7
+ // through ownedPinEdge(). Without this, opening the pin would reset it and
8
+ // replace the owner's interrupt handler.
9
+ namespace gea::platform::esp32::gpio {
10
+
11
+ // `interrupt`: the owner takes edge interrupts on the pin (false for a polled
12
+ // button, which Gpio then polls too).
13
+ void claimPin(int pin, bool interrupt);
14
+ // Interrupt context; IRAM-safe.
15
+ void ownedPinEdge(int pin);
16
+
17
+ } // namespace gea::platform::esp32::gpio
@@ -0,0 +1,185 @@
1
+ #include "services/buttons.h"
2
+
3
+ #if GEA_BOARD_HAS_BUTTONS
4
+ // Generated by the CLI from the board's and the app's "buttons" JSON.
5
+ #include "gea_board_buttons.h"
6
+ #endif
7
+
8
+ #if GEA_BOARD_HAS_BUTTONS && GEA_BOARD_BUTTON_COUNT > 0
9
+
10
+ #include "gpio_pins.h"
11
+ #include "input.h"
12
+
13
+ #include <cstddef>
14
+ #include <cstdint>
15
+
16
+ #include "driver/gpio.h"
17
+ #include "esp_attr.h"
18
+ #include "esp_log.h"
19
+ #include "freertos/FreeRTOS.h"
20
+ #include "freertos/task.h"
21
+
22
+ namespace gea::platform::esp32::buttons {
23
+
24
+ namespace {
25
+
26
+ constexpr const char *kTag = "buttons";
27
+ constexpr std::size_t kButtonCount = sizeof(board::buttons) / sizeof(board::buttons[0]);
28
+ static_assert(kButtonCount <= static_cast<std::size_t>(gea::framework::input::kMaxButtons),
29
+ "the board declares more buttons than input.h's table holds");
30
+ static_assert(kButtonCount <= 32, "one notification bit per button");
31
+ constexpr int kDebounceMs = 20;
32
+ constexpr int kPollMs = 30;
33
+ constexpr std::uint32_t kTaskStackBytes = 2560;
34
+
35
+ int gIds[kButtonCount];
36
+ TaskHandle_t gTask = nullptr;
37
+ // Settled level per button, owned by the task.
38
+ bool gPressed[kButtonCount];
39
+
40
+ // Idempotent: registerButton returns the existing id for a known code.
41
+ void registerAll()
42
+ {
43
+ for (std::size_t i = 0; i < kButtonCount; ++i) {
44
+ const Button &button = board::buttons[i];
45
+ gIds[i] = gea::framework::input::registerButton(button.code, button.key, button.keyCode, button.pin);
46
+ }
47
+ }
48
+
49
+ bool watched(const Button &button) { return button.source == Source::Gpio && button.pin >= 0; }
50
+
51
+ bool readPressed(const Button &button)
52
+ {
53
+ return gpio_get_level(static_cast<gpio_num_t>(button.pin)) == button.activeLevel;
54
+ }
55
+
56
+ // An edge disarms its pin and wakes the task with the button's bit; the task
57
+ // waits out the bounce, reports the settled level if it changed, and re-arms.
58
+ void IRAM_ATTR edge(void *arg)
59
+ {
60
+ const auto index = reinterpret_cast<std::uintptr_t>(arg);
61
+ const int pin = board::buttons[index].pin;
62
+ gpio_intr_disable(static_cast<gpio_num_t>(pin));
63
+ // A Gpio or Digital the app opened on this pin rides on this interrupt.
64
+ gea::platform::esp32::gpio::ownedPinEdge(pin);
65
+ BaseType_t woken = pdFALSE;
66
+ xTaskNotifyFromISR(gTask, 1u << index, eSetBits, &woken);
67
+ if (woken) portYIELD_FROM_ISR();
68
+ }
69
+
70
+ void settle(std::size_t index)
71
+ {
72
+ const bool pressed = readPressed(board::buttons[index]);
73
+ if (pressed == gPressed[index]) return;
74
+ gPressed[index] = pressed;
75
+ gea::framework::input::queueButton(gIds[index], pressed);
76
+ // The interrupt reported the first edge of the bounce; this is the level
77
+ // it settled on, for a Gpio or Digital that read the pin mid-bounce.
78
+ if (!board::buttons[index].poll) gea::platform::esp32::gpio::ownedPinEdge(board::buttons[index].pin);
79
+ ESP_LOGI(kTag, "%s %s", board::buttons[index].code, pressed ? "down" : "up");
80
+ }
81
+
82
+ void run(void *)
83
+ {
84
+ bool polls = false;
85
+ for (std::size_t i = 0; i < kButtonCount; ++i) polls = polls || (watched(board::buttons[i]) && board::buttons[i].poll);
86
+ // Sleeps until an edge; wakes on an interval only when a board button polls.
87
+ const TickType_t wait = polls ? pdMS_TO_TICKS(kPollMs) : portMAX_DELAY;
88
+ while (true) {
89
+ std::uint32_t edges = 0;
90
+ xTaskNotifyWait(0, UINT32_MAX, &edges, wait);
91
+ if (edges) vTaskDelay(pdMS_TO_TICKS(kDebounceMs));
92
+ for (std::size_t i = 0; i < kButtonCount; ++i) {
93
+ const Button &button = board::buttons[i];
94
+ if (!watched(button)) continue;
95
+ if (button.poll) {
96
+ settle(i);
97
+ } else if (edges & (1u << i)) {
98
+ settle(i);
99
+ gpio_intr_enable(static_cast<gpio_num_t>(button.pin));
100
+ }
101
+ }
102
+ }
103
+ }
104
+
105
+ bool configure(std::size_t index)
106
+ {
107
+ const Button &button = board::buttons[index];
108
+ const auto pin = static_cast<gpio_num_t>(button.pin);
109
+ // GPIO0 is a strapping pin the bootloader may leave on another function;
110
+ // resetting returns any pin to a plain GPIO matrix entry.
111
+ gpio_reset_pin(pin);
112
+ gpio_config_t config{};
113
+ config.pin_bit_mask = 1ULL << button.pin;
114
+ config.mode = GPIO_MODE_INPUT;
115
+ config.pull_up_en = button.activeLevel == 0 ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE;
116
+ config.pull_down_en = button.activeLevel == 0 ? GPIO_PULLDOWN_DISABLE : GPIO_PULLDOWN_ENABLE;
117
+ config.intr_type = button.poll ? GPIO_INTR_DISABLE : GPIO_INTR_ANYEDGE;
118
+ if (gpio_config(&config) != ESP_OK) return false;
119
+ gPressed[index] = readPressed(button);
120
+ gea::platform::esp32::gpio::claimPin(button.pin, !button.poll);
121
+ return true;
122
+ }
123
+
124
+ bool arm(std::size_t index)
125
+ {
126
+ const Button &button = board::buttons[index];
127
+ if (button.poll) return true;
128
+ return gpio_isr_handler_add(static_cast<gpio_num_t>(button.pin), edge,
129
+ reinterpret_cast<void *>(static_cast<std::uintptr_t>(index))) == ESP_OK;
130
+ }
131
+
132
+ } // namespace
133
+
134
+ void start()
135
+ {
136
+ registerAll();
137
+ if (gTask) return;
138
+ bool any = false;
139
+ for (std::size_t i = 0; i < kButtonCount; ++i) any = any || watched(board::buttons[i]);
140
+ if (!any) return;
141
+ const esp_err_t service = gpio_install_isr_service(0);
142
+ if (service != ESP_OK && service != ESP_ERR_INVALID_STATE) {
143
+ ESP_LOGE(kTag, "GPIO interrupt service unavailable: %s", esp_err_to_name(service));
144
+ return;
145
+ }
146
+ bool configured[kButtonCount] = {};
147
+ for (std::size_t i = 0; i < kButtonCount; ++i)
148
+ configured[i] = watched(board::buttons[i]) && configure(i);
149
+ if (xTaskCreate(run, "buttons", kTaskStackBytes, nullptr, 5, &gTask) != pdPASS) {
150
+ gTask = nullptr;
151
+ ESP_LOGE(kTag, "task unavailable");
152
+ return;
153
+ }
154
+ for (std::size_t i = 0; i < kButtonCount; ++i) {
155
+ const Button &button = board::buttons[i];
156
+ if (!watched(button)) continue;
157
+ if (configured[i] && arm(i))
158
+ ESP_LOGI(kTag, "%s on GPIO%d (%s, active %s) sends %s",
159
+ button.code, button.pin, button.poll ? "polled" : "interrupt",
160
+ button.activeLevel ? "high" : "low", *button.key ? button.key : "no key");
161
+ else
162
+ ESP_LOGE(kTag, "%s on GPIO%d could not be configured", button.code, button.pin);
163
+ }
164
+ }
165
+
166
+ int pmuButton()
167
+ {
168
+ // Registers in table order, so the board's buttons list in the order it
169
+ // declares them whichever of start() and the PMU binding runs first.
170
+ int id = -1;
171
+ for (const Button &button : board::buttons) {
172
+ const int registered = gea::framework::input::registerButton(button.code, button.key, button.keyCode, button.pin);
173
+ if (button.source == Source::Pmu && id < 0) id = registered;
174
+ }
175
+ return id;
176
+ }
177
+
178
+ } // namespace gea::platform::esp32::buttons
179
+
180
+ #else
181
+
182
+ void gea::platform::esp32::buttons::start() {}
183
+ int gea::platform::esp32::buttons::pmuButton() { return -1; }
184
+
185
+ #endif
@@ -0,0 +1,46 @@
1
+ #pragma once
2
+
3
+ #include <cstdint>
4
+
5
+ // A board's physical buttons, delivered to the app as key events whose `code`
6
+ // names the button (see input.h, registerButton). They are declared as JSON,
7
+ // never here: the board's "buttons" (its targets.json entry, composed
8
+ // definition or boards.json alias) plus the app's gea.targets.esp32.buttons.
9
+ // The CLI generates gea_board_buttons.h from that list (boards/buttons.mjs).
10
+ //
11
+ // GPIO buttons are interrupt-driven: an edge wakes one shared task, which
12
+ // debounces and reports the settled level. A button polls only when its entry
13
+ // says "poll": true. The PMU's power key belongs to the PMU binding, which
14
+ // reports it through pmuButton().
15
+ namespace gea::platform::esp32::buttons {
16
+
17
+ enum class Source : std::uint8_t {
18
+ Gpio,
19
+ // The power key of the board's PMU (AXP2101 PWRON).
20
+ Pmu,
21
+ };
22
+
23
+ struct Button {
24
+ // The silkscreen name, PascalCase: "Boot", "Power", "Key1".
25
+ const char *code;
26
+ // The key the button stands in for when an app does not ask for it by
27
+ // name, and its web keyCode. "" and 0 for a button with no alias.
28
+ const char *key = "";
29
+ int keyCode = 0;
30
+ Source source = Source::Gpio;
31
+ int pin = -1;
32
+ // The level the pin reads while the button is held. Active-low buttons
33
+ // get the internal pull-up, active-high ones the pull-down.
34
+ int activeLevel = 0;
35
+ bool poll = false;
36
+ };
37
+
38
+ // Registers the board's buttons and starts watching its GPIO buttons. A no-op
39
+ // on a board that declares none; safe to call more than once.
40
+ void start();
41
+
42
+ // The id (for input::queueButton) of the board's PMU power key, registering
43
+ // the table first if start() has not run yet; -1 when the board has none.
44
+ int pmuButton();
45
+
46
+ } // namespace gea::platform::esp32::buttons
@@ -18,6 +18,7 @@
18
18
  #include "pixel.h"
19
19
  #include "touch.h" // Touchscreen::injectEvent for GEADEV DRAG/SWIPE
20
20
  #include "wifi.h" // network::wifi() for GEADEV PING ip=/mac=
21
+ #include "host/clock.h"
21
22
  #include "host/video.h"
22
23
  #include "ui/node_features.h"
23
24
 
@@ -149,6 +150,9 @@ extern "C" bool geaDisplaySnapshotPrefersPresented() __attribute__((weak));
149
150
  // Defined by engines that rasterize straight into the panel's DMA buffers (gea-threejs):
150
151
  // captures the next presented frame, the only place the real pixels exist.
151
152
  extern "C" bool geaDisplayCapturePresentedFrame(std::uint16_t *dst, int pixelCapacity, int *width, int *height) __attribute__((weak));
153
+ // The scanout buffer the panel is showing, in native pixels, for a screenshot
154
+ // that cannot get a full-screen copy (a board whose PSRAM is spoken for).
155
+ extern "C" const std::uint16_t *gea_display_presented_framebuffer(int *width, int *height) __attribute__((weak));
152
156
 
153
157
  // Mirrors the engine's default (core/packages/engine/ui/tree_render.cpp): a target
154
158
  // that does not fuse the replay into the flush keeps a persistent framebuffer.
@@ -446,9 +450,40 @@ void writeScreenshot()
446
450
  bool heapCapsAllocated = snapshot != nullptr;
447
451
  if (!snapshot) snapshot = static_cast<std::uint16_t *>(std::malloc(pixelCount * sizeof(std::uint16_t)));
448
452
  if (!snapshot) {
449
- std::printf("GEADEV:ERR SCREENSHOT no-memory bytes=%u\n",
450
- static_cast<unsigned>(pixelCount * sizeof(std::uint16_t)));
453
+ // No room for a copy: encode straight from the buffer on the panel. A
454
+ // frame published mid-capture can tear the picture, nothing worse.
455
+ const std::uint16_t *live = gea_display_presented_framebuffer ? gea_display_presented_framebuffer(&width, &height) : nullptr;
456
+ if (!live || static_cast<std::size_t>(width) * static_cast<std::size_t>(height) != pixelCount) {
457
+ std::printf("GEADEV:ERR SCREENSHOT no-memory bytes=%u\n",
458
+ static_cast<unsigned>(pixelCount * sizeof(std::uint16_t)));
459
+ std::fflush(stdout);
460
+ return;
461
+ }
462
+ gea::framework::services::AppState::lock();
463
+ const char *liveAppId = gea::framework::apps::AppManager::currentId();
464
+ std::snprintf(appIdBuffer, sizeof(appIdBuffer), "%s", liveAppId ? liveAppId : "");
465
+ gea::framework::services::AppState::unlock();
466
+ flockfile(stdout);
467
+ std::printf("GEADEV:SCREENSHOT BEGIN width=%d height=%d encoding=rgb565-rle-v1 app=%s\n", width, height, appIdBuffer);
468
+ Base64Writer writer;
469
+ using gea::framework::graphics::pixel::toRgb565;
470
+ std::uint16_t runValue = toRgb565(live[0]);
471
+ std::uint16_t runCount = 1;
472
+ for (std::size_t i = 1; i < pixelCount; i++) {
473
+ const std::uint16_t value = toRgb565(live[i]);
474
+ if (value == runValue && runCount < 65535) {
475
+ runCount++;
476
+ continue;
477
+ }
478
+ writeRun(writer, runCount, runValue);
479
+ runValue = value;
480
+ runCount = 1;
481
+ }
482
+ writeRun(writer, runCount, runValue);
483
+ writer.finish();
484
+ std::printf("GEADEV:SCREENSHOT END\n");
451
485
  std::fflush(stdout);
486
+ funlockfile(stdout);
452
487
  return;
453
488
  }
454
489
 
@@ -687,10 +722,9 @@ void handleSetTime(char *&cursor)
687
722
  return;
688
723
  }
689
724
  const long long epoch = std::strtoll(tok, nullptr, 10);
690
- struct timeval tv;
691
- tv.tv_sec = static_cast<time_t>(epoch);
692
- tv.tv_usec = 0;
693
- if (settimeofday(&tv, nullptr) == 0) {
725
+ // Through the clock surface, not settimeofday directly, so a board with a
726
+ // battery-backed RTC keeps the time across power cycles too.
727
+ if (gea::platform::clock::setEpochMs(static_cast<double>(epoch) * 1000.0)) {
694
728
  struct timeval back;
695
729
  gettimeofday(&back, nullptr);
696
730
  std::printf("GEADEV:OK SETTIME epoch=%lld readback=%lld\n", epoch, static_cast<long long>(back.tv_sec));
@@ -984,6 +1018,26 @@ void handleTap(char *&cursor)
984
1018
  std::printf("GEADEV:OK TAP x=%d y=%d hold_ms=%d\n", x, y, holdMs);
985
1019
  }
986
1020
 
1021
+ // GEADEV PANEL: the RGB panel's refill-miss counters since the last read
1022
+ // (the weak default answers on displays without them).
1023
+ extern "C" __attribute__((weak)) void gea_display_panel_stats(char *out, int size) { std::snprintf(out, static_cast<std::size_t>(size), "unsupported"); }
1024
+
1025
+ // GEADEV PRESS x y / GEADEV RELEASE x y: one half of a touch, so a host can
1026
+ // inspect the UI while a finger is held down (a pressed highlight, say).
1027
+ void handleTouchEdge(char *&cursor, bool down)
1028
+ {
1029
+ int x = 0;
1030
+ int y = 0;
1031
+ if (!parseInt(nextToken(cursor), &x) || !parseInt(nextToken(cursor), &y)) {
1032
+ std::printf("GEADEV:ERR %s usage=GEADEV_%s_x_y\n", down ? "PRESS" : "RELEASE", down ? "PRESS" : "RELEASE");
1033
+ return;
1034
+ }
1035
+ using gea::platform::touch::Touchscreen;
1036
+ using gea::platform::touch::Phase;
1037
+ Touchscreen::injectEvent(down ? Phase::Down : Phase::Up, down, x, y);
1038
+ std::printf("GEADEV:OK %s x=%d y=%d\n", down ? "PRESS" : "RELEASE", x, y);
1039
+ }
1040
+
987
1041
  void handleDrag(char *&cursor)
988
1042
  {
989
1043
  int x1 = 0;
@@ -1077,6 +1131,32 @@ void handleKey(char *&cursor)
1077
1131
  std::printf("GEADEV:OK KEY keycode=%d\n", keyCode);
1078
1132
  }
1079
1133
 
1134
+ // Press a named board button (board.h `buttons`) as if by hand: the app sees
1135
+ // the same keydown/keyup, `code` and all. Optional DOWN or UP sends one edge.
1136
+ // With no name, lists the board's buttons.
1137
+ void handleButton(char *&cursor)
1138
+ {
1139
+ const char *name = nextToken(cursor);
1140
+ const char *edge = nextToken(cursor);
1141
+ const int count = gea::framework::input::buttonCount();
1142
+ if (!name) {
1143
+ std::printf("GEADEV:OK BUTTON count=%d", count);
1144
+ for (int id = 0; id < count; ++id) std::printf(" %s", gea::framework::input::buttonCode(id));
1145
+ std::printf("\n");
1146
+ return;
1147
+ }
1148
+ int id = -1;
1149
+ for (int i = 0; i < count && id < 0; ++i)
1150
+ if (tokenEquals(name, gea::framework::input::buttonCode(i))) id = i;
1151
+ if (id < 0 || (edge && !tokenEquals(edge, "DOWN") && !tokenEquals(edge, "UP"))) {
1152
+ std::printf("GEADEV:ERR BUTTON usage=GEADEV_BUTTON_name_[DOWN|UP]\n");
1153
+ return;
1154
+ }
1155
+ if (!edge || tokenEquals(edge, "DOWN")) gea::framework::input::queueButton(id, true);
1156
+ if (!edge || tokenEquals(edge, "UP")) gea::framework::input::queueButton(id, false);
1157
+ std::printf("GEADEV:OK BUTTON code=%s edge=%s\n", gea::framework::input::buttonCode(id), edge ? edge : "press");
1158
+ }
1159
+
1080
1160
  // Inspect or seed app-facing localStorage from the host. GET is base64 encoded
1081
1161
  // so tab/newline-rich values survive the line protocol, and secret-like key
1082
1162
  // names are denied to avoid echoing credentials into terminal logs.
@@ -2325,6 +2405,8 @@ void handleDiagnosticUpload(char *&cursor)
2325
2405
  // override. Receives the command token plus the remaining argument cursor.
2326
2406
  extern "C" bool geaHandleExtraDevCommand(const char *command, char *args);
2327
2407
 
2408
+ } // namespace
2409
+ namespace {
2328
2410
  void handleCommand(char *line, CommandSource source)
2329
2411
  {
2330
2412
  char *cursor = line;
@@ -2831,10 +2913,20 @@ void handleCommand(char *line, CommandSource source)
2831
2913
  handleTap(cursor);
2832
2914
  } else if (tokenEquals(command, "DRAG")) {
2833
2915
  handleDrag(cursor);
2916
+ } else if (tokenEquals(command, "PRESS")) {
2917
+ handleTouchEdge(cursor, true);
2918
+ } else if (tokenEquals(command, "RELEASE")) {
2919
+ handleTouchEdge(cursor, false);
2920
+ } else if (tokenEquals(command, "PANEL")) {
2921
+ static char stats[768];
2922
+ gea_display_panel_stats(stats, sizeof(stats));
2923
+ std::printf("GEADEV:PANEL %s\n", stats);
2834
2924
  } else if (tokenEquals(command, "ROTARY")) {
2835
2925
  handleRotary(cursor);
2836
2926
  } else if (tokenEquals(command, "KEY")) {
2837
2927
  handleKey(cursor);
2928
+ } else if (tokenEquals(command, "BUTTON")) {
2929
+ handleButton(cursor);
2838
2930
  } else if (tokenEquals(command, "STORAGE")) {
2839
2931
  handleStorage(cursor);
2840
2932
  } else if (tokenEquals(command, "SWIPE")) {
@@ -20,6 +20,11 @@
20
20
 
21
21
  namespace {
22
22
 
23
+ // Set between gea_memory_begin_internal/end on the task that asked: its
24
+ // allocations try internal RAM first. Random-access tables read every pixel
25
+ // cost several times more from PSRAM, whose bus the panel's scanout shares.
26
+ thread_local bool t_preferInternal = false;
27
+
23
28
  #if defined(GEA_HEAP_TRACE)
24
29
  // Diagnosis only: live bytes of C++ heap traffic by allocation size (log2
25
30
  // buckets), printed by the runtime's cycle trace (gea_heap_trace_print), so
@@ -105,7 +110,8 @@ public:
105
110
  {
106
111
  if (size == 0) size = 1;
107
112
 
108
- void *ptr = allocateWithCaps(size, alignment, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
113
+ void *ptr = t_preferInternal ? allocateWithCaps(size, alignment, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) : nullptr;
114
+ if (!ptr) ptr = allocateWithCaps(size, alignment, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
109
115
  if (!ptr) ptr = allocateWithCaps(size, alignment, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
110
116
  if (!ptr) ptr = allocateWithCaps(size, alignment, MALLOC_CAP_8BIT);
111
117
  traceHeap(ptr, true);
@@ -176,6 +182,24 @@ extern "C" void gea_heap_trace_print()
176
182
  }
177
183
  #endif
178
184
 
185
+ // The display's internal staging (the RGB panel's strip buffer) is the one
186
+ // large internal block that can move: it yields before the allocations and
187
+ // takes the tallest strip that still fits after them.
188
+ extern "C" void gea_display_release_internal(std::size_t bytes) __attribute__((weak));
189
+ extern "C" void gea_display_restore_internal() __attribute__((weak));
190
+
191
+ extern "C" void gea_memory_begin_internal(std::size_t bytes)
192
+ {
193
+ if (bytes && gea_display_release_internal) gea_display_release_internal(bytes);
194
+ t_preferInternal = true;
195
+ }
196
+
197
+ extern "C" void gea_memory_end_internal()
198
+ {
199
+ t_preferInternal = false;
200
+ if (gea_display_restore_internal) gea_display_restore_internal();
201
+ }
202
+
179
203
  void *gea::framework::memory::Allocator::allocatePreferSpiram(std::size_t size, std::size_t alignment) {
180
204
  #if defined(GEA_HEAP_TRACE)
181
205
  t_site = reinterpret_cast<std::uintptr_t>(__builtin_return_address(0));