Добавить ESP32-тест сопротивления USB-контактов

This commit is contained in:
AidarKC
2026-10-10 13:20:57 +03:00
parent 30541ae4af
commit e14e3dfa21
5 changed files with 328 additions and 2 deletions
@@ -49,11 +49,12 @@ case "${MODE}" in
lvgl-russian-font-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/lvgl_russian_font_test" ;;
usb-contact-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/usb_contact_sensor_test" ;;
usb-contact-debug-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/usb_contact_sensor_debug_test" ;;
usb-contact-resistance-test) SKETCH_DIR="${ROOT_DIR}/test_sketches/usb_contact_resistance_test" ;;
usb-contact-screen-start-stub) SKETCH_DIR="${ROOT_DIR}/test_sketches/usb_contact_screen_start_stub" ;;
lvgl-nav-minimal-test) SKETCH_DIR="${ROOT_DIR}/shine_homeserver_main" ;;
*)
echo "Unknown mode: ${MODE}" >&2
echo "Use one of: hello, widgets, audio, simple, argon2, homeserver-ui, shine-homeserver-main, shine-homeserver-ui-main, legacy-homeserver-ui, text-test, gfx-text-test, gfx-layout-test, lvgl-basic-test, lvgl-interaction-test, lvgl-touch-debug-test, lvgl-official-based-test, lvgl-subserver-touch-test, lvgl-russian-font-test, usb-contact-test, usb-contact-debug-test, usb-contact-screen-start-stub" >&2
echo "Use one of: hello, widgets, audio, simple, argon2, homeserver-ui, shine-homeserver-main, shine-homeserver-ui-main, legacy-homeserver-ui, text-test, gfx-text-test, gfx-layout-test, lvgl-basic-test, lvgl-interaction-test, lvgl-touch-debug-test, lvgl-official-based-test, lvgl-subserver-touch-test, lvgl-russian-font-test, usb-contact-test, usb-contact-debug-test, usb-contact-resistance-test, usb-contact-screen-start-stub" >&2
exit 2
;;
esac
@@ -17,6 +17,7 @@
- `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/` и теперь является основным
## Запуск
@@ -33,3 +34,4 @@
- `./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,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);
}
@@ -4,6 +4,7 @@ Diagnostic sketches:
- `main-device/test_sketches/usb_contact_sensor_test/usb_contact_sensor_test.ino`
- `main-device/test_sketches/usb_contact_sensor_debug_test/usb_contact_sensor_debug_test.ino`
- `main-device/test_sketches/usb_contact_resistance_test/usb_contact_resistance_test.ino`
- `main-device/test_sketches/usb_contact_screen_start_stub/usb_contact_screen_start_stub.ino`
## Purpose
@@ -51,6 +52,33 @@ Sensor mode starts from the physical BOOT/user button on GPIO0, active LOW. The
While in sensor mode, pressing the BOOT/user button again restarts the device and returns normal USB mode on the next boot.
## Resistance version
The `usb_contact_resistance_test` sketch starts in normal USB mode and does not reconfigure GPIO19/20 until `Start` is tapped.
Initial screen:
- title: `USB RESISTANCE TEST`
- mode: `USB mode`
- state: `READY`
- action button: `Start`
After `Start`:
- GPIO19 / USB `D-` is configured as `OUTPUT HIGH`
- GPIO20 / USB `D+` is configured as ADC input
- the measured resistance is connected between `D-` and `D+`
- a known external 10 kΩ pulldown resistor must be connected from `D+` to `GND` for meaningful ohm calculation
- UI shows calculated resistance, raw ADC value, and measured `D+` millivolts
- very low ADC voltage is shown as `OPEN`
- near-source voltage is shown as `SHORT / LOW`
The calculation assumes a divider where the unknown resistance is the high-side resistor from 3.3 V to ADC and the known 10 kΩ resistor is the low-side resistor from ADC to GND:
`Rx = 10000 * (3300 / Vadc - 1)`
This test is intended to check whether the USB contact pair can be reused as an ADC-measured resistance input, for example before wiring a 10 kΩ potentiometer. Native USB data is unavailable while resistance mode owns GPIO19/20. Pressing `Restart to USB` calls `ESP.restart()`; after reboot normal USB mode is restored.
## GFX breath monitor version
The `usb_contact_screen_start_stub` sketch is a breath monitor firmware based on the production `Arduino_GFX` display path instead of LVGL.