1

As far as I understand, yield() is used when you have long code (e.g. loop) so the background processing required by the esp8266 is not suspended for long time.

I have application where some sound should be running all the time and in the same time the program has to do other things which may includes long loops (that I call yield() inside them). Those long loops cause the sound to be discontinues.

What is the best idiomatic way of imitating the yield() function in my program so I do some thing like this:

for(int i=0;i<10000;++i){
    yield();
    customeYield();
    someFunction(i);
}

Where customeYield() contains the necessary instruction for playing the sound. It is exactly like this (I am using ESP8266Audio):

if (mp3_->isRunning()) {
    if (!mp3_->loop()) {
        mp3_->stop();
        return true;
    }
    return false;
}
return true;

Of course, I can just call it like I did (making it global or something). However, I was wondering if there is a standard way for such a task.

  • yield() must be called in some time interval or watchdog restarts the esp8266. you must call your function or the music stops. no magic. yield() does what is needed and returns. – Juraj May 4 '18 at 20:35
3

Without any hint, what you are actually doing in your long loops, it is difficult to say, what way would be the best for your case. I don't believe, there is a standard way of doing this, but there are basically only two ways of multitasking on a microcontroller:

  1. Interrupt driven: You can set a timer interrupt, that occurs regularly to call the AudioGenerator::loop() function (which fills the buffer with the next data).
  2. Non-blocking code: If you are using non-blocking code, you can first do one thing and then do another in sequence, so fast, that both codes are executed often enough. Thats exactly what the library you use is designed for (according to the README file).

If you are using more than one of these long loops in your loop() function, you may consider doing things with a finite state machine (FSM), so that you don't have to always add the AudioGenerator::loop() function to your long loops, but only once for the normal loop() function.

  • Thank you very much for your reply. Actually, yes I am using FSM. However, I did not understand exactly how this will change things? At the end, if some state main function contains long loop it will still suspend the other code from being executed.. Did I miss sth ? – Humam Helfawi May 5 '18 at 6:48
  • The FSM can help you to divide your long loops into small parts, so that one execution of the main loop() function does not take long. Then you execute the AudioGenerator::loop() in every cycle of the main loop() function. With this you suspend the long loop a bit for executing the background task (here: playing audio) in between. That is just a coding style. How exactly to divide the long loops into small parts depends on the code inside of them. I hope this is now clearer. – chrisl May 5 '18 at 13:41
  • AH Ok! I got it now.. I thought there is native support for the pattern. I am implementing it the way you advice and It seems working.. Thank you very much! – Humam Helfawi May 5 '18 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.