Hi,
I start by saying "Sorry" and that's for the lack of knowledge how you use forums to ask for help.
I know that I should precent my code in a certain way, but I'm an old fart, and don't get this advanced internet things..
New to this type of programming and should call my self a "Copy & Paste" programmer. I have only used Basic, VB and some Python before I got started with the ESP32.
Using Mr. Pschatzmann's ESP32-A2DP library, ( https://github.com/pschatzmann/ESP32-A2DP )
it's very easy to use the examples and I got a working "Bluetooth" streamer playing to my SPDIF input on the stereo direct from the ESP32.
Now to my problem...
I added a small LCD display (4x20) to show the Song, Artist and Album information.
And going trough the code and using the terminal for debug/check, I found that I could also access the time off the track.
My problem is that I can call the data and I can print it out in Serial as text with "Serial.printf("%s\n", data);" but I need to use the data in an calculation, how do I extract/convert it to an int, or char array?
The call to get the data is this:
void avrc_metadata_callback(uint8_t data1, const uint8_t *data2) {
...
//I can use the switch command and look at the "data1" and from that ID I can select what I like to send to the LCD.
//But to use this on the LCD (or in the serial terminal) i need to use this:
switch (data1) {
case 1: // Print the song name
//Serial.printf("Song: 0x%x, %s\n", data1, data2);
Serial.print("Song: ");
Serial.printf("%s\n", data2);
break;
case 2: // Print the Artist name
//Serial.printf("Artist: 0x%x, %s\n", data1, data2);
Serial.print("Artist: ");
Serial.printf("%s\n", data2);
break;
case 4: // Print the Album name
//Serial.printf("Album 0x%x, %s\n", data1, data2);
Serial.print("Album: ");
Serial.printf("%s\n", data2);
break;
case 0x40: // Print the song length.
//Serial.printf("Time: 0x%x, %s\n", data1, data2);
Serial.print("Time: ");
Serial.printf("%s\n", data2);
break;
default:
break;
}
}
So the thing I can't get around is how to use this ( Serial.printf("%s\n", data2); ) info in the data2 and get that into a variable that I can use in calculations...
I have spent several days trying the help files, checking webpages and so on...
Please... can anyone get me to understand this...
Best regards,
Niklas.
ESP32-A2DP Problem understanding the use of 'const uint8_t*'
-
- Posts: 94
- Joined: Tue Sep 07, 2021 12:07 pm
Re: ESP32-A2DP Problem understanding the use of 'const uint8_t*'
You wrote this callback:
and the library calls it like this:
data1 appears to describe what data2 points to.
data2 points to a string (most likely, I didn't look too deep) of uint8 that are const, which means your code should not try to write to them.
You should probably copy that data2 to your own buffer if you don't use it during the callback (it may not be valid after the callback.) You'll need to use strlen(data2)+1 to malloc the buffer (and be sure to free the buffer when done) and strcpy to copy the buffer. Then print from your malloc'ed copy.
It sounds like the data might be a string representation of a time, maybe something like 3:24 for 204 seconds (again, guesswork on my part.) You need to parse through the format and do the conversions. Use uint8_t *p = buffer; then use *p to get a digit and p++ to the next character. test for *p==':' to change from parsing minutes to seconds. Build the minutes and seconds separately, then combine 60*minutes+seconds.
You can avoid the malloc/copy/free if you capture the seconds as an int during the callback. I'm assuming you want to calculate something like a progress bar, and 204 seconds total duration is what you need.
Code: Select all
void avrc_metadata_callback(uint8_t data1, const uint8_t *data2) {
Code: Select all
avrc_metadata_callback(rc->meta_rsp.attr_id, rc->meta_rsp.attr_text);
data2 points to a string (most likely, I didn't look too deep) of uint8 that are const, which means your code should not try to write to them.
You should probably copy that data2 to your own buffer if you don't use it during the callback (it may not be valid after the callback.) You'll need to use strlen(data2)+1 to malloc the buffer (and be sure to free the buffer when done) and strcpy to copy the buffer. Then print from your malloc'ed copy.
It sounds like the data might be a string representation of a time, maybe something like 3:24 for 204 seconds (again, guesswork on my part.) You need to parse through the format and do the conversions. Use uint8_t *p = buffer; then use *p to get a digit and p++ to the next character. test for *p==':' to change from parsing minutes to seconds. Build the minutes and seconds separately, then combine 60*minutes+seconds.
You can avoid the malloc/copy/free if you capture the seconds as an int during the callback. I'm assuming you want to calculate something like a progress bar, and 204 seconds total duration is what you need.
Craige
Who is online
Users browsing this forum: No registered users and 79 guests