Musik-Ansicht zum Durchwischen: Cover, Lautstaerke/Spulen, Quellenwahl; Zeitzone von HA

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MrBlakeandClaude Opus 5.5 committed 2026-11-06 21:06:03 +01:00
1 parent f74d8c8c59
commit bc1afc4f23
4 files changed
+651 -80

No files matched your search

+249 -7
View File
@@ -21,6 +21,10 @@
#include <mbedtls/ecp.h>
#include <mbedtls/sha256.h>
#include <mbedtls/error.h>
#include <esp_heap_caps.h>
extern "C" {
#include <esp32/rom/tjpgd.h>
}
static constexpr int KDE_PORT = 1716;
static constexpr int MAX_PEERS = 3;
@@ -29,8 +33,9 @@ static constexpr int MAX_TRUSTED = 8;
static constexpr size_t MAX_RX = 16384;
struct Player {
String name, title, artist;
bool playing = false;
String name, title, artist, album, artUrl;
bool playing = false, canSeek = false;
int volume = -1;
int32_t pos = 0, len = 0;
uint32_t at = 0, startedAt = 0;
};
@@ -41,6 +46,8 @@ struct Peer {
mbedtls_ssl_context ssl;
String id, name, fp, rx;
int proto = 7;
uint32_t ip = 0;
bool artPayload = false;
bool paired = false;
bool sentUnpair = false;
Player pl[MAX_PLAYERS];
@@ -69,6 +76,18 @@ static char pairName[33];
static int pairAnswer = 0; // 0 none, 1 accept, 2 reject
static char pendingAction[16] = "";
static int connectedPaired = 0;
static int pendingVolume = -1;
static int32_t pendingSeek = -1;
static volatile bool selChanged = false;
static String selKey; // "peerId|player" chosen in the UI, empty = automatic
static PlayerEntry plist[MAX_PEERS * MAX_PLAYERS];
static int nList = 0;
static uint16_t* artBuf = nullptr;
static bool hasArt = false;
static uint32_t artSeq = 0;
// Task-only state for album art
static String curPeerId, curPlayer, curArtUrl, artLoadedUrl, artRequestedUrl;
// ---------------------------------------------------------------- Helpers
static int64_t packetId() {
@@ -247,29 +266,58 @@ static bool initIdentity() {
static void publish() {
Peer* bp = nullptr;
Player* best = nullptr;
Peer* pinPeer = nullptr;
Player* pin = nullptr;
PlayerEntry list[MAX_PEERS * MAX_PLAYERS];
int nl = 0;
xSemaphoreTake(mtx, portMAX_DELAY);
String sel = selKey;
xSemaphoreGive(mtx);
for (auto& p : peers) {
if (!p.used || !p.paired) continue;
for (int k = 0; k < p.nPl; k++) {
Player& pl = p.pl[k];
String key = p.id + "|" + pl.name;
bool isSel = sel.length() && key == sel;
if (isSel) { pin = &pl; pinPeer = &p; }
if (nl < MAX_PEERS * MAX_PLAYERS) {
PlayerEntry& e = list[nl++];
copyStr(e.key, sizeof e.key, key);
copyStr(e.device, sizeof e.device, p.name);
copyStr(e.player, sizeof e.player, pl.name);
copyStr(e.title, sizeof e.title, pl.title);
e.playing = pl.playing;
e.selected = isSel;
}
if (!pl.title.length()) continue;
bool better = !best || (pl.playing && !best->playing) ||
(pl.playing == best->playing && (pl.playing ? pl.startedAt > best->startedAt : pl.at > best->at));
if (better) { best = &pl; bp = &p; }
}
}
if (pin) { best = pin; bp = pinPeer; }
int conn = 0;
for (auto& p : peers) if (p.used && p.paired) conn++;
curPeerId = best ? bp->id : "";
curPlayer = best ? best->name : "";
curArtUrl = best && bp->artPayload ? best->artUrl : "";
xSemaphoreTake(mtx, portMAX_DELAY);
connectedPaired = conn;
memcpy(plist, list, sizeof(PlayerEntry) * nl);
nList = nl;
NowPlaying n;
if (best) {
n.valid = true;
n.playing = best->playing;
n.canSeek = best->canSeek;
n.volume = best->volume;
n.pinned = pin != nullptr;
copyStr(n.device, sizeof n.device, bp->name);
copyStr(n.player, sizeof n.player, best->name);
copyStr(n.title, sizeof n.title, best->title);
copyStr(n.artist, sizeof n.artist, best->artist);
copyStr(n.album, sizeof n.album, best->album);
n.posMs = best->pos;
n.lenMs = best->len;
n.atMs = best->at;
@@ -277,8 +325,12 @@ static void publish() {
pubPlayer = best->name;
}
bool changed = n.valid != pub.valid || n.playing != pub.playing || strcmp(n.title, pub.title) ||
strcmp(n.artist, pub.artist) || strcmp(n.device, pub.device) || strcmp(n.player, pub.player);
strcmp(n.artist, pub.artist) || strcmp(n.device, pub.device) || strcmp(n.player, pub.player) ||
n.volume != pub.volume || n.pinned != pub.pinned || n.lenMs != pub.lenMs ||
abs(n.posMs - pub.posMs) > 2000;
n.seq = pub.seq + (changed ? 1 : 0);
n.artSeq = artSeq;
n.hasArt = hasArt;
pub = n;
xSemaphoreGive(mtx);
}
@@ -347,6 +399,7 @@ static Player* findPlayer(Peer& p, const String& name, bool create) {
}
static void handleMpris(Peer& p, JsonObject b) {
if (b["supportAlbumArtPayload"].is<bool>()) p.artPayload = b["supportAlbumArtPayload"];
if (b["playerList"].is<JsonArray>()) {
JsonArray list = b["playerList"];
Player keep[MAX_PLAYERS];
@@ -364,6 +417,7 @@ static void handleMpris(Peer& p, JsonObject b) {
JsonDocument r;
r["player"] = name;
r["requestNowPlaying"] = true;
r["requestVolume"] = true;
sendPacket(p, "kdeconnect.mpris.request", r);
}
}
@@ -381,6 +435,10 @@ static void handleMpris(Peer& p, JsonObject b) {
pl->title = ascii(b["nowPlaying"]);
pl->artist = "";
}
if (b["album"].is<const char*>()) pl->album = ascii(b["album"]);
if (b["albumArtUrl"].is<const char*>()) pl->artUrl = (const char*)b["albumArtUrl"];
if (b["volume"].is<int>()) pl->volume = b["volume"];
if (b["canSeek"].is<bool>()) pl->canSeek = b["canSeek"];
if (b["isPlaying"].is<bool>()) pl->playing = b["isPlaying"];
if (b["length"].is<int64_t>()) pl->len = (int32_t)(b["length"].as<int64_t>());
if (b["pos"].is<int64_t>()) pl->pos = (int32_t)(b["pos"].as<int64_t>());
@@ -391,6 +449,113 @@ static void handleMpris(Peer& p, JsonObject b) {
publish();
}
// ---------------------------------------------------------------- Album art (payload transfer + JPEG decode)
struct ArtCtx {
mbedtls_ssl_context* ssl;
int64_t left;
int S, ox, oy;
};
static UINT artIn(JDEC* jd, BYTE* buf, UINT nd) {
ArtCtx* c = (ArtCtx*)jd->device;
static BYTE skip[64];
UINT done = 0;
while (done < nd && c->left > 0) {
BYTE* dst = buf ? buf + done : skip;
UINT want = buf ? nd - done : min<UINT>(nd - done, sizeof skip);
if ((int64_t)want > c->left) want = (UINT)c->left;
int r = mbedtls_ssl_read(c->ssl, dst, want);
if (r == MBEDTLS_ERR_SSL_WANT_READ || r == MBEDTLS_ERR_SSL_WANT_WRITE) continue;
if (r <= 0) break;
done += r;
c->left -= r;
}
return done;
}
static UINT artOut(JDEC* jd, void* bitmap, JRECT* rect) {
ArtCtx* c = (ArtCtx*)jd->device;
const int N = KDE_ART_SIZE;
const BYTE* px = (const BYTE*)bitmap;
for (int y = rect->top; y <= rect->bottom; y++) {
for (int x = rect->left; x <= rect->right; x++, px += 3) {
if (y < c->oy || y >= c->oy + c->S || x < c->ox || x >= c->ox + c->S) continue;
// Each source pixel covers the destination range [d0, d1): nearest neighbour, up or down.
int dy0 = ((y - c->oy) * N + c->S - 1) / c->S, dy1 = ((y + 1 - c->oy) * N + c->S - 1) / c->S;
int dx0 = ((x - c->ox) * N + c->S - 1) / c->S, dx1 = ((x + 1 - c->ox) * N + c->S - 1) / c->S;
uint16_t col = ((px[0] & 0xF8) << 8) | ((px[1] & 0xFC) << 3) | (px[2] >> 3);
for (int dy = dy0; dy < dy1 && dy < N; dy++)
for (int dx = dx0; dx < dx1 && dx < N; dx++) artBuf[dy * N + dx] = col;
}
}
return 1;
}
static void setArt(bool valid) {
xSemaphoreTake(mtx, portMAX_DELAY);
hasArt = valid;
artSeq++;
pub.artSeq = artSeq;
pub.hasArt = hasArt;
xSemaphoreGive(mtx);
}
static bool downloadArt(Peer& p, int port, int64_t size) {
if (size <= 0 || size > 2000000 || !p.ip) return false;
if (!artBuf) artBuf = (uint16_t*)malloc(KDE_ART_SIZE * KDE_ART_SIZE * 2);
if (!artBuf) return false;
if (ESP.getFreeHeap() < 60000 || heap_caps_get_largest_free_block(MALLOC_CAP_8BIT) < 18000) {
Serial.printf("KDE: zu wenig Speicher fuer Titelbild (Heap %u)\n", ESP.getFreeHeap());
return false;
}
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) return false;
struct timeval tv = {5, 0};
setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
sockaddr_in a = {};
a.sin_family = AF_INET;
a.sin_port = htons(port);
a.sin_addr.s_addr = p.ip;
if (connect(fd, (sockaddr*)&a, sizeof a) < 0) { close(fd); return false; }
mbedtls_net_context net;
mbedtls_net_init(&net);
net.fd = fd;
mbedtls_ssl_context ssl;
mbedtls_ssl_init(&ssl);
bool ok = false;
void* pool = nullptr;
if (mbedtls_ssl_setup(&ssl, &conf) == 0) {
mbedtls_ssl_set_bio(&ssl, &net, mbedtls_net_send, mbedtls_net_recv, nullptr);
uint32_t start = millis();
int r;
while ((r = mbedtls_ssl_handshake(&ssl)) == MBEDTLS_ERR_SSL_WANT_READ || r == MBEDTLS_ERR_SSL_WANT_WRITE)
if (millis() - start > 8000) break;
pool = r == 0 ? malloc(4096) : nullptr;
if (pool) {
ArtCtx c = {&ssl, size, 0, 0, 0};
JDEC jd;
JRESULT jr = jd_prepare(&jd, artIn, pool, 4096, &c);
if (jr == JDR_OK) {
int s = 0;
while (s < 3 && (int)(min(jd.width, jd.height) >> (s + 1)) >= KDE_ART_SIZE) s++;
int W = jd.width >> s, H = jd.height >> s;
c.S = min(W, H);
c.ox = (W - c.S) / 2;
c.oy = (H - c.S) / 2;
jr = jd_decomp(&jd, artOut, s);
ok = jr == JDR_OK;
}
if (!ok) Serial.printf("KDE: Titelbild nicht lesbar (%d), nur normales JPEG geht\n", (int)jr);
}
}
free(pool);
mbedtls_ssl_free(&ssl);
mbedtls_net_free(&net);
return ok;
}
static void handlePacket(Peer& p, const String& line) {
JsonDocument d;
if (deserializeJson(d, line)) return;
@@ -430,7 +595,17 @@ static void handlePacket(Peer& p, const String& line) {
if (!p.sentUnpair) { sendPair(p, false); p.sentUnpair = true; }
return;
}
if (!strcmp(type, "kdeconnect.mpris")) handleMpris(p, b);
if (!strcmp(type, "kdeconnect.mpris")) {
handleMpris(p, b);
// Album art arrives as a payload on a separate TLS connection.
int port = d["payloadTransferInfo"]["port"] | 0;
String url = b["albumArtUrl"] | "";
if (port && (b["transferringAlbumArt"] | false) && p.id == curPeerId && url == curArtUrl) {
bool ok = downloadArt(p, port, d["payloadSize"] | (int64_t)0);
artLoadedUrl = url;
setArt(ok);
}
}
}
static bool readLine(int fd, String& out, uint32_t timeoutMs) {
@@ -483,6 +658,9 @@ static void acceptPeer(int fd) {
p.id = id;
p.name = ascii(d["body"]["deviceName"] | "Geraet");
p.proto = d["body"]["protocolVersion"] | 7;
sockaddr_in pa;
socklen_t pal = sizeof pa;
if (getpeername(fd, (sockaddr*)&pa, &pal) == 0) p.ip = pa.sin_addr.s_addr;
mbedtls_net_init(&p.net);
p.net.fd = fd;
mbedtls_ssl_init(&p.ssl);
@@ -621,7 +799,14 @@ static void kdeTask(void*) {
char act[16] = "";
xSemaphoreTake(mtx, portMAX_DELAY);
if (pairAnswer) { answer = pairAnswer; answerId = pairPeerId; pairAnswer = 0; pairId = 0; }
if (pendingAction[0]) { strcpy(act, pendingAction); pendingAction[0] = 0; actPeer = pubPeerId; actPlayer = pubPlayer; }
int vol = pendingVolume;
int32_t seek = pendingSeek;
pendingVolume = -1;
pendingSeek = -1;
if (pendingAction[0]) strcpy(act, pendingAction);
pendingAction[0] = 0;
actPeer = pubPeerId;
actPlayer = pubPlayer;
xSemaphoreGive(mtx);
if (answer) {
for (auto& p : peers) {
@@ -639,13 +824,37 @@ static void kdeTask(void*) {
publish();
}
}
if (act[0]) {
if (act[0] || vol >= 0 || seek >= 0) {
for (auto& p : peers) {
if (!p.used || !p.paired || p.id != actPeer) continue;
JsonDocument b;
b["player"] = actPlayer;
b["action"] = act;
if (act[0]) b["action"] = act;
if (vol >= 0) b["setVolume"] = vol;
if (seek >= 0) b["SetPosition"] = seek;
sendPacket(p, "kdeconnect.mpris.request", b);
Player* pl = findPlayer(p, actPlayer, false);
if (pl && seek >= 0) { pl->pos = seek; pl->at = millis(); }
if (pl && vol >= 0) pl->volume = vol;
publish();
}
}
if (selChanged) { selChanged = false; publish(); }
// Album art: ask for the picture of the track shown on the display
if (curArtUrl != artLoadedUrl) {
if (hasArt) setArt(false);
if (!curArtUrl.length()) {
artLoadedUrl = "";
} else if (curArtUrl != artRequestedUrl) {
artRequestedUrl = curArtUrl;
for (auto& p : peers) {
if (!p.used || !p.paired || p.id != curPeerId) continue;
JsonDocument b;
b["player"] = curPlayer;
b["albumArtUrl"] = curArtUrl;
sendPacket(p, "kdeconnect.mpris.request", b);
}
}
}
vTaskDelay(pdMS_TO_TICKS(20));
@@ -690,6 +899,39 @@ void kdeMediaAction(const char* action) {
xSemaphoreGive(mtx);
}
const uint16_t* kdeArt() { return hasArt ? artBuf : nullptr; }
void kdeSetVolume(int v) {
if (!mtx) return;
xSemaphoreTake(mtx, portMAX_DELAY);
pendingVolume = constrain(v, 0, 100);
xSemaphoreGive(mtx);
}
void kdeSeek(int32_t ms) {
if (!mtx) return;
xSemaphoreTake(mtx, portMAX_DELAY);
pendingSeek = max<int32_t>(0, ms);
xSemaphoreGive(mtx);
}
int kdeListPlayers(PlayerEntry* out, int max) {
if (!mtx) return 0;
xSemaphoreTake(mtx, portMAX_DELAY);
int n = min(max, nList);
memcpy(out, plist, sizeof(PlayerEntry) * n);
xSemaphoreGive(mtx);
return n;
}
void kdeSelectPlayer(const char* key) {
if (!mtx) return;
xSemaphoreTake(mtx, portMAX_DELAY);
selKey = key ? key : "";
xSemaphoreGive(mtx);
selChanged = true;
}
int kdeConnectedCount() {
if (!mtx) return 0;
xSemaphoreTake(mtx, portMAX_DELAY);
+365 -69
View File
@@ -9,6 +9,7 @@
#include <XPT2046_Touchscreen.h>
#include <lvgl.h>
#include <time.h>
#include <Preferences.h>
struct Device { String entity; String name; };
@@ -671,6 +672,27 @@ static bool discoverDevices() {
return true;
}
// ---------------------------------------------------------------- Time zone
// Takes the current UTC offset from Home Assistant, so the clock always matches HA (incl. summer time).
static void syncTimezone() {
JsonDocument req;
req["template"] = "{{ now().strftime('%z') }}";
String body, resp;
serializeJson(req, body);
if (haRequest(true, "/api/template", body, &resp) != 200) return;
resp.trim();
if (resp.length() != 5 || (resp[0] != '+' && resp[0] != '-')) return;
int hh = resp.substring(1, 3).toInt(), mm = resp.substring(3, 5).toInt();
char tz[24];
// POSIX TZ uses the inverted sign: UTC+1 is "XXX-01:00".
snprintf(tz, sizeof tz, "HAT%c%02d:%02d", resp[0] == '+' ? '-' : '+', hh, mm);
if (strcmp(getenv("TZ") ? getenv("TZ") : "", tz)) {
Serial.printf("Zeitzone von HA: UTC%s (%s)\n", resp.c_str(), tz);
setenv("TZ", tz, 1);
tzset();
}
}
// ---------------------------------------------------------------- Weather
static const char* weatherText(const char* c) {
if (!strcmp(c, "sunny")) return "Sonnig";
@@ -799,11 +821,21 @@ static void pollAlerts() {
}
// ---------------------------------------------------------------- Music (KDE Connect)
static lv_obj_t *musicPanel, *mClock, *mDate, *mSource, *mTitle, *mArtist, *mBar, *mPos, *mLen, *mPlayLbl;
// Full-screen overlay with four swipeable pages: clock, cover, controls, sources/settings.
static lv_obj_t *musicPanel, *musicTv, *pageDots[4];
static lv_obj_t *mClock, *mDate, *mSource, *mTitle, *mArtist, *mBar, *mPos, *mLen, *mPlayLbl; // page 1
static lv_obj_t *cArt, *cArtPh, *cTitle, *cArtist, *cAlbum, *cBar, *cPos, *cLen, *cPlayLbl; // page 2
static lv_obj_t *sVol, *sVolLbl, *sSeek, *sSeekLbl, *sPlayLbl; // page 3
static lv_obj_t *qList, *qAuto, *qSwShow, *qSwKeep; // page 4
static lv_img_dsc_t artDsc;
static lv_obj_t* pairBox = nullptr;
static uint32_t pairShown = 0;
static NowPlaying np;
static bool musicVisible = false;
static bool musicAutoShow = true;
static bool musicKeepOn = MUSIC_KEEP_SCREEN_ON;
static PlayerEntry qEntries[12];
static int nQEntries = 0;
static void wakeScreen() {
if (screenOff) setBacklight(true);
@@ -816,30 +848,108 @@ static void showMusic(bool show) {
else lv_obj_add_flag(musicPanel, LV_OBJ_FLAG_HIDDEN);
}
static void saveMusicSettings() {
Preferences p;
p.begin("ui", false);
p.putBool("mshow", musicAutoShow);
p.putBool("mkeep", musicKeepOn);
p.end();
}
static void onMusicBtn(lv_event_t* e) {
const char* act = (const char*)lv_event_get_user_data(e);
if (act) kdeMediaAction(act);
else showMusic(false);
}
static int32_t curPos() {
int32_t pos = np.posMs + (np.playing ? (int32_t)(millis() - np.atMs) : 0);
return np.lenMs > 0 ? min(pos, np.lenMs) : pos;
}
static void onJump(lv_event_t* e) {
int32_t d = (int32_t)(intptr_t)lv_event_get_user_data(e);
int32_t to = curPos() + d;
if (np.lenMs > 0) to = min(to, np.lenMs - 1000);
kdeSeek(max<int32_t>(0, to));
}
static void onVolume(lv_event_t*) { kdeSetVolume(lv_slider_get_value(sVol)); }
static void onSeekSlider(lv_event_t*) { kdeSeek(lv_slider_get_value(sSeek) * 1000); }
static void onClockClick(lv_event_t*) {
if (np.valid) showMusic(true);
}
static lv_obj_t* musicBtn(lv_obj_t* parent, const char* sym, const char* act, int x, int w) {
static void onTile(lv_event_t*) {
lv_obj_t* t = lv_tileview_get_tile_act(musicTv);
int idx = lv_obj_get_x(t) / SCREEN_W;
for (int i = 0; i < 4; i++)
lv_obj_set_style_bg_color(pageDots[i], i == idx ? lv_color_white() : lv_color_hex(0x3a444e), 0);
}
static void onSource(lv_event_t* e) {
intptr_t i = (intptr_t)lv_event_get_user_data(e);
kdeSelectPlayer(i < 0 ? nullptr : qEntries[i].key);
}
static void onSetting(lv_event_t* e) {
lv_obj_t* sw = lv_event_get_target(e);
bool on = lv_obj_has_state(sw, LV_STATE_CHECKED);
if (sw == qSwShow) musicAutoShow = on;
else musicKeepOn = on;
saveMusicSettings();
}
static lv_obj_t* label(lv_obj_t* parent, const lv_font_t* font, lv_color_t col, int w, lv_label_long_mode_t mode) {
lv_obj_t* l = lv_label_create(parent);
if (font) lv_obj_set_style_text_font(l, font, 0);
lv_obj_set_style_text_color(l, col, 0);
if (w) { lv_obj_set_width(l, w); lv_label_set_long_mode(l, mode); }
lv_label_set_text(l, "");
return l;
}
static lv_color_t grey() { return lv_palette_lighten(LV_PALETTE_GREY, 1); }
static lv_obj_t* musicBtn(lv_obj_t* parent, const char* sym, lv_event_cb_t cb, void* ud, int x, int y, int w, int h) {
lv_obj_t* b = lv_btn_create(parent);
lv_obj_set_size(b, w, 40);
lv_obj_align(b, LV_ALIGN_BOTTOM_MID, x, -4);
lv_obj_set_style_radius(b, 20, 0);
lv_obj_set_size(b, w, h);
lv_obj_align(b, LV_ALIGN_BOTTOM_MID, x, y);
lv_obj_set_style_radius(b, h / 2, 0);
lv_obj_t* l = lv_label_create(b);
lv_obj_set_style_text_font(l, &lv_font_montserrat_20, 0);
lv_label_set_text(l, sym);
lv_obj_center(l);
lv_obj_add_event_cb(b, onMusicBtn, LV_EVENT_CLICKED, (void*)act);
lv_obj_add_event_cb(b, cb, LV_EVENT_CLICKED, ud);
return l;
}
static lv_obj_t* progress(lv_obj_t* parent, int y, int w, lv_obj_t** pos, lv_obj_t** len) {
lv_obj_t* bar = lv_bar_create(parent);
lv_obj_set_size(bar, w, 6);
lv_obj_align(bar, LV_ALIGN_TOP_MID, 0, y);
lv_bar_set_range(bar, 0, 1000);
*pos = label(parent, nullptr, grey(), 0, LV_LABEL_LONG_CLIP);
lv_obj_align(*pos, LV_ALIGN_TOP_LEFT, 8, y - 6);
*len = label(parent, nullptr, grey(), 0, LV_LABEL_LONG_CLIP);
lv_obj_align(*len, LV_ALIGN_TOP_RIGHT, -8, y - 6);
return bar;
}
static lv_obj_t* addTile(int col) {
lv_obj_t* t = lv_tileview_add_tile(musicTv, col, 0, (lv_dir_t)((col > 0 ? LV_DIR_LEFT : 0) | (col < 3 ? LV_DIR_RIGHT : 0)));
lv_obj_set_style_pad_all(t, 0, 0);
return t;
}
static void buildMusicUi() {
Preferences p;
p.begin("ui", true);
musicAutoShow = p.getBool("mshow", true);
musicKeepOn = p.getBool("mkeep", MUSIC_KEEP_SCREEN_ON);
p.end();
musicPanel = lv_obj_create(lv_layer_top());
lv_obj_set_size(musicPanel, SCREEN_W, SCREEN_H);
lv_obj_set_pos(musicPanel, 0, 0);
@@ -850,15 +960,146 @@ static void buildMusicUi() {
lv_obj_clear_flag(musicPanel, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_add_flag(musicPanel, LV_OBJ_FLAG_HIDDEN);
mClock = lv_label_create(musicPanel);
lv_obj_set_style_text_font(mClock, &lv_font_montserrat_48, 0);
musicTv = lv_tileview_create(musicPanel);
lv_obj_set_size(musicTv, SCREEN_W, SCREEN_H);
lv_obj_set_style_bg_opa(musicTv, LV_OPA_TRANSP, 0);
lv_obj_set_scrollbar_mode(musicTv, LV_SCROLLBAR_MODE_OFF);
lv_obj_add_event_cb(musicTv, onTile, LV_EVENT_VALUE_CHANGED, nullptr);
// ---- Page 1: clock + what is playing
lv_obj_t* t = addTile(0);
mClock = label(t, &lv_font_montserrat_48, lv_color_white(), 0, LV_LABEL_LONG_CLIP);
lv_label_set_text(mClock, "--:--");
lv_obj_align(mClock, LV_ALIGN_TOP_MID, 0, 4);
mDate = lv_label_create(musicPanel);
lv_obj_set_style_text_color(mDate, lv_palette_lighten(LV_PALETTE_GREY, 1), 0);
lv_label_set_text(mDate, "");
mDate = label(t, nullptr, grey(), 0, LV_LABEL_LONG_CLIP);
lv_obj_align(mDate, LV_ALIGN_TOP_MID, 0, 58);
mSource = label(t, nullptr, lv_palette_main(LV_PALETTE_LIGHT_BLUE), 300, LV_LABEL_LONG_DOT);
lv_obj_set_style_text_align(mSource, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(mSource, LV_ALIGN_TOP_MID, 0, 82);
mTitle = label(t, &lv_font_montserrat_20, lv_color_white(), 300, LV_LABEL_LONG_SCROLL_CIRCULAR);
lv_obj_set_style_text_align(mTitle, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(mTitle, LV_ALIGN_TOP_MID, 0, 100);
mArtist = label(t, nullptr, grey(), 300, LV_LABEL_LONG_DOT);
lv_obj_set_style_text_align(mArtist, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(mArtist, LV_ALIGN_TOP_MID, 0, 126);
mBar = progress(t, 156, 200, &mPos, &mLen);
musicBtn(t, LV_SYMBOL_PREV, onMusicBtn, (void*)"Previous", -80, -16, 64, 40);
mPlayLbl = musicBtn(t, LV_SYMBOL_PAUSE, onMusicBtn, (void*)"PlayPause", 0, -16, 80, 40);
musicBtn(t, LV_SYMBOL_NEXT, onMusicBtn, (void*)"Next", 80, -16, 64, 40);
// ---- Page 2: cover + essential controls
t = addTile(1);
lv_obj_t* frame = lv_obj_create(t);
lv_obj_set_size(frame, KDE_ART_SIZE, KDE_ART_SIZE);
lv_obj_set_pos(frame, 12, 12);
lv_obj_set_style_pad_all(frame, 0, 0);
lv_obj_set_style_border_width(frame, 0, 0);
lv_obj_set_style_radius(frame, 8, 0);
lv_obj_set_style_clip_corner(frame, true, 0);
lv_obj_set_style_bg_color(frame, lv_color_hex(0x1c232b), 0);
lv_obj_clear_flag(frame, LV_OBJ_FLAG_SCROLLABLE);
cArtPh = lv_label_create(frame);
lv_obj_set_style_text_font(cArtPh, &lv_font_montserrat_48, 0);
lv_obj_set_style_text_color(cArtPh, lv_color_hex(0x3a444e), 0);
lv_label_set_text(cArtPh, LV_SYMBOL_AUDIO);
lv_obj_center(cArtPh);
artDsc.header.always_zero = 0;
artDsc.header.cf = LV_IMG_CF_TRUE_COLOR;
artDsc.header.w = KDE_ART_SIZE;
artDsc.header.h = KDE_ART_SIZE;
artDsc.data_size = KDE_ART_SIZE * KDE_ART_SIZE * 2;
artDsc.data = nullptr;
cArt = lv_img_create(frame);
lv_obj_set_pos(cArt, 0, 0);
lv_obj_add_flag(cArt, LV_OBJ_FLAG_HIDDEN);
cTitle = label(t, &lv_font_montserrat_20, lv_color_white(), 166, LV_LABEL_LONG_DOT);
lv_obj_set_height(cTitle, 50);
lv_obj_set_pos(cTitle, 144, 14);
cArtist = label(t, nullptr, grey(), 166, LV_LABEL_LONG_DOT);
lv_obj_set_pos(cArtist, 144, 70);
cAlbum = label(t, nullptr, lv_color_hex(0x7a848e), 166, LV_LABEL_LONG_DOT);
lv_obj_set_pos(cAlbum, 144, 90);
cBar = progress(t, 148, 210, &cPos, &cLen);
musicBtn(t, LV_SYMBOL_PREV, onMusicBtn, (void*)"Previous", -86, -14, 64, 48);
cPlayLbl = musicBtn(t, LV_SYMBOL_PAUSE, onMusicBtn, (void*)"PlayPause", 0, -10, 72, 56);
musicBtn(t, LV_SYMBOL_NEXT, onMusicBtn, (void*)"Next", 86, -14, 64, 48);
// ---- Page 3: volume + position
t = addTile(2);
lv_obj_t* h = label(t, &lv_font_montserrat_20, lv_color_white(), 0, LV_LABEL_LONG_CLIP);
lv_label_set_text(h, LV_SYMBOL_VOLUME_MAX " Lautstaerke");
lv_obj_set_pos(h, 16, 12);
sVolLbl = label(t, &lv_font_montserrat_20, grey(), 0, LV_LABEL_LONG_CLIP);
lv_obj_align(sVolLbl, LV_ALIGN_TOP_RIGHT, -16, 12);
sVol = lv_slider_create(t);
lv_slider_set_range(sVol, 0, 100);
lv_obj_set_size(sVol, 270, 14);
lv_obj_align(sVol, LV_ALIGN_TOP_MID, 0, 50);
lv_obj_set_ext_click_area(sVol, 14);
lv_obj_add_event_cb(sVol, onVolume, LV_EVENT_RELEASED, nullptr);
h = label(t, &lv_font_montserrat_20, lv_color_white(), 0, LV_LABEL_LONG_CLIP);
lv_label_set_text(h, LV_SYMBOL_SHUFFLE " Position");
lv_obj_set_pos(h, 16, 82);
sSeekLbl = label(t, nullptr, grey(), 0, LV_LABEL_LONG_CLIP);
lv_obj_align(sSeekLbl, LV_ALIGN_TOP_RIGHT, -16, 86);
sSeek = lv_slider_create(t);
lv_slider_set_range(sSeek, 0, 1);
lv_obj_set_size(sSeek, 270, 14);
lv_obj_align(sSeek, LV_ALIGN_TOP_MID, 0, 120);
lv_obj_set_ext_click_area(sSeek, 14);
lv_obj_add_event_cb(sSeek, onSeekSlider, LV_EVENT_RELEASED, nullptr);
musicBtn(t, "-10s", onJump, (void*)(intptr_t)-10000, -96, -16, 76, 44);
sPlayLbl = musicBtn(t, LV_SYMBOL_PAUSE, onMusicBtn, (void*)"PlayPause", 0, -16, 76, 44);
musicBtn(t, "+30s", onJump, (void*)(intptr_t)30000, 96, -16, 76, 44);
// ---- Page 4: sources + settings
t = addTile(3);
lv_obj_set_flex_flow(t, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_all(t, 8, 0);
lv_obj_set_style_pad_bottom(t, 16, 0);
lv_obj_set_style_pad_row(t, 6, 0);
h = label(t, &lv_font_montserrat_20, lv_color_white(), 0, LV_LABEL_LONG_CLIP);
lv_label_set_text(h, "Quelle");
qAuto = lv_btn_create(t);
lv_obj_set_size(qAuto, 260, 36);
lv_obj_t* al = lv_label_create(qAuto);
lv_label_set_text(al, LV_SYMBOL_REFRESH " Automatisch (zuletzt gestartet)");
lv_obj_align(al, LV_ALIGN_LEFT_MID, 0, 0);
lv_obj_add_event_cb(qAuto, onSource, LV_EVENT_CLICKED, (void*)(intptr_t)-1);
qList = lv_obj_create(t);
lv_obj_set_size(qList, lv_pct(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(qList, 0, 0);
lv_obj_set_style_pad_row(qList, 6, 0);
lv_obj_set_style_bg_opa(qList, LV_OPA_TRANSP, 0);
lv_obj_set_style_border_width(qList, 0, 0);
lv_obj_set_flex_flow(qList, LV_FLEX_FLOW_COLUMN);
lv_obj_clear_flag(qList, LV_OBJ_FLAG_SCROLLABLE);
h = label(t, &lv_font_montserrat_20, lv_color_white(), 0, LV_LABEL_LONG_CLIP);
lv_label_set_text(h, "Einstellungen");
auto settingRow = [&](const char* txt, bool on) {
lv_obj_t* row = lv_obj_create(t);
lv_obj_set_size(row, lv_pct(100), 44);
lv_obj_set_style_pad_hor(row, 8, 0);
lv_obj_set_style_pad_ver(row, 0, 0);
lv_obj_clear_flag(row, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_t* l = label(row, nullptr, lv_color_white(), 210, LV_LABEL_LONG_WRAP);
lv_label_set_text(l, txt);
lv_obj_align(l, LV_ALIGN_LEFT_MID, 0, 0);
lv_obj_t* sw = lv_switch_create(row);
lv_obj_set_size(sw, 50, 26);
lv_obj_align(sw, LV_ALIGN_RIGHT_MID, 0, 0);
if (on) lv_obj_add_state(sw, LV_STATE_CHECKED);
lv_obj_add_event_cb(sw, onSetting, LV_EVENT_VALUE_CHANGED, nullptr);
return sw;
};
qSwShow = settingRow("Bei Musik automatisch einblenden", musicAutoShow);
qSwKeep = settingRow("Display bleibt an, solange Musik laeuft", musicKeepOn);
// ---- Close button + page dots on top of all pages
lv_obj_t* close = lv_btn_create(musicPanel);
lv_obj_set_size(close, 34, 34);
lv_obj_align(close, LV_ALIGN_TOP_RIGHT, -4, 4);
@@ -867,42 +1108,15 @@ static void buildMusicUi() {
lv_label_set_text(cl, LV_SYMBOL_CLOSE);
lv_obj_center(cl);
lv_obj_add_event_cb(close, onMusicBtn, LV_EVENT_CLICKED, nullptr);
mSource = lv_label_create(musicPanel);
lv_obj_set_style_text_color(mSource, lv_palette_main(LV_PALETTE_LIGHT_BLUE), 0);
lv_label_set_long_mode(mSource, LV_LABEL_LONG_DOT);
lv_obj_set_width(mSource, 300);
lv_obj_set_style_text_align(mSource, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(mSource, LV_ALIGN_TOP_MID, 0, 84);
mTitle = lv_label_create(musicPanel);
lv_obj_set_style_text_font(mTitle, &lv_font_montserrat_20, 0);
lv_label_set_long_mode(mTitle, LV_LABEL_LONG_SCROLL_CIRCULAR);
lv_obj_set_width(mTitle, 300);
lv_obj_set_style_text_align(mTitle, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(mTitle, LV_ALIGN_TOP_MID, 0, 104);
mArtist = lv_label_create(musicPanel);
lv_obj_set_style_text_color(mArtist, lv_palette_lighten(LV_PALETTE_GREY, 1), 0);
lv_label_set_long_mode(mArtist, LV_LABEL_LONG_DOT);
lv_obj_set_width(mArtist, 300);
lv_obj_set_style_text_align(mArtist, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(mArtist, LV_ALIGN_TOP_MID, 0, 132);
mBar = lv_bar_create(musicPanel);
lv_obj_set_size(mBar, 200, 6);
lv_obj_align(mBar, LV_ALIGN_TOP_MID, 0, 164);
lv_bar_set_range(mBar, 0, 1000);
mPos = lv_label_create(musicPanel);
lv_obj_set_style_text_color(mPos, lv_palette_lighten(LV_PALETTE_GREY, 1), 0);
lv_obj_align(mPos, LV_ALIGN_TOP_LEFT, 10, 158);
mLen = lv_label_create(musicPanel);
lv_obj_set_style_text_color(mLen, lv_palette_lighten(LV_PALETTE_GREY, 1), 0);
lv_obj_align(mLen, LV_ALIGN_TOP_RIGHT, -10, 158);
musicBtn(musicPanel, LV_SYMBOL_PREV, "Previous", -80, 64);
mPlayLbl = musicBtn(musicPanel, LV_SYMBOL_PAUSE, "PlayPause", 0, 80);
musicBtn(musicPanel, LV_SYMBOL_NEXT, "Next", 80, 64);
for (int i = 0; i < 4; i++) {
pageDots[i] = lv_obj_create(musicPanel);
lv_obj_set_size(pageDots[i], 6, 6);
lv_obj_set_style_radius(pageDots[i], 3, 0);
lv_obj_set_style_border_width(pageDots[i], 0, 0);
lv_obj_clear_flag(pageDots[i], (lv_obj_flag_t)(LV_OBJ_FLAG_SCROLLABLE | LV_OBJ_FLAG_CLICKABLE));
lv_obj_align(pageDots[i], LV_ALIGN_BOTTOM_MID, -15 + i * 10, -3);
}
onTile(nullptr);
lv_obj_add_flag(clockLabel, LV_OBJ_FLAG_CLICKABLE);
lv_obj_set_ext_click_area(clockLabel, 12);
@@ -922,6 +1136,55 @@ static void onPairBox(lv_event_t* e) {
pairBox = nullptr;
}
static void setProgress(lv_obj_t* bar, lv_obj_t* pl, lv_obj_t* ll, int32_t pos) {
if (np.lenMs > 0) {
char a[12], b[12];
fmtTime(a, sizeof a, pos);
fmtTime(b, sizeof b, np.lenMs);
lv_label_set_text(pl, a);
lv_label_set_text(ll, b);
lv_bar_set_value(bar, (int64_t)pos * 1000 / np.lenMs, LV_ANIM_OFF);
lv_obj_clear_flag(bar, LV_OBJ_FLAG_HIDDEN);
} else {
lv_label_set_text(pl, "");
lv_label_set_text(ll, "");
lv_obj_add_flag(bar, LV_OBJ_FLAG_HIDDEN);
}
}
// Rebuilds the source list on page 4 when the set of players changes.
static void updateSources() {
PlayerEntry e[12];
int n = kdeListPlayers(e, 12);
bool same = n == nQEntries;
for (int i = 0; same && i < n; i++)
same = !strcmp(e[i].key, qEntries[i].key) && e[i].playing == qEntries[i].playing &&
e[i].selected == qEntries[i].selected && !strcmp(e[i].title, qEntries[i].title);
if (same) return;
memcpy(qEntries, e, sizeof(PlayerEntry) * n);
nQEntries = n;
lv_obj_clean(qList);
bool anySel = false;
for (int i = 0; i < n; i++) {
anySel |= e[i].selected;
lv_obj_t* b = lv_btn_create(qList);
lv_obj_set_size(b, lv_pct(100), LV_SIZE_CONTENT);
lv_obj_set_style_pad_all(b, 6, 0);
lv_obj_set_style_bg_color(b, e[i].selected ? lv_palette_main(LV_PALETTE_BLUE) : lv_color_hex(0x2a323b), 0);
lv_obj_t* l = lv_label_create(b);
lv_obj_set_width(l, lv_pct(100));
lv_label_set_long_mode(l, LV_LABEL_LONG_DOT);
lv_label_set_text_fmt(l, "%s %s - %s\n%s", e[i].playing ? LV_SYMBOL_PLAY : LV_SYMBOL_PAUSE, e[i].player,
e[i].device, e[i].title[0] ? e[i].title : "(nichts)");
lv_obj_add_event_cb(b, onSource, LV_EVENT_CLICKED, (void*)(intptr_t)i);
}
if (!n) {
lv_obj_t* l = label(qList, nullptr, grey(), 280, LV_LABEL_LONG_WRAP);
lv_label_set_text(l, "Kein gekoppeltes Geraet verbunden");
}
lv_obj_set_style_bg_color(qAuto, anySel ? lv_color_hex(0x2a323b) : lv_palette_main(LV_PALETTE_BLUE), 0);
}
static void updateMusic() {
static const char* DAYS[] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
static const char* MONTHS[] = {"Januar", "Februar", "Maerz", "April", "Mai", "Juni", "Juli",
@@ -947,7 +1210,7 @@ static void updateMusic() {
if (!req) pairShown = 0;
// Now playing
static uint32_t lastSeq = 0;
static uint32_t lastSeq = 0, lastArt = 0;
static String lastTitle;
static bool lastPlaying = false;
static uint32_t stoppedAt = 0;
@@ -956,15 +1219,36 @@ static void updateMusic() {
lastSeq = np.seq;
bool newTrack = np.valid && np.playing && (!lastPlaying || lastTitle != np.title);
if (np.valid) {
const char* pin = np.pinned ? " " LV_SYMBOL_EYE_OPEN : "";
if (np.player[0] && strcmp(np.player, np.device))
lv_label_set_text_fmt(mSource, LV_SYMBOL_AUDIO " %s - %s", np.player, np.device);
lv_label_set_text_fmt(mSource, LV_SYMBOL_AUDIO " %s - %s%s", np.player, np.device, pin);
else
lv_label_set_text_fmt(mSource, LV_SYMBOL_AUDIO " %s", np.device);
lv_label_set_text_fmt(mSource, LV_SYMBOL_AUDIO " %s%s", np.device, pin);
lv_label_set_text(mTitle, np.title);
lv_label_set_text(mArtist, np.artist);
lv_label_set_text(mPlayLbl, np.playing ? LV_SYMBOL_PAUSE : LV_SYMBOL_PLAY);
lv_label_set_text(cTitle, np.title);
lv_label_set_text(cArtist, np.artist);
lv_label_set_text(cAlbum, np.album);
const char* ps = np.playing ? LV_SYMBOL_PAUSE : LV_SYMBOL_PLAY;
lv_label_set_text(mPlayLbl, ps);
lv_label_set_text(cPlayLbl, ps);
lv_label_set_text(sPlayLbl, ps);
if (np.volume >= 0) {
lv_obj_clear_state(sVol, LV_STATE_DISABLED);
if (!lv_slider_is_dragged(sVol)) lv_slider_set_value(sVol, np.volume, LV_ANIM_OFF);
lv_label_set_text_fmt(sVolLbl, "%d %%", np.volume);
} else {
lv_obj_add_state(sVol, LV_STATE_DISABLED);
lv_label_set_text(sVolLbl, "-");
}
if (np.canSeek && np.lenMs > 0) {
lv_obj_clear_state(sSeek, LV_STATE_DISABLED);
lv_slider_set_range(sSeek, 0, max<int32_t>(1, np.lenMs / 1000));
} else {
lv_obj_add_state(sSeek, LV_STATE_DISABLED);
}
}
if (newTrack) {
if (newTrack && musicAutoShow) {
showMusic(true);
wakeScreen();
}
@@ -975,6 +1259,21 @@ static void updateMusic() {
lastPlaying = np.valid && np.playing;
lastTitle = np.title;
}
if (np.artSeq != lastArt) {
lastArt = np.artSeq;
const uint16_t* art = kdeArt();
if (art) {
artDsc.data = (const uint8_t*)art;
lv_img_cache_invalidate_src(&artDsc);
lv_img_set_src(cArt, &artDsc);
lv_obj_invalidate(cArt);
lv_obj_clear_flag(cArt, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_flag(cArtPh, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_add_flag(cArt, LV_OBJ_FLAG_HIDDEN);
lv_obj_clear_flag(cArtPh, LV_OBJ_FLAG_HIDDEN);
}
}
if (musicVisible && (!np.valid || (!np.playing && millis() - stoppedAt > MUSIC_HIDE_S * 1000UL)))
showMusic(false);
@@ -991,21 +1290,16 @@ static void updateMusic() {
}
}
if (musicVisible) {
int32_t pos = np.posMs + (np.playing ? (int32_t)(millis() - np.atMs) : 0);
if (np.lenMs > 0) {
pos = min(pos, np.lenMs);
char a[12], b[12];
fmtTime(a, sizeof a, pos);
fmtTime(b, sizeof b, np.lenMs);
lv_label_set_text(mPos, a);
lv_label_set_text(mLen, b);
lv_bar_set_value(mBar, (int64_t)pos * 1000 / np.lenMs, LV_ANIM_OFF);
lv_obj_clear_flag(mBar, LV_OBJ_FLAG_HIDDEN);
} else {
lv_label_set_text(mPos, "");
lv_label_set_text(mLen, "");
lv_obj_add_flag(mBar, LV_OBJ_FLAG_HIDDEN);
}
int32_t pos = curPos();
setProgress(mBar, mPos, mLen, pos);
setProgress(cBar, cPos, cLen, pos);
if (!lv_slider_is_dragged(sSeek)) lv_slider_set_value(sSeek, pos / 1000, LV_ANIM_OFF);
char a[12], b[12];
fmtTime(a, sizeof a, lv_slider_is_dragged(sSeek) ? lv_slider_get_value(sSeek) * 1000 : pos);
fmtTime(b, sizeof b, np.lenMs);
if (np.lenMs > 0) lv_label_set_text_fmt(sSeekLbl, "%s / %s", a, b);
else lv_label_set_text(sSeekLbl, "");
updateSources();
}
}
@@ -1073,7 +1367,7 @@ void loop() {
}
// Backlight timeout
bool keepOn = MUSIC_KEEP_SCREEN_ON && musicVisible && np.playing;
bool keepOn = musicKeepOn && musicVisible && np.playing;
if (SCREEN_TIMEOUT_S > 0 && !screenOff && !keepOn && lv_disp_get_inactive_time(nullptr) > SCREEN_TIMEOUT_S * 1000UL)
setBacklight(false);
@@ -1131,7 +1425,9 @@ void loop() {
// Background jobs; the blocking HTTP calls only run while the user is not touching the screen.
if (screenOff || lv_disp_get_inactive_time(nullptr) > 700) {
static uint32_t lastAlert = 0, lastWeather = 0;
if (!lastAlert || now - lastAlert > 30000) { lastAlert = now ? now : 1; pollAlerts(); }
static uint32_t lastTz = 0;
if (!lastTz || now - lastTz > 900000) { lastTz = now ? now : 1; syncTimezone(); }
else if (!lastAlert || now - lastAlert > 30000) { lastAlert = now ? now : 1; pollAlerts(); }
else if (!lastWeather || now - lastWeather > 600000) { lastWeather = now ? now : 1; pollWeather(); }
}
static uint32_t lastHist = 0;