diff --git a/include/kdeconnect.h b/include/kdeconnect.h index e7b887c..f8377c6 100644 --- a/include/kdeconnect.h +++ b/include/kdeconnect.h @@ -49,5 +49,9 @@ void kdeSeek(int32_t posMs); int kdeListPlayers(PlayerEntry* out, int max); // Show this player instead of picking automatically (nullptr = automatic again). void kdeSelectPlayer(const char* key); +// The UI tells whether the cover is needed (music view open); otherwise its memory is released. +void kdeSetArtWanted(bool wanted); +// Status text for the display: connections, SD card, free memory, last error. +void kdeStatus(char* out, size_t n); // Number of paired devices currently connected. int kdeConnectedCount(); diff --git a/src/kdeconnect.cpp b/src/kdeconnect.cpp index d311584..d764ae0 100644 --- a/src/kdeconnect.cpp +++ b/src/kdeconnect.cpp @@ -527,6 +527,14 @@ static void setArt(bool valid, const char* info) { // Downloaded covers stay on the card, the same album later needs no transfer at all. static SPIClass sdSpi(VSPI); static bool sdOk = false; +static volatile bool artWanted = false; +static char lastError[48] = ""; + +static void setLastError(const char* e) { + xSemaphoreTake(mtx, portMAX_DELAY); + strncpy(lastError, e, sizeof lastError - 1); + xSemaphoreGive(mtx); +} static void initSd() { sdSpi.begin(18, 19, 23, 5); // CYD micro-SD slot @@ -638,7 +646,7 @@ static const char* decodeFile(const String& path) { static const char* openPayload(Peer& p, int port, mbedtls_net_context& net, mbedtls_ssl_context& ssl) { uint32_t freeHeap = ESP.getFreeHeap(), block = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); Serial.printf("KDE: Cover-Verbindung, Heap %u, groesster Block %u\n", freeHeap, block); - if (freeHeap < 56000 || block < 17500) return "Zu wenig Speicher"; + if (freeHeap < 52000 || block < 17500) return "Zu wenig Speicher"; int fd = socket(AF_INET, SOCK_STREAM, 0); if (fd < 0) return "Keine Verbindung"; struct timeval tv = {5, 0}; @@ -869,8 +877,11 @@ static void acceptPeer(int fd) { Peer* slot = nullptr; for (auto& p : peers) if (p.used && p.id == id) closePeer(p); // reconnect replaces the old link for (auto& p : peers) if (!p.used) { slot = &p; break; } - if (!slot || ESP.getFreeHeap() < 62000 || heap_caps_get_largest_free_block(MALLOC_CAP_8BIT) < 17500) { - Serial.printf("KDE: kein Platz fuer weitere Verbindung (Heap %u)\n", ESP.getFreeHeap()); + // A connection needs ~40 KB; keep ~12 KB for WLAN and the HA requests. + if (!slot || ESP.getFreeHeap() < 52000 || heap_caps_get_largest_free_block(MALLOC_CAP_8BIT) < 17500) { + Serial.printf("KDE: %s abgewiesen (Heap %u, Block %u)\n", ascii(d["body"]["deviceName"] | "?").c_str(), + ESP.getFreeHeap(), heap_caps_get_largest_free_block(MALLOC_CAP_8BIT)); + setLastError(!slot ? "Max. 2 Geraete gleichzeitig" : "Zu wenig Speicher fuer Verbindung"); close(fd); return; } @@ -897,6 +908,8 @@ static void acceptPeer(int fd) { char err[80]; mbedtls_strerror(r, err, sizeof err); Serial.printf("KDE: TLS mit %s fehlgeschlagen: %s\n", p.name.c_str(), err); + setLastError(r == MBEDTLS_ERR_SSL_ALLOC_FAILED ? "Zu wenig Speicher fuer Verbindung" + : "Verschluesselung fehlgeschlagen"); closePeer(p); return; } @@ -1063,8 +1076,17 @@ static void kdeTask(void*) { } if (selChanged) { selChanged = false; publish(); } - // Album art: ask for the picture of the track shown on the display - if (curArtUrl != artLoadedUrl) { + // While the music view is closed the picture buffer (29 KB) is given back; with an SD card + // the cover comes back from the card in a moment when the view opens again. + if (!artWanted) { + if (artBuf && sdOk) { + setArt(false, ""); + vTaskDelay(pdMS_TO_TICKS(100)); // let the UI drop its reference first + free(artBuf); + artBuf = nullptr; + artLoadedUrl = artRequestedUrl = ""; + } + } else if (curArtUrl != artLoadedUrl) { if (!curArtUrl.length()) { if (hasArt) setArt(false, ""); artLoadedUrl = ""; @@ -1168,6 +1190,26 @@ void kdeSelectPlayer(const char* key) { selChanged = true; } +void kdeSetArtWanted(bool wanted) { artWanted = wanted; } + +void kdeStatus(char* out, size_t n) { + if (!mtx) { snprintf(out, n, "KDE Connect: startet ..."); return; } + int conn = 0, all = 0; + String names; + for (auto& p : peers) { // read-only snapshot, good enough for a status line + if (!p.used) continue; + all++; + if (p.paired) { conn++; names += (names.length() ? ", " : "") + p.name; } + } + xSemaphoreTake(mtx, portMAX_DELAY); + snprintf(out, n, "KDE Connect: %d gekoppelt verbunden%s%s\nSD-Karte: %s - Speicher frei: %u KB%s%s", conn, + names.length() ? " (" : "", names.length() ? (names + ")").c_str() : "", + sdOk ? "ja" : "nein", (unsigned)(ESP.getFreeHeap() / 1024), lastError[0] ? "\nLetzter Fehler: " : "", + lastError); + xSemaphoreGive(mtx); + (void)all; +} + int kdeConnectedCount() { if (!mtx) return 0; xSemaphoreTake(mtx, portMAX_DELAY); diff --git a/src/main.cpp b/src/main.cpp index 16ea672..cdf09ef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -494,6 +494,8 @@ static lv_obj_t* makeRoundBtn(lv_obj_t* parent, const char* sym, intptr_t ud) { return b; } +static void addKdeInfo(); + static void buildUi() { lv_obj_t* scr = lv_scr_act(); lv_obj_set_style_bg_color(scr, lv_color_hex(0x101418), 0); @@ -560,6 +562,7 @@ static void buildUi() { lv_label_set_text(hintHist, "Lade Geraete aus HA ..."); lv_obj_t* h = lv_label_create(tabAlert); lv_label_set_text(h, "Pruefe Hinweise ..."); + addKdeInfo(); } static void populateUi() { @@ -847,8 +850,34 @@ static void sampleHistory() { } // ---------------------------------------------------------------- Alerts +static void showAlertList(); +static lv_obj_t* kdeInfo = nullptr; + +static void updateKdeInfo() { + if (!kdeInfo) return; + char buf[200]; + kdeStatus(buf, sizeof buf); + lv_label_set_text(kdeInfo, buf); +} + +static void addKdeInfo() { +#if KDE_CONNECT + kdeInfo = lv_label_create(tabAlert); + lv_obj_set_width(kdeInfo, lv_pct(100)); + lv_label_set_long_mode(kdeInfo, LV_LABEL_LONG_WRAP); + lv_obj_set_style_text_color(kdeInfo, lv_palette_lighten(LV_PALETTE_GREY, 1), 0); + updateKdeInfo(); +#endif +} + static void showAlerts() { lv_obj_clean(tabAlert); + kdeInfo = nullptr; + showAlertList(); + addKdeInfo(); +} + +static void showAlertList() { if (!nAlerts) { lv_obj_t* l = lv_label_create(tabAlert); lv_obj_set_style_text_color(l, lv_palette_main(LV_PALETTE_GREEN), 0); @@ -942,6 +971,7 @@ static uint32_t musicShownAt = 0; static void showMusic(bool show) { if (show && !musicVisible) musicShownAt = millis(); musicVisible = show; + kdeSetArtWanted(show); if (show) lv_obj_clear_flag(musicPanel, LV_OBJ_FLAG_HIDDEN); else lv_obj_add_flag(musicPanel, LV_OBJ_FLAG_HIDDEN); } @@ -1487,11 +1517,12 @@ void loop() { updateMusic(); } - // WiFi signal strength + // WiFi signal strength, KDE status static uint32_t lastRssi = 0; if (now - lastRssi > 2000) { lastRssi = now; updateRssi(); + updateKdeInfo(); } // Backlight timeout