@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
@@ -87,7 +87,10 @@ namespace gea::platform::esp32::chip_bindings::sh8601
87
87
  timer.speed_mode = kBacklightSpeedMode;
88
88
  timer.duty_resolution = LEDC_TIMER_8_BIT;
89
89
  timer.timer_num = kBacklightTimer;
90
- timer.freq_hz = 2000;
90
+ // Waveshare's own AXS15231B boards run the backlight at 50 kHz. At a
91
+ // low kHz rate the switching edges couple into the touch sensor, which
92
+ // then reports phantom contacts along one sense line.
93
+ timer.freq_hz = 50000;
91
94
  timer.clk_cfg = LEDC_AUTO_CLK;
92
95
  ESP_RETURN_ON_ERROR(ledc_timer_config(&timer), kTag, "configure backlight timer failed");
93
96
 
@@ -98,12 +101,19 @@ namespace gea::platform::esp32::chip_bindings::sh8601
98
101
  channel.timer_sel = kBacklightTimer;
99
102
  channel.duty = 0;
100
103
  channel.hpoint = 0;
104
+ #if GEA_EMBEDDED_AXS15231B_BACKLIGHT_ACTIVE_LOW
105
+ // Driven through a PMOS high-side switch (Waveshare 3.49): duty counts
106
+ // the low time, so invert the output to keep duty 0 = dark.
107
+ channel.flags.output_invert = 1;
108
+ #endif
101
109
  return ledc_channel_config(&channel);
102
110
  }
103
111
 
104
112
  esp_err_t setBacklightDuty(int brightnessPercent)
105
113
  {
106
- const std::uint32_t duty = static_cast<std::uint32_t>((brightnessPercent * 255) / 100);
114
+ // An 8-bit channel's full scale is 256, not 255: 255 still leaves one
115
+ // switching edge per period, so full brightness would never be steady.
116
+ const std::uint32_t duty = static_cast<std::uint32_t>((brightnessPercent * 256) / 100);
107
117
  ESP_RETURN_ON_ERROR(ledc_set_duty(kBacklightSpeedMode, kBacklightChannel, duty), kTag, "set backlight duty failed");
108
118
  return ledc_update_duty(kBacklightSpeedMode, kBacklightChannel);
109
119
  }
@@ -0,0 +1,169 @@
1
+ // Elecrow CrowPanel Advance board control: the slow control lines (backlight,
2
+ // touch reset, amplifier mute and shutdown) sit behind one of two parts,
3
+ // depending on the board revision, and this binding probes which at init.
4
+ //
5
+ // V1.0-V1.2: a PCA9557 I/O expander at 0x18. IO1 backlight enable, IO2 touch
6
+ // reset, IO3 amplifier mute, IO4 amplifier shutdown (high = off).
7
+ // V1.3+: an STC8H1K28 board-control MCU at 0x30 that takes single-byte
8
+ // writes with no register: 0..245 backlight (0 brightest, 245
9
+ // off), 248 enable the speaker amplifier, 250 recover touch.
10
+ // Elecrow documents no other command, so none is sent.
11
+ //
12
+ // The board definition names lines by the PCA9557's pin numbers; on an STC
13
+ // board each line maps onto the command that does the same job. Lines with no
14
+ // STC equivalent (mute, amplifier off) succeed without sending anything.
15
+
16
+ #include "chip_bindings/expanders/io_expander.h"
17
+ #include "expanders/pca9557/pca9557.h"
18
+ #include "board.h"
19
+ #include "i2c.h"
20
+
21
+ #include <algorithm>
22
+
23
+ #include "driver/i2c_master.h"
24
+ #include "esp_log.h"
25
+ #include "freertos/FreeRTOS.h"
26
+ #include "freertos/semphr.h"
27
+ #include "freertos/task.h"
28
+
29
+ namespace gea::platform::esp32::chip_bindings::expanders {
30
+ namespace {
31
+
32
+ constexpr char kTag[] = "board_ctrl";
33
+ constexpr std::uint8_t kStcAddress = 0x30;
34
+ constexpr std::uint8_t kPcaAddress = 0x18;
35
+ constexpr int kLineBacklight = 1;
36
+ constexpr int kLineTouchReset = 2;
37
+ constexpr int kLineShutdown = 4;
38
+ constexpr std::uint8_t kStcBacklightOff = 245;
39
+ constexpr std::uint8_t kStcSpeakerOn = 248;
40
+ constexpr std::uint8_t kStcTouchRecover = 250;
41
+ constexpr int kTimeoutMs = 50;
42
+
43
+ enum class Controller { None, Stc, Pca9557 };
44
+
45
+ class ElecrowBoardControl final : public IoExpander, private gea::chips::pca9557::Bus {
46
+ public:
47
+ bool init() override {
48
+ Guard guard(lock_);
49
+ if (controller_ != Controller::None) return true;
50
+ auto bus = gea::platform::i2c::Bus::primary();
51
+ if (!bus.available()) return false;
52
+ auto handle = static_cast<i2c_master_bus_handle_t>(bus.nativeHandle());
53
+ // Give the board-control MCU time to come up after power-on (Elecrow
54
+ // waits 50 ms after starting the bus).
55
+ vTaskDelay(pdMS_TO_TICKS(50));
56
+ if (i2c_master_probe(handle, kStcAddress, kTimeoutMs) == ESP_OK && attach(handle, kStcAddress)) {
57
+ controller_ = Controller::Stc;
58
+ ESP_LOGI(kTag, "STC8H board-control MCU at 0x%02x (board V1.3+)", kStcAddress);
59
+ return true;
60
+ }
61
+ if (i2c_master_probe(handle, kPcaAddress, kTimeoutMs) == ESP_OK && attach(handle, kPcaAddress) &&
62
+ pca_.configure(*this, static_cast<std::uint8_t>(gea::platform::board::expander.initialOutputs),
63
+ static_cast<std::uint8_t>(gea::platform::board::expander.initialDirections))) {
64
+ controller_ = Controller::Pca9557;
65
+ ESP_LOGI(kTag, "PCA9557 expander at 0x%02x (board V1.0-V1.2)", kPcaAddress);
66
+ return true;
67
+ }
68
+ ESP_LOGE(kTag, "neither the STC board MCU (0x30) nor the PCA9557 (0x18) answered");
69
+ return false;
70
+ }
71
+
72
+ bool writePin(int pin, bool high) override {
73
+ if (!init()) return false;
74
+ Guard guard(lock_);
75
+ if (controller_ == Controller::Pca9557) return pca_.setPin(*this, pin, high);
76
+ switch (pin) {
77
+ case kLineBacklight: return send(high ? levelCommand(backlightPercent_) : kStcBacklightOff);
78
+ case kLineTouchReset: {
79
+ if (high) return true;
80
+ // Elecrow's recovery holds INT (GPIO1) low for 120 ms after the
81
+ // command; gt911.cpp holds it across this call and ~110 ms more.
82
+ const bool sent = send(kStcTouchRecover);
83
+ vTaskDelay(pdMS_TO_TICKS(20));
84
+ return sent;
85
+ }
86
+ case kLineShutdown: return high ? true : send(kStcSpeakerOn);
87
+ default: return true;
88
+ }
89
+ }
90
+
91
+ bool readPin(int pin, bool &high) override {
92
+ if (!init()) return false;
93
+ Guard guard(lock_);
94
+ if (controller_ == Controller::Pca9557) return pca_.readPin(*this, pin, high);
95
+ high = false;
96
+ return false;
97
+ }
98
+
99
+ bool setInput(int pin, bool input) override {
100
+ if (!init()) return false;
101
+ Guard guard(lock_);
102
+ if (controller_ == Controller::Pca9557) return pca_.setInput(*this, pin, input);
103
+ return false;
104
+ }
105
+
106
+ bool setLevel(int pin, int percent) override {
107
+ if (pin != kLineBacklight || !init()) return false;
108
+ Guard guard(lock_);
109
+ if (controller_ != Controller::Stc) return false;
110
+ backlightPercent_ = std::clamp(percent, 0, 100);
111
+ return send(backlightPercent_ > 0 ? levelCommand(backlightPercent_) : kStcBacklightOff);
112
+ }
113
+
114
+ const char *name() const {
115
+ return controller_ == Controller::Stc ? "elecrow-stc8h" : controller_ == Controller::Pca9557 ? "pca9557" : "none";
116
+ }
117
+
118
+ private:
119
+ struct Guard {
120
+ explicit Guard(SemaphoreHandle_t mutex) : mutex_(mutex) { xSemaphoreTakeRecursive(mutex_, portMAX_DELAY); }
121
+ ~Guard() { xSemaphoreGiveRecursive(mutex_); }
122
+ SemaphoreHandle_t mutex_;
123
+ };
124
+
125
+ // 100% -> 0 (brightest), 1% -> 244 (dimmest that is still on).
126
+ static std::uint8_t levelCommand(int percent) {
127
+ percent = std::clamp(percent, 1, 100);
128
+ return static_cast<std::uint8_t>(((100 - percent) * (kStcBacklightOff - 1)) / 99);
129
+ }
130
+
131
+ bool attach(i2c_master_bus_handle_t handle, std::uint8_t address) {
132
+ i2c_device_config_t config{};
133
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
134
+ config.device_address = address;
135
+ config.scl_speed_hz = 400000;
136
+ return i2c_master_bus_add_device(handle, &config, &device_) == ESP_OK;
137
+ }
138
+
139
+ bool send(std::uint8_t command) {
140
+ return i2c_master_transmit(device_, &command, 1, kTimeoutMs) == ESP_OK;
141
+ }
142
+
143
+ bool write(std::uint8_t reg, std::uint8_t value) override {
144
+ const std::uint8_t bytes[] = {reg, value};
145
+ return i2c_master_transmit(device_, bytes, 2, kTimeoutMs) == ESP_OK;
146
+ }
147
+
148
+ bool read(std::uint8_t reg, std::uint8_t &value) override {
149
+ return i2c_master_transmit_receive(device_, &reg, 1, &value, 1, kTimeoutMs) == ESP_OK;
150
+ }
151
+
152
+ StaticSemaphore_t mutexStorage_{};
153
+ SemaphoreHandle_t lock_ = xSemaphoreCreateRecursiveMutexStatic(&mutexStorage_);
154
+ i2c_master_dev_handle_t device_ = nullptr;
155
+ gea::chips::pca9557::Driver pca_;
156
+ Controller controller_ = Controller::None;
157
+ int backlightPercent_ = 100;
158
+ };
159
+
160
+ ElecrowBoardControl &instance() {
161
+ static ElecrowBoardControl driver;
162
+ return driver;
163
+ }
164
+
165
+ } // namespace
166
+
167
+ IoExpander &ioExpander() { return instance(); }
168
+ const char *ioExpanderDriverName() { return instance().name(); }
169
+ } // namespace gea::platform::esp32::chip_bindings::expanders
@@ -23,6 +23,10 @@ namespace gea::platform::esp32::chip_bindings::expanders
23
23
  virtual bool readPin(int pin, bool &high) = 0;
24
24
  // Not all expanders support per-pin direction.
25
25
  virtual bool setInput(int pin, bool input) { return false; }
26
+ // A line that carries a level rather than on/off (a backlight driven by a
27
+ // board-control MCU): 0-100 percent. Expanders whose lines are plain
28
+ // outputs decline, and the caller falls back to writePin.
29
+ virtual bool setLevel(int pin, int percent) { return false; }
26
30
  };
27
31
 
28
32
  IoExpander &ioExpander();
@@ -20,6 +20,10 @@ constexpr char kTag[] = "qmi8658";
20
20
  constexpr int kI2cTimeoutMs = 100;
21
21
  constexpr int64_t kRetryIntervalUs = 30000000;
22
22
  constexpr double kStandardGravity = 9.80665;
23
+ #ifndef GEA_EMBEDDED_IMU_INVERT_X
24
+ #define GEA_EMBEDDED_IMU_INVERT_X 0
25
+ #endif
26
+ constexpr int kXSign = GEA_EMBEDDED_IMU_INVERT_X ? -1 : 1;
23
27
  constexpr std::uint8_t kProbeAddresses[] = {
24
28
  gea::chips::qmi8658::kI2cAddress,
25
29
  0x6A,
@@ -114,12 +118,15 @@ public:
114
118
  }
115
119
 
116
120
  void calibrateBias() { std::lock_guard<std::mutex> lock(mutex_); if (ensureInitialized()) driver_.calibrateBias(); }
117
- int tiltX() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? driver_.tiltX() : 0; }
121
+ // A board whose QMI8658 sits mirrored along the screen's horizontal axis
122
+ // (the AMOLED 1.75C) sets GEA_EMBEDDED_IMU_INVERT_X, which flips every
123
+ // X-axis reading -- tilt, acceleration and rotation -- into screen terms.
124
+ int tiltX() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? kXSign * driver_.tiltX() : 0; }
118
125
  int tiltY() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? driver_.tiltY() : 0; }
119
- double accelerationX() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? driver_.accelerationX() : 0.0; }
126
+ double accelerationX() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? kXSign * driver_.accelerationX() : 0.0; }
120
127
  double accelerationY() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? driver_.accelerationY() : 0.0; }
121
128
  double accelerationZ() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? driver_.accelerationZ() : kStandardGravity; }
122
- double gyroscopeX() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? driver_.gyroscopeX() : 0.0; }
129
+ double gyroscopeX() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? kXSign * driver_.gyroscopeX() : 0.0; }
123
130
  double gyroscopeY() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? driver_.gyroscopeY() : 0.0; }
124
131
  double gyroscopeZ() { std::lock_guard<std::mutex> lock(mutex_); return ensureInitialized() ? driver_.gyroscopeZ() : 0.0; }
125
132
 
@@ -16,6 +16,12 @@
16
16
  #include "driver/i2c_master.h"
17
17
  #include "esp_err.h"
18
18
  #include "esp_log.h"
19
+ #include "freertos/FreeRTOS.h"
20
+ #include "freertos/task.h"
21
+ #if GEA_BOARD_HAS_BUTTONS
22
+ #include "input.h"
23
+ #include "services/buttons.h"
24
+ #endif
19
25
 
20
26
  namespace {
21
27
 
@@ -155,6 +161,9 @@ public:
155
161
  initialized_ = pmu_.begin();
156
162
  if (initialized_) ESP_LOGI(kTag, "measurement channels enabled");
157
163
  else ESP_LOGE(kTag, "measurement channel enable failed");
164
+ #if GEA_BOARD_HAS_BUTTONS
165
+ if (initialized_) startPowerKey();
166
+ #endif
158
167
  return initialized_;
159
168
  }
160
169
 
@@ -166,6 +175,55 @@ public:
166
175
  private:
167
176
  Axp2101Binding() : pmu_(bus_) {}
168
177
 
178
+ #if GEA_BOARD_HAS_BUTTONS
179
+ // The power key is the board's Source::Pmu button, when it declares one.
180
+ // The PMU's interrupt line is not wired to the ESP32 on these boards (the
181
+ // vendor's own example polls it too), so its latched edge bits are read on
182
+ // an interval -- the one button with no interrupt to wait on.
183
+ static constexpr int kPowerKeyPollMs = 30;
184
+
185
+ void startPowerKey() {
186
+ if (powerKeyTask_) return;
187
+ powerKeyButton_ = gea::platform::esp32::buttons::pmuButton();
188
+ if (powerKeyButton_ < 0 || !pmu_.enablePowerKeyEdges()) return;
189
+ if (xTaskCreate(powerKeyTask, "pmu_key", 2560, this, 4, &powerKeyTask_) != pdPASS) {
190
+ powerKeyTask_ = nullptr;
191
+ ESP_LOGE(kTag, "power key task unavailable");
192
+ return;
193
+ }
194
+ ESP_LOGI(kTag, "PWR key is button %s (polled)", gea::framework::input::buttonCode(powerKeyButton_));
195
+ }
196
+
197
+ static void powerKeyTask(void *arg) {
198
+ auto *self = static_cast<Axp2101Binding *>(arg);
199
+ bool pressed = false;
200
+ while (true) {
201
+ vTaskDelay(pdMS_TO_TICKS(kPowerKeyPollMs));
202
+ bool fell = false;
203
+ bool rose = false;
204
+ if (!self->pmu_.takePowerKeyEdges(fell, rose) || (!fell && !rose)) continue;
205
+ // Both latched means a whole edge pair fit between two reads: replay
206
+ // them in the order the current state allows.
207
+ for (int step = 0; step < 2; ++step) {
208
+ if (!pressed && fell) {
209
+ fell = false;
210
+ pressed = true;
211
+ } else if (pressed && rose) {
212
+ rose = false;
213
+ pressed = false;
214
+ } else {
215
+ continue;
216
+ }
217
+ gea::framework::input::queueButton(self->powerKeyButton_, pressed);
218
+ ESP_LOGI(kTag, "PWR %s", pressed ? "down" : "up");
219
+ }
220
+ }
221
+ }
222
+
223
+ int powerKeyButton_ = -1;
224
+ TaskHandle_t powerKeyTask_ = nullptr;
225
+ #endif
226
+
169
227
  EspI2cRegisterBus bus_;
170
228
  gea::chips::axp2101::PowerManagementUnit pmu_;
171
229
  bool initialized_ = false;
@@ -0,0 +1,129 @@
1
+ // BM8563 real-time clock on the board's primary I2C bus.
2
+ //
3
+ // The RTC keeps wall-clock time across power cycles (it has its own coin cell),
4
+ // so it is the source of the system clock at boot: restoreSystemTime() reads it
5
+ // and calls settimeofday() unless the chip reports it lost power. Every later
6
+ // wall-clock change made through gea::platform::clock::setEpochMs (the
7
+ // companion's SETTIME, an app setting the time) is written back to the chip,
8
+ // which overrides the host's settimeofday-only default.
9
+
10
+ #include "rtc/bm8563/bm8563.h"
11
+ #include "host/clock.h"
12
+ #include "i2c.h"
13
+
14
+ #include <cmath>
15
+ #include <ctime>
16
+ #include <sys/time.h>
17
+
18
+ #include "driver/i2c_master.h"
19
+ #include "esp_log.h"
20
+
21
+ namespace gea::platform::rtc {
22
+ void restoreSystemTime();
23
+ }
24
+
25
+ namespace {
26
+
27
+ constexpr char kTag[] = "bm8563";
28
+ constexpr int kI2cTimeoutMs = 50;
29
+
30
+ class EspI2cRegisterBus final : public gea::chips::bm8563::RegisterBus {
31
+ public:
32
+ bool attach() {
33
+ if (device_) return true;
34
+ auto bus = gea::platform::i2c::Bus::primary();
35
+ if (!bus.available()) return false;
36
+ i2c_device_config_t config{};
37
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
38
+ config.device_address = gea::chips::bm8563::kI2cAddress;
39
+ config.scl_speed_hz = gea::chips::bm8563::kI2cFrequencyHz;
40
+ return i2c_master_bus_add_device(static_cast<i2c_master_bus_handle_t>(bus.nativeHandle()), &config, &device_) ==
41
+ ESP_OK;
42
+ }
43
+
44
+ bool writeRegisters(std::uint8_t reg, const std::uint8_t *data, std::size_t length) override {
45
+ std::uint8_t buffer[16];
46
+ if (length + 1 > sizeof(buffer)) return false;
47
+ buffer[0] = reg;
48
+ for (std::size_t i = 0; i < length; ++i) buffer[i + 1] = data[i];
49
+ return i2c_master_transmit(device_, buffer, length + 1, kI2cTimeoutMs) == ESP_OK;
50
+ }
51
+
52
+ bool readRegisters(std::uint8_t reg, std::uint8_t *data, std::size_t length) override {
53
+ return i2c_master_transmit_receive(device_, &reg, 1, data, length, kI2cTimeoutMs) == ESP_OK;
54
+ }
55
+
56
+ private:
57
+ i2c_master_dev_handle_t device_ = nullptr;
58
+ };
59
+
60
+ EspI2cRegisterBus g_bus;
61
+ gea::chips::bm8563::RealTimeClock g_clock(g_bus);
62
+ bool g_ready = false;
63
+
64
+ bool ready() {
65
+ if (g_ready) return true;
66
+ if (!g_bus.attach()) {
67
+ ESP_LOGW(kTag, "I2C bus unavailable; no RTC");
68
+ return false;
69
+ }
70
+ if (!g_clock.begin()) {
71
+ ESP_LOGW(kTag, "RTC did not answer at 0x%02x", gea::chips::bm8563::kI2cAddress);
72
+ return false;
73
+ }
74
+ g_ready = true;
75
+ return true;
76
+ }
77
+
78
+ // The RTC holds UTC, which is what the system clock (time_t) counts.
79
+ bool writeUtc(time_t seconds) {
80
+ if (!ready()) return false;
81
+ std::tm utc{};
82
+ gmtime_r(&seconds, &utc);
83
+ gea::chips::bm8563::DateTime time;
84
+ time.year = utc.tm_year + 1900;
85
+ time.month = utc.tm_mon + 1;
86
+ time.day = utc.tm_mday;
87
+ time.weekday = utc.tm_wday;
88
+ time.hour = utc.tm_hour;
89
+ time.minute = utc.tm_min;
90
+ time.second = utc.tm_sec;
91
+ return g_clock.setDateTime(time);
92
+ }
93
+
94
+ } // namespace
95
+
96
+ void gea::platform::rtc::restoreSystemTime() {
97
+ if (!ready()) return;
98
+ gea::chips::bm8563::DateTime time;
99
+ bool valid = false;
100
+ if (!g_clock.readDateTime(time, valid)) {
101
+ ESP_LOGW(kTag, "RTC read failed");
102
+ return;
103
+ }
104
+ if (!valid) {
105
+ ESP_LOGI(kTag, "RTC lost power; system clock left unset until the time is set");
106
+ return;
107
+ }
108
+ std::tm utc{};
109
+ utc.tm_year = time.year - 1900;
110
+ utc.tm_mon = time.month - 1;
111
+ utc.tm_mday = time.day;
112
+ utc.tm_hour = time.hour;
113
+ utc.tm_min = time.minute;
114
+ utc.tm_sec = time.second;
115
+ const time_t seconds = timegm(&utc);
116
+ timeval now{seconds, 0};
117
+ settimeofday(&now, nullptr);
118
+ ESP_LOGI(kTag, "system clock restored from RTC: %04d-%02d-%02d %02d:%02d:%02d UTC", time.year, time.month, time.day,
119
+ time.hour, time.minute, time.second);
120
+ }
121
+
122
+ bool gea::platform::clock::setEpochMs(double timestamp) {
123
+ if (!std::isfinite(timestamp) || timestamp < 0) return false;
124
+ const auto seconds = static_cast<time_t>(timestamp / 1000);
125
+ timeval now{seconds, static_cast<suseconds_t>((timestamp - seconds * 1000.0) * 1000)};
126
+ if (settimeofday(&now, nullptr) != 0) return false;
127
+ if (!writeUtc(seconds)) ESP_LOGW(kTag, "system clock set, but the RTC write failed");
128
+ return true;
129
+ }
@@ -69,12 +69,16 @@ constexpr gpio_num_t kInterruptPin = gea::platform::board::touch.interrupt;
69
69
  constexpr int kPollIntervalMs = 10;
70
70
  constexpr int kTaskPriority = 10;
71
71
 
72
- #if GEA_BOARD_TOUCH_CST9220
73
- // The CST9220 raises INT (low) once per report. Reading between reports finds
74
- // the frame the driver already acknowledged -- it logs "Malformed report
75
- // header" and drops the sample -- so this binding reads on INT instead of on
76
- // a timer. While a finger is down it still re-reads after kLiftTimeoutMs of
77
- // silence, so a missed final report cannot leave a finger stuck down.
72
+ #ifndef GEA_EMBEDDED_TOUCH_POLL
73
+ #define GEA_EMBEDDED_TOUCH_POLL 0
74
+ #endif
75
+ // The controller raises INT (low) once per report. Reading between reports
76
+ // finds a frame the driver already acknowledged (the CST9220 logs "Malformed
77
+ // report header" and drops it), so a board that wires INT reads on it instead
78
+ // of on a timer. While a finger is down it still re-reads after kLiftTimeoutMs
79
+ // of silence, so a missed final report cannot leave a finger stuck down. Only a
80
+ // board without INT, or one that sets GEA_EMBEDDED_TOUCH_POLL, polls.
81
+ constexpr bool kInterruptDriven = kInterruptPin != GPIO_NUM_NC && !GEA_EMBEDDED_TOUCH_POLL;
78
82
  constexpr int kLiftTimeoutMs = 60;
79
83
 
80
84
  void IRAM_ATTR touchInterrupt(void *arg)
@@ -84,7 +88,6 @@ void IRAM_ATTR touchInterrupt(void *arg)
84
88
  if (*task) vTaskNotifyGiveFromISR(*task, &woken);
85
89
  portYIELD_FROM_ISR(woken);
86
90
  }
87
- #endif
88
91
 
89
92
  } // namespace
90
93
 
@@ -129,7 +132,7 @@ esp_err_t TouchController::begin()
129
132
  if (err != ESP_OK) return err;
130
133
 
131
134
  initialized_ = true;
132
- ESP_LOGI(kTag, "Touch controller ready (polling)");
135
+ ESP_LOGI(kTag, "Touch controller ready (%s)", kInterruptDriven ? "interrupt" : "polling");
133
136
  return ESP_OK;
134
137
  }
135
138
 
@@ -189,14 +192,14 @@ esp_err_t TouchController::attachTouchDevice()
189
192
  ESP_LOGE(kTag, "Failed to create CST92xx touch device: %s", esp_err_to_name(err));
190
193
  return err;
191
194
  }
192
- #if GEA_BOARD_TOUCH_CST9220
193
- // The driver configured INT as a falling-edge input; hook it to the task.
194
- err = gpio_install_isr_service(0);
195
- if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) return err;
196
- err = gpio_isr_handler_add(kInterruptPin, touchInterrupt, &task_);
197
- if (err == ESP_OK) err = gpio_intr_enable(kInterruptPin);
198
- if (err != ESP_OK) ESP_LOGE(kTag, "Touch INT GPIO%d hook failed: %s", static_cast<int>(kInterruptPin), esp_err_to_name(err));
199
- #endif
195
+ if constexpr (kInterruptDriven) {
196
+ // The driver configured INT as a falling-edge input; hook it to the task.
197
+ err = gpio_install_isr_service(0);
198
+ if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) return err;
199
+ err = gpio_isr_handler_add(kInterruptPin, touchInterrupt, &task_);
200
+ if (err == ESP_OK) err = gpio_intr_enable(kInterruptPin);
201
+ if (err != ESP_OK) ESP_LOGE(kTag, "Touch INT GPIO%d hook failed: %s", static_cast<int>(kInterruptPin), esp_err_to_name(err));
202
+ }
200
203
  return err;
201
204
  }
202
205
 
@@ -255,13 +258,13 @@ void TouchController::pollLoop()
255
258
  int slotY[kMaxFingers] = {0, 0};
256
259
 
257
260
  while (true) {
258
- #if GEA_BOARD_TOUCH_CST9220
259
- const bool anyActive = slotActive[0] || slotActive[1];
260
- if (ulTaskNotifyTake(pdTRUE, anyActive ? pdMS_TO_TICKS(kLiftTimeoutMs) : portMAX_DELAY) == 0 && !anyActive)
261
- continue;
262
- #else
263
- vTaskDelay(pdMS_TO_TICKS(kPollIntervalMs));
264
- #endif
261
+ if constexpr (kInterruptDriven) {
262
+ const bool anyActive = slotActive[0] || slotActive[1];
263
+ if (ulTaskNotifyTake(pdTRUE, anyActive ? pdMS_TO_TICKS(kLiftTimeoutMs) : portMAX_DELAY) == 0 && !anyActive)
264
+ continue;
265
+ } else {
266
+ vTaskDelay(pdMS_TO_TICKS(kPollIntervalMs));
267
+ }
265
268
 
266
269
  const MultiSample multi = readMulti();
267
270
  if (multi.count > 0) {
@@ -133,6 +133,8 @@ public:
133
133
  enabled_ = enabled;
134
134
 
135
135
  if (!enabled_) {
136
+ scanPending_ = false;
137
+ connectAfterScan_ = false;
136
138
  if (apNetif_ || apOwnsRadio_)
137
139
  stopAccessPoint();
138
140
  if (!initialized_)
@@ -420,8 +422,16 @@ public:
420
422
  }
421
423
 
422
424
  void scan() override {
423
- if (!initialized_ || !enabled_ || scanning_)
425
+ if (!enabled_ || scanning_)
424
426
  return;
427
+ // Opt-in radio: enabled() is already true before the first bring-up, so a
428
+ // scan request is what starts it. Run the scan once the station starts.
429
+ if (!initialized_ || !started_) {
430
+ scanPending_ = true;
431
+ if (!initialized_)
432
+ ensureBringUp();
433
+ return;
434
+ }
425
435
 
426
436
  wifi_scan_config_t scanConfig = {};
427
437
  scanConfig.scan_type = WIFI_SCAN_TYPE_ACTIVE;
@@ -432,7 +442,7 @@ public:
432
442
  scanning_ = true;
433
443
  }
434
444
 
435
- bool scanning() const override { return scanning_; }
445
+ bool scanning() const override { return scanning_ || scanPending_; }
436
446
  int scanCount() const override { return scanCount_; }
437
447
 
438
448
  gea::framework::network::WifiNetwork networkAt(int index) const override {
@@ -486,6 +496,16 @@ private:
486
496
  void handleEvent(esp_event_base_t eventBase, std::int32_t eventId,
487
497
  void *eventData) {
488
498
  if (eventBase == WIFI_EVENT && eventId == WIFI_EVENT_STA_START) {
499
+ // A scan requested before the radio was up runs first; the connect
500
+ // follows from SCAN_DONE so the two do not contend for the radio.
501
+ if (scanPending_) {
502
+ scanPending_ = false;
503
+ scan();
504
+ if (scanning_) {
505
+ connectAfterScan_ = true;
506
+ return;
507
+ }
508
+ }
489
509
  if (enabled_ && ssid_[0] != '\0')
490
510
  esp_wifi_connect();
491
511
  } else if (eventBase == WIFI_EVENT &&
@@ -508,6 +528,11 @@ private:
508
528
  gea::framework::services::NetworkServices::startDeferredServers();
509
529
  } else if (eventBase == WIFI_EVENT && eventId == WIFI_EVENT_SCAN_DONE) {
510
530
  handleScanDone();
531
+ if (connectAfterScan_) {
532
+ connectAfterScan_ = false;
533
+ if (enabled_ && ssid_[0] != '\0')
534
+ esp_wifi_connect();
535
+ }
511
536
  }
512
537
  }
513
538
 
@@ -618,6 +643,7 @@ private:
618
643
  debugState_.store(4, std::memory_order_relaxed);
619
644
  ESP_LOGE(kTag, "esp_wifi_init failed (%s); continuing without WiFi",
620
645
  esp_err_to_name(err));
646
+ scanPending_ = false;
621
647
  return false;
622
648
  }
623
649
  initialized_ = true;
@@ -856,6 +882,8 @@ private:
856
882
  char ip_[16] = "0.0.0.0";
857
883
  char mac_[18] = "";
858
884
  bool scanning_ = false;
885
+ bool scanPending_ = false;
886
+ bool connectAfterScan_ = false;
859
887
  int scanCount_ = 0;
860
888
  char scanSsids_[kScanMax][33]{};
861
889
  int scanRssi_[kScanMax]{};
@@ -0,0 +1,13 @@
1
+ # The board's physical buttons come from JSON (the board's "buttons" plus the
2
+ # app's changes); the CLI generates gea_board_buttons.h and passes its
3
+ # directory. With none, the button service compiles to nothing.
4
+ function(gea_board_buttons target)
5
+ set(_dir "${GEA_BOARD_BUTTONS_DIR}")
6
+ if(NOT _dir AND DEFINED ENV{GEA_BOARD_BUTTONS_DIR})
7
+ set(_dir "$ENV{GEA_BOARD_BUTTONS_DIR}")
8
+ endif()
9
+ if(_dir)
10
+ target_include_directories(${target} PRIVATE "${_dir}")
11
+ target_compile_definitions(${target} PRIVATE GEA_BOARD_HAS_BUTTONS=1)
12
+ endif()
13
+ endfunction()