Skip to content

Commit

Permalink
Show track number on Fb2k screen
Browse files Browse the repository at this point in the history
  • Loading branch information
akasaka committed Dec 14, 2024
1 parent fb0bd45 commit 8dca723
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 22 deletions.
2 changes: 2 additions & 0 deletions include/service/foo_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
3 changes: 1 addition & 2 deletions include/views/idle_screens/fb2k.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
2 changes: 1 addition & 1 deletion src/network/admin_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: <pre>%track number%|%artist%|%title%</pre>, and main delimiter to: |");
GP.SPOILER_END();
GP.BREAK();

Expand Down
54 changes: 40 additions & 14 deletions src/service/foo_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -115,24 +120,45 @@ void parse_line(char * line) {
dummy = parse_field(&current);
dummy = parse_field(&current);

char * artist = parse_field(&current);
strncpy(artist_buf, artist, TEXT_BUF_SIZE);
if(dummy != nullptr) {
char * trkno_ascii = parse_field(&current);
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(&current);
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);
}

Expand Down
26 changes: 21 additions & 5 deletions src/views/idle_screens/fb2k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit 8dca723

Please sign in to comment.