Очистить структуру ESP32-скетчей

This commit is contained in:
AidarKC
2026-12-03 13:44:08 +03:00
parent eae8280062
commit 08969ecb13
35 changed files with 21 additions and 21 deletions
@@ -0,0 +1,37 @@
# Test Sketches
Набор старых отдельных диагностических скетчей для `Waveshare ESP32-S3-Touch-AMOLED-2.16`.
Скетчи в этой папке нужны для быстрой проверки конкретных гипотез и не являются основным UI проекта.
Основной скетч сейчас лежит в `main-device/shine_homeserver_main/`.
## Список
- `gfx_text_render_test/` - проверка рендера текста через `Arduino_GFX` и сравнение с `U8g2`
- `gfx_button_layout_test/` - проверка геометрии кнопок, особенно нижних рядов и широких кнопок
- `lvgl_basic_test/` - минимальный тест `LVGL` с заголовком, текстом и кнопками
- `lvgl_interaction_test/` - расширенный тест `LVGL` с 9 кнопками, touch-вводом и статусом нажатия
- `lvgl_touch_debug_test/` - диагностика touch: сырые координаты, точка касания и одна большая кнопка `LVGL`
- `lvgl_official_based_test/` - минимальный наш экран поверх максимально близкой к официальному `05_LVGL_Widgets` инициализации
- `lvgl_subserver_touch_test/` - старый гибридный тест: `LVGL`-экран с инициализацией дисплея и чтением touch из старого `shine_homeserver_ui`; подтверждён на реальном устройстве
- `lvgl_russian_font_test/` - тест кастомного кириллического `LVGL`-шрифта с русскими кнопками, длинными строками и рабочим touch
- `usb_contact_sensor_test/` - тест переключения нативных USB D-/D+ (`GPIO19/20`) в режим простого замыкаемого контакта после нажатия Start; после перезагрузки снова обычный USB
- `usb_contact_sensor_debug_test/` - debug-версия теста замыкания контакта: экранная кнопка проверяет touch, а переход в sensor mode выполняется физической кнопкой BOOT
- `usb_contact_resistance_test/` - тест измерения сопротивления между USB D-/D+ через ADC на GPIO20; рассчитан на внешний известный резистор 10 kΩ с D+ на GND
- `lvgl_nav_minimal_test/` - старое тестовое имя, этот скетч перенесён в `shine_homeserver_main/` и теперь является основным
## Запуск
Использовать через `burn.sh`:
- `./burn.sh gfx-text-test`
- `./burn.sh gfx-layout-test`
- `./burn.sh lvgl-basic-test`
- `./burn.sh lvgl-interaction-test`
- `./burn.sh lvgl-touch-debug-test`
- `./burn.sh lvgl-official-based-test`
- `./burn.sh lvgl-subserver-touch-test`
- `./burn.sh lvgl-russian-font-test`
- `./burn.sh usb-contact-test`
- `./burn.sh usb-contact-debug-test`
- `./burn.sh usb-contact-resistance-test`
@@ -0,0 +1,93 @@
#include <Arduino.h>
#include <Wire.h>
#include <Arduino_GFX_Library.h>
#define PIN_LCD_CS 12
#define PIN_LCD_SCLK 38
#define PIN_LCD_D0 4
#define PIN_LCD_D1 5
#define PIN_LCD_D2 6
#define PIN_LCD_D3 7
#define PIN_LCD_RST 2
#define PIN_I2C_SDA 15
#define PIN_I2C_SCL 14
#define DISP_W 480
#define DISP_H 480
#define C_BG 0x0841u
#define C_PANEL 0x1082u
#define C_CARD 0x18C3u
#define C_BORDER 0x39C7u
#define C_TEXT 0xFFFFu
#define C_OK 0x3666u
#define C_WARN 0xECA0u
#define C_BUTTON 0x2145u
#define C_BUTTON2 0x3186u
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
PIN_LCD_CS, PIN_LCD_SCLK, PIN_LCD_D0, PIN_LCD_D1, PIN_LCD_D2, PIN_LCD_D3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, PIN_LCD_RST, 0, DISP_W, DISP_H, 0, 0, 0, 0);
static void drawPanel(int x, int y, int w, int h, uint16_t fill, uint16_t border, int radius = 10) {
gfx->fillRoundRect(x, y, w, h, radius, fill);
gfx->drawRoundRect(x, y, w, h, radius, border);
}
static void drawText(int x, int y, const char *text, uint16_t color, uint8_t size = 2) {
gfx->setFont();
gfx->setTextSize(size);
gfx->setTextColor(color);
gfx->setCursor(x, y);
gfx->print(text);
}
static void drawButton(int x, int y, int w, int h, uint16_t fill, const char *label, uint8_t size = 2) {
drawPanel(x, y, w, h, fill, C_BORDER, 12);
gfx->setFont();
gfx->setTextSize(size);
gfx->setTextColor(C_TEXT);
int textW = strlen(label) * 6 * size;
int textX = x + (w - textW) / 2;
if (textX < x + 8) {
textX = x + 8;
}
int textY = y + (h + 8 * size) / 2 - 2;
gfx->setCursor(textX, textY);
gfx->print(label);
}
static void drawScreen() {
gfx->fillScreen(C_BG);
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
drawText(24, 42, "BUTTON LAYOUT TEST", C_TEXT, 2);
drawText(24, 70, "Check bottom and wide buttons", C_WARN, 1);
drawButton(20, 110, 136, 52, C_BUTTON, "STATUS");
drawButton(172, 110, 136, 52, C_BUTTON, "CONNECT");
drawButton(324, 110, 136, 52, C_BUTTON, "ACCOUNT");
drawButton(20, 180, 136, 52, C_BUTTON, "WALLET");
drawButton(172, 180, 136, 52, C_BUTTON, "REQUESTS");
drawButton(324, 180, 136, 52, C_BUTTON, "SETTINGS");
drawButton(20, 270, 212, 48, C_BUTTON2, "BACK");
drawButton(248, 270, 212, 48, C_OK, "REFRESH");
drawButton(20, 338, 440, 40, C_BUTTON2, "FULL WIDTH BUTTON", 2);
drawButton(20, 390, 440, 56, C_OK, "REGISTER DEVICE", 2);
}
void setup() {
Serial.begin(115200);
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL);
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
drawScreen();
}
void loop() {
delay(1000);
}
@@ -0,0 +1,104 @@
#include <Arduino.h>
#include <Wire.h>
#include <Arduino_GFX_Library.h>
#include <U8g2lib.h>
#define PIN_LCD_CS 12
#define PIN_LCD_SCLK 38
#define PIN_LCD_D0 4
#define PIN_LCD_D1 5
#define PIN_LCD_D2 6
#define PIN_LCD_D3 7
#define PIN_LCD_RST 2
#define PIN_I2C_SDA 15
#define PIN_I2C_SCL 14
#define DISP_W 480
#define DISP_H 480
#define C_BG 0x0841u
#define C_PANEL 0x1082u
#define C_CARD 0x18C3u
#define C_BORDER 0x39C7u
#define C_TEXT 0xFFFFu
#define C_MUTE 0xBDF7u
#define C_OK 0x3666u
#define C_WARN 0xECA0u
#define C_BUTTON 0x2145u
#define C_BUTTON2 0x3186u
#define FONT_BODY u8g2_font_9x15_t_cyrillic
#define FONT_SMALL u8g2_font_6x13_t_cyrillic
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
PIN_LCD_CS, PIN_LCD_SCLK, PIN_LCD_D0, PIN_LCD_D1, PIN_LCD_D2, PIN_LCD_D3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, PIN_LCD_RST, 0, DISP_W, DISP_H, 0, 0, 0, 0);
static void drawPanel(int x, int y, int w, int h, uint16_t fill, uint16_t border, int radius = 10) {
gfx->fillRoundRect(x, y, w, h, radius, fill);
gfx->drawRoundRect(x, y, w, h, radius, border);
}
static void drawDefaultText(int x, int y, const char *text, uint16_t color, uint8_t size = 1) {
gfx->setFont();
gfx->setTextSize(size);
gfx->setTextColor(color);
gfx->setCursor(x, y);
gfx->print(text);
}
static void drawU8Text(int x, int y, const String &text, uint16_t color, const uint8_t *font) {
gfx->setTextSize(1);
gfx->setFont(font);
gfx->setTextColor(color);
gfx->setCursor(x, y);
gfx->print(text);
}
static void drawButton(int x, int y, int w, int h, uint16_t fill, const String &label, const uint8_t *font, bool useDefaultFont = false) {
drawPanel(x, y, w, h, fill, C_BORDER, 12);
if (useDefaultFont) {
drawDefaultText(x + 14, y + 30, label.c_str(), C_TEXT, 2);
return;
}
drawU8Text(x + 14, y + 32, label, C_TEXT, font);
}
static void drawScreen() {
gfx->fillScreen(C_BG);
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
drawDefaultText(28, 42, "TEXT TEST 123", C_TEXT, 2);
drawU8Text(28, 72, "Default ASCII above", C_MUTE, (const uint8_t *)FONT_SMALL);
drawPanel(24, 90, 432, 72, C_CARD, C_BORDER, 12);
drawDefaultText(38, 118, "A: Default font ABC123", C_TEXT, 2);
drawDefaultText(38, 146, "Visible? then base path OK", C_WARN, 1);
drawPanel(24, 174, 432, 84, C_CARD, C_BORDER, 12);
drawU8Text(38, 204, "B: U8g2 ASCII abc123", C_TEXT, (const uint8_t *)FONT_BODY);
drawU8Text(38, 230, "C: Русский текст 123", C_OK, (const uint8_t *)FONT_BODY);
drawPanel(24, 270, 432, 84, C_CARD, C_BORDER, 12);
drawU8Text(38, 298, "D: Мелкий шрифт кнопок", C_TEXT, (const uint8_t *)FONT_SMALL);
drawU8Text(38, 320, "Если это видно, FONT_SMALL жив", C_MUTE, (const uint8_t *)FONT_SMALL);
drawButton(24, 368, 128, 72, C_BUTTON, "BTN 1", nullptr, true);
drawButton(176, 368, 128, 72, C_OK, "abc123", (const uint8_t *)FONT_BODY);
drawButton(328, 368, 128, 72, C_BUTTON2, "Русский", (const uint8_t *)FONT_BODY);
}
void setup() {
Serial.begin(115200);
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL);
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->setUTF8Print(true);
drawScreen();
}
void loop() {
delay(1000);
}
@@ -0,0 +1,125 @@
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <Arduino_GFX_Library.h>
#include "pin_config.h"
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
static lv_disp_draw_buf_t gDrawBuf;
static lv_color_t *gBuf1 = nullptr;
static lv_color_t *gBuf2 = nullptr;
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
static void lvglRounderCb(lv_disp_drv_t *dispDrv, lv_area_t *area) {
LV_UNUSED(dispDrv);
if (area->x1 % 2 != 0) area->x1--;
if (area->y1 % 2 != 0) area->y1--;
if (area->x2 % 2 == 0) area->x2++;
if (area->y2 % 2 == 0) area->y2++;
}
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
uint32_t w = area->x2 - area->x1 + 1;
uint32_t h = area->y2 - area->y1 + 1;
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void lvglTick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
static void createUi() {
lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0x0B1320), 0);
lv_obj_set_style_bg_opa(lv_scr_act(), LV_OPA_COVER, 0);
lv_obj_t *title = lv_label_create(lv_scr_act());
lv_label_set_text(title, "LVGL BASIC TEST");
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 18);
lv_obj_t *subtitle = lv_label_create(lv_scr_act());
lv_label_set_text(subtitle, "If this text is visible, LVGL path works.");
lv_obj_set_width(subtitle, 420);
lv_label_set_long_mode(subtitle, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_color(subtitle, lv_color_hex(0xD5DCE5), 0);
lv_obj_set_style_text_font(subtitle, &lv_font_montserrat_16, 0);
lv_obj_align(subtitle, LV_ALIGN_TOP_MID, 0, 58);
const char *labels[3] = {"Button One", "Second Button", "Bottom Action"};
const lv_color_t colors[3] = {
lv_color_hex(0x2B4C7E),
lv_color_hex(0x2E8B57),
lv_color_hex(0xB45F06),
};
const lv_coord_t ys[3] = {150, 236, 340};
const lv_coord_t hs[3] = {58, 58, 64};
for (int i = 0; i < 3; i++) {
lv_obj_t *btn = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn, 360, hs[i]);
lv_obj_align(btn, LV_ALIGN_TOP_MID, 0, ys[i]);
lv_obj_set_style_radius(btn, 14, 0);
lv_obj_set_style_bg_color(btn, colors[i], 0);
lv_obj_set_style_border_width(btn, 2, 0);
lv_obj_set_style_border_color(btn, lv_color_hex(0x7F96B0), 0);
lv_obj_t *label = lv_label_create(btn);
lv_label_set_text(label, labels[i]);
lv_obj_set_style_text_font(label, &lv_font_montserrat_20, 0);
lv_obj_set_style_text_color(label, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(label);
}
}
void setup() {
Serial.begin(115200);
Wire.begin(IIC_SDA, IIC_SCL);
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
lv_init();
uint32_t screenWidth = gfx->width();
uint32_t screenHeight = gfx->height();
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = screenWidth;
dispDrv.ver_res = screenHeight;
dispDrv.flush_cb = lvglFlushCb;
dispDrv.rounder_cb = lvglRounderCb;
dispDrv.draw_buf = &gDrawBuf;
lv_disp_drv_register(&dispDrv);
const esp_timer_create_args_t lvglTickTimerArgs = {
.callback = &lvglTick,
.name = "lvgl_tick"};
esp_timer_handle_t lvglTickTimer = nullptr;
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
createUi();
Serial.println("LVGL basic test ready");
}
void loop() {
lv_timer_handler();
delay(5);
}
@@ -0,0 +1,214 @@
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <Arduino_GFX_Library.h>
#include "pin_config.h"
#include "TouchDrvCSTXXX.hpp"
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
static lv_disp_draw_buf_t gDrawBuf;
static lv_color_t *gBuf1 = nullptr;
static lv_color_t *gBuf2 = nullptr;
static lv_obj_t *gStatusLabel = nullptr;
static uint32_t gPressCount = 0;
static TouchDrvCST92xx gTouch;
static int16_t gTouchX[5];
static int16_t gTouchY[5];
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
uint32_t w = area->x2 - area->x1 + 1;
uint32_t h = area->y2 - area->y1 + 1;
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void lvglTick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
LV_UNUSED(indevDrv);
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
if (touched > 0) {
data->state = LV_INDEV_STATE_PR;
data->point.x = gTouchX[0];
data->point.y = gTouchY[0];
return;
}
data->state = LV_INDEV_STATE_REL;
}
static void buttonEventCb(lv_event_t *event) {
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
return;
}
lv_obj_t *btn = lv_event_get_target(event);
const char *name = (const char *)lv_event_get_user_data(event);
gPressCount++;
lv_obj_set_style_outline_width(btn, 4, 0);
lv_obj_set_style_outline_color(btn, lv_color_hex(0xFFF3B0), 0);
lv_obj_set_style_outline_pad(btn, 2, 0);
String status = "Pressed: ";
status += name;
status += " (#";
status += String(gPressCount);
status += ")";
lv_label_set_text(gStatusLabel, status.c_str());
Serial.println(status);
}
static void createButton(lv_obj_t *parent, const char *labelText, lv_coord_t col, lv_coord_t row, uint32_t colorHex) {
static const lv_coord_t kStartX = 22;
static const lv_coord_t kStartY = 122;
static const lv_coord_t kButtonW = 134;
static const lv_coord_t kButtonH = 64;
static const lv_coord_t kGapX = 18;
static const lv_coord_t kGapY = 16;
lv_obj_t *btn = lv_btn_create(parent);
lv_obj_set_size(btn, kButtonW, kButtonH);
lv_obj_set_pos(btn, kStartX + col * (kButtonW + kGapX), kStartY + row * (kButtonH + kGapY));
lv_obj_set_style_radius(btn, 16, 0);
lv_obj_set_style_bg_color(btn, lv_color_hex(colorHex), 0);
lv_obj_set_style_border_width(btn, 2, 0);
lv_obj_set_style_border_color(btn, lv_color_hex(0x7F96B0), 0);
lv_obj_set_style_shadow_width(btn, 0, 0);
lv_obj_add_event_cb(btn, buttonEventCb, LV_EVENT_CLICKED, (void *)labelText);
lv_obj_t *label = lv_label_create(btn);
lv_label_set_text(label, labelText);
lv_obj_set_style_text_font(label, &lv_font_montserrat_18, 0);
lv_obj_set_style_text_color(label, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(label);
}
static void createUi() {
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0B1320), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_t *title = lv_label_create(screen);
lv_label_set_text(title, "LVGL INTERACTION TEST");
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 14);
lv_obj_t *subtitle = lv_label_create(screen);
lv_label_set_text(subtitle, "Tap buttons. Bottom label must change on every press.");
lv_obj_set_width(subtitle, 420);
lv_label_set_long_mode(subtitle, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_color(subtitle, lv_color_hex(0xD5DCE5), 0);
lv_obj_set_style_text_font(subtitle, &lv_font_montserrat_14, 0);
lv_obj_align(subtitle, LV_ALIGN_TOP_MID, 0, 48);
createButton(screen, "Status", 0, 0, 0x355C7D);
createButton(screen, "Connect", 1, 0, 0x2A9D8F);
createButton(screen, "Wallet", 2, 0, 0x457B9D);
createButton(screen, "Requests", 0, 1, 0xE76F51);
createButton(screen, "Settings", 1, 1, 0x8D5A97);
createButton(screen, "Register", 2, 1, 0x6A994E);
createButton(screen, "Approve", 0, 2, 0xBC6C25);
createButton(screen, "Reject", 1, 2, 0xB56576);
createButton(screen, "Back", 2, 2, 0x6C757D);
lv_obj_t *statusPanel = lv_obj_create(screen);
lv_obj_set_size(statusPanel, 436, 56);
lv_obj_align(statusPanel, LV_ALIGN_BOTTOM_MID, 0, -16);
lv_obj_set_style_radius(statusPanel, 14, 0);
lv_obj_set_style_bg_color(statusPanel, lv_color_hex(0x162033), 0);
lv_obj_set_style_border_width(statusPanel, 2, 0);
lv_obj_set_style_border_color(statusPanel, lv_color_hex(0x415A77), 0);
lv_obj_set_style_pad_all(statusPanel, 10, 0);
gStatusLabel = lv_label_create(statusPanel);
lv_label_set_text(gStatusLabel, "Pressed: none");
lv_obj_set_style_text_font(gStatusLabel, &lv_font_montserrat_18, 0);
lv_obj_set_style_text_color(gStatusLabel, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(gStatusLabel);
}
void setup() {
Serial.begin(115200);
Wire.begin(IIC_SDA, IIC_SCL);
pinMode(TP_RST, OUTPUT);
pinMode(TP_INT, INPUT);
digitalWrite(TP_RST, LOW);
delay(30);
digitalWrite(TP_RST, HIGH);
delay(50);
delay(1000);
gTouch.setPins(TP_RST, TP_INT);
bool touchOk = gTouch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
if (!touchOk) {
Serial.println("Touch init failed");
} else {
Serial.print("Touch model: ");
Serial.println(gTouch.getModelName());
gTouch.sleep();
gTouch.reset();
gTouch.setMaxCoordinates(480, 480);
gTouch.setSwapXY(true);
gTouch.setMirrorXY(true, false);
}
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->fillScreen(0x0000);
lv_init();
uint32_t screenWidth = gfx->width();
uint32_t screenHeight = gfx->height();
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = screenWidth;
dispDrv.ver_res = screenHeight;
dispDrv.flush_cb = lvglFlushCb;
dispDrv.draw_buf = &gDrawBuf;
lv_disp_drv_register(&dispDrv);
static lv_indev_drv_t indevDrv;
lv_indev_drv_init(&indevDrv);
indevDrv.type = LV_INDEV_TYPE_POINTER;
indevDrv.read_cb = lvglTouchRead;
lv_indev_drv_register(&indevDrv);
const esp_timer_create_args_t lvglTickTimerArgs = {
.callback = &lvglTick,
.name = "lvgl_tick"};
esp_timer_handle_t lvglTickTimer = nullptr;
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
createUi();
Serial.println("LVGL interaction test ready");
}
void loop() {
lv_timer_handler();
delay(5);
}
@@ -0,0 +1,252 @@
#include <lvgl.h>
#include "Arduino_GFX_Library.h"
#include "pin_config.h"
#include "TouchDrvCSTXXX.hpp"
#include "lv_conf.h"
#include "HWCDC.h"
#include <Wire.h>
HWCDC USBSerial;
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
static lv_disp_draw_buf_t draw_buf;
static lv_color_t *buf1 = nullptr;
static lv_color_t *buf2 = nullptr;
static lv_obj_t *status_label = nullptr;
static uint32_t click_count = 0;
Arduino_DataBus *bus = new Arduino_ESP32QSPI(
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
bus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
TouchDrvCST92xx touch;
int16_t touch_x[5], touch_y[5];
bool isPressed = false;
uint32_t screenWidth = 0;
uint32_t screenHeight = 0;
#if LV_USE_LOG != 0
void my_print(const char *buf) {
USBSerial.printf(buf);
USBSerial.flush();
}
#endif
static void example_lvgl_rounder_cb(struct _lv_disp_drv_t *disp_drv, lv_area_t *area) {
LV_UNUSED(disp_drv);
if (area->x1 % 2 != 0) area->x1--;
if (area->y1 % 2 != 0) area->y1--;
if (area->x2 % 2 == 0) area->x2++;
if (area->y2 % 2 == 0) area->y2++;
}
static void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) {
uint32_t w = (area->x2 - area->x1 + 1);
uint32_t h = (area->y2 - area->y1 + 1);
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&color_p->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&color_p->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void example_increase_lvgl_tick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
static void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) {
LV_UNUSED(indev_driver);
if (isPressed) {
uint8_t touched = touch.getPoint(touch_x, touch_y, touch.getSupportTouchPoint());
if (touched) {
isPressed = false;
data->state = LV_INDEV_STATE_PR;
data->point.x = touch_x[0];
data->point.y = touch_y[0];
USBSerial.print("Touch x=");
USBSerial.print(touch_x[0]);
USBSerial.print(" y=");
USBSerial.println(touch_y[0]);
} else {
data->state = LV_INDEV_STATE_REL;
}
} else {
data->state = LV_INDEV_STATE_REL;
}
}
static void button_event_cb(lv_event_t *event) {
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
return;
}
const char *name = (const char *)lv_event_get_user_data(event);
click_count++;
String text = "Pressed: ";
text += name;
text += " (#";
text += String(click_count);
text += ")";
lv_label_set_text(status_label, text.c_str());
USBSerial.println(text);
}
static lv_obj_t *make_button(lv_obj_t *parent, const char *label_text, lv_coord_t x, lv_coord_t y, lv_coord_t w, lv_coord_t h, lv_color_t bg) {
lv_obj_t *btn = lv_btn_create(parent);
lv_obj_set_size(btn, w, h);
lv_obj_set_pos(btn, x, y);
lv_obj_set_style_radius(btn, 16, 0);
lv_obj_set_style_bg_color(btn, bg, 0);
lv_obj_set_style_border_width(btn, 2, 0);
lv_obj_set_style_border_color(btn, lv_color_hex(0x8AA4BF), 0);
lv_obj_set_style_shadow_width(btn, 0, 0);
lv_obj_add_event_cb(btn, button_event_cb, LV_EVENT_CLICKED, (void *)label_text);
lv_obj_t *label = lv_label_create(btn);
lv_label_set_text(label, label_text);
lv_obj_set_style_text_font(label, &lv_font_montserrat_18, 0);
lv_obj_set_style_text_color(label, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(label);
return btn;
}
static void create_ui() {
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0B1320), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_t *title = lv_label_create(screen);
lv_label_set_text(title, "LVGL OFFICIAL BASED TEST");
lv_obj_set_style_text_font(title, &lv_font_montserrat_20, 0);
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 14);
lv_obj_t *hint = lv_label_create(screen);
lv_label_set_text(hint, "Based on official LVGL_Widgets init. Tap buttons and watch the status.");
lv_obj_set_width(hint, 430);
lv_label_set_long_mode(hint, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_font(hint, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(hint, lv_color_hex(0xD4DDE7), 0);
lv_obj_align(hint, LV_ALIGN_TOP_MID, 0, 44);
make_button(screen, "Status", 22, 120, 136, 60, lv_color_hex(0x355C7D));
make_button(screen, "Connect", 174, 120, 136, 60, lv_color_hex(0x2A9D8F));
make_button(screen, "Wallet", 326, 120, 132, 60, lv_color_hex(0x457B9D));
make_button(screen, "Requests", 22, 196, 136, 60, lv_color_hex(0xE76F51));
make_button(screen, "Settings",174, 196, 136, 60, lv_color_hex(0x8D5A97));
make_button(screen, "Register",326, 196, 132, 60, lv_color_hex(0x6A994E));
make_button(screen, "Approve", 22, 272, 136, 60, lv_color_hex(0xBC6C25));
make_button(screen, "Reject", 174, 272, 136, 60, lv_color_hex(0xB56576));
make_button(screen, "Back", 326, 272, 132, 60, lv_color_hex(0x6C757D));
lv_obj_t *bottom_btn = make_button(screen, "Bottom Action", 22, 350, 436, 64, lv_color_hex(0x1D6F42));
lv_obj_set_style_text_font(lv_obj_get_child(bottom_btn, 0), &lv_font_montserrat_20, 0);
lv_obj_t *status_panel = lv_obj_create(screen);
lv_obj_set_size(status_panel, 436, 42);
lv_obj_set_pos(status_panel, 22, 426);
lv_obj_set_style_radius(status_panel, 12, 0);
lv_obj_set_style_bg_color(status_panel, lv_color_hex(0x17263A), 0);
lv_obj_set_style_border_width(status_panel, 2, 0);
lv_obj_set_style_border_color(status_panel, lv_color_hex(0x496582), 0);
lv_obj_set_style_pad_all(status_panel, 6, 0);
status_label = lv_label_create(status_panel);
lv_label_set_text(status_label, "Pressed: none");
lv_obj_set_style_text_font(status_label, &lv_font_montserrat_16, 0);
lv_obj_set_style_text_color(status_label, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(status_label);
}
void setup() {
USBSerial.begin(115200);
Wire.begin(IIC_SDA, IIC_SCL);
digitalWrite(TP_RST, LOW);
delay(30);
digitalWrite(TP_RST, HIGH);
delay(50);
delay(1000);
Wire.begin(IIC_SDA, IIC_SCL);
touch.setPins(TP_RST, TP_INT);
bool result = touch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
if (result == false) {
USBSerial.println("touch is not online...");
while (1) delay(1000);
}
USBSerial.print("Touch model: ");
USBSerial.println(touch.getModelName());
touch.sleep();
touch.reset();
touch.setMaxCoordinates(480, 480);
touch.setSwapXY(true);
touch.setMirrorXY(true, false);
attachInterrupt(
TP_INT, []() {
isPressed = true;
},
FALLING);
gfx->begin();
gfx->setBrightness(200);
bus->writeC8D8(0x36, 0xA0);
screenWidth = gfx->width();
screenHeight = gfx->height();
lv_init();
buf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
buf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
#if LV_USE_LOG != 0
lv_log_register_print_cb(my_print);
#endif
lv_disp_draw_buf_init(&draw_buf, buf1, buf2, screenWidth * screenHeight / 4);
static lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = screenWidth;
disp_drv.ver_res = screenHeight;
disp_drv.flush_cb = my_disp_flush;
disp_drv.rounder_cb = example_lvgl_rounder_cb;
disp_drv.draw_buf = &draw_buf;
disp_drv.sw_rotate = 1;
lv_disp_drv_register(&disp_drv);
static lv_indev_drv_t indev_drv;
lv_indev_drv_init(&indev_drv);
indev_drv.type = LV_INDEV_TYPE_POINTER;
indev_drv.read_cb = my_touchpad_read;
lv_indev_drv_register(&indev_drv);
const esp_timer_create_args_t lvgl_tick_timer_args = {
.callback = &example_increase_lvgl_tick,
.name = "lvgl_tick"
};
esp_timer_handle_t lvgl_tick_timer = NULL;
esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer);
esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
create_ui();
USBSerial.println("LVGL official based test ready");
}
void loop() {
lv_timer_handler();
delay(5);
}
@@ -0,0 +1,4 @@
#pragma once
#include <lvgl.h>
LV_FONT_DECLARE(lv_font_ru_18);
LV_FONT_DECLARE(lv_font_ru_24);
@@ -0,0 +1,264 @@
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <Arduino_GFX_Library.h>
#include <TouchDrvCSTXXX.hpp>
#include "lv_font_ru.h"
#define PIN_LCD_CS 12
#define PIN_LCD_SCLK 38
#define PIN_LCD_D0 4
#define PIN_LCD_D1 5
#define PIN_LCD_D2 6
#define PIN_LCD_D3 7
#define PIN_LCD_RST 2
#define PIN_I2C_SDA 15
#define PIN_I2C_SCL 14
#define PIN_TP_INT 11
#define DISP_W 480
#define DISP_H 480
#define LVGL_TICK_MS 2
#define TEST_VERSION "v1-ru-font"
static lv_disp_draw_buf_t gDrawBuf;
static lv_color_t *gBuf1 = nullptr;
static lv_color_t *gBuf2 = nullptr;
static lv_obj_t *gStatusLabel = nullptr;
static lv_obj_t *gTouchLabel = nullptr;
static uint32_t gClickCount = 0;
static bool gTouchActive = false;
static int16_t gLastX = -1;
static int16_t gLastY = -1;
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
PIN_LCD_CS, PIN_LCD_SCLK, PIN_LCD_D0, PIN_LCD_D1, PIN_LCD_D2, PIN_LCD_D3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, PIN_LCD_RST, 0, DISP_W, DISP_H, 0, 0, 0, 0);
TouchDrvCST92xx gTouch;
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
uint32_t w = area->x2 - area->x1 + 1;
uint32_t h = area->y2 - area->y1 + 1;
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void lvglTick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(LVGL_TICK_MS);
}
static void updateTouchLabel() {
if (!gTouchLabel) {
return;
}
char text[96];
if (gTouchActive) {
snprintf(text, sizeof(text), "Касание: x=%d y=%d", gLastX, gLastY);
} else {
snprintf(text, sizeof(text), "Касание: нет x=%d y=%d", gLastX, gLastY);
}
lv_label_set_text(gTouchLabel, text);
}
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
LV_UNUSED(indevDrv);
int16_t x = 0;
int16_t y = 0;
bool touching = gTouch.getPoint(&x, &y, 1) > 0;
if (touching) {
gTouchActive = true;
gLastX = x;
gLastY = y;
data->state = LV_INDEV_STATE_PR;
data->point.x = x;
data->point.y = y;
} else {
gTouchActive = false;
data->state = LV_INDEV_STATE_REL;
data->point.x = gLastX >= 0 ? gLastX : 0;
data->point.y = gLastY >= 0 ? gLastY : 0;
}
updateTouchLabel();
}
static void buttonEventCb(lv_event_t *event) {
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
return;
}
const char *name = (const char *)lv_event_get_user_data(event);
gClickCount++;
char text[128];
snprintf(text, sizeof(text), "Нажато: %s (%lu)", name, (unsigned long)gClickCount);
lv_label_set_text(gStatusLabel, text);
Serial.println(text);
}
static lv_obj_t *makeButton(lv_obj_t *parent,
const char *labelText,
lv_coord_t x,
lv_coord_t y,
lv_coord_t w,
lv_coord_t h,
uint32_t bgColor) {
lv_obj_t *btn = lv_btn_create(parent);
lv_obj_set_size(btn, w, h);
lv_obj_set_pos(btn, x, y);
lv_obj_set_style_radius(btn, 16, 0);
lv_obj_set_style_bg_color(btn, lv_color_hex(bgColor), 0);
lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(btn, 2, 0);
lv_obj_set_style_border_color(btn, lv_color_hex(0x6F8BA4), 0);
lv_obj_set_style_shadow_width(btn, 0, 0);
lv_obj_set_style_pad_all(btn, 4, 0);
lv_obj_add_event_cb(btn, buttonEventCb, LV_EVENT_CLICKED, (void *)labelText);
lv_obj_t *label = lv_label_create(btn);
lv_label_set_text(label, labelText);
lv_obj_set_width(label, w - 12);
lv_label_set_long_mode(label, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_style_text_color(label, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(label, &lv_font_ru_18, 0);
lv_obj_center(label);
return btn;
}
static void createUi() {
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x08131E), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_set_style_pad_all(screen, 0, 0);
lv_obj_set_scrollbar_mode(screen, LV_SCROLLBAR_MODE_OFF);
lv_obj_t *title = lv_label_create(screen);
lv_label_set_text(title, "Русский тест LVGL");
lv_obj_set_style_text_font(title, &lv_font_ru_24, 0);
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
lv_obj_t *version = lv_label_create(screen);
lv_label_set_text(version, "Версия: " TEST_VERSION);
lv_obj_set_style_text_font(version, &lv_font_ru_18, 0);
lv_obj_set_style_text_color(version, lv_color_hex(0xD8E0EA), 0);
lv_obj_align(version, LV_ALIGN_TOP_MID, 0, 42);
lv_obj_t *hint = lv_label_create(screen);
lv_label_set_text(hint, "Проверка кириллицы, длинных слов, кнопок и нажатий.");
lv_obj_set_width(hint, 436);
lv_label_set_long_mode(hint, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_font(hint, &lv_font_ru_18, 0);
lv_obj_set_style_text_color(hint, lv_color_hex(0xD8E0EA), 0);
lv_obj_align(hint, LV_ALIGN_TOP_MID, 0, 72);
makeButton(screen, "Статус", 22, 126, 136, 62, 0x355C7D);
makeButton(screen, "Подключение", 172, 126, 136, 62, 0x2A9D8F);
makeButton(screen, "Кошелёк", 322, 126, 136, 62, 0x457B9D);
makeButton(screen, "Запросы", 22, 204, 136, 62, 0xE76F51);
makeButton(screen, "Настройки", 172, 204, 136, 62, 0x8D5A97);
makeButton(screen, "Регистрация", 322, 204, 136, 62, 0x6A994E);
makeButton(screen, "Разрешить", 22, 282, 136, 62, 0xBC6C25);
makeButton(screen, "Отклонить", 172, 282, 136, 62, 0xB56576);
makeButton(screen, "Назад", 322, 282, 136, 62, 0x6C757D);
makeButton(screen, "Проверка переноса русского текста", 22, 360, 436, 64, 0x1D6F42);
lv_obj_t *statusPanel = lv_obj_create(screen);
lv_obj_set_size(statusPanel, 436, 24);
lv_obj_set_pos(statusPanel, 22, 428);
lv_obj_set_style_radius(statusPanel, 10, 0);
lv_obj_set_style_bg_color(statusPanel, lv_color_hex(0x162435), 0);
lv_obj_set_style_bg_opa(statusPanel, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(statusPanel, 1, 0);
lv_obj_set_style_border_color(statusPanel, lv_color_hex(0x4D6986), 0);
lv_obj_set_style_pad_all(statusPanel, 2, 0);
gStatusLabel = lv_label_create(statusPanel);
lv_label_set_text(gStatusLabel, "Нажато: ничего");
lv_obj_set_style_text_font(gStatusLabel, &lv_font_ru_18, 0);
lv_obj_set_style_text_color(gStatusLabel, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(gStatusLabel);
lv_obj_t *touchPanel = lv_obj_create(screen);
lv_obj_set_size(touchPanel, 436, 24);
lv_obj_set_pos(touchPanel, 22, 454);
lv_obj_set_style_radius(touchPanel, 10, 0);
lv_obj_set_style_bg_color(touchPanel, lv_color_hex(0x162435), 0);
lv_obj_set_style_bg_opa(touchPanel, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(touchPanel, 1, 0);
lv_obj_set_style_border_color(touchPanel, lv_color_hex(0x4D6986), 0);
lv_obj_set_style_pad_all(touchPanel, 2, 0);
gTouchLabel = lv_label_create(touchPanel);
lv_label_set_text(gTouchLabel, "Касание: нет x=-1 y=-1");
lv_obj_set_style_text_font(gTouchLabel, &lv_font_ru_18, 0);
lv_obj_set_style_text_color(gTouchLabel, lv_color_hex(0xD8E0EA), 0);
lv_obj_center(gTouchLabel);
}
void setup() {
Serial.begin(115200);
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL);
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->fillScreen(0x0000);
gTouch.setPins(PIN_TP_INT, -1);
gTouch.begin(Wire, CST92XX_SLAVE_ADDRESS, PIN_I2C_SDA, PIN_I2C_SCL);
gTouch.setMaxCoordinates(DISP_W, DISP_H);
gTouch.setSwapXY(true);
gTouch.setMirrorXY(true, false);
lv_init();
uint32_t screenWidth = gfx->width();
uint32_t screenHeight = gfx->height();
uint32_t bufPixels = screenWidth * 40;
gBuf1 = (lv_color_t *)heap_caps_malloc(bufPixels * sizeof(lv_color_t), MALLOC_CAP_DMA);
gBuf2 = (lv_color_t *)heap_caps_malloc(bufPixels * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, bufPixels);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = screenWidth;
dispDrv.ver_res = screenHeight;
dispDrv.flush_cb = lvglFlushCb;
dispDrv.draw_buf = &gDrawBuf;
lv_disp_drv_register(&dispDrv);
static lv_indev_drv_t indevDrv;
lv_indev_drv_init(&indevDrv);
indevDrv.type = LV_INDEV_TYPE_POINTER;
indevDrv.read_cb = lvglTouchRead;
lv_indev_drv_register(&indevDrv);
const esp_timer_create_args_t tickArgs = {
.callback = &lvglTick,
.name = "lvgl_tick"};
esp_timer_handle_t tickTimer = nullptr;
esp_timer_create(&tickArgs, &tickTimer);
esp_timer_start_periodic(tickTimer, LVGL_TICK_MS * 1000);
createUi();
Serial.println("Русский LVGL font test ready: " TEST_VERSION);
}
void loop() {
lv_timer_handler();
delay(5);
}
@@ -0,0 +1,256 @@
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <Arduino_GFX_Library.h>
#include <TouchDrvCSTXXX.hpp>
// Подтверждено на устройстве: LVGL-рендер работает вместе с touch-путём из shine_homeserver_ui.
#define PIN_LCD_CS 12
#define PIN_LCD_SCLK 38
#define PIN_LCD_D0 4
#define PIN_LCD_D1 5
#define PIN_LCD_D2 6
#define PIN_LCD_D3 7
#define PIN_LCD_RST 2
#define PIN_I2C_SDA 15
#define PIN_I2C_SCL 14
#define PIN_TP_INT 11
#define DISP_W 480
#define DISP_H 480
#define LVGL_TICK_MS 2
#define TEST_VERSION "v1-subtouch"
static lv_disp_draw_buf_t gDrawBuf;
static lv_color_t *gBuf1 = nullptr;
static lv_color_t *gBuf2 = nullptr;
static lv_obj_t *gStatusLabel = nullptr;
static lv_obj_t *gTouchLabel = nullptr;
static lv_obj_t *gVersionLabel = nullptr;
static uint32_t gClickCount = 0;
static bool gTouchActive = false;
static int16_t gLastX = -1;
static int16_t gLastY = -1;
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
PIN_LCD_CS, PIN_LCD_SCLK, PIN_LCD_D0, PIN_LCD_D1, PIN_LCD_D2, PIN_LCD_D3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, PIN_LCD_RST, 0, DISP_W, DISP_H, 0, 0, 0, 0);
TouchDrvCST92xx gTouch;
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
uint32_t w = area->x2 - area->x1 + 1;
uint32_t h = area->y2 - area->y1 + 1;
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void lvglTick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(LVGL_TICK_MS);
}
static void updateTouchLabel() {
if (!gTouchLabel) {
return;
}
char text[96];
if (gTouchActive) {
snprintf(text, sizeof(text), "Touch: pressed x=%d y=%d", gLastX, gLastY);
} else {
snprintf(text, sizeof(text), "Touch: released x=%d y=%d", gLastX, gLastY);
}
lv_label_set_text(gTouchLabel, text);
}
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
LV_UNUSED(indevDrv);
int16_t x = 0;
int16_t y = 0;
bool touching = gTouch.getPoint(&x, &y, 1) > 0;
if (touching) {
gTouchActive = true;
gLastX = x;
gLastY = y;
data->state = LV_INDEV_STATE_PR;
data->point.x = x;
data->point.y = y;
} else {
gTouchActive = false;
data->state = LV_INDEV_STATE_REL;
data->point.x = gLastX >= 0 ? gLastX : 0;
data->point.y = gLastY >= 0 ? gLastY : 0;
}
updateTouchLabel();
}
static void buttonEventCb(lv_event_t *event) {
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
return;
}
const char *name = (const char *)lv_event_get_user_data(event);
gClickCount++;
char text[96];
snprintf(text, sizeof(text), "Pressed: %s (#%lu)", name, (unsigned long)gClickCount);
lv_label_set_text(gStatusLabel, text);
Serial.println(text);
}
static lv_obj_t *makeButton(lv_obj_t *parent,
const char *labelText,
lv_coord_t x,
lv_coord_t y,
lv_coord_t w,
lv_coord_t h,
uint32_t bgColor) {
lv_obj_t *btn = lv_btn_create(parent);
lv_obj_set_size(btn, w, h);
lv_obj_set_pos(btn, x, y);
lv_obj_set_style_radius(btn, 16, 0);
lv_obj_set_style_bg_color(btn, lv_color_hex(bgColor), 0);
lv_obj_set_style_bg_opa(btn, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(btn, 2, 0);
lv_obj_set_style_border_color(btn, lv_color_hex(0x6F8BA4), 0);
lv_obj_set_style_shadow_width(btn, 0, 0);
lv_obj_add_event_cb(btn, buttonEventCb, LV_EVENT_CLICKED, (void *)labelText);
lv_obj_t *label = lv_label_create(btn);
lv_label_set_text(label, labelText);
lv_obj_set_style_text_color(label, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(label, &lv_font_montserrat_18, 0);
lv_obj_center(label);
return btn;
}
static void createUi() {
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x07111C), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_set_style_pad_all(screen, 0, 0);
lv_obj_set_scrollbar_mode(screen, LV_SCROLLBAR_MODE_OFF);
gVersionLabel = lv_label_create(screen);
lv_label_set_text(gVersionLabel, "LVGL + subserver touch " TEST_VERSION);
lv_obj_set_style_text_font(gVersionLabel, &lv_font_montserrat_18, 0);
lv_obj_set_style_text_color(gVersionLabel, lv_color_hex(0xFFFFFF), 0);
lv_obj_align(gVersionLabel, LV_ALIGN_TOP_MID, 0, 12);
lv_obj_t *subtitle = lv_label_create(screen);
lv_label_set_text(subtitle, "Touch path comes from shine_homeserver_ui. Tap buttons and watch status.");
lv_obj_set_width(subtitle, 436);
lv_label_set_long_mode(subtitle, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_font(subtitle, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(subtitle, lv_color_hex(0xD6DEE7), 0);
lv_obj_align(subtitle, LV_ALIGN_TOP_MID, 0, 42);
makeButton(screen, "Status", 22, 110, 136, 62, 0x355C7D);
makeButton(screen, "Connect", 172, 110, 136, 62, 0x2A9D8F);
makeButton(screen, "Wallet", 322, 110, 136, 62, 0x457B9D);
makeButton(screen, "Requests", 22, 188, 136, 62, 0xE76F51);
makeButton(screen, "Settings", 172, 188, 136, 62, 0x8D5A97);
makeButton(screen, "Register", 322, 188, 136, 62, 0x6A994E);
makeButton(screen, "Approve", 22, 266, 136, 62, 0xBC6C25);
makeButton(screen, "Reject", 172, 266, 136, 62, 0xB56576);
makeButton(screen, "Back", 322, 266, 136, 62, 0x6C757D);
makeButton(screen, "Bottom Action", 22, 346, 436, 68, 0x1D6F42);
lv_obj_t *statusPanel = lv_obj_create(screen);
lv_obj_set_size(statusPanel, 436, 26);
lv_obj_set_pos(statusPanel, 22, 420);
lv_obj_set_style_radius(statusPanel, 10, 0);
lv_obj_set_style_bg_color(statusPanel, lv_color_hex(0x162435), 0);
lv_obj_set_style_bg_opa(statusPanel, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(statusPanel, 1, 0);
lv_obj_set_style_border_color(statusPanel, lv_color_hex(0x4D6986), 0);
lv_obj_set_style_pad_all(statusPanel, 2, 0);
gStatusLabel = lv_label_create(statusPanel);
lv_label_set_text(gStatusLabel, "Pressed: none");
lv_obj_set_style_text_font(gStatusLabel, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(gStatusLabel, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(gStatusLabel);
lv_obj_t *touchPanel = lv_obj_create(screen);
lv_obj_set_size(touchPanel, 436, 26);
lv_obj_set_pos(touchPanel, 22, 450);
lv_obj_set_style_radius(touchPanel, 10, 0);
lv_obj_set_style_bg_color(touchPanel, lv_color_hex(0x162435), 0);
lv_obj_set_style_bg_opa(touchPanel, LV_OPA_COVER, 0);
lv_obj_set_style_border_width(touchPanel, 1, 0);
lv_obj_set_style_border_color(touchPanel, lv_color_hex(0x4D6986), 0);
lv_obj_set_style_pad_all(touchPanel, 2, 0);
gTouchLabel = lv_label_create(touchPanel);
lv_label_set_text(gTouchLabel, "Touch: released x=-1 y=-1");
lv_obj_set_style_text_font(gTouchLabel, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(gTouchLabel, lv_color_hex(0xD6DEE7), 0);
lv_obj_center(gTouchLabel);
}
void setup() {
Serial.begin(115200);
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL);
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->fillScreen(0x0000);
gTouch.setPins(PIN_TP_INT, -1);
gTouch.begin(Wire, CST92XX_SLAVE_ADDRESS, PIN_I2C_SDA, PIN_I2C_SCL);
gTouch.setMaxCoordinates(DISP_W, DISP_H);
gTouch.setSwapXY(true);
gTouch.setMirrorXY(true, false);
lv_init();
uint32_t screenWidth = gfx->width();
uint32_t screenHeight = gfx->height();
uint32_t bufPixels = screenWidth * 40;
gBuf1 = (lv_color_t *)heap_caps_malloc(bufPixels * sizeof(lv_color_t), MALLOC_CAP_DMA);
gBuf2 = (lv_color_t *)heap_caps_malloc(bufPixels * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, bufPixels);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = screenWidth;
dispDrv.ver_res = screenHeight;
dispDrv.flush_cb = lvglFlushCb;
dispDrv.draw_buf = &gDrawBuf;
lv_disp_drv_register(&dispDrv);
static lv_indev_drv_t indevDrv;
lv_indev_drv_init(&indevDrv);
indevDrv.type = LV_INDEV_TYPE_POINTER;
indevDrv.read_cb = lvglTouchRead;
lv_indev_drv_register(&indevDrv);
const esp_timer_create_args_t tickArgs = {
.callback = &lvglTick,
.name = "lvgl_tick"};
esp_timer_handle_t tickTimer = nullptr;
esp_timer_create(&tickArgs, &tickTimer);
esp_timer_start_periodic(tickTimer, LVGL_TICK_MS * 1000);
createUi();
Serial.println("LVGL + subserver touch test ready: " TEST_VERSION);
}
void loop() {
lv_timer_handler();
delay(5);
}
@@ -0,0 +1,218 @@
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <Arduino_GFX_Library.h>
#include "pin_config.h"
#include "TouchDrvCSTXXX.hpp"
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
static lv_disp_draw_buf_t gDrawBuf;
static lv_color_t *gBuf1 = nullptr;
static lv_color_t *gBuf2 = nullptr;
static lv_obj_t *gRawLabel = nullptr;
static lv_obj_t *gEventLabel = nullptr;
static lv_obj_t *gMarker = nullptr;
static TouchDrvCST92xx gTouch;
static int16_t gTouchX[5];
static int16_t gTouchY[5];
static bool gTouchPressed = false;
static uint32_t gRawTouchCount = 0;
static uint32_t gLvglButtonCount = 0;
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
uint32_t w = area->x2 - area->x1 + 1;
uint32_t h = area->y2 - area->y1 + 1;
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void lvglTick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
LV_UNUSED(indevDrv);
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
if (touched > 0) {
data->state = LV_INDEV_STATE_PR;
data->point.x = gTouchX[0];
data->point.y = gTouchY[0];
return;
}
data->state = LV_INDEV_STATE_REL;
}
static void bigButtonEventCb(lv_event_t *event) {
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
return;
}
gLvglButtonCount++;
String status = "LVGL button clicked: ";
status += String(gLvglButtonCount);
lv_label_set_text(gEventLabel, status.c_str());
Serial.println(status);
}
static void updateRawTouchState() {
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
if (touched > 0) {
gTouchPressed = true;
gRawTouchCount++;
String line1 = "RAW pressed";
String line2 = "x=" + String(gTouchX[0]) + " y=" + String(gTouchY[0]) + " n=" + String(gRawTouchCount);
String text = line1 + "\n" + line2;
lv_label_set_text(gRawLabel, text.c_str());
int markerX = gTouchX[0] - 8;
int markerY = gTouchY[0] - 8;
if (markerX < 0) markerX = 0;
if (markerY < 0) markerY = 0;
lv_obj_set_pos(gMarker, markerX, markerY);
lv_obj_clear_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
return;
}
if (gTouchPressed) {
gTouchPressed = false;
lv_label_set_text(gRawLabel, "RAW released\nTouch ended");
lv_obj_add_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
}
}
static void createUi() {
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x000000), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_t *title = lv_label_create(screen);
lv_label_set_text(title, "LVGL TOUCH DEBUG");
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 12);
gRawLabel = lv_label_create(screen);
lv_label_set_text(gRawLabel, "RAW released\nTouch not seen yet");
lv_obj_set_style_text_font(gRawLabel, &lv_font_montserrat_18, 0);
lv_obj_set_style_text_color(gRawLabel, lv_color_hex(0x9EE493), 0);
lv_obj_align(gRawLabel, LV_ALIGN_TOP_LEFT, 16, 56);
gEventLabel = lv_label_create(screen);
lv_label_set_text(gEventLabel, "LVGL button clicked: 0");
lv_obj_set_style_text_font(gEventLabel, &lv_font_montserrat_18, 0);
lv_obj_set_style_text_color(gEventLabel, lv_color_hex(0xFFD166), 0);
lv_obj_align(gEventLabel, LV_ALIGN_TOP_LEFT, 16, 118);
lv_obj_t *btn = lv_btn_create(screen);
lv_obj_set_size(btn, 360, 110);
lv_obj_align(btn, LV_ALIGN_CENTER, 0, 36);
lv_obj_set_style_radius(btn, 18, 0);
lv_obj_set_style_bg_color(btn, lv_color_hex(0x2A9D8F), 0);
lv_obj_set_style_border_width(btn, 3, 0);
lv_obj_set_style_border_color(btn, lv_color_hex(0xBDE0FE), 0);
lv_obj_add_event_cb(btn, bigButtonEventCb, LV_EVENT_CLICKED, nullptr);
lv_obj_t *btnLabel = lv_label_create(btn);
lv_label_set_text(btnLabel, "Tap Here");
lv_obj_set_style_text_font(btnLabel, &lv_font_montserrat_24, 0);
lv_obj_set_style_text_color(btnLabel, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(btnLabel);
lv_obj_t *hint = lv_label_create(screen);
lv_label_set_text(hint, "Need both: RAW coords change and LVGL button click.");
lv_obj_set_width(hint, 430);
lv_label_set_long_mode(hint, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_font(hint, &lv_font_montserrat_16, 0);
lv_obj_set_style_text_color(hint, lv_color_hex(0xD9D9D9), 0);
lv_obj_align(hint, LV_ALIGN_BOTTOM_MID, 0, -34);
gMarker = lv_obj_create(screen);
lv_obj_set_size(gMarker, 16, 16);
lv_obj_set_style_radius(gMarker, LV_RADIUS_CIRCLE, 0);
lv_obj_set_style_bg_color(gMarker, lv_color_hex(0xFF4D6D), 0);
lv_obj_set_style_border_width(gMarker, 2, 0);
lv_obj_set_style_border_color(gMarker, lv_color_hex(0xFFFFFF), 0);
lv_obj_add_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
}
void setup() {
Serial.begin(115200);
Wire.begin(IIC_SDA, IIC_SCL);
pinMode(TP_RST, OUTPUT);
pinMode(TP_INT, INPUT);
digitalWrite(TP_RST, LOW);
delay(30);
digitalWrite(TP_RST, HIGH);
delay(50);
delay(1000);
gTouch.setPins(TP_RST, TP_INT);
bool touchOk = gTouch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
if (!touchOk) {
Serial.println("Touch init failed");
} else {
Serial.print("Touch model: ");
Serial.println(gTouch.getModelName());
gTouch.sleep();
gTouch.reset();
gTouch.setMaxCoordinates(480, 480);
gTouch.setSwapXY(true);
gTouch.setMirrorXY(true, false);
}
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->fillScreen(0x0000);
lv_init();
uint32_t screenWidth = gfx->width();
uint32_t screenHeight = gfx->height();
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = screenWidth;
dispDrv.ver_res = screenHeight;
dispDrv.flush_cb = lvglFlushCb;
dispDrv.draw_buf = &gDrawBuf;
lv_disp_drv_register(&dispDrv);
static lv_indev_drv_t indevDrv;
lv_indev_drv_init(&indevDrv);
indevDrv.type = LV_INDEV_TYPE_POINTER;
indevDrv.read_cb = lvglTouchRead;
lv_indev_drv_register(&indevDrv);
const esp_timer_create_args_t lvglTickTimerArgs = {
.callback = &lvglTick,
.name = "lvgl_tick"};
esp_timer_handle_t lvglTickTimer = nullptr;
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
createUi();
Serial.println("LVGL touch debug test ready");
}
void loop() {
updateRawTouchState();
lv_timer_handler();
delay(5);
}
@@ -0,0 +1,295 @@
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <Arduino_GFX_Library.h>
#include "pin_config.h"
#include "TouchDrvCSTXXX.hpp"
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
// ESP32-S3 native USB data pins.
static constexpr int kUsbDmPin = 19; // USB D-
static constexpr int kUsbDpPin = 20; // USB D+, ADC input in resistance mode.
static constexpr uint32_t kUiRefreshMs = 120;
static constexpr uint32_t kAdcSamples = 32;
// For a stable ohm reading connect a known resistor from D+ / GPIO20 to GND.
// The measured resistance must be connected between D- / GPIO19 and D+ / GPIO20.
static constexpr float kKnownPulldownOhms = 10000.0f;
static constexpr float kSourceMillivolts = 3300.0f;
static constexpr float kOpenThresholdMillivolts = 25.0f;
static lv_disp_draw_buf_t gDrawBuf;
static lv_color_t *gBuf1 = nullptr;
static lv_color_t *gBuf2 = nullptr;
static lv_obj_t *gModeLabel = nullptr;
static lv_obj_t *gResistanceLabel = nullptr;
static lv_obj_t *gRawLabel = nullptr;
static lv_obj_t *gHintLabel = nullptr;
static lv_obj_t *gActionButton = nullptr;
static lv_obj_t *gActionButtonLabel = nullptr;
static TouchDrvCST92xx gTouch;
static int16_t gTouchX[5];
static int16_t gTouchY[5];
static bool gSensorMode = false;
static uint32_t gLastUiRefreshMs = 0;
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
uint32_t w = area->x2 - area->x1 + 1;
uint32_t h = area->y2 - area->y1 + 1;
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void lvglTick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
LV_UNUSED(indevDrv);
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
if (touched > 0) {
data->state = LV_INDEV_STATE_PR;
data->point.x = gTouchX[0];
data->point.y = gTouchY[0];
return;
}
data->state = LV_INDEV_STATE_REL;
}
static uint32_t readAverageRaw() {
uint32_t sum = 0;
for (uint32_t i = 0; i < kAdcSamples; i++) {
sum += analogRead(kUsbDpPin);
delayMicroseconds(250);
}
return sum / kAdcSamples;
}
static uint32_t readAverageMillivolts() {
uint32_t sum = 0;
for (uint32_t i = 0; i < kAdcSamples; i++) {
sum += analogReadMilliVolts(kUsbDpPin);
delayMicroseconds(250);
}
return sum / kAdcSamples;
}
static void setResistanceText(float ohms) {
if (ohms >= 1000000.0f) {
String text = String(ohms / 1000000.0f, 2) + " Mohm";
lv_label_set_text(gResistanceLabel, text.c_str());
} else if (ohms >= 1000.0f) {
String text = String(ohms / 1000.0f, 2) + " kohm";
lv_label_set_text(gResistanceLabel, text.c_str());
} else {
String text = String(ohms, 0) + " ohm";
lv_label_set_text(gResistanceLabel, text.c_str());
}
}
static void updateResistanceUi() {
if (!gSensorMode) {
return;
}
uint32_t now = millis();
if (now - gLastUiRefreshMs < kUiRefreshMs) {
return;
}
gLastUiRefreshMs = now;
uint32_t raw = readAverageRaw();
uint32_t millivolts = readAverageMillivolts();
if (millivolts <= kOpenThresholdMillivolts) {
lv_label_set_text(gResistanceLabel, "OPEN");
lv_obj_set_style_text_color(gResistanceLabel, lv_color_hex(0xFFD166), 0);
} else if (millivolts >= (uint32_t)(kSourceMillivolts - 80.0f)) {
lv_label_set_text(gResistanceLabel, "SHORT / LOW");
lv_obj_set_style_text_color(gResistanceLabel, lv_color_hex(0x7CFF8A), 0);
} else {
float rx = kKnownPulldownOhms * ((kSourceMillivolts / (float)millivolts) - 1.0f);
lv_obj_set_style_text_color(gResistanceLabel, lv_color_hex(0x7CFF8A), 0);
setResistanceText(rx);
}
String rawText = "ADC raw: " + String(raw);
rawText += "\nD+ voltage: " + String(millivolts) + " mV";
rawText += "\nKnown pulldown: 10 kohm to GND";
lv_label_set_text(gRawLabel, rawText.c_str());
}
static void enterSensorMode() {
Serial.flush();
delay(30);
pinMode(kUsbDmPin, OUTPUT);
digitalWrite(kUsbDmPin, HIGH);
pinMode(kUsbDpPin, INPUT);
analogReadResolution(12);
analogSetPinAttenuation(kUsbDpPin, ADC_11db);
gSensorMode = true;
gLastUiRefreshMs = 0;
lv_label_set_text(gModeLabel, "Resistance mode");
lv_label_set_text(gHintLabel, "D- is 3.3V source. D+ is ADC input.\nUse 10 kohm from D+ to GND for accurate readings.");
lv_label_set_text(gActionButtonLabel, "Restart to USB");
updateResistanceUi();
}
static void actionButtonEventCb(lv_event_t *event) {
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
return;
}
if (!gSensorMode) {
enterSensorMode();
return;
}
lv_label_set_text(gModeLabel, "Restarting...");
lv_label_set_text(gHintLabel, "USB mode will return after reboot.");
lv_timer_handler();
delay(150);
ESP.restart();
}
static void createUi() {
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0B1320), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_t *title = lv_label_create(screen);
lv_label_set_text(title, "USB RESISTANCE TEST");
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 24);
gModeLabel = lv_label_create(screen);
lv_label_set_text(gModeLabel, "USB mode");
lv_obj_set_style_text_color(gModeLabel, lv_color_hex(0xB8C8DC), 0);
lv_obj_set_style_text_font(gModeLabel, &lv_font_montserrat_18, 0);
lv_obj_align(gModeLabel, LV_ALIGN_TOP_MID, 0, 68);
gResistanceLabel = lv_label_create(screen);
lv_label_set_text(gResistanceLabel, "READY");
lv_obj_set_style_text_color(gResistanceLabel, lv_color_hex(0x72D6FF), 0);
lv_obj_set_style_text_font(gResistanceLabel, &lv_font_montserrat_40, 0);
lv_obj_align(gResistanceLabel, LV_ALIGN_CENTER, 0, -72);
gRawLabel = lv_label_create(screen);
lv_label_set_text(gRawLabel, "ADC raw: -\nD+ voltage: -\nKnown pulldown: 10 kohm to GND");
lv_obj_set_width(gRawLabel, 420);
lv_label_set_long_mode(gRawLabel, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_align(gRawLabel, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_style_text_color(gRawLabel, lv_color_hex(0xD5DCE5), 0);
lv_obj_set_style_text_font(gRawLabel, &lv_font_montserrat_16, 0);
lv_obj_align(gRawLabel, LV_ALIGN_CENTER, 0, -8);
gHintLabel = lv_label_create(screen);
lv_label_set_text(gHintLabel, "USB is normal now.\nTap Start to measure resistance between D- and D+.");
lv_obj_set_width(gHintLabel, 420);
lv_label_set_long_mode(gHintLabel, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_align(gHintLabel, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_style_text_color(gHintLabel, lv_color_hex(0xAAB7C6), 0);
lv_obj_set_style_text_font(gHintLabel, &lv_font_montserrat_14, 0);
lv_obj_align(gHintLabel, LV_ALIGN_CENTER, 0, 72);
gActionButton = lv_btn_create(screen);
lv_obj_set_size(gActionButton, 260, 72);
lv_obj_align(gActionButton, LV_ALIGN_BOTTOM_MID, 0, -42);
lv_obj_set_style_radius(gActionButton, 18, 0);
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0x2A9D8F), 0);
lv_obj_set_style_border_width(gActionButton, 0, 0);
lv_obj_add_event_cb(gActionButton, actionButtonEventCb, LV_EVENT_CLICKED, nullptr);
gActionButtonLabel = lv_label_create(gActionButton);
lv_label_set_text(gActionButtonLabel, "Start");
lv_obj_set_style_text_color(gActionButtonLabel, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(gActionButtonLabel, &lv_font_montserrat_22, 0);
lv_obj_center(gActionButtonLabel);
}
void setup() {
Serial.begin(115200);
Wire.begin(IIC_SDA, IIC_SCL);
pinMode(TP_RST, OUTPUT);
pinMode(TP_INT, INPUT);
digitalWrite(TP_RST, LOW);
delay(30);
digitalWrite(TP_RST, HIGH);
delay(50);
delay(1000);
gTouch.setPins(TP_RST, TP_INT);
bool touchOk = gTouch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
if (!touchOk) {
Serial.println("Touch init failed");
} else {
gTouch.sleep();
gTouch.reset();
gTouch.setMaxCoordinates(480, 480);
gTouch.setSwapXY(true);
gTouch.setMirrorXY(true, false);
}
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->fillScreen(0x0000);
lv_init();
uint32_t screenWidth = gfx->width();
uint32_t screenHeight = gfx->height();
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = screenWidth;
dispDrv.ver_res = screenHeight;
dispDrv.flush_cb = lvglFlushCb;
dispDrv.draw_buf = &gDrawBuf;
lv_disp_drv_register(&dispDrv);
static lv_indev_drv_t indevDrv;
lv_indev_drv_init(&indevDrv);
indevDrv.type = LV_INDEV_TYPE_POINTER;
indevDrv.read_cb = lvglTouchRead;
lv_indev_drv_register(&indevDrv);
const esp_timer_create_args_t lvglTickTimerArgs = {
.callback = &lvglTick,
.name = "lvgl_tick"};
esp_timer_handle_t lvglTickTimer = nullptr;
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
createUi();
Serial.println("USB resistance test ready; native USB remains active until Start is tapped.");
}
void loop() {
updateResistanceUi();
lv_timer_handler();
delay(5);
}
@@ -0,0 +1,600 @@
#include <Arduino.h>
#include <Wire.h>
#include <math.h>
#include <Preferences.h>
#include <Arduino_GFX_Library.h>
#include <TouchDrvCSTXXX.hpp>
#include "ESP_I2S.h"
#include "es8311.h"
#define PIN_LCD_CS 12
#define PIN_LCD_SCLK 38
#define PIN_LCD_D0 4
#define PIN_LCD_D1 5
#define PIN_LCD_D2 6
#define PIN_LCD_D3 7
#define PIN_LCD_RST 2
#define PIN_I2C_SDA 15
#define PIN_I2C_SCL 14
#define PIN_TP_INT 11
#define PIN_USER_BTN 0
#define PIN_PA 46
#define PIN_I2S_BCLK 9
#define PIN_I2S_LRCK 45
#define PIN_I2S_DOUT 8
#define PIN_I2S_DIN 10
#define PIN_I2S_MCLK 42
#define DISP_W 480
#define DISP_H 480
static constexpr int kUsbDmPin = 19;
static constexpr int kUsbDpPin = 20;
static constexpr uint32_t kContactDebounceMs = 80;
static constexpr uint32_t kButtonDebounceMs = 45;
static constexpr uint32_t kUiPeriodMs = 200;
static constexpr uint32_t kSampleRate = 16000;
static constexpr uint32_t kMinCycleMs = 1500;
static constexpr uint32_t kPauseAfterMs = 60000;
#define C_BG 0x0841u
#define C_PANEL 0x1082u
#define C_CARD 0x18C3u
#define C_BORDER 0x39C7u
#define C_TEXT 0xFFFFu
#define C_MUTE 0xBDF7u
#define C_ACCENT 0x5D5Fu
#define C_OK 0x3666u
#define C_BAD 0xD145u
#define C_BUTTON 0x2145u
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
PIN_LCD_CS, PIN_LCD_SCLK, PIN_LCD_D0, PIN_LCD_D1, PIN_LCD_D2, PIN_LCD_D3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, PIN_LCD_RST, 0, DISP_W, DISP_H, 0, 0, 0, 0);
TouchDrvCST92xx gTouch;
I2SClass gI2s;
Preferences gPrefs;
struct Rect {
int16_t x;
int16_t y;
int16_t w;
int16_t h;
};
static const Rect kStartButton = {96, 348, 288, 78};
static const Rect kLimitSlider = {42, 132, 396, 44};
static const Rect kTickSlider = {42, 246, 396, 44};
static const Rect kAlertSlider = {42, 360, 396, 44};
static const uint16_t kLimitsSec[] = {5, 7, 10, 15, 20};
static const int16_t kTickAmpByVolume[] = {0, 40, 80, 140, 220, 320, 440, 580, 720, 800, 1600};
static const int16_t kAlertAmpByVolume[] = {0, 150, 300, 500, 800, 12000, 12000, 12000, 12000, 12000, 12000};
static bool gStarted = false;
static bool gSettingsOpen = false;
static bool gAudioReady = false;
static bool gStableClosed = false;
static bool gLastRawClosed = false;
static bool gStaticDrawn = false;
static bool gNeedRedraw = true;
static bool gNeedStaticRedraw = true;
static bool gLastDrawnClosed = false;
static bool gLastDrawnPaused = false;
static bool gTouchDown = false;
static bool gUserButtonDown = false;
static int16_t gTouchStartX = 0;
static int16_t gTouchStartY = 0;
static int16_t gTouchLastX = 0;
static int16_t gTouchLastY = 0;
static uint32_t gLastRawChangeMs = 0;
static uint32_t gStartedMs = 0;
static uint32_t gLastCycleStartMs = 0;
static uint32_t gLastUiMs = 0;
static uint32_t gUserButtonChangeMs = 0;
static uint32_t gCycleMs[5] = {};
static uint8_t gCycleCount = 0;
static uint8_t gCycleNext = 0;
static uint8_t gLimitIndex = 2;
static uint8_t gTickVolume = 3;
static uint8_t gAlertVolume = 10;
static uint8_t gShortStreak = 0;
static char gLastTopLine[48] = "";
static char gLastStreakLine[48] = "";
static void drawPanel(int x, int y, int w, int h, uint16_t fill, uint16_t border, int radius = 10) {
gfx->fillRoundRect(x, y, w, h, radius, fill);
gfx->drawRoundRect(x, y, w, h, radius, border);
}
static void drawText(int x, int y, const char *text, uint16_t color, uint8_t size = 2) {
gfx->setFont();
gfx->setTextSize(size);
gfx->setTextColor(color);
gfx->setCursor(x, y);
gfx->print(text);
}
static void drawCenteredText(int x, int y, int w, const char *text, uint16_t color, uint8_t size = 2) {
int textW = strlen(text) * 6 * size;
int textX = x + (w - textW) / 2;
if (textX < x + 8) {
textX = x + 8;
}
drawText(textX, y, text, color, size);
}
static void drawButton(const Rect &rect, const char *label, uint8_t size = 2) {
drawPanel(rect.x, rect.y, rect.w, rect.h, C_BUTTON, C_BORDER, 12);
drawCenteredText(rect.x, rect.y + 18, rect.w, label, C_TEXT, size);
}
static bool contains(const Rect &rect, int16_t x, int16_t y) {
return x >= rect.x && x < rect.x + rect.w && y >= rect.y && y < rect.y + rect.h;
}
static void clearArea(int x, int y, int w, int h, uint16_t fill) {
gfx->fillRect(x, y, w, h, fill);
}
static void formatSec(char *out, size_t outSize, uint32_t ms) {
snprintf(out, outSize, "%lu.%01lus", (unsigned long)(ms / 1000), (unsigned long)((ms % 1000) / 100));
}
static void formatClock(char *out, size_t outSize, uint32_t ms) {
uint32_t sec = ms / 1000;
snprintf(out, outSize, "%02lu:%02lu", (unsigned long)(sec / 60), (unsigned long)(sec % 60));
}
static uint32_t averageCycleMs() {
if (gCycleCount == 0) {
return 0;
}
uint32_t sum = 0;
for (uint8_t i = 0; i < gCycleCount; i++) {
sum += gCycleMs[i];
}
return sum / gCycleCount;
}
static void resetCachedUiText() {
gLastTopLine[0] = '\0';
gLastStreakLine[0] = '\0';
}
static void loadSettings() {
gPrefs.begin("breath-mon", false);
gLimitIndex = gPrefs.getUChar("limit", gLimitIndex);
gTickVolume = gPrefs.getUChar("tick", gTickVolume);
gAlertVolume = gPrefs.getUChar("alert", gAlertVolume);
if (gLimitIndex > 4) {
gLimitIndex = 2;
}
if (gTickVolume > 10) {
gTickVolume = 3;
}
if (gAlertVolume > 10) {
gAlertVolume = 10;
}
}
static void saveSettings() {
gPrefs.putUChar("limit", gLimitIndex);
gPrefs.putUChar("tick", gTickVolume);
gPrefs.putUChar("alert", gAlertVolume);
}
static bool isAutoPaused() {
return gStarted && gLastCycleStartMs != 0 && (millis() - gLastCycleStartMs) > kPauseAfterMs;
}
static esp_err_t codecInit() {
es8311_handle_t esHandle = es8311_create(0, ES8311_ADDRRES_0);
if (!esHandle) {
return ESP_FAIL;
}
const es8311_clock_config_t clk = {
.mclk_inverted = false,
.sclk_inverted = false,
.mclk_from_mclk_pin = true,
.mclk_frequency = (int)kSampleRate * 256,
.sample_frequency = (int)kSampleRate,
};
if (es8311_init(esHandle, &clk, ES8311_RESOLUTION_16, ES8311_RESOLUTION_16) != ESP_OK) {
return ESP_FAIL;
}
es8311_voice_volume_set(esHandle, 100, nullptr);
es8311_voice_mute(esHandle, false);
return ESP_OK;
}
static void initAudio() {
pinMode(PIN_PA, OUTPUT);
digitalWrite(PIN_PA, HIGH);
gI2s.setPins(PIN_I2S_BCLK, PIN_I2S_LRCK, PIN_I2S_DOUT, PIN_I2S_DIN, PIN_I2S_MCLK);
if (!gI2s.begin(I2S_MODE_STD, kSampleRate, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO, I2S_STD_SLOT_BOTH)) {
Serial.println("I2S init failed");
return;
}
gAudioReady = (codecInit() == ESP_OK);
Serial.println(gAudioReady ? "Audio init OK" : "Audio codec init failed");
}
static void playBeep(uint16_t freq, uint16_t samples, int16_t amp) {
if (!gAudioReady || amp <= 0) {
return;
}
static int16_t toneBuf[1600 * 2];
if (samples > 1600) {
samples = 1600;
}
for (int i = 0; i < samples; i++) {
float s = sinf(2.0f * 3.1415926f * (float)freq * ((float)i / (float)kSampleRate));
int16_t v = (int16_t)(s * amp);
toneBuf[i * 2] = v;
toneBuf[i * 2 + 1] = v;
}
gI2s.write((uint8_t *)toneBuf, samples * 2 * sizeof(int16_t));
}
static void playCycleTick() {
playBeep(880, 480, kTickAmpByVolume[gTickVolume]);
}
static void playAlertBeep() {
playBeep(1200, 1600, kAlertAmpByVolume[gAlertVolume]);
}
static void alertIfNeeded() {
if (gShortStreak == 0) {
return;
}
uint8_t count = min<uint8_t>(gShortStreak, 5);
for (uint8_t i = 0; i < count; i++) {
playAlertBeep();
delay(80);
}
}
static void addCycle(uint32_t cycleMs) {
gCycleMs[gCycleNext] = cycleMs;
gCycleNext = (gCycleNext + 1) % 5;
if (gCycleCount < 5) {
gCycleCount++;
}
uint32_t avgMs = averageCycleMs();
if (avgMs > 0 && avgMs < (uint32_t)kLimitsSec[gLimitIndex] * 1000UL) {
if (gShortStreak < 5) {
gShortStreak++;
}
} else {
gShortStreak = 0;
}
}
static void drawReadyScreen() {
gfx->fillScreen(C_BG);
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
drawText(28, 42, "BREATH MONITOR", C_TEXT, 3);
drawText(28, 78, "USB contact sensor", C_MUTE, 2);
drawPanel(34, 132, 412, 160, C_CARD, C_BORDER, 14);
drawCenteredText(34, 166, 412, "READY", C_ACCENT, 4);
drawCenteredText(34, 222, 412, "Closed = exhale", C_TEXT, 2);
drawCenteredText(34, 252, 412, "Open = inhale", C_MUTE, 2);
drawButton(kStartButton, "START", 3);
}
static void drawSlider(const Rect &rect, const char *title, const char *value, uint8_t pos, uint8_t maxPos) {
drawText(rect.x, rect.y - 28, title, C_TEXT, 2);
drawText(rect.x + 278, rect.y - 28, value, C_ACCENT, 2);
drawPanel(rect.x, rect.y, rect.w, rect.h, C_BUTTON, C_BORDER, 12);
int trackX = rect.x + 22;
int trackW = rect.w - 44;
int trackY = rect.y + rect.h / 2 - 3;
gfx->fillRect(trackX, trackY, trackW, 6, C_BORDER);
int knobX = trackX + (trackW * pos) / max<uint8_t>(1, maxPos);
gfx->fillRoundRect(knobX - 10, rect.y + 8, 20, rect.h - 16, 8, C_ACCENT);
}
static void drawSettingsScreen() {
char value[24];
gfx->fillScreen(C_BG);
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
drawText(28, 36, "SETTINGS", C_TEXT, 3);
drawText(28, 72, "Press top button to close", C_MUTE, 2);
snprintf(value, sizeof(value), "%us", kLimitsSec[gLimitIndex]);
drawSlider(kLimitSlider, "Limit", value, gLimitIndex, 4);
snprintf(value, sizeof(value), "%u/10", gTickVolume);
drawSlider(kTickSlider, "Cycle tick volume", value, gTickVolume, 10);
snprintf(value, sizeof(value), "%u/10", gAlertVolume);
drawSlider(kAlertSlider, "Alert volume", value, gAlertVolume, 10);
gStaticDrawn = false;
gNeedStaticRedraw = true;
}
static void drawMonitorStatic() {
gfx->fillScreen(C_BG);
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
drawText(28, 32, "BREATH MONITOR", C_TEXT, 2);
drawPanel(28, 96, 424, 104, C_CARD, C_BORDER, 14);
drawPanel(28, 216, 200, 72, C_CARD, C_BORDER, 10);
drawText(42, 236, "Current", C_MUTE, 1);
drawPanel(252, 216, 200, 72, C_CARD, C_BORDER, 10);
drawText(266, 236, "Average 5", C_MUTE, 1);
drawPanel(28, 304, 200, 64, C_CARD, C_BORDER, 10);
drawText(42, 322, "From start", C_MUTE, 1);
drawPanel(252, 304, 200, 64, C_CARD, C_BORDER, 10);
drawText(266, 322, "Limit", C_MUTE, 1);
gStaticDrawn = true;
gLastDrawnClosed = !gStableClosed;
gLastDrawnPaused = !isAutoPaused();
resetCachedUiText();
}
static void updateMonitorValues(bool force) {
char line[48];
char value[16];
uint32_t now = millis();
uint32_t runMs = now - gStartedMs;
uint32_t currentMs = (gLastCycleStartMs == 0) ? 0 : now - gLastCycleStartMs;
bool paused = isAutoPaused();
if (paused) {
currentMs = now - gLastCycleStartMs - kPauseAfterMs;
}
uint32_t avgMs = averageCycleMs();
if (force || gLastDrawnClosed != gStableClosed || gLastDrawnPaused != paused) {
clearArea(42, 118, 396, 68, C_CARD);
if (paused) {
drawCenteredText(28, 122, 424, "PAUSE", C_BAD, 4);
drawCenteredText(28, 172, 424, "no cycle for 60s", C_MUTE, 2);
} else {
drawCenteredText(28, 122, 424, gStableClosed ? "EXHALE" : "INHALE",
gStableClosed ? C_OK : C_ACCENT, 4);
drawCenteredText(28, 172, 424, gStableClosed ? "contact closed" : "contact open", C_MUTE, 2);
}
gLastDrawnClosed = gStableClosed;
gLastDrawnPaused = paused;
}
snprintf(line, sizeof(line), "Limit: %us top button: settings", kLimitsSec[gLimitIndex]);
if (strcmp(line, gLastTopLine) != 0) {
clearArea(28, 58, 424, 28, C_PANEL);
drawText(28, 60, line, C_MUTE, 2);
strncpy(gLastTopLine, line, sizeof(gLastTopLine) - 1);
gLastTopLine[sizeof(gLastTopLine) - 1] = '\0';
}
clearArea(42, 254, 160, 28, C_CARD);
formatSec(value, sizeof(value), currentMs);
drawText(42, 256, value, C_TEXT, 3);
clearArea(266, 254, 160, 28, C_CARD);
if (avgMs > 0) {
formatSec(value, sizeof(value), avgMs);
} else {
snprintf(value, sizeof(value), "--");
}
drawText(266, 256, value, C_TEXT, 3);
clearArea(42, 340, 160, 24, C_CARD);
formatClock(value, sizeof(value), runMs);
drawText(42, 342, value, C_TEXT, 2);
clearArea(266, 340, 160, 24, C_CARD);
snprintf(value, sizeof(value), "%us", kLimitsSec[gLimitIndex]);
drawText(266, 342, value, C_TEXT, 2);
snprintf(line, sizeof(line), "Short avg streak: %u", gShortStreak);
if (strcmp(line, gLastStreakLine) != 0) {
clearArea(28, 398, 280, 28, C_PANEL);
drawText(32, 402, line, gShortStreak > 0 ? C_BAD : C_MUTE, 2);
strncpy(gLastStreakLine, line, sizeof(gLastStreakLine) - 1);
gLastStreakLine[sizeof(gLastStreakLine) - 1] = '\0';
}
}
static void redrawScreen() {
if (gSettingsOpen) {
drawSettingsScreen();
} else if (gStarted) {
if (gNeedStaticRedraw || !gStaticDrawn) {
drawMonitorStatic();
gNeedStaticRedraw = false;
}
updateMonitorValues(true);
} else {
drawReadyScreen();
}
gNeedRedraw = false;
gLastUiMs = millis();
}
static void enterSensorMode() {
Serial.flush();
delay(30);
pinMode(kUsbDmPin, OUTPUT);
digitalWrite(kUsbDmPin, LOW);
pinMode(kUsbDpPin, INPUT_PULLUP);
uint32_t now = millis();
gStarted = true;
gStartedMs = now;
gLastCycleStartMs = 0;
gLastRawClosed = (digitalRead(kUsbDpPin) == LOW);
gStableClosed = gLastRawClosed;
gLastRawChangeMs = now;
if (gStableClosed) {
gLastCycleStartMs = now;
playCycleTick();
}
gNeedStaticRedraw = true;
gNeedRedraw = true;
}
static void toggleSettings() {
gSettingsOpen = !gSettingsOpen;
gNeedStaticRedraw = true;
gNeedRedraw = true;
resetCachedUiText();
}
static void handleStableContact(bool closed) {
uint32_t now = millis();
gStableClosed = closed;
if (closed) {
if (gLastCycleStartMs != 0) {
uint32_t cycleMs = now - gLastCycleStartMs;
if (cycleMs >= kPauseAfterMs) {
gShortStreak = 0;
gLastCycleStartMs = now;
playCycleTick();
} else if (cycleMs >= kMinCycleMs) {
addCycle(cycleMs);
gLastCycleStartMs = now;
playCycleTick();
alertIfNeeded();
}
} else {
gLastCycleStartMs = now;
playCycleTick();
}
}
gNeedRedraw = !gSettingsOpen;
}
static void pollContact() {
if (!gStarted) {
return;
}
bool rawClosed = (digitalRead(kUsbDpPin) == LOW);
uint32_t now = millis();
if (rawClosed != gLastRawClosed) {
gLastRawClosed = rawClosed;
gLastRawChangeMs = now;
}
if ((now - gLastRawChangeMs) >= kContactDebounceMs && gStableClosed != gLastRawClosed) {
handleStableContact(gLastRawClosed);
}
}
static void pollUserButton() {
bool down = digitalRead(PIN_USER_BTN) == LOW;
uint32_t now = millis();
if (down != gUserButtonDown && (now - gUserButtonChangeMs) >= kButtonDebounceMs) {
gUserButtonDown = down;
gUserButtonChangeMs = now;
if (down) {
toggleSettings();
}
}
}
static uint8_t sliderValueFromX(const Rect &rect, int16_t x, uint8_t maxValue) {
int16_t left = rect.x + 22;
int16_t right = rect.x + rect.w - 22;
if (x < left) x = left;
if (x > right) x = right;
return (uint8_t)(((uint32_t)(x - left) * maxValue + (right - left) / 2) / (right - left));
}
static void handleSettingsTouch(int16_t x, int16_t y) {
if (contains(kLimitSlider, x, y)) {
uint8_t next = sliderValueFromX(kLimitSlider, x, 4);
if (next == gLimitIndex) {
return;
}
gLimitIndex = next;
} else if (contains(kTickSlider, x, y)) {
uint8_t next = sliderValueFromX(kTickSlider, x, 10);
if (next == gTickVolume) {
return;
}
gTickVolume = next;
playCycleTick();
} else if (contains(kAlertSlider, x, y)) {
uint8_t next = sliderValueFromX(kAlertSlider, x, 10);
if (next == gAlertVolume) {
return;
}
gAlertVolume = next;
playAlertBeep();
} else {
return;
}
saveSettings();
gNeedRedraw = true;
}
static void pollTouch() {
int16_t x = 0;
int16_t y = 0;
bool touching = gTouch.getPoint(&x, &y, 1) > 0;
if (touching && !gTouchDown) {
gTouchDown = true;
gTouchStartX = x;
gTouchStartY = y;
gTouchLastX = x;
gTouchLastY = y;
return;
}
if (touching && gTouchDown) {
gTouchLastX = x;
gTouchLastY = y;
return;
}
if (!touching && gTouchDown) {
gTouchDown = false;
int dx = abs(gTouchLastX - gTouchStartX);
int dy = abs(gTouchLastY - gTouchStartY);
if (dx >= 14 || dy >= 14) {
return;
}
if (gSettingsOpen) {
handleSettingsTouch(gTouchLastX, gTouchLastY);
} else if (!gStarted && contains(kStartButton, gTouchLastX, gTouchLastY)) {
enterSensorMode();
}
}
}
void setup() {
Serial.begin(115200);
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL);
pinMode(PIN_USER_BTN, INPUT_PULLUP);
loadSettings();
initAudio();
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->fillScreen(C_BG);
gTouch.setPins(PIN_TP_INT, -1);
bool touchOk = gTouch.begin(Wire, CST92XX_SLAVE_ADDRESS, PIN_I2C_SDA, PIN_I2C_SCL);
gTouch.setMaxCoordinates(DISP_W, DISP_H);
gTouch.setSwapXY(true);
gTouch.setMirrorXY(true, false);
Serial.println("Breath monitor ready.");
Serial.println(touchOk ? "Touch init OK" : "Touch init failed");
gNeedRedraw = true;
}
void loop() {
pollUserButton();
pollTouch();
pollContact();
if (gStarted && !gSettingsOpen && millis() - gLastUiMs >= kUiPeriodMs) {
updateMonitorValues(false);
gLastUiMs = millis();
}
if (gNeedRedraw) {
redrawScreen();
}
delay(10);
}
@@ -0,0 +1,407 @@
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <Arduino_GFX_Library.h>
#include "pin_config.h"
#include "TouchDrvCSTXXX.hpp"
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
// ESP32-S3 native USB data pins.
static constexpr int kUsbDmPin = 19; // USB D-
static constexpr int kUsbDpPin = 20; // USB D+
static constexpr int kUserButtonPin = 0; // BOOT/user button, active LOW.
static constexpr uint32_t kDebounceMs = 20;
static constexpr uint32_t kEnterDelayMs = 250;
static constexpr uint32_t kButtonDebounceMs = 35;
static lv_disp_draw_buf_t gDrawBuf;
static lv_color_t *gBuf1 = nullptr;
static lv_color_t *gBuf2 = nullptr;
static lv_obj_t *gRawLabel = nullptr;
static lv_obj_t *gEventLabel = nullptr;
static lv_obj_t *gModeLabel = nullptr;
static lv_obj_t *gStateLabel = nullptr;
static lv_obj_t *gActionButton = nullptr;
static lv_obj_t *gActionButtonLabel = nullptr;
static lv_obj_t *gMarker = nullptr;
static TouchDrvCST92xx gTouch;
static int16_t gTouchX[5];
static int16_t gTouchY[5];
static volatile bool gTouchPressed = false;
static volatile int16_t gLastTouchX = -1;
static volatile int16_t gLastTouchY = -1;
static volatile uint32_t gRawTouchCount = 0;
static uint32_t gButtonPressCount = 0;
static uint32_t gButtonClickCount = 0;
static bool gLastButtonRawPressed = false;
static bool gButtonStablePressed = false;
static uint32_t gLastButtonRawChangeMs = 0;
static bool gSensorMode = false;
static bool gEnterPending = false;
static uint32_t gEnterAtMs = 0;
static bool gStableClosed = false;
static bool gLastRawClosed = false;
static uint32_t gLastRawChangeMs = 0;
static uint32_t gLastUiRefreshMs = 0;
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
uint32_t w = area->x2 - area->x1 + 1;
uint32_t h = area->y2 - area->y1 + 1;
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void lvglTick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
LV_UNUSED(indevDrv);
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
if (touched > 0) {
gTouchPressed = true;
gLastTouchX = gTouchX[0];
gLastTouchY = gTouchY[0];
gRawTouchCount++;
data->state = LV_INDEV_STATE_PR;
data->point.x = gTouchX[0];
data->point.y = gTouchY[0];
return;
}
gTouchPressed = false;
data->state = LV_INDEV_STATE_REL;
}
static void setStateUi(bool closed) {
if (closed) {
lv_label_set_text(gStateLabel, "CLOSED");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x6DFF8B), 0);
} else {
lv_label_set_text(gStateLabel, "OPEN");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0xFFD166), 0);
}
}
static void updateTouchDiagnostics() {
uint32_t now = millis();
if (now - gLastUiRefreshMs < 40) return;
gLastUiRefreshMs = now;
String raw = gTouchPressed ? "RAW: PRESSED" : "RAW: released";
raw += "\nx=" + String((int)gLastTouchX) + " y=" + String((int)gLastTouchY);
raw += " n=" + String((uint32_t)gRawTouchCount);
lv_label_set_text(gRawLabel, raw.c_str());
if (gTouchPressed && gLastTouchX >= 0 && gLastTouchY >= 0) {
int markerX = gLastTouchX - 8;
int markerY = gLastTouchY - 8;
if (markerX < 0) markerX = 0;
if (markerY < 0) markerY = 0;
lv_obj_set_pos(gMarker, markerX, markerY);
lv_obj_clear_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
}
}
static void actuallyEnterSensorMode() {
gEnterPending = false;
// Make the visual transition happen BEFORE the USB data pins are repurposed.
lv_label_set_text(gModeLabel, "SENSOR MODE");
lv_label_set_text(gActionButtonLabel, "Restart to USB");
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0xA33D4F), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0xC94F65), LV_PART_MAIN | LV_STATE_PRESSED);
lv_label_set_text(gEventLabel, "GPIO19=D- LOW GPIO20=D+ INPUT_PULLUP");
lv_label_set_text(gStateLabel, "SWITCHING...");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x72D6FF), 0);
lv_refr_now(nullptr);
delay(60);
// Intentionally repurpose only USB D-/D+. These are not LCD or touch pins.
// Do NOT call Serial.flush() here: USB CDC may already be disappearing.
pinMode(kUsbDmPin, OUTPUT);
digitalWrite(kUsbDmPin, LOW);
pinMode(kUsbDpPin, INPUT_PULLUP);
gSensorMode = true;
gLastRawClosed = (digitalRead(kUsbDpPin) == LOW);
gStableClosed = gLastRawClosed;
gLastRawChangeMs = millis();
setStateUi(gStableClosed);
}
static void actionButtonEventCb(lv_event_t *event) {
lv_event_code_t code = lv_event_get_code(event);
if (code == LV_EVENT_PRESSED) {
gButtonPressCount++;
String text = "BUTTON PRESSED #" + String(gButtonPressCount);
lv_label_set_text(gEventLabel, text.c_str());
if (!gSensorMode && !gEnterPending) {
lv_label_set_text(gActionButtonLabel, "Starting...");
lv_label_set_text(gStateLabel, "TOUCH OK");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x7CFF8A), 0);
}
return;
}
if (code != LV_EVENT_CLICKED) return;
gButtonClickCount++;
if (!gSensorMode) {
lv_label_set_text(gModeLabel, "USB MODE");
lv_label_set_text(gActionButtonLabel, "TOUCH OK");
lv_label_set_text(gStateLabel, "PRESS BOOT");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0xFFD166), 0);
String text = "TOUCH CLICK OK #" + String(gButtonClickCount) + " use BOOT button";
lv_label_set_text(gEventLabel, text.c_str());
lv_refr_now(nullptr);
return;
}
lv_label_set_text(gModeLabel, "RESTARTING TO USB...");
lv_label_set_text(gActionButtonLabel, "Restarting...");
lv_label_set_text(gStateLabel, "USB NEXT BOOT");
lv_refr_now(nullptr);
delay(180);
ESP.restart();
}
static void requestEnterSensorMode(const char *source) {
if (gSensorMode || gEnterPending) {
return;
}
gEnterPending = true;
gEnterAtMs = millis() + kEnterDelayMs;
lv_label_set_text(gModeLabel, "START RECEIVED");
lv_label_set_text(gActionButtonLabel, "Starting sensor...");
String text = String(source) + " OK waiting 250 ms";
lv_label_set_text(gEventLabel, text.c_str());
lv_label_set_text(gStateLabel, "STARTING...");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x7CFF8A), 0);
lv_refr_now(nullptr);
}
static void pollUserButton() {
bool rawPressed = (digitalRead(kUserButtonPin) == LOW);
uint32_t now = millis();
if (rawPressed != gLastButtonRawPressed) {
gLastButtonRawPressed = rawPressed;
gLastButtonRawChangeMs = now;
}
if ((now - gLastButtonRawChangeMs) < kButtonDebounceMs || rawPressed == gButtonStablePressed) {
return;
}
gButtonStablePressed = rawPressed;
if (!gButtonStablePressed) {
return;
}
gButtonClickCount++;
if (!gSensorMode) {
requestEnterSensorMode("BOOT BUTTON");
return;
}
lv_label_set_text(gModeLabel, "RESTARTING TO USB...");
lv_label_set_text(gActionButtonLabel, "Restarting...");
lv_label_set_text(gStateLabel, "USB NEXT BOOT");
lv_refr_now(nullptr);
delay(180);
ESP.restart();
}
static void createUi() {
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x000000), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_t *title = lv_label_create(screen);
lv_label_set_text(title, "USB CONTACT DEBUG");
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);
gRawLabel = lv_label_create(screen);
lv_label_set_text(gRawLabel, "RAW: released\nx=-1 y=-1 n=0");
lv_obj_set_style_text_font(gRawLabel, &lv_font_montserrat_16, 0);
lv_obj_set_style_text_color(gRawLabel, lv_color_hex(0x9EE493), 0);
lv_obj_align(gRawLabel, LV_ALIGN_TOP_LEFT, 14, 48);
gModeLabel = lv_label_create(screen);
lv_label_set_text(gModeLabel, "USB MODE");
lv_obj_set_style_text_font(gModeLabel, &lv_font_montserrat_18, 0);
lv_obj_set_style_text_color(gModeLabel, lv_color_hex(0x72D6FF), 0);
lv_obj_align(gModeLabel, LV_ALIGN_TOP_RIGHT, -14, 50);
gEventLabel = lv_label_create(screen);
lv_label_set_text(gEventLabel, "Button events: waiting");
lv_obj_set_width(gEventLabel, 440);
lv_label_set_long_mode(gEventLabel, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_align(gEventLabel, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_style_text_font(gEventLabel, &lv_font_montserrat_16, 0);
lv_obj_set_style_text_color(gEventLabel, lv_color_hex(0xFFD166), 0);
lv_obj_align(gEventLabel, LV_ALIGN_TOP_MID, 0, 102);
gStateLabel = lv_label_create(screen);
lv_label_set_text(gStateLabel, "READY");
lv_obj_set_style_text_font(gStateLabel, &lv_font_montserrat_40, 0);
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x72D6FF), 0);
lv_obj_align(gStateLabel, LV_ALIGN_CENTER, 0, -52);
// Put the button in almost the same position/size as the known-working touch debug sketch.
gActionButton = lv_btn_create(screen);
lv_obj_set_size(gActionButton, 360, 110);
lv_obj_align(gActionButton, LV_ALIGN_CENTER, 0, 52);
lv_obj_set_style_radius(gActionButton, 18, 0);
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0x2A9D8F), LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0x47C7B6), LV_PART_MAIN | LV_STATE_PRESSED);
lv_obj_set_style_border_width(gActionButton, 3, 0);
lv_obj_set_style_border_color(gActionButton, lv_color_hex(0xBDE0FE), 0);
lv_obj_add_event_cb(gActionButton, actionButtonEventCb, LV_EVENT_ALL, nullptr);
gActionButtonLabel = lv_label_create(gActionButton);
lv_label_set_text(gActionButtonLabel, "START");
lv_obj_set_style_text_font(gActionButtonLabel, &lv_font_montserrat_28, 0);
lv_obj_set_style_text_color(gActionButtonLabel, lv_color_hex(0xFFFFFF), 0);
lv_obj_center(gActionButtonLabel);
lv_obj_t *hint = lv_label_create(screen);
lv_label_set_text(hint, "Press BOOT button to START. Touch button only checks touch UI.");
lv_obj_set_width(hint, 440);
lv_label_set_long_mode(hint, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_align(hint, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_style_text_font(hint, &lv_font_montserrat_14, 0);
lv_obj_set_style_text_color(hint, lv_color_hex(0xD9D9D9), 0);
lv_obj_align(hint, LV_ALIGN_BOTTOM_MID, 0, -18);
gMarker = lv_obj_create(screen);
lv_obj_set_size(gMarker, 16, 16);
lv_obj_set_style_radius(gMarker, LV_RADIUS_CIRCLE, 0);
lv_obj_set_style_bg_color(gMarker, lv_color_hex(0xFF4D6D), 0);
lv_obj_set_style_border_width(gMarker, 2, 0);
lv_obj_set_style_border_color(gMarker, lv_color_hex(0xFFFFFF), 0);
lv_obj_add_flag(gMarker, LV_OBJ_FLAG_HIDDEN);
}
static void pollContact() {
if (!gSensorMode) return;
bool rawClosed = (digitalRead(kUsbDpPin) == LOW);
uint32_t now = millis();
if (rawClosed != gLastRawClosed) {
gLastRawClosed = rawClosed;
gLastRawChangeMs = now;
}
if ((now - gLastRawChangeMs) >= kDebounceMs && gStableClosed != gLastRawClosed) {
gStableClosed = gLastRawClosed;
setStateUi(gStableClosed);
}
}
void setup() {
Serial.begin(115200);
pinMode(kUserButtonPin, INPUT_PULLUP);
Wire.begin(IIC_SDA, IIC_SCL);
pinMode(TP_RST, OUTPUT);
pinMode(TP_INT, INPUT);
digitalWrite(TP_RST, LOW);
delay(30);
digitalWrite(TP_RST, HIGH);
delay(50);
delay(1000);
gTouch.setPins(TP_RST, TP_INT);
bool touchOk = gTouch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
if (!touchOk) {
Serial.println("Touch init failed");
} else {
Serial.print("Touch model: ");
Serial.println(gTouch.getModelName());
gTouch.sleep();
gTouch.reset();
gTouch.setMaxCoordinates(480, 480);
gTouch.setSwapXY(true);
gTouch.setMirrorXY(true, false);
}
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->fillScreen(0x0000);
lv_init();
uint32_t screenWidth = gfx->width();
uint32_t screenHeight = gfx->height();
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = screenWidth;
dispDrv.ver_res = screenHeight;
dispDrv.flush_cb = lvglFlushCb;
dispDrv.draw_buf = &gDrawBuf;
lv_disp_drv_register(&dispDrv);
static lv_indev_drv_t indevDrv;
lv_indev_drv_init(&indevDrv);
indevDrv.type = LV_INDEV_TYPE_POINTER;
indevDrv.read_cb = lvglTouchRead;
lv_indev_drv_register(&indevDrv);
const esp_timer_create_args_t lvglTickTimerArgs = {
.callback = &lvglTick,
.name = "lvgl_tick"};
esp_timer_handle_t lvglTickTimer = nullptr;
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
createUi();
Serial.println("USB contact sensor DEBUG ready. USB pins remain untouched until START click.");
}
void loop() {
updateTouchDiagnostics();
pollUserButton();
pollContact();
if (gEnterPending && (int32_t)(millis() - gEnterAtMs) >= 0) {
actuallyEnterSensorMode();
}
lv_timer_handler();
delay(5);
}
@@ -0,0 +1,250 @@
#include <Arduino.h>
#include <Wire.h>
#include <lvgl.h>
#include <Arduino_GFX_Library.h>
#include "pin_config.h"
#include "TouchDrvCSTXXX.hpp"
#define EXAMPLE_LVGL_TICK_PERIOD_MS 2
// ESP32-S3 native USB pins.
static constexpr int kUsbDmPin = 19; // USB D-
static constexpr int kUsbDpPin = 20; // USB D+
static constexpr uint32_t kDebounceMs = 20;
static lv_disp_draw_buf_t gDrawBuf;
static lv_color_t *gBuf1 = nullptr;
static lv_color_t *gBuf2 = nullptr;
static lv_obj_t *gModeLabel = nullptr;
static lv_obj_t *gStateLabel = nullptr;
static lv_obj_t *gHintLabel = nullptr;
static lv_obj_t *gActionButton = nullptr;
static lv_obj_t *gActionButtonLabel = nullptr;
static TouchDrvCST92xx gTouch;
static int16_t gTouchX[5];
static int16_t gTouchY[5];
static bool gSensorMode = false;
static bool gStableClosed = false;
static bool gLastRawClosed = false;
static uint32_t gLastRawChangeMs = 0;
Arduino_DataBus *gBus = new Arduino_ESP32QSPI(
LCD_CS, LCD_SCLK, LCD_SDIO0, LCD_SDIO1, LCD_SDIO2, LCD_SDIO3);
Arduino_CO5300 *gfx = new Arduino_CO5300(
gBus, LCD_RESET, 0, LCD_WIDTH, LCD_HEIGHT, 0, 0, 0, 0);
static void lvglFlushCb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *colorP) {
uint32_t w = area->x2 - area->x1 + 1;
uint32_t h = area->y2 - area->y1 + 1;
#if (LV_COLOR_16_SWAP != 0)
gfx->draw16bitBeRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#else
gfx->draw16bitRGBBitmap(area->x1, area->y1, (uint16_t *)&colorP->full, w, h);
#endif
lv_disp_flush_ready(disp);
}
static void lvglTick(void *arg) {
LV_UNUSED(arg);
lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
}
static void lvglTouchRead(lv_indev_drv_t *indevDrv, lv_indev_data_t *data) {
LV_UNUSED(indevDrv);
uint8_t touched = gTouch.getPoint(gTouchX, gTouchY, gTouch.getSupportTouchPoint());
if (touched > 0) {
data->state = LV_INDEV_STATE_PR;
data->point.x = gTouchX[0];
data->point.y = gTouchY[0];
return;
}
data->state = LV_INDEV_STATE_REL;
}
static void setStateUi(bool closed) {
if (closed) {
lv_label_set_text(gStateLabel, "CLOSED");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x7CFF8A), 0);
} else {
lv_label_set_text(gStateLabel, "OPEN");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0xFFCC66), 0);
}
}
static void enterSensorMode() {
// From this moment the native USB data pair is no longer USB.
// D- is held LOW, D+ is read with the ESP32-S3 internal pull-up.
// A passive switch between D- and D+ therefore reads LOW when closed.
Serial.flush();
delay(30);
pinMode(kUsbDmPin, OUTPUT);
digitalWrite(kUsbDmPin, LOW);
pinMode(kUsbDpPin, INPUT_PULLUP);
gSensorMode = true;
gLastRawClosed = (digitalRead(kUsbDpPin) == LOW);
gStableClosed = gLastRawClosed;
gLastRawChangeMs = millis();
lv_label_set_text(gModeLabel, "Sensor mode");
lv_label_set_text(gHintLabel, "USB D- / D+ are now the contact input.\nDo not connect this port to a computer.");
lv_label_set_text(gActionButtonLabel, "Restart to USB");
setStateUi(gStableClosed);
}
static void actionButtonEventCb(lv_event_t *event) {
if (lv_event_get_code(event) != LV_EVENT_CLICKED) {
return;
}
if (!gSensorMode) {
enterSensorMode();
return;
}
lv_label_set_text(gModeLabel, "Restarting...");
lv_label_set_text(gHintLabel, "USB mode will return after reboot.");
lv_timer_handler();
delay(150);
ESP.restart();
}
static void createUi() {
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x0B1320), 0);
lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0);
lv_obj_t *title = lv_label_create(screen);
lv_label_set_text(title, "USB CONTACT TEST");
lv_obj_set_style_text_color(title, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(title, &lv_font_montserrat_22, 0);
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 26);
gModeLabel = lv_label_create(screen);
lv_label_set_text(gModeLabel, "USB mode");
lv_obj_set_style_text_color(gModeLabel, lv_color_hex(0xB8C8DC), 0);
lv_obj_set_style_text_font(gModeLabel, &lv_font_montserrat_18, 0);
lv_obj_align(gModeLabel, LV_ALIGN_TOP_MID, 0, 72);
gStateLabel = lv_label_create(screen);
lv_label_set_text(gStateLabel, "READY");
lv_obj_set_style_text_color(gStateLabel, lv_color_hex(0x72D6FF), 0);
lv_obj_set_style_text_font(gStateLabel, &lv_font_montserrat_40, 0);
lv_obj_align(gStateLabel, LV_ALIGN_CENTER, 0, -56);
gHintLabel = lv_label_create(screen);
lv_label_set_text(gHintLabel, "USB is normal now.\nTap Start to use D- / D+ as a contact sensor.");
lv_obj_set_width(gHintLabel, 390);
lv_label_set_long_mode(gHintLabel, LV_LABEL_LONG_WRAP);
lv_obj_set_style_text_align(gHintLabel, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_set_style_text_color(gHintLabel, lv_color_hex(0xD5DCE5), 0);
lv_obj_set_style_text_font(gHintLabel, &lv_font_montserrat_16, 0);
lv_obj_align(gHintLabel, LV_ALIGN_CENTER, 0, 18);
gActionButton = lv_btn_create(screen);
lv_obj_set_size(gActionButton, 260, 72);
lv_obj_align(gActionButton, LV_ALIGN_BOTTOM_MID, 0, -48);
lv_obj_set_style_radius(gActionButton, 18, 0);
lv_obj_set_style_bg_color(gActionButton, lv_color_hex(0x2A9D8F), 0);
lv_obj_set_style_border_width(gActionButton, 0, 0);
lv_obj_add_event_cb(gActionButton, actionButtonEventCb, LV_EVENT_CLICKED, nullptr);
gActionButtonLabel = lv_label_create(gActionButton);
lv_label_set_text(gActionButtonLabel, "Start");
lv_obj_set_style_text_color(gActionButtonLabel, lv_color_hex(0xFFFFFF), 0);
lv_obj_set_style_text_font(gActionButtonLabel, &lv_font_montserrat_22, 0);
lv_obj_center(gActionButtonLabel);
}
static void pollContact() {
if (!gSensorMode) {
return;
}
bool rawClosed = (digitalRead(kUsbDpPin) == LOW);
uint32_t now = millis();
if (rawClosed != gLastRawClosed) {
gLastRawClosed = rawClosed;
gLastRawChangeMs = now;
}
if ((now - gLastRawChangeMs) >= kDebounceMs && gStableClosed != gLastRawClosed) {
gStableClosed = gLastRawClosed;
setStateUi(gStableClosed);
}
}
void setup() {
// With the project's FQBN this is the normal ESP32-S3 native USB serial mode.
// No sensor mode is persisted: every reset starts here with normal USB again.
Serial.begin(115200);
Wire.begin(IIC_SDA, IIC_SCL);
pinMode(TP_RST, OUTPUT);
pinMode(TP_INT, INPUT);
digitalWrite(TP_RST, LOW);
delay(30);
digitalWrite(TP_RST, HIGH);
delay(50);
delay(1000);
gTouch.setPins(TP_RST, TP_INT);
bool touchOk = gTouch.begin(Wire, 0x5A, IIC_SDA, IIC_SCL);
if (!touchOk) {
Serial.println("Touch init failed");
} else {
gTouch.sleep();
gTouch.reset();
gTouch.setMaxCoordinates(480, 480);
gTouch.setSwapXY(true);
gTouch.setMirrorXY(true, false);
}
gfx->begin();
gBus->writeC8D8(0x36, 0xA0);
gfx->setBrightness(220);
gfx->fillScreen(0x0000);
lv_init();
uint32_t screenWidth = gfx->width();
uint32_t screenHeight = gfx->height();
gBuf1 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
gBuf2 = (lv_color_t *)heap_caps_malloc(screenWidth * screenHeight / 4 * sizeof(lv_color_t), MALLOC_CAP_DMA);
lv_disp_draw_buf_init(&gDrawBuf, gBuf1, gBuf2, screenWidth * screenHeight / 4);
static lv_disp_drv_t dispDrv;
lv_disp_drv_init(&dispDrv);
dispDrv.hor_res = screenWidth;
dispDrv.ver_res = screenHeight;
dispDrv.flush_cb = lvglFlushCb;
dispDrv.draw_buf = &gDrawBuf;
lv_disp_drv_register(&dispDrv);
static lv_indev_drv_t indevDrv;
lv_indev_drv_init(&indevDrv);
indevDrv.type = LV_INDEV_TYPE_POINTER;
indevDrv.read_cb = lvglTouchRead;
lv_indev_drv_register(&indevDrv);
const esp_timer_create_args_t lvglTickTimerArgs = {
.callback = &lvglTick,
.name = "lvgl_tick"};
esp_timer_handle_t lvglTickTimer = nullptr;
esp_timer_create(&lvglTickTimerArgs, &lvglTickTimer);
esp_timer_start_periodic(lvglTickTimer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000);
createUi();
Serial.println("USB contact sensor test ready; native USB remains active until Start is tapped.");
}
void loop() {
pollContact();
lv_timer_handler();
delay(5);
}