How to pause and resume song using Mbed code

In MbedOS 6+ is wait(1) function replaced with thread_sleep_for(1000) so

thread_sleep_for((0.8*interval[i]/10)*1000); 
// same like
wait((0.8*interval[i]/10)); 

and it is a calculation how long the tone have to play.

BR, Jan

@JohnnyK Do u have any idea like how we do next or previous button to control the song index?

It’s just a movement through a matrix tables. So you just increase and decrease song index.

// for next
  song_index++; //  increase song index
  note = 0; // reset note idex
  beat  = 0; // reset beat idex

// for previous
  song_index--; //  decrease song index
  note  = 0; // reset note idex
  beat  = 0; // reset beat idex

BR, Jan

@JohnnyK Thanks for the information. In addition, do u have any idea like how we place thread (Rtos) for lcd display inside udf? I saw mostly they place the thread at int main() but how do we know when it can run inside the udf, if i wan place the thread inside our udf for display like pause/resume , stop , play and so on.

For example: display the thread in my pause loop

With UDF you mean User Defined Function I suppose, but I am not sure I understand.
You want something like this?

void play(){
   // a code for plaing a song
   // Start a thread what will call a code for show PLAY string with a NAME of song on the display
}

BR, Jan

Thanks for the information . I want to ask inside ISR can use if else statement?

Of course, you can use what do you want inside ISR, but you want to have it short as possible and nothing complicated. So you want to avoid use printf, delays, read a peripherals (UART, SPI, I2C…) and so on.
Usually is good to use a bool flag, same like with buttonPlayPress above, and process it in a thread or take it out of ISR context via EventQueue - API references and tutorials | Mbed OS 6 Documentation (first example)

BR, Jan

Thanks for the information. Do mbed have goto function?

That has nothing together with Mbed, it is a feature of programing languages, in this case C++

goto statement - cppreference.com

BR, Jan

hi @JohnnyK , for mbed rtos thread cannot use goto statement?

From my point of view you can use it every you want but it must be use very carefully.
What is the reason of usage? I think it is possible to it without that.

BR, Jan

Mbed itself doesn’t directly interact with audio playback functionalities. However, you can achieve a similar effect for controlling song or ringtones playback using Mbed for user input and potentially other functionalities. Here’s a general approach:

1. User Input:

  • Use an InterruptIn object connected to a button on your Mbed board.
  • Define a function triggered by the button press interrupt.

2. State Variable:

  • Create a boolean variable (e.g., isPlaying) to track the playback state (playing or paused).

3. Button Press Function:

  • In the interrupt function triggered by the button press, check the current playback state.
    • If isPlaying is true (playing):
      • Update isPlaying to false (paused).
      • (Functionality Specific): Implement logic to pause the audio playback based on your chosen audio playback method (explained later).
    • If isPlaying is false (paused):
      • Update isPlaying to true (playing).
      • (Functionality Specific): Implement logic to resume the audio playback.

4. Main Loop:

  • In your main loop, you can display the current playback state (playing/paused) or perform other actions based on the state.
    Example code
    #include “mbed.h”

InterruptIn button(BUTTON1);
bool isPlaying = false;

void buttonPress() {
if (isPlaying) {
isPlaying = false;
// Implement logic to pause audio playback (replace with your specific method)
} else {
isPlaying = true;
// Implement logic to resume audio playback (replace with your specific method)
}
}

int main() {
button.fall(callback(&buttonPress));
while (1) {
// You can display the playback state or perform other actions here
if (isPlaying) {
// Display “Playing”
} else {
// Display “Paused”
}
}
}