awef

Dependents:   KeypadPiano

Fork of Speaker by ze chen

Speaker.h

Committer:
UnitedHolmes
Date:
2015-12-08
Revision:
1:bd28eb0ec6f8
Parent:
0:41292907c9cb

File content as of revision 1:bd28eb0ec6f8:

#include "mbed.h"
// new class to play a note on Speaker based on PwmOut class
class Speaker
{
public:
    Speaker(PinName pin) : _pin(pin) {
// _pin(pin) means pass pin to the Speaker Constructor
    }
// class method to play a note based on PwmOut class
    void PlayNote(float frequency, float frequency2, float duration, float volume) {
        if (frequency == 0) {
            _pin.period(1.0/frequency2);
            _pin = volume/2.0;
            wait(duration);
            }
        else if(frequency2 == 0) {
            _pin.period(1.0/frequency);
            _pin = volume/2.0;
            wait(duration);
            }
        else {
           _pin.period(1.0/frequency);
            _pin = volume/2.0;
        wait(.008);
        _pin = 0.0;
        _pin.period(1.0/frequency2);
        _pin = volume/2.0;
        wait(.008);
        }
    }
    
    void StopPlaying() {
        _pin = 0;
        wait(0.001);
    }
 
private:
    PwmOut _pin;
};