Home Forums Sparkfun MP3 Shield Library Support Forum Setting volume when track is finishing Reply To: Setting volume when track is finishing

#1900
Anonymous
Inactive

Ok, I made some tests with several Serial.println statements and now I can tell you more about what is happening.

I will explain with a timed sequence of events that may happen if you quikly spin the encoder generating interrupts that enable volume increase or decrease:

1) Spin the encoder right before the track ends

2) one interrupt is generated and the main loop enters in the portion of code to change volume.

3) The code to change volume uses methods getVolume and setVolume.

4) These methods calls Mp3ReadRegister and Mp3WriteRegister. In these functions the problem happens in this portion of code:

if(playing_state == playback) {

//see if it is already ready for more
refill();

//attach refill interrupt off DREQ line, pin 2
enableRefill();
}

5) The above call to refill() happens to be the last one, the refill function will call disableRefill() and flushdata.

6) After calling refill() the program jumps back and the function enableRefill() is executed. It should not be in this case.

7) DREQ interrupt is re-enabled and refill() function (callback) keeps executing endless, this is why my program stucks. I don’t know why the interrupt keeps firing if the codec does not need more data (noise in DREQ pin maybe?)

 

Anyway I conclude that if a MP3WriteRegister or MP3ReadRegister occurs in such a way that the call to refill() function is the last one, we have a not predicted situation, with strange behavior.

In order to check if this is the cause of the problem, I modified the library code in those 2 functions, including a new playing_state check:

if(playing_state == playback) {

//see if it is already ready for more
refill();

//attach refill interrupt off DREQ line, pin 2
if(playing_state == playback) {
enableRefill();
}
}

After that I spinned the encoder as fast as I could and it worked as expected, without freezing.

I don’t know if this solution is the best one, but it seems to work. What do you think?

Feel free to ask if my text was not clear or if you have any other question.

Best regards!

Miguel