buzzer: fixed sounds not stopping and added extra functions

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Mihai Tudor Panu 2015-03-02 17:24:09 -08:00
parent 3810e7f2cb
commit 0aa3d59487
2 changed files with 61 additions and 13 deletions

View File

@ -28,23 +28,47 @@
#include "buzzer.h"
using namespace upm;
using namespace std;
Buzzer::Buzzer (int pinNumber) {
m_pwm_context = mraa_pwm_init (pinNumber);
Buzzer::Buzzer(int pinNumber) {
m_pwm_context = mraa_pwm_init(pinNumber);
if(m_pwm_context == 0)
{
cerr << __FUNCTION__ << ": Error initializing PWM on pin " << pinNumber << endl;
return;
}
m_name = "Buzzer";
mraa_pwm_enable (m_pwm_context, 1);
Buzzer::setVolume(1.0);
}
int Buzzer::playSound (int note, int delay) {
mraa_pwm_enable (m_pwm_context, 1);
mraa_pwm_period_us (m_pwm_context, note);
mraa_pwm_pulsewidth_us (m_pwm_context, note / 2);
usleep (delay);
mraa_pwm_enable (m_pwm_context, 0);
void Buzzer::setVolume(float vol){
m_volume = vol;
}
float Buzzer::getVolume(){
return m_volume;
}
int Buzzer::playSound(int note, int delay) {
mraa_pwm_period_us(m_pwm_context, note);
mraa_pwm_write(m_pwm_context, m_volume * 0.5);
if(delay){
usleep(delay);
Buzzer::stopSound();
}
return note;
}
void Buzzer::stopSound(){
// Has to be checked out on a scope and make sure it's flat 0
mraa_pwm_period_us(m_pwm_context, 1);
mraa_pwm_write(m_pwm_context, 0);
}
Buzzer::~Buzzer() {
Buzzer::stopSound();
mraa_pwm_enable(m_pwm_context, 0);
mraa_pwm_close(m_pwm_context);
}

View File

@ -54,7 +54,7 @@ namespace upm {
class Buzzer {
public:
/**
* Instantiates a Buzzer object
* Instantiates a Buzzer object.
*
* @param pinNumber Buzzer pin number
*/
@ -66,17 +66,40 @@ class Buzzer {
~Buzzer ();
/**
* Play a tone for a certain amount of time.
* Play a tone for a certain amount of time or indefinitely. When delay
* is not used, the sound can be stopped by calling stopSound().
*
* @param note the note to be played (DO, RE, MI, etc...)
* @param delay time in microsec for playing the sound
* @param note the note to be played (DO, RE, MI, etc...) or frequency
* @param delay time in microseconds for playing the sound, a value of
* 0 plays the sound indefinitely
*
* @return the note played
*/
int playSound (int note, int delay);
/**
* Return name of the component
* Stops the sound currently playing. Has to be called when playSound()
* does not set a delay value.
*/
void stopSound();
/**
* Sets the volume for the buzzer, but may affect the sound timbre.
* Works best with halved values, e.g. 1.0, 0.5, 0.25, ...
*
* @param vol the value to set the volume to from 0.0 to 1.0
*/
void setVolume(float vol);
/**
* Gets the buzzer volume.
*
* @return the value the volume was set to
*/
float getVolume();
/**
* Return name of the component.
*
* @return name of the sensor
*/
@ -88,5 +111,6 @@ class Buzzer {
std::string m_name;
private:
mraa_pwm_context m_pwm_context;
float m_volume;
};
}