Displaying song metadata on OLED screen

how do i display the song metadata on the oled screen?
I found a library for turning my esp32 into a bluetooth receiver, i have made good progress on it and it is working amazingly. I have added a oled screen to it, and figured out how to make text scroll (song titles can be long, this makes it so the full title and song author can be displayed on the display.)

The code i currently got is:

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "BluetoothA2DPSink.h"
#define SCREEN_WIDTH 128 // OLED display width in pixels
#define SCREEN_HEIGHT 32 // OLED display height in pixels
#define OLED_RESET     4 // Reset pin # 
#define SCREEN_ADDRESS 0x3C // 0x3D for 128x64, 0x3C for 128x32 (some x64 models also use 0x3C)
unsigned long minutes = 60000; //60k ms -> 1 minute
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
BluetoothA2DPSink a2dp_sink;

bool is_active = true;

void data_received_callback() {
  Serial.println("Data packet received");
}
void avrc_metadata_callback(uint8_t data1, const uint8_t *data2) {
  Serial.printf("0x%x, %s\n", data1, data2);
}

char message[] = "X";   //THIS IS WHAT I CAN'T FIGURE OUT
int x, minX;

void setup() {
  pinMode(2, OUTPUT);
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setTextWrap(false);
  x = display.width();
  minX = -12 * strlen(message);  // 12 = 6 pixels/character * text size 2

  Serial.begin(115200);
  i2s_pin_config_t my_pin_config = {
    .bck_io_num = 26,      //BCK -> IO26
    .ws_io_num = 25,       //LCK / LRCK -> IO25
    .data_out_num = 18,    //DIN - > IO18
    .data_in_num = I2S_PIN_NO_CHANGE
  };
  a2dp_sink.set_pin_config(my_pin_config);
  a2dp_sink.set_avrc_metadata_callback(avrc_metadata_callback);
  a2dp_sink.start("Bluetooth :)");
}

void loop() {
  digitalWrite(2, (millis() / 400) % 2);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.setTextSize(1);
  display.print("currently playing:"); // top row will say.. x
  display.setTextSize(2);
  display.setCursor(-x, 10); // (x,10) -> right to left (-x,10) left to right
  display.print(message); //scrolling message (song metadata)
  display.display();
  x = x - 1; // scroll speed, to increase change x=x-2; to x=x-1 // to increase change x=x-1; to x=x-2 for example
  if (x < minX) x = display.width();

}

The song metadata shows up properly on the serial monitor,
It shows this ,for example:
0x1, SONG TITLE
0x2, SONG AUTHOR

So i need to bring that data over into:

char message[] = "X";   

I have no idea on how to do this, hopefully someone can break my mind loose on how to approach this.
I have tried scrapping everything and starting again just trying to show the metadata on the screen, but i also can't figure that out. Especially trying to show all the text on the screen without scrolling, so the scrolling is a neccesity.

Greets, Arik Nel
-Belgium

If

  Serial.printf("0x%x, %s\n", data1, data2);

prints the metadata on the Serial monitor then data2 holds the metadata and you could, therefore, print it directly on the OLED

If you want to copy it to the message array, which will need to be declared large enough to hold it, then look at the strcpy() function that allows you to copy one string to another

Thanks UKHeliBob, i have searched it up and it is making sense. I will try it and notify if i run into any problems here.
Greets

Hi there, i have found some examples online but i don't understand it fully.

/* strcpy example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str1[]="Sample string";
  char str2[40];
  char str3[40];
  strcpy (str2,str1);
  strcpy (str3,"copy successful");
  printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
  return 0;
}

This is what i found on the cplusplus website,
But i can't figure out how to bring over the string for the metadata in the char properly in my code.
It will be great if you could clear it up a bit more for me, my english is not 100% so my excuse if you don't understand what i am trying to say
Once again thanks for your help

Take a look at this Arduino example of using strcpy()

/* strcpy example */

void setup()
{
  Serial.begin(115200);
  char message[100] = ""; //space for plenty of characters
  char *data = "Song title";
  Serial.print("message at start = ");
  Serial.println(message);
  strcpy(message, data);  //copy the string in data to the message array
  Serial.print("message after copy = ");
  Serial.println(message);
}

void loop()
{
}

Okay thanks, this cleared it up. Now how do i get the correct string to be copied?
The library i used calls the metadata up using this:

}
void avrc_metadata_callback(uint8_t data1, const uint8_t *data2) {
  Serial.printf("0x%x, %s\n", data1, data2);
}

When you print data1 and data2 to the Serial monitor you say that you see what you are looking to put on the OLED. So you have the metadata in those 2 variables and it can be either copied to message or used as is with the names data1 and data2, perhaps passed to a function to do the actual output to the OLED

Hi, i understand that. It works fine, but i have no idea how to implement strcpy to copy the string. Some clarification on that would be awesome.

The example in reply #5 shows you how. You already get the metadata in 2 variables, data1 and data2 to print it, and can copy it to massage if that is what you want to do

Sorry for bothering once again, but i really can't figure it out using strcpy.
I have no idea how to place it in my code, every way i try that makes sense gives out errors

When the avrc_metadata_callback() function is called it has 2 parameters, data1 and data2 which you print to the Serial monitor. Suppose that you copied data2 into the message array in that function, then you could use it to display the metadata on the OLED in loop()

There is, however, a problem if the message array is not available for the copy to happen in the function, so, declare message as a 30 character array, but with no definition of what it contains, at the start of the sketch, like this

char message[30];

it will then be available anywhere in the sketch which will allow you to copy data2 to it in the avrc_metadata_callback() function

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.