From dbfe5bf7cf521e338c0bf1f16bb91a04b69e7ba4 Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 27 Oct 2026 18:35:00 +0300 Subject: [PATCH] add time, button ui --- include/config.h | 9 +++++++-- src/display_task.cpp | 47 +++++++++++++++++++++++++++++++++----------- src/web_page.h | 32 ++++++++++++++++++------------ src/web_task.cpp | 1 + 4 files changed, 63 insertions(+), 26 deletions(-) diff --git a/include/config.h b/include/config.h index 732e66e..d0148e0 100644 --- a/include/config.h +++ b/include/config.h @@ -1,8 +1,8 @@ #pragma once // ---------- Wi-Fi ---------- -#define WIFI_SSID "your_ssid" -#define WIFI_PASSWORD "your_password" +#define WIFI_SSID "skynet" +#define WIFI_PASSWORD "0218kirill" #define WIFI_CONNECT_TIMEOUT_MS 15000 // If the station connection fails, the ESP32 starts its own access point @@ -11,6 +11,11 @@ #define MDNS_HOSTNAME "volume" // http://volume.local +// ---------- Time (NTP, works only in station mode) ---------- +#define NTP_SERVER1 "pool.ntp.org" +#define NTP_SERVER2 "time.google.com" +#define TIME_ZONE "MSK-3" // POSIX TZ string, e.g. "CET-1CEST,M3.5.0,M10.5.0/3" + // ---------- Pins (chosen to not clash with the TFT pins in platformio.ini) ---------- #define PIN_ENC_CLK 32 #define PIN_ENC_DT 33 diff --git a/src/display_task.cpp b/src/display_task.cpp index 8e94256..a72ae6b 100644 --- a/src/display_task.cpp +++ b/src/display_task.cpp @@ -13,18 +13,19 @@ struct Rect { // Landscape 320x240 layout static const int kW = 320; -static const Rect kBar = {20, 150, 280, 24}; -static const Rect kBarTouch = {0, 136, 320, 50}; // larger than the bar for easier dragging -static const Rect kRelay = {60, 190, 200, 40}; -static const Rect kMinus = {10, 55, 60, 70}; -static const Rect kPlus = {250, 55, 60, 70}; +static const Rect kRelay = {10, 46, 120, 48}; +static const int kClockX = 310, kClockY = 70; // right-middle of the clock text +static const Rect kMinus = {10, 108, 60, 70}; +static const Rect kPlus = {250, 108, 60, 70}; +static const int kVolumeY = 120; +static const Rect kBar = {20, 196, 280, 24}; +static const Rect kBarTouch = {0, 184, 320, 50}; // larger than the bar for easier dragging static const uint16_t kBg = TFT_BLACK; static const uint16_t kAccent = 0x3D3F; // light blue static const uint16_t kBarBg = 0x2945; // dark grey static const uint16_t kBtn = 0x2945; static const uint16_t kBtnPressed = 0x3D3F; -static const uint16_t kOn = TFT_GREEN; static const uint16_t kOff = 0x4208; // grey enum class TouchTarget { None, Minus, Plus, Bar, Relay }; @@ -52,10 +53,10 @@ static void drawVolume(uint8_t volume) { tft.setTextColor(kAccent, kBg); tft.setTextDatum(TR_DATUM); tft.setTextPadding(tft.textWidth("100", 7)); - tft.drawNumber(volume, 200, 70, 7); + tft.drawNumber(volume, 200, kVolumeY, 7); tft.setTextPadding(0); tft.setTextDatum(TL_DATUM); - tft.drawString("%", 208, 94, 4); + tft.drawString("%", 208, kVolumeY + 24, 4); int fill = kBar.w * volume / 100; tft.fillRect(kBar.x, kBar.y, fill, kBar.h, kAccent); @@ -63,11 +64,29 @@ static void drawVolume(uint8_t volume) { } static void drawRelay(bool on) { - uint16_t bg = on ? kOn : kOff; + uint16_t bg = on ? kAccent : kOff; tft.fillRoundRect(kRelay.x, kRelay.y, kRelay.w, kRelay.h, 10, bg); - tft.setTextColor(on ? TFT_BLACK : TFT_WHITE, bg); + tft.setTextColor(TFT_WHITE, bg); tft.setTextDatum(MC_DATUM); - tft.drawString(on ? "POWER: ON" : "POWER: OFF", kRelay.x + kRelay.w / 2, kRelay.y + kRelay.h / 2 + 1, 4); + tft.drawString(on ? "ON" : "OFF", kRelay.x + kRelay.w / 2, kRelay.y + kRelay.h / 2 + 1, 4); +} + +// Returns minutes since midnight, or -1 while the time is not synced via NTP +static int currentMinutes() { + struct tm t; + if (!getLocalTime(&t, 0)) return -1; + return t.tm_hour * 60 + t.tm_min; +} + +static void drawClock(int minutes) { + char buf[6]; + if (minutes < 0) strcpy(buf, "--:--"); + else snprintf(buf, sizeof(buf), "%02d:%02d", minutes / 60, minutes % 60); + tft.setTextColor(TFT_WHITE, kBg); + tft.setTextDatum(MR_DATUM); + tft.setTextPadding(tft.textWidth("88:88", 7)); + tft.drawString(buf, kClockX, kClockY, 7); + tft.setTextPadding(0); } static String networkText() { @@ -129,6 +148,7 @@ void displayTask(void *arg) { int lastVolume = -1; int lastRelay = -1; String lastNet; + int lastMinutes = -2; TickType_t lastNetCheck = 0; TouchTarget active = TouchTarget::None; @@ -204,6 +224,11 @@ void displayTask(void *arg) { drawNetwork(net); lastNet = net; } + int minutes = currentMinutes(); + if (minutes != lastMinutes) { + drawClock(minutes); + lastMinutes = minutes; + } } vTaskDelay(pdMS_TO_TICKS(DISPLAY_UPDATE_MS)); diff --git a/src/web_page.h b/src/web_page.h index 3d94096..d193508 100644 --- a/src/web_page.h +++ b/src/web_page.h @@ -2,13 +2,13 @@ #include static const char INDEX_HTML[] PROGMEM = R"HTML( - + -Громкость +Volume
-

Регулятор громкости

+

Volume control

+
+ + +
--%
- -
подключение…
+
connecting…