From b1c42efa82d7911711469fa2fc6019fb0bf0eacb693ecd7a9dc21d78464c8949 Mon Sep 17 00:00:00 2001 From: AidarKC Date: Wed, 7 Oct 2026 08:47:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B9=20=D1=82=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D0=B9=20ESP32-=D1=81=D0=BA=D0=B5?= =?UTF-8?q?=D1=82=D1=87=20=D0=B2=D0=BD=D0=B5=20=D0=BF=D1=80=D0=BE=D0=B5?= =?UTF-8?q?=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usb_contact_screen_start_stub.ino | 146 ++++++++++++++---- .../reference/usb_contact_sensor_test_spec.md | 5 +- VERSION.properties | 2 +- 3 files changed, 120 insertions(+), 33 deletions(-) diff --git a/ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/main-device/test_sketches/usb_contact_screen_start_stub/usb_contact_screen_start_stub.ino b/ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/main-device/test_sketches/usb_contact_screen_start_stub/usb_contact_screen_start_stub.ino index 440347d3..07278d6f 100644 --- a/ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/main-device/test_sketches/usb_contact_screen_start_stub/usb_contact_screen_start_stub.ino +++ b/ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/main-device/test_sketches/usb_contact_screen_start_stub/usb_contact_screen_start_stub.ino @@ -33,6 +33,8 @@ 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 @@ -61,9 +63,13 @@ struct Rect { }; 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 bool gStarted = false; +static bool gSettingsOpen = false; static bool gAudioReady = false; static bool gStableClosed = false; static bool gLastRawClosed = false; @@ -71,6 +77,7 @@ 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; @@ -86,6 +93,8 @@ 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 void drawPanel(int x, int y, int w, int h, uint16_t fill, uint16_t border, int radius = 10) { @@ -119,6 +128,10 @@ 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)); } @@ -139,6 +152,10 @@ static uint32_t averageCycleMs() { return sum / gCycleCount; } +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) { @@ -172,7 +189,7 @@ static void initAudio() { } static void playBeep(uint16_t freq, uint16_t samples, int16_t amp) { - if (!gAudioReady) { + if (!gAudioReady || amp <= 0) { return; } static int16_t toneBuf[1600 * 2]; @@ -189,11 +206,11 @@ static void playBeep(uint16_t freq, uint16_t samples, int16_t amp) { } static void playCycleTick() { - playBeep(880, 480, 2500); + playBeep(880, 480, (int16_t)(gTickVolume * 800)); } static void playAlertBeep() { - playBeep(1200, 1600, 12000); + playBeep(1200, 1600, (int16_t)(gAlertVolume * 1200)); } static void alertIfNeeded() { @@ -229,17 +246,41 @@ static void drawReadyScreen() { 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 clearArea(int x, int y, int w, int h, uint16_t fill) { - gfx->fillRect(x, y, w, h, fill); +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(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() { @@ -247,21 +288,17 @@ static void drawMonitorStatic() { 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(); } static void updateMonitorValues(bool force) { @@ -270,18 +307,28 @@ static void updateMonitorValues(bool force) { 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) { + if (force || gLastDrawnClosed != gStableClosed || gLastDrawnPaused != paused) { clearArea(42, 118, 396, 68, C_CARD); - 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); + 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; } clearArea(28, 58, 424, 28, C_PANEL); - snprintf(line, sizeof(line), "Limit: %us alert by average", kLimitsSec[gLimitIndex]); + snprintf(line, sizeof(line), "Limit: %us top button: settings", kLimitsSec[gLimitIndex]); drawText(28, 60, line, C_MUTE, 2); clearArea(42, 254, 160, 28, C_CARD); @@ -304,13 +351,15 @@ static void updateMonitorValues(bool force) { snprintf(value, sizeof(value), "%us", kLimitsSec[gLimitIndex]); drawText(266, 342, value, C_TEXT, 2); - clearArea(28, 398, 250, 28, C_PANEL); + clearArea(28, 398, 280, 28, C_PANEL); snprintf(line, sizeof(line), "Short avg streak: %u", gShortStreak); drawText(32, 402, line, gShortStreak > 0 ? C_BAD : C_MUTE, 2); } static void redrawScreen() { - if (gStarted) { + if (gSettingsOpen) { + drawSettingsScreen(); + } else if (gStarted) { if (gNeedStaticRedraw || !gStaticDrawn) { drawMonitorStatic(); gNeedStaticRedraw = false; @@ -326,7 +375,6 @@ static void redrawScreen() { static void enterSensorMode() { Serial.flush(); delay(30); - pinMode(kUsbDmPin, OUTPUT); digitalWrite(kUsbDmPin, LOW); pinMode(kUsbDpPin, INPUT_PULLUP); @@ -346,8 +394,9 @@ static void enterSensorMode() { gNeedRedraw = true; } -static void nextLimit() { - gLimitIndex = (gLimitIndex + 1) % (sizeof(kLimitsSec) / sizeof(kLimitsSec[0])); +static void toggleSettings() { + gSettingsOpen = !gSettingsOpen; + gNeedStaticRedraw = true; gNeedRedraw = true; } @@ -356,22 +405,29 @@ static void handleStableContact(bool closed) { gStableClosed = closed; if (closed) { if (gLastCycleStartMs != 0) { - addCycle(now - gLastCycleStartMs); - playCycleTick(); - alertIfNeeded(); + 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(); } - gLastCycleStartMs = now; } - gNeedRedraw = true; + gNeedRedraw = !gSettingsOpen; } static void pollContact() { if (!gStarted) { return; } - bool rawClosed = (digitalRead(kUsbDpPin) == LOW); uint32_t now = millis(); if (rawClosed != gLastRawClosed) { @@ -390,11 +446,34 @@ static void pollUserButton() { gUserButtonDown = down; gUserButtonChangeMs = now; if (down) { - nextLimit(); + 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)) { + gLimitIndex = sliderValueFromX(kLimitSlider, x, 4); + } else if (contains(kTickSlider, x, y)) { + gTickVolume = sliderValueFromX(kTickSlider, x, 10); + playCycleTick(); + } else if (contains(kAlertSlider, x, y)) { + gAlertVolume = sliderValueFromX(kAlertSlider, x, 10); + playAlertBeep(); + } else { + return; + } + gNeedRedraw = true; +} + static void pollTouch() { int16_t x = 0; int16_t y = 0; @@ -416,7 +495,12 @@ static void pollTouch() { gTouchDown = false; int dx = abs(gTouchLastX - gTouchStartX); int dy = abs(gTouchLastY - gTouchStartY); - if (dx < 14 && dy < 14 && !gStarted && contains(kStartButton, gTouchLastX, gTouchLastY)) { + if (dx >= 14 || dy >= 14) { + return; + } + if (gSettingsOpen) { + handleSettingsTouch(gTouchLastX, gTouchLastY); + } else if (!gStarted && contains(kStartButton, gTouchLastX, gTouchLastY)) { enterSensorMode(); } } @@ -448,7 +532,7 @@ void loop() { pollUserButton(); pollTouch(); pollContact(); - if (gStarted && millis() - gLastUiMs >= kUiPeriodMs) { + if (gStarted && !gSettingsOpen && millis() - gLastUiMs >= kUiPeriodMs) { updateMonitorValues(false); gLastUiMs = millis(); } diff --git a/ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/reference/usb_contact_sensor_test_spec.md b/ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/reference/usb_contact_sensor_test_spec.md index b043a403..cdadd1f9 100644 --- a/ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/reference/usb_contact_sensor_test_spec.md +++ b/ESP32/esp32/ESP32-S3-Touch-AMOLED-2.16/reference/usb_contact_sensor_test_spec.md @@ -71,11 +71,14 @@ After tapping `START`: - one breathing cycle is measured from one stable `EXHALE` start to the next stable `EXHALE` start - contact changes use an 80 ms debounce - UI shows current cycle duration, arithmetic average duration over the last 5 completed cycles, and elapsed time from start -- the physical user/BOOT button cycles the short-cycle limit through `5`, `7`, `10`, `15`, `20` seconds +- the physical user/BOOT button opens and closes the settings screen +- settings screen has three touch sliders: limit `5/7/10/15/20` seconds, quiet cycle tick volume `0..10`, and alert volume `0..10` - the working screen uses a static frame and only updates value regions to avoid display flicker - the `START` button is a flat rounded rectangle with a border; there is no pause button in this version - every new cycle start plays one quiet confirmation beep - if the arithmetic average of the last up to 5 completed cycles is shorter than the selected limit, the device plays warning beeps: 1 beep for the first short average in a row, 2 beeps for the second, up to 5 beeps +- completed cycles shorter than 1.5 seconds are ignored as contact bounce +- if the current cycle exceeds 60 seconds, the main screen shows `PAUSE`; that long cycle is skipped and the next `EXHALE` start begins a fresh cycle from zero - reset/power cycle restores ordinary native USB boot This version is intended to monitor breathing cadence from a dry-contact USB D-/D+ sensor. diff --git a/VERSION.properties b/VERSION.properties index 97d507e9..866a3ecf 100644 --- a/VERSION.properties +++ b/VERSION.properties @@ -1,2 +1,2 @@ -client.version=1.14.11 +client.version=1.14.12 server.version=1.12.11