Обновить обработку аватаров и вложений

This commit is contained in:
AidarKC
2026-10-07 14:05:01 +03:00
parent 7b8e178723
commit d8f42c769c
35 changed files with 628 additions and 222 deletions
@@ -1,6 +1,7 @@
#include <Arduino.h>
#include <Wire.h>
#include <math.h>
#include <Preferences.h>
#include <Arduino_GFX_Library.h>
#include <TouchDrvCSTXXX.hpp>
#include "ESP_I2S.h"
@@ -54,6 +55,7 @@ Arduino_CO5300 *gfx = new Arduino_CO5300(
TouchDrvCST92xx gTouch;
I2SClass gI2s;
Preferences gPrefs;
struct Rect {
int16_t x;
@@ -67,6 +69,8 @@ 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;
@@ -96,6 +100,8 @@ 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);
@@ -152,6 +158,33 @@ static uint32_t averageCycleMs() {
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;
}
@@ -206,11 +239,11 @@ static void playBeep(uint16_t freq, uint16_t samples, int16_t amp) {
}
static void playCycleTick() {
playBeep(880, 480, (int16_t)(gTickVolume * 800));
playBeep(880, 480, kTickAmpByVolume[gTickVolume]);
}
static void playAlertBeep() {
playBeep(1200, 1600, (int16_t)(gAlertVolume * 1200));
playBeep(1200, 1600, kAlertAmpByVolume[gAlertVolume]);
}
static void alertIfNeeded() {
@@ -299,6 +332,7 @@ static void drawMonitorStatic() {
gStaticDrawn = true;
gLastDrawnClosed = !gStableClosed;
gLastDrawnPaused = !isAutoPaused();
resetCachedUiText();
}
static void updateMonitorValues(bool force) {
@@ -327,9 +361,13 @@ static void updateMonitorValues(bool force) {
gLastDrawnPaused = paused;
}
clearArea(28, 58, 424, 28, C_PANEL);
snprintf(line, sizeof(line), "Limit: %us top button: settings", kLimitsSec[gLimitIndex]);
drawText(28, 60, line, C_MUTE, 2);
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);
@@ -351,9 +389,13 @@ static void updateMonitorValues(bool force) {
snprintf(value, sizeof(value), "%us", kLimitsSec[gLimitIndex]);
drawText(266, 342, value, C_TEXT, 2);
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);
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() {
@@ -398,6 +440,7 @@ static void toggleSettings() {
gSettingsOpen = !gSettingsOpen;
gNeedStaticRedraw = true;
gNeedRedraw = true;
resetCachedUiText();
}
static void handleStableContact(bool closed) {
@@ -461,16 +504,29 @@ static uint8_t sliderValueFromX(const Rect &rect, int16_t x, uint8_t maxValue) {
static void handleSettingsTouch(int16_t x, int16_t y) {
if (contains(kLimitSlider, x, y)) {
gLimitIndex = sliderValueFromX(kLimitSlider, x, 4);
uint8_t next = sliderValueFromX(kLimitSlider, x, 4);
if (next == gLimitIndex) {
return;
}
gLimitIndex = next;
} else if (contains(kTickSlider, x, y)) {
gTickVolume = sliderValueFromX(kTickSlider, x, 10);
uint8_t next = sliderValueFromX(kTickSlider, x, 10);
if (next == gTickVolume) {
return;
}
gTickVolume = next;
playCycleTick();
} else if (contains(kAlertSlider, x, y)) {
gAlertVolume = sliderValueFromX(kAlertSlider, x, 10);
uint8_t next = sliderValueFromX(kAlertSlider, x, 10);
if (next == gAlertVolume) {
return;
}
gAlertVolume = next;
playAlertBeep();
} else {
return;
}
saveSettings();
gNeedRedraw = true;
}
@@ -510,6 +566,7 @@ void setup() {
Serial.begin(115200);
Wire.begin(PIN_I2C_SDA, PIN_I2C_SCL);
pinMode(PIN_USER_BTN, INPUT_PULLUP);
loadSettings();
initAudio();
gfx->begin();