From 8dca7236ca123bff1bbfdb57e92d729acc243c37 Mon Sep 17 00:00:00 2001 From: akasaka Date: Sat, 14 Dec 2024 10:47:04 +0900 Subject: [PATCH] Show track number on Fb2k screen --- include/service/foo_client.h | 2 ++ include/views/idle_screens/fb2k.h | 3 +- src/network/admin_panel.cpp | 2 +- src/service/foo_client.cpp | 54 +++++++++++++++++++++++-------- src/views/idle_screens/fb2k.cpp | 26 ++++++++++++--- 5 files changed, 65 insertions(+), 22 deletions(-) diff --git a/include/service/foo_client.h b/include/service/foo_client.h index 07ef9e2..3ba28e1 100644 --- a/include/service/foo_client.h +++ b/include/service/foo_client.h @@ -12,4 +12,6 @@ void foo_client_begin(); bool foo_is_playing(); void foo_get_title(char *, size_t); void foo_get_artist(char *, size_t); +int foo_get_track_number(); + TickType_t foo_last_recv(); diff --git a/include/views/idle_screens/fb2k.h b/include/views/idle_screens/fb2k.h index 031e52c..156e846 100644 --- a/include/views/idle_screens/fb2k.h +++ b/include/views/idle_screens/fb2k.h @@ -13,10 +13,9 @@ class Fb2kView: public Screen { private: void update_if_needed(); const font_definition_t * font; - char artist_buffer[128]; - char title_buffer[128]; TickType_t last_update; StringScroll * top_line; StringScroll * bottom_line; + StringScroll * track_nr; SpriteView * icon; }; \ No newline at end of file diff --git a/src/network/admin_panel.cpp b/src/network/admin_panel.cpp index 20d9bcc..586efb8 100644 --- a/src/network/admin_panel.cpp +++ b/src/network/admin_panel.cpp @@ -520,7 +520,7 @@ static void build() { GP.SPOILER_BEGIN("Foobar2000", GP_BLUE); render_string("Control Server IP", PREFS_KEY_FOOBAR_SERVER); render_int("Control Server Port:", PREFS_KEY_FOOBAR_PORT); - GP.SPAN("Please set the format in foo_controlserver to: %artist%|%title%, and main delimiter to: |"); + GP.SPAN("Please set the format in foo_controlserver to:
%track number%|%artist%|%title%
, and main delimiter to: |"); GP.SPOILER_END(); GP.BREAK(); diff --git a/src/service/foo_client.cpp b/src/service/foo_client.cpp index ab5aa09..172c945 100644 --- a/src/service/foo_client.cpp +++ b/src/service/foo_client.cpp @@ -26,6 +26,7 @@ static char recv_buf[RECV_BUF_SIZE]; static char title_buf[TEXT_BUF_SIZE]; static char artist_buf[TEXT_BUF_SIZE]; +static int track_no = -1; static bool play_sts = false; static SemaphoreHandle_t sts_semaphore; static TickType_t last_recv = 0; @@ -65,6 +66,10 @@ void foo_get_artist(char * buf, size_t buf_size) { xSemaphoreGive(sts_semaphore); } +int foo_get_track_number() { + return track_no; +} + TickType_t foo_last_recv() { return last_recv; } @@ -115,24 +120,45 @@ void parse_line(char * line) { dummy = parse_field(¤t); dummy = parse_field(¤t); - char * artist = parse_field(¤t); - strncpy(artist_buf, artist, TEXT_BUF_SIZE); + if(dummy != nullptr) { + char * trkno_ascii = parse_field(¤t); + if(trkno_ascii != nullptr) { + if(trkno_ascii[0] == '?') { + track_no = -1; // Not known track number + } else { + if(sscanf(trkno_ascii, "%d", &track_no) != 1) { + ESP_LOGI(LOG_TAG, "TrkNr parse failed: %s", trkno_ascii); + track_no = -1; + } + } - // Cannot use parse_field here in case the track name might contain the separator character - char * txt = current; - size_t txt_len = strlen(txt); - txt[txt_len - 1] = 0; + char * artist = parse_field(¤t); + if(artist != nullptr) { + strncpy(artist_buf, artist, TEXT_BUF_SIZE); - if(!xSemaphoreTake(sts_semaphore, pdMS_TO_TICKS(1000))) { - ESP_LOGE(LOG_TAG, "Semaphore timed out"); - return; - } + // Cannot use parse_field here in case the track name might contain the separator character + char * txt = current; + size_t txt_len = strlen(txt); + txt[txt_len - 1] = 0; - play_sts = (type == 111); - strncpy(title_buf, txt, TEXT_BUF_SIZE); - ESP_LOGI(LOG_TAG, "Track: %s, Artist: %s, Play_sts: %i", title_buf, artist_buf, play_sts); - last_recv = xTaskGetTickCount(); + if(!xSemaphoreTake(sts_semaphore, pdMS_TO_TICKS(1000))) { + ESP_LOGE(LOG_TAG, "Semaphore timed out"); + return; + } + play_sts = (type == 111); + strncpy(title_buf, txt, TEXT_BUF_SIZE); + ESP_LOGI(LOG_TAG, "Track Nr.%i: %s, Artist: %s, Play_sts: %i", track_no, title_buf, artist_buf, play_sts); + last_recv = xTaskGetTickCount(); + } else { + ESP_LOGE(LOG_TAG, "Could not read artist: unexpected end of message"); + } + } else { + ESP_LOGE(LOG_TAG, "Could not read TrkNr: unexpected end of message"); + } + } else { + ESP_LOGE(LOG_TAG, "Could not read metadata: unexpected end of message"); + } xSemaphoreGive(sts_semaphore); } diff --git a/src/views/idle_screens/fb2k.cpp b/src/views/idle_screens/fb2k.cpp index 09fced3..fb189ba 100644 --- a/src/views/idle_screens/fb2k.cpp +++ b/src/views/idle_screens/fb2k.cpp @@ -9,27 +9,30 @@ static char LOG_TAG[] = "FOO"; Fb2kView::Fb2kView() { font = find_font(FONT_STYLE_UI_TEXT); - memset(artist_buffer, 0, 128); - memset(title_buffer, 0, 128); last_update = 0; bottom_line = new StringScroll(font); bottom_line->x_offset = 17; bottom_line->set_y_position(8); - bottom_line->set_string(title_buffer); add_composable(bottom_line); top_line = new StringScroll(font); top_line->x_offset = 17; top_line->set_y_position(0); - top_line->set_string(artist_buffer); add_composable(top_line); icon = new SpriteView(&music_icns); icon->width = 16; add_composable(icon); + track_nr = new StringScroll(find_font(FONT_STYLE_HUD_DIGITS)); + track_nr->render_mode = TEXT_OUTLINED | TEXT_NO_BACKGROUND; + track_nr->stopped = true; + track_nr->set_y_position(10); + track_nr->hidden = true; + add_composable(track_nr); + top_line->holdoff = 50; bottom_line->holdoff = 50; @@ -43,12 +46,25 @@ void Fb2kView::update_if_needed() { if(ts != last_update) { last_update = ts; + char artist_buffer[128] = { 0 }; + char title_buffer[128] = { 0 }; foo_get_artist(artist_buffer, 128); foo_get_title(title_buffer, 128); + int trk_no = foo_get_track_number(); - ESP_LOGI(LOG_TAG, "New now playing info: %s - %s", artist_buffer, title_buffer); + ESP_LOGI(LOG_TAG, "New now playing info: %i: %s - %s", trk_no, artist_buffer, title_buffer); top_line->set_string(artist_buffer); bottom_line->set_string(title_buffer); + if(trk_no <= 0) { + track_nr->hidden = true; + } else { + track_nr->hidden = false; + char tnbuf[4] = { 0 }; + snprintf(tnbuf, 4, "%02d", trk_no); + tnbuf[3] = 0; + track_nr->set_string(tnbuf); + track_nr->x_offset = icon->x_offset + icon->width - track_nr->string_width + 1; + } int width = std::max(top_line->string_width, bottom_line->string_width); top_line->string_width = width;