@geastack/targets 0.1.107 → 0.1.108
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.
- package/build-config.json +73 -0
- package/package.json +6 -6
- package/targets/esp32/chip_bindings/displays/sh8601.cpp +12 -2
- package/targets/esp32/chip_bindings/imu/qmi8658.cpp +10 -3
- package/targets/esp32/chip_bindings/power/axp2101.cpp +58 -0
- package/targets/esp32/chip_bindings/touch/cst9217.cpp +26 -23
- package/targets/esp32/connectivity/wifi.cpp +30 -2
- package/targets/esp32/gea_board_buttons.cmake +13 -0
- package/targets/esp32/gpio.cpp +124 -1
- package/targets/esp32/gpio_pins.h +17 -0
- package/targets/esp32/services/buttons.cpp +185 -0
- package/targets/esp32/services/buttons.h +46 -0
- package/targets/esp32/services/device_control.cpp +28 -0
- package/targets/esp32-s3-touch-amoled-1.75c/CMakeLists.txt +11 -0
- package/targets/esp32-s3-touch-amoled-1.75c/main/CMakeLists.txt +54 -0
- package/targets/esp32-s3-touch-amoled-1.75c/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-touch-amoled-1.75c/main/gea-dependencies.json +42 -0
- package/targets/esp32-s3-touch-amoled-1.75c/main/idf_component.yml +10 -0
- package/targets/esp32-s3-touch-amoled-1.75c/main/include/board.h +113 -0
- package/targets/esp32-s3-touch-amoled-1.75c/partitions.csv +9 -0
- package/targets/esp32-s3-touch-amoled-1.75c/sdkconfig.defaults +279 -0
- package/targets/esp32-s3-touch-amoled-1.8/main/CMakeLists.txt +10 -1
- package/targets/esp32-s3-touch-amoled-1.8/main/app_main.cpp +2 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/CMakeLists.txt +13 -0
- package/targets/esp32-s3-touch-amoled-2.06/main/app_main.cpp +2 -0
- package/targets/esp32-s3-touch-amoled-2.06/sdkconfig.defaults +5 -3
- package/targets/esp32-s3-touch-lcd-3.49/CMakeLists.txt +11 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/CMakeLists.txt +82 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/Kconfig.projbuild +12 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/gea-dependencies.json +42 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/idf_component.yml +8 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/include/axs15231b_panel_init.h +9 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/include/board.h +160 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/panel_reset.cpp +93 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/power.cpp +60 -0
- package/targets/esp32-s3-touch-lcd-3.49/main/touch.cpp +327 -0
- package/targets/esp32-s3-touch-lcd-3.49/partitions.csv +7 -0
- package/targets/esp32-s3-touch-lcd-3.49/sdkconfig.defaults +279 -0
- package/targets.json +60 -1
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "driver/gpio.h"
|
|
4
|
+
#include "driver/spi_master.h"
|
|
5
|
+
|
|
6
|
+
// Waveshare ESP32-S3-Touch-LCD-3.49: a 172x640 IPS bar on an AXS15231B QSPI
|
|
7
|
+
// controller. Pin assignments: the vendor's ESP-IDF examples
|
|
8
|
+
// (Examples/ESP-IDF/*/main/user_config.h and codec_board/board_cfg.txt,
|
|
9
|
+
// board S3_LCD_3_49).
|
|
10
|
+
//
|
|
11
|
+
// The board has two I2C buses. The touch half of the AXS15231B answers at 0x3b
|
|
12
|
+
// on its own pair (GPIO17/18); everything else -- the QMI8658 IMU, PCF85063
|
|
13
|
+
// RTC, ES8311 DAC, ES7210 microphone ADC and TCA9554 expander -- shares the
|
|
14
|
+
// GPIO47/48 bus, which is the runtime's primary bus.
|
|
15
|
+
namespace gea::platform::board {
|
|
16
|
+
|
|
17
|
+
struct I2cBusConfig {
|
|
18
|
+
gpio_num_t sda;
|
|
19
|
+
gpio_num_t scl;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
struct QspiDisplayConfig {
|
|
23
|
+
spi_host_device_t spiHost;
|
|
24
|
+
gpio_num_t cs;
|
|
25
|
+
gpio_num_t pclk;
|
|
26
|
+
gpio_num_t data0;
|
|
27
|
+
gpio_num_t data1;
|
|
28
|
+
gpio_num_t data2;
|
|
29
|
+
gpio_num_t data3;
|
|
30
|
+
gpio_num_t reset;
|
|
31
|
+
gpio_num_t te;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
struct TouchConfig {
|
|
35
|
+
I2cBusConfig bus;
|
|
36
|
+
gpio_num_t reset;
|
|
37
|
+
// The TCA9554's open-drain INT, which falls when any expander input --
|
|
38
|
+
// the touch controller's INT among them -- changes.
|
|
39
|
+
gpio_num_t interrupt;
|
|
40
|
+
// The expander pin the touch controller's own INT lands on.
|
|
41
|
+
int expanderInterruptPin;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
struct Es8311AudioConfig {
|
|
45
|
+
int i2sPort;
|
|
46
|
+
gpio_num_t mclk;
|
|
47
|
+
gpio_num_t bclk;
|
|
48
|
+
gpio_num_t ws;
|
|
49
|
+
gpio_num_t dout;
|
|
50
|
+
gpio_num_t din;
|
|
51
|
+
gpio_num_t powerAmplifier;
|
|
52
|
+
// The four microphones are on an ES7210 ADC (I2C 0x40) sharing the I2S
|
|
53
|
+
// bus; the ES8311's own input is not wired to a microphone.
|
|
54
|
+
int es7210Address;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// TCA9554 pin numbers (0-based, the chip's own), -1 where a line is not
|
|
58
|
+
// routed through it.
|
|
59
|
+
struct IoExpanderConfig {
|
|
60
|
+
int initialOutputs;
|
|
61
|
+
int address;
|
|
62
|
+
int initialDirections;
|
|
63
|
+
int powerAmplifier;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
struct SdMmcConfig {
|
|
67
|
+
gpio_num_t clk;
|
|
68
|
+
gpio_num_t cmd;
|
|
69
|
+
gpio_num_t data0;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
struct BatteryConfig {
|
|
73
|
+
// VBAT through a 1:3 divider into ADC1.
|
|
74
|
+
gpio_num_t sense;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
struct LauncherButtonConfig {
|
|
78
|
+
gpio_num_t pin;
|
|
79
|
+
int activeLevel;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
class Esp32S3TouchLcd349 {
|
|
83
|
+
public:
|
|
84
|
+
static constexpr I2cBusConfig i2c{
|
|
85
|
+
.sda = GPIO_NUM_47,
|
|
86
|
+
.scl = GPIO_NUM_48,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// V2 board. Backlight PWM is GPIO42, active low, gated by the expander's
|
|
90
|
+
// BL_EN (GEA_EMBEDDED_AXS15231B_BACKLIGHT_* in main/CMakeLists.txt). The
|
|
91
|
+
// panel reset is the expander's LCD_RST, so no GPIO reset here; GPIO21 is
|
|
92
|
+
// the tearing-effect line, which the runtime does not use.
|
|
93
|
+
static constexpr QspiDisplayConfig display{
|
|
94
|
+
.spiHost = SPI2_HOST,
|
|
95
|
+
.cs = GPIO_NUM_9,
|
|
96
|
+
.pclk = GPIO_NUM_10,
|
|
97
|
+
.data0 = GPIO_NUM_11,
|
|
98
|
+
.data1 = GPIO_NUM_12,
|
|
99
|
+
.data2 = GPIO_NUM_13,
|
|
100
|
+
.data3 = GPIO_NUM_14,
|
|
101
|
+
.reset = GPIO_NUM_NC,
|
|
102
|
+
.te = GPIO_NUM_NC,
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Touch reset is not routed. Touch INT reaches the ESP32 through the
|
|
106
|
+
// expander: EXIO0, signalled on the expander's INT at GPIO8.
|
|
107
|
+
static constexpr TouchConfig touch{
|
|
108
|
+
.bus = {.sda = GPIO_NUM_17, .scl = GPIO_NUM_18},
|
|
109
|
+
.reset = GPIO_NUM_NC,
|
|
110
|
+
.interrupt = GPIO_NUM_8,
|
|
111
|
+
.expanderInterruptPin = 0,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
static constexpr Es8311AudioConfig audio{
|
|
115
|
+
// I2S_NUM_AUTO, spelled as its value; see the AMOLED 2.06 board.h.
|
|
116
|
+
.i2sPort = -1,
|
|
117
|
+
.mclk = GPIO_NUM_7,
|
|
118
|
+
.bclk = GPIO_NUM_15,
|
|
119
|
+
.ws = GPIO_NUM_46,
|
|
120
|
+
.dout = GPIO_NUM_45,
|
|
121
|
+
.din = GPIO_NUM_6,
|
|
122
|
+
// The speaker amplifier is enabled through the TCA9554 (expander below).
|
|
123
|
+
.powerAmplifier = GPIO_NUM_NC,
|
|
124
|
+
.es7210Address = 0x40,
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
using Board = Esp32S3TouchLcd349;
|
|
129
|
+
|
|
130
|
+
inline constexpr auto i2c = Board::i2c;
|
|
131
|
+
inline constexpr auto display = Board::display;
|
|
132
|
+
inline constexpr auto touch = Board::touch;
|
|
133
|
+
inline constexpr auto audio = Board::audio;
|
|
134
|
+
// TCA9554 at 0x20 on the primary bus (V2, waveshareteam
|
|
135
|
+
// ESP32-S3-Touch-LCD-3.49-V2 user_config.h): EXIO0 touch INT, EXIO1 BL_EN,
|
|
136
|
+
// EXIO2/3 IMU INT1/2, EXIO4 RTC INT, EXIO5 LCD_RST, EXIO6 SYS_EN, EXIO7 the
|
|
137
|
+
// speaker amplifier. BL_EN, LCD_RST and the amplifier are outputs; BL_EN and
|
|
138
|
+
// LCD_RST start high so the panel stays lit and out of reset, the amplifier
|
|
139
|
+
// starts off and follows the speaker.
|
|
140
|
+
inline constexpr IoExpanderConfig expander{
|
|
141
|
+
.initialOutputs = 0x22,
|
|
142
|
+
.address = 0x20,
|
|
143
|
+
.initialDirections = 0x5D,
|
|
144
|
+
.powerAmplifier = 7,
|
|
145
|
+
};
|
|
146
|
+
// TF slot, SDMMC 1-bit (vendor Examples/ESP-IDF/04_SD_Card).
|
|
147
|
+
inline constexpr SdMmcConfig storage{
|
|
148
|
+
.clk = GPIO_NUM_41,
|
|
149
|
+
.cmd = GPIO_NUM_39,
|
|
150
|
+
.data0 = GPIO_NUM_40,
|
|
151
|
+
};
|
|
152
|
+
inline constexpr BatteryConfig battery{.sense = GPIO_NUM_4};
|
|
153
|
+
// BOOT. The app-facing buttons (BOOT and PWR on GPIO16) are declared in
|
|
154
|
+
// targets.json.
|
|
155
|
+
inline constexpr LauncherButtonConfig launcherButton{
|
|
156
|
+
.pin = GPIO_NUM_0,
|
|
157
|
+
.activeLevel = 0,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
} // namespace gea::platform::board
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Bring the panel out of reset and enable its backlight before the display
|
|
2
|
+
// driver talks to it.
|
|
3
|
+
//
|
|
4
|
+
// On the V2 board the AXS15231B's reset is the TCA9554's EXIO5 and the
|
|
5
|
+
// backlight supply is gated by EXIO1 (BL_EN); neither is a GPIO, so
|
|
6
|
+
// board::display.reset is GPIO_NUM_NC and the panel driver has nothing to
|
|
7
|
+
// toggle. The vendor's V2 examples (example_lcd_exio_init / example_lcd_reset)
|
|
8
|
+
// drive the same two pins at the same point.
|
|
9
|
+
//
|
|
10
|
+
// Display init runs before the board's shared I2C bus is brought up, so this
|
|
11
|
+
// opens its own short-lived master bus and closes it again; the later bus
|
|
12
|
+
// creation in i2c.cpp then proceeds normally.
|
|
13
|
+
#include "board.h"
|
|
14
|
+
|
|
15
|
+
#include <cstdint>
|
|
16
|
+
|
|
17
|
+
#include "driver/i2c_master.h"
|
|
18
|
+
#include "esp_err.h"
|
|
19
|
+
#include "esp_log.h"
|
|
20
|
+
#include "freertos/FreeRTOS.h"
|
|
21
|
+
#include "freertos/task.h"
|
|
22
|
+
|
|
23
|
+
namespace
|
|
24
|
+
{
|
|
25
|
+
constexpr char kTag[] = "panel_reset";
|
|
26
|
+
constexpr std::uint8_t kOutputRegister = 0x01;
|
|
27
|
+
constexpr std::uint8_t kConfigRegister = 0x03;
|
|
28
|
+
constexpr std::uint8_t kBacklightEnable = 1u << 1;
|
|
29
|
+
constexpr std::uint8_t kPanelReset = 1u << 5;
|
|
30
|
+
|
|
31
|
+
bool writeRegister(i2c_master_dev_handle_t device, std::uint8_t reg, std::uint8_t value)
|
|
32
|
+
{
|
|
33
|
+
const std::uint8_t bytes[] = {reg, value};
|
|
34
|
+
return i2c_master_transmit(device, bytes, sizeof(bytes), 100) == ESP_OK;
|
|
35
|
+
}
|
|
36
|
+
} // namespace
|
|
37
|
+
|
|
38
|
+
namespace gea::platform::board
|
|
39
|
+
{
|
|
40
|
+
void prepareDisplayPanel()
|
|
41
|
+
{
|
|
42
|
+
i2c_master_bus_config_t busConfig = {};
|
|
43
|
+
busConfig.i2c_port = I2C_NUM_0;
|
|
44
|
+
busConfig.sda_io_num = i2c.sda;
|
|
45
|
+
busConfig.scl_io_num = i2c.scl;
|
|
46
|
+
busConfig.clk_source = I2C_CLK_SRC_DEFAULT;
|
|
47
|
+
busConfig.glitch_ignore_cnt = 7;
|
|
48
|
+
busConfig.flags.enable_internal_pullup = true;
|
|
49
|
+
|
|
50
|
+
i2c_master_bus_handle_t bus = nullptr;
|
|
51
|
+
esp_err_t err = i2c_new_master_bus(&busConfig, &bus);
|
|
52
|
+
if (err != ESP_OK)
|
|
53
|
+
{
|
|
54
|
+
ESP_LOGE(kTag, "I2C bus for the panel reset failed: %s", esp_err_to_name(err));
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
i2c_device_config_t deviceConfig = {};
|
|
59
|
+
deviceConfig.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
|
60
|
+
deviceConfig.device_address = expander.address;
|
|
61
|
+
deviceConfig.scl_speed_hz = 400000;
|
|
62
|
+
i2c_master_dev_handle_t device = nullptr;
|
|
63
|
+
err = i2c_master_bus_add_device(bus, &deviceConfig, &device);
|
|
64
|
+
if (err != ESP_OK)
|
|
65
|
+
{
|
|
66
|
+
ESP_LOGE(kTag, "TCA9554 attach failed: %s", esp_err_to_name(err));
|
|
67
|
+
i2c_del_master_bus(bus);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Outputs first, so pins switching to output come up at the levels the
|
|
72
|
+
// board expects; then the reset pulse with the backlight enabled.
|
|
73
|
+
const std::uint8_t idle = static_cast<std::uint8_t>(expander.initialOutputs);
|
|
74
|
+
const bool ok = writeRegister(device, kOutputRegister, idle) &&
|
|
75
|
+
writeRegister(device, kConfigRegister, static_cast<std::uint8_t>(expander.initialDirections));
|
|
76
|
+
if (ok)
|
|
77
|
+
{
|
|
78
|
+
vTaskDelay(pdMS_TO_TICKS(10));
|
|
79
|
+
writeRegister(device, kOutputRegister, static_cast<std::uint8_t>(idle & ~kPanelReset));
|
|
80
|
+
vTaskDelay(pdMS_TO_TICKS(20));
|
|
81
|
+
writeRegister(device, kOutputRegister, static_cast<std::uint8_t>(idle | kPanelReset | kBacklightEnable));
|
|
82
|
+
vTaskDelay(pdMS_TO_TICKS(120));
|
|
83
|
+
ESP_LOGI(kTag, "panel reset and backlight enable through TCA9554 (EXIO5, EXIO1)");
|
|
84
|
+
}
|
|
85
|
+
else
|
|
86
|
+
{
|
|
87
|
+
ESP_LOGE(kTag, "TCA9554 not responding at 0x%02x", expander.address);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
i2c_master_bus_rm_device(device);
|
|
91
|
+
i2c_del_master_bus(bus);
|
|
92
|
+
}
|
|
93
|
+
} // namespace gea::platform::board
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#include "power.h"
|
|
2
|
+
#include "board.h"
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <mutex>
|
|
5
|
+
#include "esp_adc/adc_oneshot.h"
|
|
6
|
+
#include "esp_adc/adc_cali.h"
|
|
7
|
+
#include "esp_adc/adc_cali_scheme.h"
|
|
8
|
+
#include "esp_log.h"
|
|
9
|
+
|
|
10
|
+
// No PMIC on this board: the battery is read through a divider on ADC1
|
|
11
|
+
// (vendor Examples/ESP-IDF/01_ADC_Test). Charge state is not wired to a GPIO,
|
|
12
|
+
// so Power::charging() keeps the host default.
|
|
13
|
+
namespace gea::platform::power {
|
|
14
|
+
namespace {
|
|
15
|
+
static_assert(board::battery.sense == GPIO_NUM_4, "battery sense is ADC1 channel 3 only on GPIO4");
|
|
16
|
+
constexpr adc_channel_t kChannel = ADC_CHANNEL_3;
|
|
17
|
+
std::mutex mutex;
|
|
18
|
+
adc_oneshot_unit_handle_t adc = nullptr;
|
|
19
|
+
adc_cali_handle_t calibration = nullptr;
|
|
20
|
+
bool ready = false;
|
|
21
|
+
}
|
|
22
|
+
bool Power::init() {
|
|
23
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
24
|
+
if (ready) return true;
|
|
25
|
+
adc_oneshot_unit_init_cfg_t unit{};
|
|
26
|
+
unit.unit_id = ADC_UNIT_1;
|
|
27
|
+
if (!adc && adc_oneshot_new_unit(&unit, &adc) != ESP_OK) return false;
|
|
28
|
+
adc_oneshot_chan_cfg_t channel{};
|
|
29
|
+
channel.atten = ADC_ATTEN_DB_12;
|
|
30
|
+
channel.bitwidth = ADC_BITWIDTH_DEFAULT;
|
|
31
|
+
if (adc_oneshot_config_channel(adc, kChannel, &channel) != ESP_OK) return false;
|
|
32
|
+
adc_cali_curve_fitting_config_t cal{};
|
|
33
|
+
cal.unit_id = ADC_UNIT_1;
|
|
34
|
+
cal.chan = kChannel;
|
|
35
|
+
cal.atten = ADC_ATTEN_DB_12;
|
|
36
|
+
cal.bitwidth = ADC_BITWIDTH_DEFAULT;
|
|
37
|
+
if (adc_cali_create_scheme_curve_fitting(&cal, &calibration) != ESP_OK) {
|
|
38
|
+
calibration = nullptr;
|
|
39
|
+
ESP_LOGW("lcd349_power", "ADC calibration unavailable; battery percentage unknown");
|
|
40
|
+
}
|
|
41
|
+
ready = true;
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
int Power::batteryPercent() {
|
|
45
|
+
if (!init()) return -1;
|
|
46
|
+
std::lock_guard<std::mutex> lock(mutex);
|
|
47
|
+
if (!calibration) return -1;
|
|
48
|
+
int total = 0;
|
|
49
|
+
for (int i = 0; i < 8; ++i) {
|
|
50
|
+
int raw = 0;
|
|
51
|
+
if (adc_oneshot_read(adc, kChannel, &raw) != ESP_OK) return -1;
|
|
52
|
+
total += raw;
|
|
53
|
+
}
|
|
54
|
+
int mv = 0;
|
|
55
|
+
if (adc_cali_raw_to_voltage(calibration, total / 8, &mv) != ESP_OK) return -1;
|
|
56
|
+
// Vendor divider: Vbat = ADC voltage * 3. Percentage is a voltage estimate.
|
|
57
|
+
mv *= 3;
|
|
58
|
+
return std::clamp((mv - 3300) * 100 / 900, 0, 100);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
#include "board.h"
|
|
2
|
+
#include "chip_bindings/expanders/io_expander.h"
|
|
3
|
+
#include "i2c.h"
|
|
4
|
+
#include "touch.h"
|
|
5
|
+
|
|
6
|
+
#include <algorithm>
|
|
7
|
+
#include <atomic>
|
|
8
|
+
#include <cstddef>
|
|
9
|
+
#include <cstdint>
|
|
10
|
+
|
|
11
|
+
#include "driver/gpio.h"
|
|
12
|
+
#include "driver/i2c_master.h"
|
|
13
|
+
#include "esp_attr.h"
|
|
14
|
+
#include "esp_check.h"
|
|
15
|
+
#include "esp_err.h"
|
|
16
|
+
#include "esp_log.h"
|
|
17
|
+
#include "esp_timer.h"
|
|
18
|
+
#include "freertos/FreeRTOS.h"
|
|
19
|
+
#include "freertos/task.h"
|
|
20
|
+
|
|
21
|
+
namespace {
|
|
22
|
+
|
|
23
|
+
using Phase = gea::platform::touch::Phase;
|
|
24
|
+
using Touchscreen = gea::platform::touch::Touchscreen;
|
|
25
|
+
|
|
26
|
+
constexpr char kTag[] = "axs15231b-touch";
|
|
27
|
+
constexpr std::uint8_t kAxs15231Address = 0x3b;
|
|
28
|
+
constexpr int kNativeWidth = 172;
|
|
29
|
+
constexpr int kNativeHeight = 640;
|
|
30
|
+
constexpr int kPollIntervalMs = 10;
|
|
31
|
+
#ifndef GEA_EMBEDDED_TOUCH_TASK_PRIORITY
|
|
32
|
+
#define GEA_EMBEDDED_TOUCH_TASK_PRIORITY 4
|
|
33
|
+
#endif
|
|
34
|
+
constexpr int kTaskPriority = GEA_EMBEDDED_TOUCH_TASK_PRIORITY;
|
|
35
|
+
constexpr std::uint32_t kTaskStackWords = 3072;
|
|
36
|
+
|
|
37
|
+
struct TouchSample {
|
|
38
|
+
bool touching = false;
|
|
39
|
+
int x = 0;
|
|
40
|
+
int y = 0;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
struct Contact {
|
|
44
|
+
int id = 0;
|
|
45
|
+
int x = 0;
|
|
46
|
+
int y = 0;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// One controller report: up to two contacts, each with the controller's own id.
|
|
50
|
+
struct HardwareSample {
|
|
51
|
+
bool valid = false;
|
|
52
|
+
int count = 0;
|
|
53
|
+
Contact contacts[2]{};
|
|
54
|
+
|
|
55
|
+
const Contact *find(int id) const
|
|
56
|
+
{
|
|
57
|
+
for (int i = 0; i < count; ++i)
|
|
58
|
+
if (contacts[i].id == id) return &contacts[i];
|
|
59
|
+
return nullptr;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
Touchscreen::Observer gObserver = nullptr;
|
|
64
|
+
Touchscreen::PointerObserver gPointerObserver = nullptr;
|
|
65
|
+
i2c_master_dev_handle_t gDevice = nullptr;
|
|
66
|
+
TouchSample gCurrent{};
|
|
67
|
+
TouchSample gLatestMove{};
|
|
68
|
+
std::atomic<bool> gLatestMoveQueued{false};
|
|
69
|
+
TaskHandle_t gTask = nullptr;
|
|
70
|
+
StaticTask_t gTaskControlBlock{};
|
|
71
|
+
#if GEA_EMBEDDED_TOUCH_TASK_STACK_EXTERNAL
|
|
72
|
+
EXT_RAM_BSS_ATTR
|
|
73
|
+
#endif
|
|
74
|
+
StackType_t gTaskStack[kTaskStackWords]{};
|
|
75
|
+
|
|
76
|
+
void notify(Phase phase, const TouchSample &sample)
|
|
77
|
+
{
|
|
78
|
+
if (gPointerObserver) {
|
|
79
|
+
gPointerObserver(phase, sample.touching, sample.x, sample.y, 0);
|
|
80
|
+
} else if (gObserver) {
|
|
81
|
+
gObserver(phase, sample.touching, sample.x, sample.y);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
void notifyMove(const TouchSample &sample)
|
|
86
|
+
{
|
|
87
|
+
gLatestMove = sample;
|
|
88
|
+
if (gLatestMoveQueued.exchange(true, std::memory_order_acq_rel)) return;
|
|
89
|
+
notify(Phase::Move, sample);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
HardwareSample readHardware()
|
|
93
|
+
{
|
|
94
|
+
static constexpr std::uint8_t command[] = {
|
|
95
|
+
0xb5, 0xab, 0xa5, 0x5a, 0, 0, 0, 0x0e, 0, 0, 0};
|
|
96
|
+
std::uint8_t report[14]{};
|
|
97
|
+
// Command and report in one transaction with a repeated start, at the
|
|
98
|
+
// vendor's 300 kHz -- exactly as Waveshare's V2 examples read this panel.
|
|
99
|
+
if (i2c_master_transmit_receive(gDevice, command, sizeof(command), report, sizeof(report), 20) != ESP_OK)
|
|
100
|
+
return {};
|
|
101
|
+
if (report[0] == 0xff || report[1] > 2) return {};
|
|
102
|
+
HardwareSample sample{.valid = true};
|
|
103
|
+
// report[1] is the contact count; each contact is six bytes from report[2]:
|
|
104
|
+
// [0] event (bits 7:6: 0 down, 1 up, 2 contact, 3 none) | along 11:8
|
|
105
|
+
// [1] along 7:0
|
|
106
|
+
// [2] contact id (bits 7:4) | across 11:8
|
|
107
|
+
// [3] across 7:0
|
|
108
|
+
// [4..5] report sequence
|
|
109
|
+
// The controller keeps contacts in fixed slots, so the first slot is not
|
|
110
|
+
// necessarily the finger that went down first; contacts are matched by id.
|
|
111
|
+
for (int i = 0; i < report[1]; ++i) {
|
|
112
|
+
const std::uint8_t *point = report + 2 + 6 * i;
|
|
113
|
+
const unsigned event = point[0] >> 6;
|
|
114
|
+
if (event == 1 || event == 3) continue;
|
|
115
|
+
// The controller reports along the glass's long axis first: its X
|
|
116
|
+
// runs down the 640-pixel side from the bottom, its Y across the
|
|
117
|
+
// 172-pixel side. The vendor example maps it the same way into native
|
|
118
|
+
// portrait.
|
|
119
|
+
const int along = ((point[0] & 0x0f) << 8) | point[1];
|
|
120
|
+
const int across = ((point[2] & 0x0f) << 8) | point[3];
|
|
121
|
+
// Edge reports can land one past the glass; clamp them as the vendor does.
|
|
122
|
+
if (along > kNativeHeight + 8 || across > kNativeWidth + 8) continue;
|
|
123
|
+
Contact &contact = sample.contacts[sample.count++];
|
|
124
|
+
contact.id = point[2] >> 4;
|
|
125
|
+
contact.x = std::min(across, kNativeWidth - 1);
|
|
126
|
+
contact.y = std::clamp(kNativeHeight - along, 0, kNativeHeight - 1);
|
|
127
|
+
}
|
|
128
|
+
return sample;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#ifndef GEA_EMBEDDED_TOUCH_POLL
|
|
132
|
+
#define GEA_EMBEDDED_TOUCH_POLL 0
|
|
133
|
+
#endif
|
|
134
|
+
// The controller's INT reaches the ESP32 through the expander, whose own INT
|
|
135
|
+
// (GPIO8) falls whenever one of its inputs changes. Reads follow that line:
|
|
136
|
+
// the controller is read when it signals, and while a finger is down it is
|
|
137
|
+
// also read every kPollIntervalMs so a lift is seen even if its edge is
|
|
138
|
+
// shared with a change the expander already reported. With no finger down
|
|
139
|
+
// the task sleeps until the next edge. GEA_EMBEDDED_TOUCH_POLL=1 opts back
|
|
140
|
+
// into reading on a fixed interval.
|
|
141
|
+
constexpr gpio_num_t kInterruptPin = gea::platform::board::touch.interrupt;
|
|
142
|
+
constexpr bool kInterruptDriven = kInterruptPin != GPIO_NUM_NC && !GEA_EMBEDDED_TOUCH_POLL;
|
|
143
|
+
|
|
144
|
+
void IRAM_ATTR expanderInterrupt(void *)
|
|
145
|
+
{
|
|
146
|
+
BaseType_t woken = pdFALSE;
|
|
147
|
+
if (gTask) vTaskNotifyGiveFromISR(gTask, &woken);
|
|
148
|
+
if (woken) portYIELD_FROM_ISR();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Reading the expander's input port releases its INT, so the next change
|
|
152
|
+
// produces a fresh falling edge.
|
|
153
|
+
void acknowledgeExpander()
|
|
154
|
+
{
|
|
155
|
+
bool level = true;
|
|
156
|
+
gea::platform::esp32::chip_bindings::expanders::ioExpander().readPin(
|
|
157
|
+
gea::platform::board::touch.expanderInterruptPin, level);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// The primary pointer follows one contact id from its down to its lift; a
|
|
161
|
+
// second contact never takes it over mid-gesture. A contact that is still
|
|
162
|
+
// present when the primary lifts belongs to that gesture and cannot start a new
|
|
163
|
+
// one until it lifts too -- otherwise a second contact left on the glass turns
|
|
164
|
+
// every lift into a fresh tap at its position.
|
|
165
|
+
void pollLoop()
|
|
166
|
+
{
|
|
167
|
+
int primaryId = -1;
|
|
168
|
+
unsigned staleIds = 0;
|
|
169
|
+
bool anyContact = false;
|
|
170
|
+
if constexpr (kInterruptDriven) acknowledgeExpander();
|
|
171
|
+
while (true) {
|
|
172
|
+
if constexpr (kInterruptDriven) {
|
|
173
|
+
const TickType_t wait = anyContact ? pdMS_TO_TICKS(kPollIntervalMs) : portMAX_DELAY;
|
|
174
|
+
if (ulTaskNotifyTake(pdTRUE, wait) > 0) acknowledgeExpander();
|
|
175
|
+
} else {
|
|
176
|
+
vTaskDelay(pdMS_TO_TICKS(kPollIntervalMs));
|
|
177
|
+
}
|
|
178
|
+
const HardwareSample result = readHardware();
|
|
179
|
+
if (!result.valid) continue;
|
|
180
|
+
anyContact = result.count > 0;
|
|
181
|
+
|
|
182
|
+
unsigned present = 0;
|
|
183
|
+
for (int i = 0; i < result.count; ++i) present |= 1u << result.contacts[i].id;
|
|
184
|
+
staleIds &= present;
|
|
185
|
+
|
|
186
|
+
if (primaryId >= 0) {
|
|
187
|
+
const Contact *contact = result.find(primaryId);
|
|
188
|
+
if (contact) {
|
|
189
|
+
if (contact->x != gCurrent.x || contact->y != gCurrent.y) {
|
|
190
|
+
const TouchSample next{true, contact->x, contact->y};
|
|
191
|
+
notifyMove(next);
|
|
192
|
+
gCurrent = next;
|
|
193
|
+
}
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
const TouchSample lifted{false, gCurrent.x, gCurrent.y};
|
|
197
|
+
notify(Phase::Up, lifted);
|
|
198
|
+
gCurrent = lifted;
|
|
199
|
+
primaryId = -1;
|
|
200
|
+
staleIds = present;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
for (int i = 0; i < result.count; ++i) {
|
|
204
|
+
const Contact &contact = result.contacts[i];
|
|
205
|
+
if (staleIds & (1u << contact.id)) continue;
|
|
206
|
+
primaryId = contact.id;
|
|
207
|
+
const TouchSample next{true, contact.x, contact.y};
|
|
208
|
+
gLatestMove = next;
|
|
209
|
+
notify(Phase::Down, next);
|
|
210
|
+
gCurrent = next;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
esp_err_t attachInterrupt()
|
|
217
|
+
{
|
|
218
|
+
if constexpr (!kInterruptDriven) return ESP_OK;
|
|
219
|
+
if (!gea::platform::esp32::chip_bindings::expanders::ioExpander().init()) {
|
|
220
|
+
ESP_LOGE(kTag, "expander unavailable for the touch interrupt");
|
|
221
|
+
return ESP_FAIL;
|
|
222
|
+
}
|
|
223
|
+
gpio_config_t config{};
|
|
224
|
+
config.pin_bit_mask = 1ULL << kInterruptPin;
|
|
225
|
+
config.mode = GPIO_MODE_INPUT;
|
|
226
|
+
config.pull_up_en = GPIO_PULLUP_ENABLE;
|
|
227
|
+
config.intr_type = GPIO_INTR_NEGEDGE;
|
|
228
|
+
ESP_RETURN_ON_ERROR(gpio_config(&config), kTag, "touch interrupt pin");
|
|
229
|
+
const esp_err_t service = gpio_install_isr_service(0);
|
|
230
|
+
if (service != ESP_OK && service != ESP_ERR_INVALID_STATE) return service;
|
|
231
|
+
return gpio_isr_handler_add(kInterruptPin, expanderInterrupt, nullptr);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
void taskEntry(void *)
|
|
235
|
+
{
|
|
236
|
+
pollLoop();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Touch sits on its own I2C pair, apart from the sensor/codec bus that
|
|
240
|
+
// Bus::primary() owns.
|
|
241
|
+
i2c_master_bus_handle_t touchBus()
|
|
242
|
+
{
|
|
243
|
+
static i2c_master_bus_handle_t bus = nullptr;
|
|
244
|
+
if (bus) return bus;
|
|
245
|
+
i2c_master_bus_config_t config{};
|
|
246
|
+
config.i2c_port = -1;
|
|
247
|
+
config.sda_io_num = gea::platform::board::touch.bus.sda;
|
|
248
|
+
config.scl_io_num = gea::platform::board::touch.bus.scl;
|
|
249
|
+
config.clk_source = I2C_CLK_SRC_DEFAULT;
|
|
250
|
+
config.glitch_ignore_cnt = 7;
|
|
251
|
+
config.flags.enable_internal_pullup = true;
|
|
252
|
+
if (i2c_new_master_bus(&config, &bus) != ESP_OK) {
|
|
253
|
+
ESP_LOGE(kTag, "touch I2C bus init failed");
|
|
254
|
+
bus = nullptr;
|
|
255
|
+
}
|
|
256
|
+
return bus;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
esp_err_t attachDevice(i2c_master_bus_handle_t bus)
|
|
260
|
+
{
|
|
261
|
+
ESP_RETURN_ON_ERROR(i2c_master_probe(bus, kAxs15231Address, 100), kTag, "touch probe");
|
|
262
|
+
i2c_device_config_t config{};
|
|
263
|
+
config.dev_addr_length = I2C_ADDR_BIT_LEN_7;
|
|
264
|
+
config.device_address = kAxs15231Address;
|
|
265
|
+
config.scl_speed_hz = 300000;
|
|
266
|
+
return i2c_master_bus_add_device(bus, &config, &gDevice);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
esp_err_t startTask()
|
|
270
|
+
{
|
|
271
|
+
gTask = xTaskCreateStaticPinnedToCore(taskEntry, "axs15231b-touch", kTaskStackWords,
|
|
272
|
+
nullptr, kTaskPriority, gTaskStack, &gTaskControlBlock, 1);
|
|
273
|
+
return gTask ? ESP_OK : ESP_ERR_NO_MEM;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
} // namespace
|
|
277
|
+
|
|
278
|
+
void gea::platform::touch::Touchscreen::setObserver(Observer observer)
|
|
279
|
+
{
|
|
280
|
+
gObserver = observer;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
void gea::platform::touch::Touchscreen::setPointerObserver(PointerObserver observer)
|
|
284
|
+
{
|
|
285
|
+
gPointerObserver = observer;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
bool gea::platform::touch::Touchscreen::init()
|
|
289
|
+
{
|
|
290
|
+
if (gTask) return true;
|
|
291
|
+
i2c_master_bus_handle_t bus = touchBus();
|
|
292
|
+
if (!bus) return false;
|
|
293
|
+
|
|
294
|
+
if (attachDevice(bus) != ESP_OK) return false;
|
|
295
|
+
if (startTask() != ESP_OK) return false;
|
|
296
|
+
// The task is created first so an edge arriving during setup has a target.
|
|
297
|
+
if (attachInterrupt() != ESP_OK) return false;
|
|
298
|
+
ESP_LOGI(kTag, "Physical touch ready (%s)", kInterruptDriven ? "interrupt" : "polling");
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
int gea::platform::touch::Touchscreen::read(int *x, int *y)
|
|
303
|
+
{
|
|
304
|
+
if (x) *x = gCurrent.x;
|
|
305
|
+
if (y) *y = gCurrent.y;
|
|
306
|
+
return gCurrent.touching ? 1 : 0;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
int gea::platform::touch::Touchscreen::readCached(int *x, int *y)
|
|
310
|
+
{
|
|
311
|
+
return read(x, y);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
void gea::platform::touch::Touchscreen::consumeLatestMove(int *x, int *y)
|
|
315
|
+
{
|
|
316
|
+
gLatestMoveQueued.store(false, std::memory_order_release);
|
|
317
|
+
if (x) *x = gLatestMove.x;
|
|
318
|
+
if (y) *y = gLatestMove.y;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
void gea::platform::touch::Touchscreen::injectEvent(Phase phase, bool touching, int x, int y)
|
|
322
|
+
{
|
|
323
|
+
const TouchSample sample{touching, x, y};
|
|
324
|
+
gCurrent = sample;
|
|
325
|
+
if (phase == Phase::Move) notifyMove(sample);
|
|
326
|
+
else notify(phase, sample);
|
|
327
|
+
}
|