@geastack/targets 0.1.99 → 0.1.101

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 (48) hide show
  1. package/build-config.json +10 -2
  2. package/package.json +7 -7
  3. package/targets/cmake/geatsc-generated-archive-build.mjs +32 -1
  4. package/targets/esp32/app_project.cmake +2 -2
  5. package/targets/esp32/chip_bindings/audio/echo_cancellation.cpp +392 -28
  6. package/targets/esp32/chip_bindings/audio/echo_cancellation.h +18 -2
  7. package/targets/esp32/chip_bindings/audio/es8311.cpp +117 -13
  8. package/targets/esp32/chip_bindings/displays/co5300.cpp +68 -28
  9. package/targets/esp32/components/gea_framework/CMakeLists.txt +17 -1
  10. package/targets/esp32/components/gea_framework/idf_component.yml +4 -0
  11. package/targets/esp32/components/gea_vpx/CMakeLists.txt +1 -1
  12. package/targets/esp32/connectivity/wifi.cpp +800 -543
  13. package/targets/esp32/display.cpp +121 -26
  14. package/targets/esp32/gea_framework.cmake +22 -28
  15. package/targets/esp32/host_sources.cmake +1 -0
  16. package/targets/esp32/services/comparison_benchmark.h +137 -0
  17. package/targets/esp32/services/comparison_gesture.h +133 -0
  18. package/targets/esp32/services/comparison_input_trace.h +64 -0
  19. package/targets/esp32/services/comparison_upload_capture.h +125 -0
  20. package/targets/esp32/services/debugger.h +634 -0
  21. package/targets/esp32/services/device_control.cpp +965 -20
  22. package/targets/esp32/services/frame_scheduler.cpp +128 -4
  23. package/targets/esp32/services/heap.cpp +125 -0
  24. package/targets/esp32/services/opus_benchmark.h +1 -1
  25. package/targets/esp32/services/storage_service.cpp +119 -104
  26. package/targets/esp32-p4-m5stack-tab5/dependencies.lock +27 -16
  27. package/targets/esp32-s3-m5stack-stopwatch/README.md +14 -9
  28. package/targets/esp32-s3-m5stack-stopwatch/dependencies.lock +54 -1
  29. package/targets/esp32-s3-m5stack-stopwatch/main/CMakeLists.txt +10 -7
  30. package/targets/esp32-s3-m5stack-stopwatch/main/buttons.cpp +80 -0
  31. package/targets/esp32-s3-m5stack-stopwatch/main/idf_component.yml +18 -0
  32. package/targets/esp32-s3-m5stack-stopwatch/main/include/board.h +38 -17
  33. package/targets/esp32-s3-m5stack-stopwatch/main/panel_reset.cpp +132 -129
  34. package/targets/esp32-s3-m5stack-stopwatch/main/power.cpp +253 -0
  35. package/targets/esp32-s3-m5stack-stopwatch/main/touch.cpp +158 -88
  36. package/targets/esp32-s3-m5stack-stopwatch/sdkconfig.defaults +13 -0
  37. package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +30 -3
  38. package/targets/esp32-s3-touch-amoled-2.06/dependencies.lock +10 -148
  39. package/targets/esp32-s31-espressif-mosaico/README.md +34 -0
  40. package/targets/esp32-s31-espressif-mosaico/dependencies.lock +49 -1
  41. package/targets/esp32-s31-espressif-mosaico/main/CMakeLists.txt +23 -5
  42. package/targets/esp32-s31-espressif-mosaico/main/board_power.cpp +117 -0
  43. package/targets/esp32-s31-espressif-mosaico/main/camera.cpp +326 -0
  44. package/targets/esp32-s31-espressif-mosaico/main/idf_component.yml +11 -0
  45. package/targets/esp32-s31-espressif-mosaico/main/include/board.h +23 -0
  46. package/targets/esp32-s31-espressif-mosaico/main/nand_mount.cpp +172 -0
  47. package/targets/esp32-s31-espressif-mosaico/partitions.csv +7 -3
  48. package/targets/esp32-s31-espressif-mosaico/sdkconfig.defaults +24 -0
@@ -4,14 +4,14 @@
4
4
  // https://github.com/m5stack/M5StopWatch-UserDemo/blob/main/main/hal/hal_ioe.cpp
5
5
  // https://github.com/m5stack/M5IOE1/blob/main/src/M5IOE1.h
6
6
  // https://github.com/m5stack/M5PM1/blob/main/src/M5PM1.h
7
- #include "panel_power.h"
8
- #include "i2c.h"
9
7
  #include "driver/i2c_master.h"
10
8
  #include "esp_err.h"
11
9
  #include "esp_log.h"
12
10
  #include "esp_rom_sys.h"
13
11
  #include "freertos/FreeRTOS.h"
14
12
  #include "freertos/task.h"
13
+ #include "i2c.h"
14
+ #include "panel_power.h"
15
15
  #include <cstddef>
16
16
  #include <cstdint>
17
17
  #include <mutex>
@@ -40,160 +40,163 @@ i2c_master_dev_handle_t pm1 = nullptr;
40
40
  i2c_master_dev_handle_t ioe = nullptr;
41
41
  bool initialized = false;
42
42
 
43
- void delayMs(std::uint32_t milliseconds)
44
- {
45
- vTaskDelay(pdMS_TO_TICKS(milliseconds));
43
+ void delayMs(std::uint32_t milliseconds) {
44
+ vTaskDelay(pdMS_TO_TICKS(milliseconds));
46
45
  }
47
46
 
48
- esp_err_t readRegisters(i2c_master_dev_handle_t device, std::uint8_t reg,
49
- std::uint8_t *bytes, std::size_t size)
50
- {
51
- esp_err_t result = ESP_FAIL;
52
- for (int attempt = 0; attempt < 3; ++attempt) {
53
- result = i2c_master_transmit_receive(device, &reg, 1, bytes, size, 100);
54
- // IOE1/PM1 firmware needs processing time between transactions.
55
- esp_rom_delay_us(500);
56
- if (result == ESP_OK) return result;
57
- if (attempt < 2) delayMs(50);
47
+ esp_err_t readRegisters(i2c_master_dev_handle_t device, std::uint8_t reg, std::uint8_t* bytes,
48
+ std::size_t size) {
49
+ esp_err_t result = ESP_FAIL;
50
+ for (int attempt = 0; attempt < 3; ++attempt) {
51
+ result = i2c_master_transmit_receive(device, &reg, 1, bytes, size, 100);
52
+ // IOE1/PM1 firmware needs processing time between transactions.
53
+ esp_rom_delay_us(500);
54
+ if (result == ESP_OK) {
55
+ return result;
56
+ }
57
+ if (attempt < 2) {
58
+ delayMs(50);
58
59
  }
59
- return result;
60
+ }
61
+ return result;
60
62
  }
61
63
 
62
- esp_err_t writeRegisters(i2c_master_dev_handle_t device, const std::uint8_t *bytes,
63
- std::size_t size)
64
- {
65
- esp_err_t result = ESP_FAIL;
66
- for (int attempt = 0; attempt < 3; ++attempt) {
67
- result = i2c_master_transmit(device, bytes, size, 100);
68
- esp_rom_delay_us(500);
69
- if (result == ESP_OK) return result;
70
- if (attempt < 2) delayMs(50);
64
+ esp_err_t writeRegisters(i2c_master_dev_handle_t device, const std::uint8_t* bytes,
65
+ std::size_t size) {
66
+ esp_err_t result = ESP_FAIL;
67
+ for (int attempt = 0; attempt < 3; ++attempt) {
68
+ result = i2c_master_transmit(device, bytes, size, 100);
69
+ esp_rom_delay_us(500);
70
+ if (result == ESP_OK) {
71
+ return result;
72
+ }
73
+ if (attempt < 2) {
74
+ delayMs(50);
71
75
  }
72
- return result;
76
+ }
77
+ return result;
73
78
  }
74
79
 
75
- std::uint8_t read8(i2c_master_dev_handle_t device, std::uint8_t reg)
76
- {
77
- std::uint8_t value = 0;
78
- ESP_ERROR_CHECK(readRegisters(device, reg, &value, 1));
79
- return value;
80
+ std::uint8_t read8(i2c_master_dev_handle_t device, std::uint8_t reg) {
81
+ std::uint8_t value = 0;
82
+ ESP_ERROR_CHECK(readRegisters(device, reg, &value, 1));
83
+ return value;
80
84
  }
81
85
 
82
- std::uint16_t read16(i2c_master_dev_handle_t device, std::uint8_t reg)
83
- {
84
- std::uint8_t bytes[2]{};
85
- ESP_ERROR_CHECK(readRegisters(device, reg, bytes, sizeof(bytes)));
86
- return static_cast<std::uint16_t>(bytes[0]) |
87
- (static_cast<std::uint16_t>(bytes[1]) << 8);
86
+ std::uint16_t read16(i2c_master_dev_handle_t device, std::uint8_t reg) {
87
+ std::uint8_t bytes[2]{};
88
+ ESP_ERROR_CHECK(readRegisters(device, reg, bytes, sizeof(bytes)));
89
+ return static_cast<std::uint16_t>(bytes[0]) | (static_cast<std::uint16_t>(bytes[1]) << 8);
88
90
  }
89
91
 
90
- void write8(i2c_master_dev_handle_t device, std::uint8_t reg, std::uint8_t value)
91
- {
92
- const std::uint8_t bytes[]{reg, value};
93
- ESP_ERROR_CHECK(writeRegisters(device, bytes, sizeof(bytes)));
94
- if (read8(device, reg) != value) ESP_ERROR_CHECK(ESP_ERR_INVALID_RESPONSE);
92
+ void write8(i2c_master_dev_handle_t device, std::uint8_t reg, std::uint8_t value) {
93
+ const std::uint8_t bytes[]{reg, value};
94
+ ESP_ERROR_CHECK(writeRegisters(device, bytes, sizeof(bytes)));
95
+ if (read8(device, reg) != value) {
96
+ ESP_ERROR_CHECK(ESP_ERR_INVALID_RESPONSE);
97
+ }
95
98
  }
96
99
 
97
- void write16(i2c_master_dev_handle_t device, std::uint8_t reg, std::uint16_t value)
98
- {
99
- const std::uint8_t bytes[]{reg, static_cast<std::uint8_t>(value),
100
- static_cast<std::uint8_t>(value >> 8)};
101
- ESP_ERROR_CHECK(writeRegisters(device, bytes, sizeof(bytes)));
102
- if (read16(device, reg) != value) ESP_ERROR_CHECK(ESP_ERR_INVALID_RESPONSE);
100
+ void write16(i2c_master_dev_handle_t device, std::uint8_t reg, std::uint16_t value) {
101
+ const std::uint8_t bytes[]{reg, static_cast<std::uint8_t>(value),
102
+ static_cast<std::uint8_t>(value >> 8)};
103
+ ESP_ERROR_CHECK(writeRegisters(device, bytes, sizeof(bytes)));
104
+ if (read16(device, reg) != value) {
105
+ ESP_ERROR_CHECK(ESP_ERR_INVALID_RESPONSE);
106
+ }
103
107
  }
104
108
 
105
- void updateOutput(std::uint16_t mask, std::uint16_t value)
106
- {
107
- const std::uint16_t previous = read16(ioe, kIoeOutput);
108
- write16(ioe, kIoeOutput, (previous & ~mask) | (value & mask));
109
+ void updateOutput(std::uint16_t mask, std::uint16_t value) {
110
+ const std::uint16_t previous = read16(ioe, kIoeOutput);
111
+ write16(ioe, kIoeOutput, (previous & ~mask) | (value & mask));
109
112
  }
110
113
 
111
- i2c_master_dev_handle_t attachDevice(i2c_master_bus_handle_t bus,
112
- std::uint8_t address)
113
- {
114
- // The first START wakes a sleeping PM1/IOE1 and may legitimately NACK.
115
- (void)i2c_master_probe(bus, address, 20);
116
- delayMs(10);
117
- i2c_device_config_t config{};
118
- config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
119
- config.device_address = address;
120
- config.scl_speed_hz = 100000;
121
- i2c_master_dev_handle_t device = nullptr;
122
- ESP_ERROR_CHECK(i2c_master_bus_add_device(bus, &config, &device));
123
- return device;
114
+ i2c_master_dev_handle_t attachDevice(i2c_master_bus_handle_t bus, std::uint8_t address) {
115
+ // The first START wakes a sleeping PM1/IOE1 and may legitimately NACK.
116
+ (void)i2c_master_probe(bus, address, 20);
117
+ delayMs(10);
118
+ i2c_device_config_t config{};
119
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
120
+ config.device_address = address;
121
+ config.scl_speed_hz = 100000;
122
+ i2c_master_dev_handle_t device = nullptr;
123
+ ESP_ERROR_CHECK(i2c_master_bus_add_device(bus, &config, &device));
124
+ return device;
124
125
  }
125
126
 
126
- void initializeLocked()
127
- {
128
- if (initialized) return;
129
- const auto sharedBus = gea::platform::i2c::Bus::primary();
130
- if (!sharedBus.available()) ESP_ERROR_CHECK(ESP_ERR_INVALID_STATE);
131
- const auto bus = static_cast<i2c_master_bus_handle_t>(sharedBus.nativeHandle());
132
-
133
- pm1 = attachDevice(bus, kPm1Address);
134
- std::uint8_t pm1Identity[4]{};
135
- ESP_ERROR_CHECK(readRegisters(pm1, 0x00, pm1Identity, sizeof(pm1Identity)));
136
- // Factory firmware disables both I2C sleep and the PM1 watchdog. The
137
- // watchdog is independent of ESP-IDF's watchdog and otherwise resets us.
138
- for (int attempt = 0; attempt < 2; ++attempt) {
139
- write8(pm1, kPm1I2cConfig, read8(pm1, kPm1I2cConfig) & ~0x0FU);
140
- }
141
- write8(pm1, kPm1Watchdog, 0);
142
-
143
- std::uint8_t ioeIdentity[3]{};
144
- std::uint8_t ioeAddress = 0x4F;
127
+ void initializeLocked() {
128
+ if (initialized) {
129
+ return;
130
+ }
131
+ const auto sharedBus = gea::platform::i2c::Bus::primary();
132
+ if (!sharedBus.available()) {
133
+ ESP_ERROR_CHECK(ESP_ERR_INVALID_STATE);
134
+ }
135
+ const auto bus = static_cast<i2c_master_bus_handle_t>(sharedBus.nativeHandle());
136
+
137
+ pm1 = attachDevice(bus, kPm1Address);
138
+ std::uint8_t pm1Identity[4]{};
139
+ ESP_ERROR_CHECK(readRegisters(pm1, 0x00, pm1Identity, sizeof(pm1Identity)));
140
+ // Factory firmware disables both I2C sleep and the PM1 watchdog. The
141
+ // watchdog is independent of ESP-IDF's watchdog and otherwise resets us.
142
+ for (int attempt = 0; attempt < 2; ++attempt) {
143
+ write8(pm1, kPm1I2cConfig, read8(pm1, kPm1I2cConfig) & ~0x0FU);
144
+ }
145
+ write8(pm1, kPm1Watchdog, 0);
146
+
147
+ std::uint8_t ioeIdentity[3]{};
148
+ std::uint8_t ioeAddress = 0x4F;
149
+ ioe = attachDevice(bus, ioeAddress);
150
+ if (readRegisters(ioe, 0x00, ioeIdentity, sizeof(ioeIdentity)) != ESP_OK) {
151
+ ESP_ERROR_CHECK(i2c_master_bus_rm_device(ioe));
152
+ ioeAddress = 0x6F;
145
153
  ioe = attachDevice(bus, ioeAddress);
146
- if (readRegisters(ioe, 0x00, ioeIdentity, sizeof(ioeIdentity)) != ESP_OK) {
147
- ESP_ERROR_CHECK(i2c_master_bus_rm_device(ioe));
148
- ioeAddress = 0x6F;
149
- ioe = attachDevice(bus, ioeAddress);
150
- ESP_ERROR_CHECK(readRegisters(ioe, 0x00, ioeIdentity, sizeof(ioeIdentity)));
151
- }
152
- for (int attempt = 0; attempt < 2; ++attempt) {
153
- write8(ioe, kIoeI2cConfig, read8(ioe, kIoeI2cConfig) & ~0x0FU);
154
- }
155
-
156
- // PIN_8 can be PWM channel 2. A warm factory state must not override the
157
- // display's power latch with PWM, pull resistors or an open-drain output.
158
- write16(ioe, kIoePowerPwm, read16(ioe, kIoePowerPwm) & ~0x8000U);
159
- updateOutput(kPanelPins, kOledPower); // Hold resets low before driving pins.
160
- write16(ioe, kIoePullUp, read16(ioe, kIoePullUp) & ~kPanelPins);
161
- write16(ioe, kIoePullDown, read16(ioe, kIoePullDown) & ~kPanelPins);
162
- write16(ioe, kIoeDrive, read16(ioe, kIoeDrive) & ~kPanelPins);
163
- write16(ioe, kIoeMode, read16(ioe, kIoeMode) | kPanelPins);
164
-
165
- bool powered = false;
166
- for (int attempt = 0; attempt < 4; ++attempt) {
167
- delayMs(80);
168
- if ((read16(ioe, kIoeInput) & kOledPower) != 0) {
169
- powered = true;
170
- break;
171
- }
172
- updateOutput(kOledPower, kOledPower);
154
+ ESP_ERROR_CHECK(readRegisters(ioe, 0x00, ioeIdentity, sizeof(ioeIdentity)));
155
+ }
156
+ for (int attempt = 0; attempt < 2; ++attempt) {
157
+ write8(ioe, kIoeI2cConfig, read8(ioe, kIoeI2cConfig) & ~0x0FU);
158
+ }
159
+
160
+ // PIN_8 can be PWM channel 2. A warm factory state must not override the
161
+ // display's power latch with PWM, pull resistors or an open-drain output.
162
+ write16(ioe, kIoePowerPwm, read16(ioe, kIoePowerPwm) & ~0x8000U);
163
+ updateOutput(kPanelPins, kOledPower); // Hold resets low before driving pins.
164
+ write16(ioe, kIoePullUp, read16(ioe, kIoePullUp) & ~kPanelPins);
165
+ write16(ioe, kIoePullDown, read16(ioe, kIoePullDown) & ~kPanelPins);
166
+ write16(ioe, kIoeDrive, read16(ioe, kIoeDrive) & ~kPanelPins);
167
+ write16(ioe, kIoeMode, read16(ioe, kIoeMode) | kPanelPins);
168
+
169
+ bool powered = false;
170
+ for (int attempt = 0; attempt < 4; ++attempt) {
171
+ delayMs(80);
172
+ if ((read16(ioe, kIoeInput) & kOledPower) != 0) {
173
+ powered = true;
174
+ break;
173
175
  }
174
- if (!powered) ESP_ERROR_CHECK(ESP_ERR_INVALID_STATE);
175
- updateOutput(kResets, kResets);
176
- delayMs(50);
177
- initialized = true;
178
- ESP_LOGI(kTag, "PM1 watchdog disabled; IOE1 0x%02X AMOLED power and resets ready",
179
- ioeAddress);
176
+ updateOutput(kOledPower, kOledPower);
177
+ }
178
+ if (!powered) {
179
+ ESP_ERROR_CHECK(ESP_ERR_INVALID_STATE);
180
+ }
181
+ updateOutput(kResets, kResets);
182
+ delayMs(50);
183
+ initialized = true;
184
+ ESP_LOGI(kTag, "PM1 watchdog disabled; IOE1 0x%02X AMOLED power and resets ready", ioeAddress);
180
185
  }
181
186
  } // namespace
182
187
 
183
188
  namespace gea::platform::board {
184
- void prepareDisplayPanel()
185
- {
186
- const std::lock_guard<std::mutex> guard(powerMutex);
187
- initializeLocked();
189
+ void prepareDisplayPanel() {
190
+ const std::lock_guard<std::mutex> guard(powerMutex);
191
+ initializeLocked();
188
192
  }
189
193
 
190
- void resetTouchPanel()
191
- {
192
- const std::lock_guard<std::mutex> guard(powerMutex);
193
- initializeLocked();
194
- updateOutput(kTouchReset, 0);
195
- delayMs(10);
196
- updateOutput(kTouchReset, kTouchReset);
197
- delayMs(50);
194
+ void resetTouchPanel() {
195
+ const std::lock_guard<std::mutex> guard(powerMutex);
196
+ initializeLocked();
197
+ updateOutput(kTouchReset, 0);
198
+ delayMs(10);
199
+ updateOutput(kTouchReset, kTouchReset);
200
+ delayMs(50);
198
201
  }
199
202
  } // namespace gea::platform::board
@@ -0,0 +1,253 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ #include "power.h"
3
+ #include "board.h"
4
+ #include "driver/i2c_master.h"
5
+ #include "esp_timer.h"
6
+ #include "freertos/FreeRTOS.h"
7
+ #include "freertos/task.h"
8
+ #include "host/clock.h"
9
+ #include "i2c.h"
10
+ #include <algorithm>
11
+ #include <atomic>
12
+ #include <ctime>
13
+ #include <mutex>
14
+ #include <sys/time.h>
15
+
16
+ namespace {
17
+ i2c_master_dev_handle_t ioe = nullptr, pm = nullptr, rtc = nullptr;
18
+ std::mutex busMutex;
19
+ std::atomic<int64_t> motorUntil{0};
20
+ int filteredMv = 0;
21
+ int64_t nextBattery = 0;
22
+ int batteryLevel = -1;
23
+ bool externalPower = false;
24
+ bool ready = false;
25
+
26
+ bool attach(i2c_master_dev_handle_t& dev, int address) {
27
+ auto bus =
28
+ static_cast<i2c_master_bus_handle_t>(gea::platform::i2c::Bus::primary().nativeHandle());
29
+ if (!bus) {
30
+ return false;
31
+ }
32
+ i2c_device_config_t config{};
33
+ config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
34
+ config.device_address = address;
35
+ config.scl_speed_hz = 100000;
36
+ return i2c_master_bus_add_device(bus, &config, &dev) == ESP_OK;
37
+ }
38
+
39
+ bool read(i2c_master_dev_handle_t dev, uint8_t reg, uint8_t* data, size_t count) {
40
+ if (!dev) {
41
+ return false;
42
+ }
43
+ bool ok = i2c_master_transmit_receive(dev, &reg, 1, data, count, 50) == ESP_OK;
44
+ // PM1 / IOE firmware needs transaction processing time.
45
+ if (dev == ioe || dev == pm) {
46
+ vTaskDelay(1);
47
+ }
48
+ return ok;
49
+ }
50
+
51
+ bool write(i2c_master_dev_handle_t dev, uint8_t reg, const uint8_t* data, size_t count) {
52
+ if (!dev || count > 8) {
53
+ return false;
54
+ }
55
+ uint8_t bytes[9]{reg};
56
+ std::copy(data, data + count, bytes + 1);
57
+ bool ok = i2c_master_transmit(dev, bytes, count + 1, 50) == ESP_OK;
58
+ if (dev == ioe || dev == pm) {
59
+ vTaskDelay(1);
60
+ }
61
+ return ok;
62
+ }
63
+
64
+ uint16_t read16(i2c_master_dev_handle_t dev, uint8_t reg) {
65
+ uint8_t data[2]{};
66
+ read(dev, reg, data, 2);
67
+ return data[0] | (data[1] << 8);
68
+ }
69
+
70
+ bool write16(i2c_master_dev_handle_t dev, uint8_t reg, uint16_t value) {
71
+ uint8_t data[]{uint8_t(value), uint8_t(value >> 8)};
72
+ return write(dev, reg, data, 2);
73
+ }
74
+
75
+ void pin(int bit, bool level) {
76
+ uint16_t mask = 1 << bit;
77
+ write16(ioe, 0x09, read16(ioe, 0x09) & ~mask);
78
+ write16(ioe, 0x0B, read16(ioe, 0x0B) & ~mask);
79
+ write16(ioe, 0x13, read16(ioe, 0x13) & ~mask);
80
+ write16(ioe, 0x03, read16(ioe, 0x03) | mask);
81
+ write16(ioe, 0x05, (read16(ioe, 0x05) & ~mask) | (level ? mask : 0));
82
+ }
83
+
84
+ int bcd(uint8_t n) {
85
+ return (n >> 4) * 10 + (n & 15);
86
+ }
87
+
88
+ uint8_t packed(int n) {
89
+ return ((n / 10) << 4) | (n % 10);
90
+ }
91
+
92
+ void readClock() {
93
+ uint8_t d[7]{};
94
+ if (!read(rtc, 0x10, d, 7)) {
95
+ return;
96
+ }
97
+ tm time{};
98
+ time.tm_sec = bcd(d[0] & 127);
99
+ time.tm_min = bcd(d[1] & 127);
100
+ time.tm_hour = bcd(d[2] & 63);
101
+ time.tm_mday = bcd(d[4] & 63);
102
+ time.tm_mon = bcd(d[5] & 31) - 1;
103
+ time.tm_year = bcd(d[6]) + 100;
104
+ if (time.tm_mday < 1 || time.tm_mon < 0 || time.tm_mon > 11 || time.tm_hour > 23) {
105
+ return;
106
+ }
107
+ timeval value{mktime(&time), 0};
108
+ settimeofday(&value, nullptr);
109
+ }
110
+
111
+ void updateBattery() {
112
+ const auto now = esp_timer_get_time();
113
+ if (now < nextBattery) {
114
+ return;
115
+ }
116
+ nextBattery = now + 1000000;
117
+ uint8_t bytes[2]{};
118
+ if (read(pm, 0x22, bytes, 2)) {
119
+ const int mv = bytes[0] | (bytes[1] << 8);
120
+ filteredMv = filteredMv ? (filteredMv * 7 + mv + 4) / 8 : mv;
121
+ batteryLevel = std::clamp((filteredMv - 3300) * 100 / 900, 0, 100);
122
+ }
123
+ if (read(pm, 0x24, bytes, 2)) {
124
+ externalPower = (bytes[0] | (bytes[1] << 8)) > 4000;
125
+ }
126
+ }
127
+
128
+ void motorTask(void*) {
129
+ while (true) {
130
+ {
131
+ std::lock_guard<std::mutex> lock(busMutex);
132
+ if (motorUntil > 0 && esp_timer_get_time() >= motorUntil) {
133
+ write16(ioe, 0x1B, 0x8000);
134
+ motorUntil = 0;
135
+ }
136
+ }
137
+ vTaskDelay(pdMS_TO_TICKS(5));
138
+ }
139
+ }
140
+ } // namespace
141
+
142
+ namespace gea::platform::power {
143
+ bool Power::init() {
144
+ std::lock_guard<std::mutex> lock(busMutex);
145
+ if (ready) {
146
+ return true;
147
+ }
148
+ gea::platform::board::prepareDisplayPanel();
149
+ if (!attach(ioe, 0x4F) || !attach(pm, 0x6E) || !attach(rtc, 0x32)) {
150
+ return false;
151
+ }
152
+ uint8_t identity[3]{};
153
+ if (!read(ioe, 0, identity, 3)) {
154
+ i2c_master_bus_rm_device(ioe);
155
+ ioe = nullptr;
156
+ if (!attach(ioe, 0x6F) || !read(ioe, 0, identity, 3)) {
157
+ return false;
158
+ }
159
+ }
160
+ pin(0, false);
161
+ pin(2, true);
162
+ pin(8, false);
163
+ pin(9, false);
164
+ gpio_set_direction(GPIO_NUM_14, GPIO_MODE_OUTPUT);
165
+ gpio_set_level(GPIO_NUM_14, 0);
166
+ write16(ioe, 0x25, 5000);
167
+ write16(ioe, 0x1B, 0x8000);
168
+ setenv("TZ", "UTC0", 1);
169
+ tzset();
170
+ readClock();
171
+ updateBattery();
172
+ if (xTaskCreate(motorTask, "stopwatch_motor", 3072, nullptr, 5, nullptr) != pdPASS) {
173
+ return false;
174
+ }
175
+ ready = true;
176
+ return true;
177
+ }
178
+
179
+ int Power::batteryPercent() {
180
+ std::lock_guard<std::mutex> lock(busMutex);
181
+ updateBattery();
182
+ return batteryLevel;
183
+ }
184
+
185
+ bool Power::charging() {
186
+ std::lock_guard<std::mutex> lock(busMutex);
187
+ updateBattery();
188
+ return externalPower;
189
+ }
190
+
191
+ bool Power::vibrate(double duration, int strength) {
192
+ std::lock_guard<std::mutex> lock(busMutex);
193
+ if (!ready) {
194
+ return false;
195
+ }
196
+ #if defined(GEA_EMBEDDED_COMPARISON_BENCHMARK) && GEA_EMBEDDED_COMPARISON_BENCHMARK
197
+ const int duty = 0;
198
+ (void)strength;
199
+ #else
200
+ const int duty = duration > 0 && strength > 0 ? 25 + std::clamp(strength, 0, 100) * 75 / 100 : 0;
201
+ #endif
202
+ const bool ok = write16(ioe, 0x1B, uint16_t(duty * 4095 / 100) | 0x8000);
203
+ motorUntil = duty ? esp_timer_get_time() + int64_t(duration * 1000) : 0;
204
+ return ok;
205
+ }
206
+ } // namespace gea::platform::power
207
+
208
+ namespace gea::platform::board {
209
+ bool setSpeakerPower(bool enabled) {
210
+ #if defined(GEA_EMBEDDED_COMPARISON_BENCHMARK) && GEA_EMBEDDED_COMPARISON_BENCHMARK
211
+ enabled = false;
212
+ #endif
213
+ std::lock_guard<std::mutex> lock(busMutex);
214
+ if (!ready) {
215
+ return false;
216
+ }
217
+ pin(9, enabled);
218
+ gpio_set_level(GPIO_NUM_14, enabled ? 1 : 0);
219
+ vTaskDelay(pdMS_TO_TICKS(10));
220
+ return true;
221
+ }
222
+ } // namespace gea::platform::board
223
+
224
+ namespace gea::platform::clock {
225
+ bool setEpochMs(double timestamp) {
226
+ const time_t value = time_t(timestamp / 1000);
227
+ tm time{};
228
+ gmtime_r(&value, &time);
229
+ if (time.tm_year < 100 || time.tm_year > 199) {
230
+ return false;
231
+ }
232
+ std::lock_guard<std::mutex> lock(busMutex);
233
+ uint8_t control = 0;
234
+ if (!read(rtc, 0x1E, &control, 1)) {
235
+ return false;
236
+ }
237
+ const uint8_t stopped = control | 0x40;
238
+ if (!write(rtc, 0x1E, &stopped, 1)) {
239
+ return false;
240
+ }
241
+ const uint8_t d[]{packed(time.tm_sec), packed(time.tm_min), packed(time.tm_hour),
242
+ packed(time.tm_wday), packed(time.tm_mday), packed(time.tm_mon + 1),
243
+ packed(time.tm_year - 100)};
244
+ bool ok = write(rtc, 0x10, d, 7);
245
+ control &= ~0x40;
246
+ ok = write(rtc, 0x1E, &control, 1) && ok;
247
+ if (ok) {
248
+ timeval now{value, 0};
249
+ settimeofday(&now, nullptr);
250
+ }
251
+ return ok;
252
+ }
253
+ } // namespace gea::platform::clock