KDE-Status im Glocken-Tab, Cover-Speicher freigeben wenn Musik-Ansicht zu, Speicherschwelle 52 KB

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-06 21:35:04 +01:00
1 parent 0c9c955705
commit 1f59cb4393
3 files changed
+83 -6

No files matched your search

+4
View File
@@ -49,5 +49,9 @@ void kdeSeek(int32_t posMs);
int kdeListPlayers(PlayerEntry* out, int max); int kdeListPlayers(PlayerEntry* out, int max);
// Show this player instead of picking automatically (nullptr = automatic again). // Show this player instead of picking automatically (nullptr = automatic again).
void kdeSelectPlayer(const char* key); 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. // Number of paired devices currently connected.
int kdeConnectedCount(); int kdeConnectedCount();
+47 -5
View File
@@ -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. // Downloaded covers stay on the card, the same album later needs no transfer at all.
static SPIClass sdSpi(VSPI); static SPIClass sdSpi(VSPI);
static bool sdOk = false; 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() { static void initSd() {
sdSpi.begin(18, 19, 23, 5); // CYD micro-SD slot 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) { 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); 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); 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); int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) return "Keine Verbindung"; if (fd < 0) return "Keine Verbindung";
struct timeval tv = {5, 0}; struct timeval tv = {5, 0};
@@ -869,8 +877,11 @@ static void acceptPeer(int fd) {
Peer* slot = nullptr; 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 && p.id == id) closePeer(p); // reconnect replaces the old link
for (auto& p : peers) if (!p.used) { slot = &p; break; } 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) { // A connection needs ~40 KB; keep ~12 KB for WLAN and the HA requests.
Serial.printf("KDE: kein Platz fuer weitere Verbindung (Heap %u)\n", ESP.getFreeHeap()); 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); close(fd);
return; return;
} }
@@ -897,6 +908,8 @@ static void acceptPeer(int fd) {
char err[80]; char err[80];
mbedtls_strerror(r, err, sizeof err); mbedtls_strerror(r, err, sizeof err);
Serial.printf("KDE: TLS mit %s fehlgeschlagen: %s\n", p.name.c_str(), 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); closePeer(p);
return; return;
} }
@@ -1063,8 +1076,17 @@ static void kdeTask(void*) {
} }
if (selChanged) { selChanged = false; publish(); } if (selChanged) { selChanged = false; publish(); }
// Album art: ask for the picture of the track shown on the display // While the music view is closed the picture buffer (29 KB) is given back; with an SD card
if (curArtUrl != artLoadedUrl) { // 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 (!curArtUrl.length()) {
if (hasArt) setArt(false, ""); if (hasArt) setArt(false, "");
artLoadedUrl = ""; artLoadedUrl = "";
@@ -1168,6 +1190,26 @@ void kdeSelectPlayer(const char* key) {
selChanged = true; 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() { int kdeConnectedCount() {
if (!mtx) return 0; if (!mtx) return 0;
xSemaphoreTake(mtx, portMAX_DELAY); xSemaphoreTake(mtx, portMAX_DELAY);
+32 -1
View File
@@ -494,6 +494,8 @@ static lv_obj_t* makeRoundBtn(lv_obj_t* parent, const char* sym, intptr_t ud) {
return b; return b;
} }
static void addKdeInfo();
static void buildUi() { static void buildUi() {
lv_obj_t* scr = lv_scr_act(); lv_obj_t* scr = lv_scr_act();
lv_obj_set_style_bg_color(scr, lv_color_hex(0x101418), 0); 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_label_set_text(hintHist, "Lade Geraete aus HA ...");
lv_obj_t* h = lv_label_create(tabAlert); lv_obj_t* h = lv_label_create(tabAlert);
lv_label_set_text(h, "Pruefe Hinweise ..."); lv_label_set_text(h, "Pruefe Hinweise ...");
addKdeInfo();
} }
static void populateUi() { static void populateUi() {
@@ -847,8 +850,34 @@ static void sampleHistory() {
} }
// ---------------------------------------------------------------- Alerts // ---------------------------------------------------------------- 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() { static void showAlerts() {
lv_obj_clean(tabAlert); lv_obj_clean(tabAlert);
kdeInfo = nullptr;
showAlertList();
addKdeInfo();
}
static void showAlertList() {
if (!nAlerts) { if (!nAlerts) {
lv_obj_t* l = lv_label_create(tabAlert); lv_obj_t* l = lv_label_create(tabAlert);
lv_obj_set_style_text_color(l, lv_palette_main(LV_PALETTE_GREEN), 0); 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) { static void showMusic(bool show) {
if (show && !musicVisible) musicShownAt = millis(); if (show && !musicVisible) musicShownAt = millis();
musicVisible = show; musicVisible = show;
kdeSetArtWanted(show);
if (show) lv_obj_clear_flag(musicPanel, LV_OBJ_FLAG_HIDDEN); if (show) lv_obj_clear_flag(musicPanel, LV_OBJ_FLAG_HIDDEN);
else lv_obj_add_flag(musicPanel, LV_OBJ_FLAG_HIDDEN); else lv_obj_add_flag(musicPanel, LV_OBJ_FLAG_HIDDEN);
} }
@@ -1487,11 +1517,12 @@ void loop() {
updateMusic(); updateMusic();
} }
// WiFi signal strength // WiFi signal strength, KDE status
static uint32_t lastRssi = 0; static uint32_t lastRssi = 0;
if (now - lastRssi > 2000) { if (now - lastRssi > 2000) {
lastRssi = now; lastRssi = now;
updateRssi(); updateRssi();
updateKdeInfo();
} }
// Backlight timeout // Backlight timeout