Новый тестовый ESP32-скетч вне проекта

This commit is contained in:
AidarKC
2026-10-07 08:47:28 +03:00
parent acd5d4406d
commit b1c42efa82
3 changed files with 120 additions and 33 deletions
@@ -33,6 +33,8 @@ static constexpr uint32_t kContactDebounceMs = 80;
static constexpr uint32_t kButtonDebounceMs = 45; static constexpr uint32_t kButtonDebounceMs = 45;
static constexpr uint32_t kUiPeriodMs = 200; static constexpr uint32_t kUiPeriodMs = 200;
static constexpr uint32_t kSampleRate = 16000; static constexpr uint32_t kSampleRate = 16000;
static constexpr uint32_t kMinCycleMs = 1500;
static constexpr uint32_t kPauseAfterMs = 60000;
#define C_BG 0x0841u #define C_BG 0x0841u
#define C_PANEL 0x1082u #define C_PANEL 0x1082u
@@ -61,9 +63,13 @@ struct Rect {
}; };
static const Rect kStartButton = {96, 348, 288, 78}; 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 uint16_t kLimitsSec[] = {5, 7, 10, 15, 20};
static bool gStarted = false; static bool gStarted = false;
static bool gSettingsOpen = false;
static bool gAudioReady = false; static bool gAudioReady = false;
static bool gStableClosed = false; static bool gStableClosed = false;
static bool gLastRawClosed = false; static bool gLastRawClosed = false;
@@ -71,6 +77,7 @@ static bool gStaticDrawn = false;
static bool gNeedRedraw = true; static bool gNeedRedraw = true;
static bool gNeedStaticRedraw = true; static bool gNeedStaticRedraw = true;
static bool gLastDrawnClosed = false; static bool gLastDrawnClosed = false;
static bool gLastDrawnPaused = false;
static bool gTouchDown = false; static bool gTouchDown = false;
static bool gUserButtonDown = false; static bool gUserButtonDown = false;
static int16_t gTouchStartX = 0; static int16_t gTouchStartX = 0;
@@ -86,6 +93,8 @@ static uint32_t gCycleMs[5] = {};
static uint8_t gCycleCount = 0; static uint8_t gCycleCount = 0;
static uint8_t gCycleNext = 0; static uint8_t gCycleNext = 0;
static uint8_t gLimitIndex = 2; static uint8_t gLimitIndex = 2;
static uint8_t gTickVolume = 3;
static uint8_t gAlertVolume = 10;
static uint8_t gShortStreak = 0; 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) { 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; 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) { 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)); 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; return sum / gCycleCount;
} }
static bool isAutoPaused() {
return gStarted && gLastCycleStartMs != 0 && (millis() - gLastCycleStartMs) > kPauseAfterMs;
}
static esp_err_t codecInit() { static esp_err_t codecInit() {
es8311_handle_t esHandle = es8311_create(0, ES8311_ADDRRES_0); es8311_handle_t esHandle = es8311_create(0, ES8311_ADDRRES_0);
if (!esHandle) { if (!esHandle) {
@@ -172,7 +189,7 @@ static void initAudio() {
} }
static void playBeep(uint16_t freq, uint16_t samples, int16_t amp) { static void playBeep(uint16_t freq, uint16_t samples, int16_t amp) {
if (!gAudioReady) { if (!gAudioReady || amp <= 0) {
return; return;
} }
static int16_t toneBuf[1600 * 2]; 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() { static void playCycleTick() {
playBeep(880, 480, 2500); playBeep(880, 480, (int16_t)(gTickVolume * 800));
} }
static void playAlertBeep() { static void playAlertBeep() {
playBeep(1200, 1600, 12000); playBeep(1200, 1600, (int16_t)(gAlertVolume * 1200));
} }
static void alertIfNeeded() { static void alertIfNeeded() {
@@ -229,17 +246,41 @@ static void drawReadyScreen() {
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16); drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
drawText(28, 42, "BREATH MONITOR", C_TEXT, 3); drawText(28, 42, "BREATH MONITOR", C_TEXT, 3);
drawText(28, 78, "USB contact sensor", C_MUTE, 2); drawText(28, 78, "USB contact sensor", C_MUTE, 2);
drawPanel(34, 132, 412, 160, C_CARD, C_BORDER, 14); drawPanel(34, 132, 412, 160, C_CARD, C_BORDER, 14);
drawCenteredText(34, 166, 412, "READY", C_ACCENT, 4); drawCenteredText(34, 166, 412, "READY", C_ACCENT, 4);
drawCenteredText(34, 222, 412, "Closed = exhale", C_TEXT, 2); drawCenteredText(34, 222, 412, "Closed = exhale", C_TEXT, 2);
drawCenteredText(34, 252, 412, "Open = inhale", C_MUTE, 2); drawCenteredText(34, 252, 412, "Open = inhale", C_MUTE, 2);
drawButton(kStartButton, "START", 3); drawButton(kStartButton, "START", 3);
} }
static void clearArea(int x, int y, int w, int h, uint16_t fill) { static void drawSlider(const Rect &rect, const char *title, const char *value, uint8_t pos, uint8_t maxPos) {
gfx->fillRect(x, y, w, h, fill); 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() { static void drawMonitorStatic() {
@@ -247,21 +288,17 @@ static void drawMonitorStatic() {
drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16); drawPanel(12, 12, 456, 456, C_PANEL, C_BORDER, 16);
drawText(28, 32, "BREATH MONITOR", C_TEXT, 2); drawText(28, 32, "BREATH MONITOR", C_TEXT, 2);
drawPanel(28, 96, 424, 104, C_CARD, C_BORDER, 14); drawPanel(28, 96, 424, 104, C_CARD, C_BORDER, 14);
drawPanel(28, 216, 200, 72, C_CARD, C_BORDER, 10); drawPanel(28, 216, 200, 72, C_CARD, C_BORDER, 10);
drawText(42, 236, "Current", C_MUTE, 1); drawText(42, 236, "Current", C_MUTE, 1);
drawPanel(252, 216, 200, 72, C_CARD, C_BORDER, 10); drawPanel(252, 216, 200, 72, C_CARD, C_BORDER, 10);
drawText(266, 236, "Average 5", C_MUTE, 1); drawText(266, 236, "Average 5", C_MUTE, 1);
drawPanel(28, 304, 200, 64, C_CARD, C_BORDER, 10); drawPanel(28, 304, 200, 64, C_CARD, C_BORDER, 10);
drawText(42, 322, "From start", C_MUTE, 1); drawText(42, 322, "From start", C_MUTE, 1);
drawPanel(252, 304, 200, 64, C_CARD, C_BORDER, 10); drawPanel(252, 304, 200, 64, C_CARD, C_BORDER, 10);
drawText(266, 322, "Limit", C_MUTE, 1); drawText(266, 322, "Limit", C_MUTE, 1);
gStaticDrawn = true; gStaticDrawn = true;
gLastDrawnClosed = !gStableClosed; gLastDrawnClosed = !gStableClosed;
gLastDrawnPaused = !isAutoPaused();
} }
static void updateMonitorValues(bool force) { static void updateMonitorValues(bool force) {
@@ -270,18 +307,28 @@ static void updateMonitorValues(bool force) {
uint32_t now = millis(); uint32_t now = millis();
uint32_t runMs = now - gStartedMs; uint32_t runMs = now - gStartedMs;
uint32_t currentMs = (gLastCycleStartMs == 0) ? 0 : now - gLastCycleStartMs; uint32_t currentMs = (gLastCycleStartMs == 0) ? 0 : now - gLastCycleStartMs;
bool paused = isAutoPaused();
if (paused) {
currentMs = now - gLastCycleStartMs - kPauseAfterMs;
}
uint32_t avgMs = averageCycleMs(); uint32_t avgMs = averageCycleMs();
if (force || gLastDrawnClosed != gStableClosed) { if (force || gLastDrawnClosed != gStableClosed || gLastDrawnPaused != paused) {
clearArea(42, 118, 396, 68, C_CARD); clearArea(42, 118, 396, 68, C_CARD);
drawCenteredText(28, 122, 424, gStableClosed ? "EXHALE" : "INHALE", if (paused) {
gStableClosed ? C_OK : C_ACCENT, 4); drawCenteredText(28, 122, 424, "PAUSE", C_BAD, 4);
drawCenteredText(28, 172, 424, gStableClosed ? "contact closed" : "contact open", C_MUTE, 2); 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; gLastDrawnClosed = gStableClosed;
gLastDrawnPaused = paused;
} }
clearArea(28, 58, 424, 28, C_PANEL); 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); drawText(28, 60, line, C_MUTE, 2);
clearArea(42, 254, 160, 28, C_CARD); clearArea(42, 254, 160, 28, C_CARD);
@@ -304,13 +351,15 @@ static void updateMonitorValues(bool force) {
snprintf(value, sizeof(value), "%us", kLimitsSec[gLimitIndex]); snprintf(value, sizeof(value), "%us", kLimitsSec[gLimitIndex]);
drawText(266, 342, value, C_TEXT, 2); 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); snprintf(line, sizeof(line), "Short avg streak: %u", gShortStreak);
drawText(32, 402, line, gShortStreak > 0 ? C_BAD : C_MUTE, 2); drawText(32, 402, line, gShortStreak > 0 ? C_BAD : C_MUTE, 2);
} }
static void redrawScreen() { static void redrawScreen() {
if (gStarted) { if (gSettingsOpen) {
drawSettingsScreen();
} else if (gStarted) {
if (gNeedStaticRedraw || !gStaticDrawn) { if (gNeedStaticRedraw || !gStaticDrawn) {
drawMonitorStatic(); drawMonitorStatic();
gNeedStaticRedraw = false; gNeedStaticRedraw = false;
@@ -326,7 +375,6 @@ static void redrawScreen() {
static void enterSensorMode() { static void enterSensorMode() {
Serial.flush(); Serial.flush();
delay(30); delay(30);
pinMode(kUsbDmPin, OUTPUT); pinMode(kUsbDmPin, OUTPUT);
digitalWrite(kUsbDmPin, LOW); digitalWrite(kUsbDmPin, LOW);
pinMode(kUsbDpPin, INPUT_PULLUP); pinMode(kUsbDpPin, INPUT_PULLUP);
@@ -346,8 +394,9 @@ static void enterSensorMode() {
gNeedRedraw = true; gNeedRedraw = true;
} }
static void nextLimit() { static void toggleSettings() {
gLimitIndex = (gLimitIndex + 1) % (sizeof(kLimitsSec) / sizeof(kLimitsSec[0])); gSettingsOpen = !gSettingsOpen;
gNeedStaticRedraw = true;
gNeedRedraw = true; gNeedRedraw = true;
} }
@@ -356,22 +405,29 @@ static void handleStableContact(bool closed) {
gStableClosed = closed; gStableClosed = closed;
if (closed) { if (closed) {
if (gLastCycleStartMs != 0) { if (gLastCycleStartMs != 0) {
addCycle(now - gLastCycleStartMs); uint32_t cycleMs = now - gLastCycleStartMs;
playCycleTick(); if (cycleMs >= kPauseAfterMs) {
alertIfNeeded(); gShortStreak = 0;
gLastCycleStartMs = now;
playCycleTick();
} else if (cycleMs >= kMinCycleMs) {
addCycle(cycleMs);
gLastCycleStartMs = now;
playCycleTick();
alertIfNeeded();
}
} else { } else {
gLastCycleStartMs = now;
playCycleTick(); playCycleTick();
} }
gLastCycleStartMs = now;
} }
gNeedRedraw = true; gNeedRedraw = !gSettingsOpen;
} }
static void pollContact() { static void pollContact() {
if (!gStarted) { if (!gStarted) {
return; return;
} }
bool rawClosed = (digitalRead(kUsbDpPin) == LOW); bool rawClosed = (digitalRead(kUsbDpPin) == LOW);
uint32_t now = millis(); uint32_t now = millis();
if (rawClosed != gLastRawClosed) { if (rawClosed != gLastRawClosed) {
@@ -390,11 +446,34 @@ static void pollUserButton() {
gUserButtonDown = down; gUserButtonDown = down;
gUserButtonChangeMs = now; gUserButtonChangeMs = now;
if (down) { 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() { static void pollTouch() {
int16_t x = 0; int16_t x = 0;
int16_t y = 0; int16_t y = 0;
@@ -416,7 +495,12 @@ static void pollTouch() {
gTouchDown = false; gTouchDown = false;
int dx = abs(gTouchLastX - gTouchStartX); int dx = abs(gTouchLastX - gTouchStartX);
int dy = abs(gTouchLastY - gTouchStartY); 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(); enterSensorMode();
} }
} }
@@ -448,7 +532,7 @@ void loop() {
pollUserButton(); pollUserButton();
pollTouch(); pollTouch();
pollContact(); pollContact();
if (gStarted && millis() - gLastUiMs >= kUiPeriodMs) { if (gStarted && !gSettingsOpen && millis() - gLastUiMs >= kUiPeriodMs) {
updateMonitorValues(false); updateMonitorValues(false);
gLastUiMs = millis(); gLastUiMs = millis();
} }
@@ -71,11 +71,14 @@ After tapping `START`:
- one breathing cycle is measured from one stable `EXHALE` start to the next stable `EXHALE` start - one breathing cycle is measured from one stable `EXHALE` start to the next stable `EXHALE` start
- contact changes use an 80 ms debounce - 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 - 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 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 - 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 - 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 - 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 - reset/power cycle restores ordinary native USB boot
This version is intended to monitor breathing cadence from a dry-contact USB D-/D+ sensor. This version is intended to monitor breathing cadence from a dry-contact USB D-/D+ sensor.
+1 -1
View File
@@ -1,2 +1,2 @@
client.version=1.14.11 client.version=1.14.12
server.version=1.12.11 server.version=1.12.11