Add touch screen support to freenove 2.8 (#1901)
* Add touch screen support to freenove 2.8 Volume and brightness levels on vertical/horizontal swipe Listening, chat state and wifi config on press/long press * Simplified Removed swipes. Touch quiet unresponsive on Freenove board. * Remove unused parts * Modify long tap duration in TouchTask Updated touch driver logic to change long tap duration from 600ms to 3000ms. * Cosmetic fix
This commit is contained in:
@ -14,6 +14,9 @@
|
||||
#include <esp_log.h>
|
||||
#include <driver/i2c_master.h>
|
||||
#include <driver/spi_common.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_timer.h>
|
||||
|
||||
#include "led/single_led.h"
|
||||
#include "system_reset.h"
|
||||
@ -21,11 +24,99 @@
|
||||
|
||||
#define TAG "FreenoveESP32S3Display"
|
||||
|
||||
class TouchDriver {
|
||||
public:
|
||||
TouchDriver() : dev_(nullptr) {}
|
||||
|
||||
bool Init(i2c_master_bus_handle_t bus, uint8_t addr) {
|
||||
i2c_device_config_t cfg = {
|
||||
.device_address = addr,
|
||||
.scl_speed_hz = 400000,
|
||||
.scl_wait_us = 0,
|
||||
};
|
||||
return i2c_master_bus_add_device(bus, &cfg, &dev_) == ESP_OK;
|
||||
}
|
||||
|
||||
bool Read(bool &touched, uint16_t &x, uint16_t &y) {
|
||||
touched = false;
|
||||
x = y = 0;
|
||||
if (!dev_) return false;
|
||||
|
||||
uint8_t reg = 0x02;
|
||||
uint8_t buf[5];
|
||||
if (i2c_master_transmit_receive(dev_, ®, 1, buf, 5, 50) != ESP_OK) return false;
|
||||
|
||||
uint8_t points = buf[0] & 0x0F;
|
||||
if (points == 0) return true;
|
||||
|
||||
touched = true;
|
||||
x = ((buf[1] & 0x0F) << 8) | buf[2];
|
||||
y = ((buf[3] & 0x0F) << 8) | buf[4];
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
i2c_master_dev_handle_t dev_;
|
||||
};
|
||||
|
||||
class FreenoveESP32S3Display : public WifiBoard {
|
||||
private:
|
||||
private:
|
||||
Button boot_button_;
|
||||
LcdDisplay *display_;
|
||||
i2c_master_bus_handle_t codec_i2c_bus_;
|
||||
TouchDriver touch_;
|
||||
|
||||
static void TouchTask(void *arg) {
|
||||
auto *self = static_cast<FreenoveESP32S3Display*>(arg);
|
||||
auto &app = Application::GetInstance();
|
||||
|
||||
uint32_t last_tap = 0;
|
||||
uint32_t down_start = 0;
|
||||
bool down = false;
|
||||
|
||||
while (true) {
|
||||
bool t;
|
||||
uint16_t x, y;
|
||||
self->touch_.Read(t, x, y);
|
||||
|
||||
uint32_t now = esp_timer_get_time() / 1000;
|
||||
|
||||
if (t) {
|
||||
if (!down) {
|
||||
down = true;
|
||||
down_start = now;
|
||||
}
|
||||
}
|
||||
|
||||
if (!t && down) {
|
||||
down = false;
|
||||
|
||||
uint32_t press = now - down_start;
|
||||
|
||||
// long tap
|
||||
if (press > 3000) {
|
||||
self->EnterWifiConfigMode();
|
||||
} else {
|
||||
// double tap
|
||||
if (now - last_tap < 250) {
|
||||
app.StartListening();
|
||||
last_tap = 0;
|
||||
} else {
|
||||
// single tap
|
||||
app.ToggleChatState();
|
||||
last_tap = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
}
|
||||
|
||||
void InitializeTouch() {
|
||||
if (!touch_.Init(codec_i2c_bus_, 0x38)) return;
|
||||
xTaskCreatePinnedToCore(TouchTask, "touch_task", 4096, this, 5, nullptr, 0);
|
||||
}
|
||||
|
||||
void InitializeI2c() {
|
||||
i2c_master_bus_config_t i2c_bus_cfg = {
|
||||
@ -64,7 +155,6 @@ class FreenoveESP32S3Display : public WifiBoard {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void InitializeLcdDisplay() {
|
||||
esp_lcd_panel_io_handle_t panel_io = nullptr;
|
||||
esp_lcd_panel_handle_t panel = nullptr;
|
||||
@ -103,13 +193,13 @@ class FreenoveESP32S3Display : public WifiBoard {
|
||||
void InitializeTools() {
|
||||
}
|
||||
|
||||
|
||||
public:
|
||||
public:
|
||||
FreenoveESP32S3Display(): boot_button_(BOOT_BUTTON_GPIO)
|
||||
{
|
||||
InitializeI2c();
|
||||
InitializeSpi();
|
||||
InitializeLcdDisplay();
|
||||
InitializeTouch();
|
||||
InitializeButtons();
|
||||
InitializeTools();
|
||||
GetBacklight()->SetBrightness(100);
|
||||
@ -134,7 +224,6 @@ class FreenoveESP32S3Display : public WifiBoard {
|
||||
static PwmBacklight backlight(DISPLAY_BACKLIGHT_PIN, DISPLAY_BACKLIGHT_OUTPUT_INVERT);
|
||||
return &backlight;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
DECLARE_BOARD(FreenoveESP32S3Display);
|
||||
Reference in New Issue
Block a user