Hi Peter,
Some additional notes for completeness.
In your last example, the code will work because you have declared a global timeout object called toff. But note it may not actually be the ideal approach as you may actually really want to make it a member of the Beep class, just like the PwmOut object. Having a global object means it is shared by any and all instances of the Beep class, which (although you might not see them in this case) could cause problems if that is not what was intended.
If you make the Timeout a member of the class, like PwmOut, every instance of this object would include a Timeout object.
So for example, you could do the following:
Beep.h
#ifndef MBED_BEEP_H
#define MBED_BEEP_H
#include "mbed.h"
class Beep {
public:
Beep (PinName pin);
void beep (float frequency, float time);
void nobeep();
private :
PwmOut _pwm;
Timeout _toff;
};
#endif
and
Beep.cpp
#include "Beep.h"
#include "mbed.h"
Beep::Beep(PinName pin) : _pwm(pin) {
_pwm.write(0.0); // after creating it have to be off
}
void Beep::nobeep() {
_pwm.write(0.0);
}
void Beep::beep(float freq, float time) {
_pwm.period(1.0/freq);
_pwm.write(0.5); // 50% duty cycle - beep on
_toff.attach(this,&Beep::nobeep, time); // time to off
}
Now you can see that it is attaching to the timeout which is part of the Beep object, so you can create as many Beep's as you want :)
Hope that is useful.
Simon
If I use the timeout with a c function I get the address of the function to call with &f.
If I try to do this inside a C++ class to get the function address I get the error :
"Nonstandard form for taking the address of a member function ... I think I get a pointer to a object ?
How can I get the pointer to the class member function to attach the timeout?
Regards, Peter