Stop a Timer from within class member callback

I have a timer like this:

DFRobotDFPlayerMini myDFPlayer;
Timer fade(1000, &DFRobotDFPlayerMini::fadeOut, myDFPlayer);

I’d like to stop() the timer from the member function like this:

void DFRobotDFPlayerMini::fadeOut()
{
  int currentVolume = this->readVolume();
  if (currentVolume)
  {
    this->sendStack(0x06, --currentVolume);  // lower the volume by one
  }
  else 
  {
    this->stop();  // stop playing the music
    this->volume(_volume);  //reset the volume to saved setting
    //stop the Timer here??
  }
}

can anyone point me in the right direction? Can this be done?

@BulldogLowell, fade is declared global so I don’t see why you can’t simply stop the timer using fade.stop(). Have you tried?

1 Like
void DFRobotDFPlayerMini::fadeOut()
{
  int currentVolume = this->readVolume();
  if (currentVolume)
  {
    this->sendStack(0x06, --currentVolume);  // lower the volume by one
  }
  else
  {
    fade.stop();
    this->stop();  // stop playing the music
    this->volume(_volume);  //reset the volume to saved setting
  }
}
DFRobotDFPlayerMini.cpp: In member function 'void DFRobotDFPlayerMini::fadeOut()':
DFRobotDFPlayerMini.cpp:298:5: error: 'fade' was not declared in this scope
     fade.stop();

BUT…

adding:

extern Timer fade;

in the library’s header file lets me compile… I’ll have to test it later tonight.

Oh, did I forget mention that I had the class in a header and an implementation file? :flushed:

Thanks for shaking the cobwebs loose!

1 Like

But that would require your lib user to declare a global timer fade for only that reason.
Shouldn’t that timer rather be a private property of the class itself?
You’d provide the parameters for the timing but the object should be in charge of how to do the timing.

Just to stress the aparent misconception

DFRobotDFPlayerMini myDFPlayer;
Timer fade(1000, &DFRobotDFPlayerMini::fadeOut, myDFPlayer);
...
void DFRobotDFPlayerMini::fadeOut()
{
  int currentVolume = this->readVolume();
  if (currentVolume)
  {
    this->sendStack(0x06, --currentVolume);  // lower the volume by one
  }
  else 
  {
    this->stop();  // <-- stop() is no method of DFRobotDFPlayerMini (or at least doesn't refer to the timer "fade")
    this->volume(_volume);  //reset the volume to saved setting
    //stop the Timer here??
  }
}

this refers to the object myDFPlayer and not to fade hence the missing stop() method.

yeah, the stop() method in DFRobotDFPlayerMini::fadeOut() is not my attempt to stop the timer. I expected to put that where it says: stop the timer here??. :smirk:

Yes… for this one project, I’m OK with that. But building a proper/published library, well… I wouldn’t want to do it that way. It is too awkward expecting (depending on) someone look for a global variable in the header to use one member function. I liked the timer because it left me without having to create yet another method (i.e. ```update()`` ) to time/execute the diminuendo.

After I’m done wth the current project I’m working, I’m likely to get to this (and a few other methods that came about as a result of my using the DFRobotDFPlayerMini library) into a proper Particle version.

Any/all recommendations and assistance are welcome. :grinning:

1 Like