From 0aa3d594877172f6e8c9860332c04da72b3e8820 Mon Sep 17 00:00:00 2001 From: Mihai Tudor Panu Date: Mon, 2 Mar 2015 17:24:09 -0800 Subject: [PATCH] buzzer: fixed sounds not stopping and added extra functions Signed-off-by: Mihai Tudor Panu --- src/buzzer/buzzer.cxx | 40 ++++++++++++++++++++++++++++++++-------- src/buzzer/buzzer.h | 34 +++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/src/buzzer/buzzer.cxx b/src/buzzer/buzzer.cxx index 29d0be4c..227645b6 100644 --- a/src/buzzer/buzzer.cxx +++ b/src/buzzer/buzzer.cxx @@ -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); } diff --git a/src/buzzer/buzzer.h b/src/buzzer/buzzer.h index c5b5fb49..aee6d8f4 100644 --- a/src/buzzer/buzzer.h +++ b/src/buzzer/buzzer.h @@ -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; }; }