From 7b9fbd8738e6c0c8b2a655c913b90a386ba7a693 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Fri, 28 Oct 2016 16:47:01 -0600 Subject: [PATCH] buzzer: C implementation; C++ wraps C Signed-off-by: Jon Trulson --- examples/c++/buzzer-sound.cxx | 15 +- examples/c/CMakeLists.txt | 1 + examples/c/buzzer.c | 68 +++++++++ examples/java/BuzzerSample.java | 23 +-- examples/java/CMakeLists.txt | 1 - examples/javascript/buzzer.js | 20 ++- examples/python/buzzer.py | 10 +- src/buzzer/CMakeLists.txt | 14 +- src/buzzer/buzzer.c | 142 ++++++++++++++++++ src/buzzer/buzzer.cxx | 62 ++++---- src/buzzer/buzzer.h | 115 ++++++++++++++ src/buzzer/buzzer.hpp | 83 +++++----- .../buzzer/buzzer_tones.h | 50 ++---- src/buzzer/javaupm_buzzer.i | 7 +- src/buzzer/jsupm_buzzer.i | 3 +- src/buzzer/pyupm_buzzer.i | 1 + 16 files changed, 461 insertions(+), 154 deletions(-) create mode 100644 examples/c/buzzer.c create mode 100644 src/buzzer/buzzer.c create mode 100644 src/buzzer/buzzer.h rename examples/java/Buzzer_soundSample.java => src/buzzer/buzzer_tones.h (54%) diff --git a/examples/c++/buzzer-sound.cxx b/examples/c++/buzzer-sound.cxx index 604904a6..8b127413 100644 --- a/examples/c++/buzzer-sound.cxx +++ b/examples/c++/buzzer-sound.cxx @@ -31,18 +31,21 @@ int main(int argc, char **argv) { //! [Interesting] - int chord[] = { DO, RE, MI, FA, SOL, LA, SI, DO, SI }; + int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI, + BUZZER_FA, BUZZER_SOL, BUZZER_LA, + BUZZER_SI }; // create Buzzer instance upm::Buzzer* sound = new upm::Buzzer(5); // print sensor name std::cout << sound->name() << std::endl; - // play sound (DO, RE, MI, etc...), pausing for 0.1 seconds between notes - for (int chord_ind = 0; chord_ind < 7; chord_ind++) { - // play each note for one second - std::cout << sound->playSound(chord[chord_ind], 1000000) << std::endl; - usleep(100000); + // play each sound (DO, RE, MI, etc...) for .5 seconds, pausing + // for 0.1 seconds between notes + for (int chord_ind = 0; chord_ind < 7; chord_ind++) + { + std::cout << sound->playSound(chord[chord_ind], 500000) << std::endl; + upm_delay_ms(100); } //! [Interesting] diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 0a660ff7..9318fe54 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -131,6 +131,7 @@ add_example (md) add_example (linefinder) add_example (uln200xa) add_example (mma7660) +add_example (buzzer) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/buzzer.c b/examples/c/buzzer.c new file mode 100644 index 00000000..763354df --- /dev/null +++ b/examples/c/buzzer.c @@ -0,0 +1,68 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include +#include + + +int +main(int argc, char **argv) +{ +//! [Interesting] + int chord[] = { BUZZER_DO, BUZZER_RE, BUZZER_MI, + BUZZER_FA, BUZZER_SOL, BUZZER_LA, + BUZZER_SI }; + + // create Buzzer context, using PWM pin 5 + buzzer_context sound = buzzer_init(5); + + if (!sound) + { + printf("buzzer_init() failed\n"); + return 1; + } + + printf("Playing...\n"); + + // play each sound (DO, RE, MI, etc...) for .5 seconds, pausing + // for 0.1 seconds between notes + for (int chord_ind = 0; chord_ind < 7; chord_ind++) + { + buzzer_play_sound(sound, chord[chord_ind], 500000); + printf("%d\n", chord[chord_ind]); + upm_delay_ms(100); + } + + printf("Exiting...\n"); + + buzzer_close(sound); + +//! [Interesting] + + return 0; +} diff --git a/examples/java/BuzzerSample.java b/examples/java/BuzzerSample.java index e94be7fe..88003e84 100644 --- a/examples/java/BuzzerSample.java +++ b/examples/java/BuzzerSample.java @@ -22,28 +22,29 @@ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +import upm_buzzer.Buzzer; public class BuzzerSample { public static void main(String[] args) throws InterruptedException { // ! [Interesting] int chord[] = { - upm_buzzer.javaupm_buzzer.DO, - upm_buzzer.javaupm_buzzer.RE, - upm_buzzer.javaupm_buzzer.MI, - upm_buzzer.javaupm_buzzer.FA, - upm_buzzer.javaupm_buzzer.SOL, - upm_buzzer.javaupm_buzzer.LA, - upm_buzzer.javaupm_buzzer.SI}; + upm_buzzer.javaupm_buzzer.BUZZER_DO, + upm_buzzer.javaupm_buzzer.BUZZER_RE, + upm_buzzer.javaupm_buzzer.BUZZER_MI, + upm_buzzer.javaupm_buzzer.BUZZER_FA, + upm_buzzer.javaupm_buzzer.BUZZER_SOL, + upm_buzzer.javaupm_buzzer.BUZZER_LA, + upm_buzzer.javaupm_buzzer.BUZZER_SI}; // Instantiate a buzzer on digital pin D5 - upm_buzzer.Buzzer sound = new upm_buzzer.Buzzer(5); + Buzzer sound = new Buzzer(5); // print sensor name System.out.println(sound.name()); for (int i = 0; i < chord.length; i++) { - // play each note for one second - int note = sound.playSound(chord[i], 1000000); + // play each note for one half second + int note = sound.playSound(chord[i], 500000); System.out.println(note); Thread.sleep(100); @@ -52,4 +53,4 @@ public class BuzzerSample { sound.stopSound(); } -} \ No newline at end of file +} diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index e6908fdf..9bf6c386 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -25,7 +25,6 @@ add_example(Apds9002 apds9002) add_example(BISS0001Sample biss0001) add_example(BMPX8XSample bmpx8x) add_example(BuzzerSample buzzer) -add_example(Buzzer_soundSample buzzer) add_example(CJQ4435Sample cjq4435) add_example(DS1307Sample ds1307) add_example(ENC03RSample enc03r) diff --git a/examples/javascript/buzzer.js b/examples/javascript/buzzer.js index f51583a1..6f20914a 100644 --- a/examples/javascript/buzzer.js +++ b/examples/javascript/buzzer.js @@ -26,15 +26,13 @@ var upmBuzzer = require("jsupm_buzzer"); // Initialize on GPIO 5 var myBuzzer = new upmBuzzer.Buzzer(5); var chords = []; -chords.push(upmBuzzer.DO); -chords.push(upmBuzzer.RE); -chords.push(upmBuzzer.MI); -chords.push(upmBuzzer.FA); -chords.push(upmBuzzer.SOL); -chords.push(upmBuzzer.LA); -chords.push(upmBuzzer.SI); -chords.push(upmBuzzer.DO); -chords.push(upmBuzzer.SI); +chords.push(upmBuzzer.BUZZER_DO); +chords.push(upmBuzzer.BUZZER_RE); +chords.push(upmBuzzer.BUZZER_MI); +chords.push(upmBuzzer.BUZZER_FA); +chords.push(upmBuzzer.BUZZER_SOL); +chords.push(upmBuzzer.BUZZER_LA); +chords.push(upmBuzzer.BUZZER_SI); var chordIndex = 0; // Print sensor name @@ -44,8 +42,8 @@ function melody() { if (chords.length != 0) { - //Play sound for one second - console.log( myBuzzer.playSound(chords[chordIndex], 1000000) ); + //Play sound for one half second + console.log( myBuzzer.playSound(chords[chordIndex], 500000) ); chordIndex++; //Reset the sound to start from the beginning. if (chordIndex > chords.length - 1) diff --git a/examples/python/buzzer.py b/examples/python/buzzer.py index 2233e5d0..645e7bf6 100755 --- a/examples/python/buzzer.py +++ b/examples/python/buzzer.py @@ -28,17 +28,17 @@ def main(): # Create the buzzer object using GPIO pin 5 buzzer = upmBuzzer.Buzzer(5) - chords = [upmBuzzer.DO, upmBuzzer.RE, upmBuzzer.MI, upmBuzzer.FA, - upmBuzzer.SOL, upmBuzzer.LA, upmBuzzer.SI, upmBuzzer.DO, - upmBuzzer.SI]; + chords = [upmBuzzer.BUZZER_DO, upmBuzzer.BUZZER_RE, upmBuzzer.BUZZER_MI, + upmBuzzer.BUZZER_FA, upmBuzzer.BUZZER_SOL, upmBuzzer.BUZZER_LA, + upmBuzzer.BUZZER_SI]; # Print sensor name print(buzzer.name()) # Play sound (DO, RE, MI, etc.), pausing for 0.1 seconds between notes for chord_ind in range (0,7): - # play each note for one second - print(buzzer.playSound(chords[chord_ind], 1000000)) + # play each note for a half second + print(buzzer.playSound(chords[chord_ind], 500000)) time.sleep(0.1) print("exiting application") diff --git a/src/buzzer/CMakeLists.txt b/src/buzzer/CMakeLists.txt index 5e7e8a54..05231f81 100644 --- a/src/buzzer/CMakeLists.txt +++ b/src/buzzer/CMakeLists.txt @@ -1,5 +1,9 @@ -set (libname "buzzer") -set (libdescription "Buzzer") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init() +upm_mixed_module_init (NAME buzzer + DESCRIPTION "Buzzer" + C_HDR buzzer.h buzzer_tones.h + C_SRC buzzer.c + CPP_HDR buzzer.hpp + CPP_SRC buzzer.cxx +# FTI_SRC buzzer_fti.c + CPP_WRAPS_C + REQUIRES mraa) diff --git a/src/buzzer/buzzer.c b/src/buzzer/buzzer.c new file mode 100644 index 00000000..2e58696b --- /dev/null +++ b/src/buzzer/buzzer.c @@ -0,0 +1,142 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * based on original C++ driver by + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include "buzzer.h" + +buzzer_context buzzer_init(int pin) +{ + buzzer_context dev = + (buzzer_context)malloc(sizeof(struct _buzzer_context)); + + if (!dev) + return NULL; + + dev->pwm = NULL; + dev->volume = 0.0; + dev->initialized = false; + + // make sure MRAA is initialized + int mraa_rv; + if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) + { + printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv); + buzzer_close(dev); + return NULL; + } + + // MRAA contexts... + if ( !(dev->pwm = mraa_pwm_init(pin)) ) + { + printf("%s: mraa_pwm_init() failed\n", + __FUNCTION__); + buzzer_close(dev); + return NULL; + } + + mraa_pwm_enable(dev->pwm, 1); + + buzzer_set_volume(dev, 1.0); + + dev->initialized = true; + return dev; +} + +void buzzer_close(buzzer_context dev) +{ + assert(dev != NULL); + + if (dev->initialized) + buzzer_stop_sound(dev); + + if (dev->pwm) + { + mraa_pwm_enable(dev->pwm, 0); + mraa_pwm_close(dev->pwm); + } + + free(dev); +} + +void buzzer_set_volume(const buzzer_context dev, float vol) +{ + assert(dev != NULL); + + dev->volume = vol; +} + +float buzzer_get_volume(const buzzer_context dev) +{ + assert(dev != NULL); + + return dev->volume; +} + +upm_result_t buzzer_play_sound(const buzzer_context dev, int note, int delay) +{ + assert(dev != NULL); + + if (mraa_pwm_period_us(dev->pwm, note)) + { + printf("%s: mraa_pwm_period_us() failed\n", __FUNCTION__); + return UPM_ERROR_OPERATION_FAILED; + } + + if (mraa_pwm_write(dev->pwm, dev->volume * 0.5)) + { + printf("%s: mraa_pwm_write() failed\n", __FUNCTION__); + return UPM_ERROR_OPERATION_FAILED; + } + + if (delay >= 0) + { + upm_delay_us(delay); + return buzzer_stop_sound(dev); + } + + return UPM_SUCCESS; +} + +upm_result_t buzzer_stop_sound(const buzzer_context dev) +{ + assert(dev != NULL); + + mraa_result_t rv = MRAA_SUCCESS; + + rv = mraa_pwm_period_us(dev->pwm, 1); + rv = mraa_pwm_write(dev->pwm, 0); + + if (rv) + { + return UPM_ERROR_OPERATION_FAILED; + } + + return UPM_SUCCESS; +} diff --git a/src/buzzer/buzzer.cxx b/src/buzzer/buzzer.cxx index 4150f631..bb296665 100644 --- a/src/buzzer/buzzer.cxx +++ b/src/buzzer/buzzer.cxx @@ -1,4 +1,8 @@ /* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * based on original C++ driver by * Author: Yevgeniy Kiveisha * Copyright (c) 2014 Intel Corporation. * @@ -32,46 +36,40 @@ using namespace upm; using namespace std; -Buzzer::Buzzer(int pinNumber) { - m_pwm_context = mraa_pwm_init(pinNumber); - if(m_pwm_context == 0) - { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_pwm_init() failed, invalid pin?"); - return; - } - m_name = "Buzzer"; - mraa_pwm_enable (m_pwm_context, 1); - Buzzer::setVolume(1.0); +Buzzer::Buzzer(int pinNumber) : m_buzzer(buzzer_init(pinNumber)) +{ + if (!m_buzzer) + throw std::runtime_error(std::string(__FUNCTION__) + + ": buzzer_init() failed"); } -void Buzzer::setVolume(float vol){ - m_volume = vol; +Buzzer::~Buzzer() +{ + buzzer_close(m_buzzer); } -float Buzzer::getVolume(){ - return m_volume; +void Buzzer::setVolume(float vol) +{ + buzzer_set_volume(m_buzzer, vol); } -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(); - } +float Buzzer::getVolume() +{ + return buzzer_get_volume(m_buzzer); +} + +int Buzzer::playSound(int note, int delay) +{ + if (buzzer_play_sound(m_buzzer, note, delay)) + throw std::runtime_error(std::string(__FUNCTION__) + + ": buzzer_play_sound() failed"); 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); +void Buzzer::stopSound() +{ + if (buzzer_stop_sound(m_buzzer)) + throw std::runtime_error(std::string(__FUNCTION__) + + ": buzzer_stop_sound() failed"); } diff --git a/src/buzzer/buzzer.h b/src/buzzer/buzzer.h new file mode 100644 index 00000000..a4788200 --- /dev/null +++ b/src/buzzer/buzzer.h @@ -0,0 +1,115 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * based on original C++ driver by + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include +#include +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * @file buzzer.h + * @library buzzer + * @brief C API for the buzzer driver + * + * @include buzzer.c + */ + + /** + * Device context + */ + typedef struct _buzzer_context { + mraa_pwm_context pwm; + + float volume; + bool initialized; + } *buzzer_context; + + /** + * Initialize a buzzer context. + * + * @param pin Buzzer pin number (pwm capable) + * @return buzzer device context + */ + buzzer_context buzzer_init(int pin); + + /** + * Buzzer object destructor. + * + * @param dev Device context. + */ + void buzzer_close(buzzer_context dev); + + /** + * Plays a tone for a certain amount of time or indefinitely. When delay + * is not used, the sound can be stopped by calling stopSound(). + * + * @param dev Device context. + * @param note Note to play (DO, RE, MI, etc.) or frequency + * @param delay Time in microseconds for which to play the sound; + * if the value is 0, the sound is played indefinitely + * @return UPM result + */ + upm_result_t buzzer_play_sound(const buzzer_context dev, int note, + int delay); + + /** + * Stops the sound currently playing. Should be called when playSound() + * does not have a delay value. + * + * @param dev Device context. + * @return UPM result + */ + upm_result_t buzzer_stop_sound(const buzzer_context dev); + + /** + * 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, etc. + * + * @param dev Device context. + * @param vol Value to set the volume to, from 0.0 to 1.0 + */ + void buzzer_set_volume(const buzzer_context dev, float vol); + + /** + * Gets the buzzer volume. + * + * @param dev Device context. + * @return Value the volume was set to + */ + float buzzer_get_volume(const buzzer_context dev); + +#ifdef __cplusplus +} +#endif diff --git a/src/buzzer/buzzer.hpp b/src/buzzer/buzzer.hpp index ca3c8083..2364b904 100644 --- a/src/buzzer/buzzer.hpp +++ b/src/buzzer/buzzer.hpp @@ -1,4 +1,8 @@ /* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * based on original C++ driver by * Author: Yevgeniy Kiveisha * Copyright (c) 2014 Intel Corporation. * @@ -24,69 +28,62 @@ #pragma once #include -#include - -#define DO 3800 // 262 Hz - C4 -#define RE 3400 // 294 Hz - D -#define MI 3000 // 330 Hz - E -#define FA 2900 // 349 Hz - F -#define SOL 2550 // 392 Hz - G -#define LA 2270 // 440 Hz - A -#define SI 2000 // 494 Hz - B +#include namespace upm { -/** - * @brief Buzzer library - * @defgroup buzzer libupm-buzzer - * @ingroup seeed pwm sound gsk - */ + /** + * @brief Buzzer library + * @defgroup buzzer libupm-buzzer + * @ingroup seeed pwm sound gsk + */ -/** - * @library buzzer - * @sensor buzzer - * @comname Grove Buzzer - * @type sound - * @man seeed - * @con pwm - * @kit gsk - * - * @brief API for the Buzzer component - * - * This module defines the Buzzer interface for libbuzzer. - * This sensor can make different tones when connected to - * a pin capable of analog pulse-width modulation. It emits - * sound using a piezoelectric material that vibrates at different - * frequencies based on the input voltage. - * - * @image html buzzer.jpg - * @snippet buzzer-sound.cxx Interesting - */ -class Buzzer { + /** + * @library buzzer + * @sensor buzzer + * @comname Grove Buzzer + * @type sound + * @man seeed + * @con pwm + * @kit gsk + * + * @brief API for the Buzzer component + * + * This module defines the Buzzer interface for libbuzzer. + * This sensor can make different tones when connected to + * a pin capable of analog pulse-width modulation. It emits + * sound using a piezoelectric material that vibrates at different + * frequencies based on the input voltage. + * + * @image html buzzer.jpg + * @snippet buzzer-sound.cxx Interesting + */ + + class Buzzer { public: /** * Instantiates a Buzzer object. * * @param pinNumber Buzzer pin number */ - Buzzer (int pinNumber); + Buzzer(int pinNumber); /** * Buzzer object destructor. */ - ~Buzzer (); + ~Buzzer(); /** * Plays 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 Note to play (DO, RE, MI, etc.) or frequency - * @param delay Time in microseconds for which to play the sound; if the value is - * 0, the sound is played indefinitely + * @param delay Time in microseconds for which to play the + * sound; if the value is 0, the sound is played indefinitely * * @return Note played */ - int playSound (int note, int delay); + int playSound(int note, int delay); /** * Stops the sound currently playing. Should be called when playSound() @@ -120,8 +117,6 @@ class Buzzer { } protected: std::string m_name; - private: - mraa_pwm_context m_pwm_context; - float m_volume; -}; + buzzer_context m_buzzer; + }; } diff --git a/examples/java/Buzzer_soundSample.java b/src/buzzer/buzzer_tones.h similarity index 54% rename from examples/java/Buzzer_soundSample.java rename to src/buzzer/buzzer_tones.h index 576d8886..969c098a 100644 --- a/examples/java/Buzzer_soundSample.java +++ b/src/buzzer/buzzer_tones.h @@ -1,6 +1,10 @@ /* - * Author: Stefan Andritoiu - * Copyright (c) 2015 Intel Corporation. + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * based on original C++ driver by + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -21,37 +25,13 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#pragma once -public class Buzzer_soundSample { - - public static void main(String[] args) throws InterruptedException { - //! [Interesting] - int chord[] = { - upm_buzzer.javaupm_buzzer.DO, - upm_buzzer.javaupm_buzzer.RE, - upm_buzzer.javaupm_buzzer.MI, - upm_buzzer.javaupm_buzzer.FA, - upm_buzzer.javaupm_buzzer.SOL, - upm_buzzer.javaupm_buzzer.LA, - upm_buzzer.javaupm_buzzer.SI - }; - - // Instantiate a buzzer on digital pin D5 - upm_buzzer.Buzzer sound = new upm_buzzer.Buzzer(5); - - // print sensor name - System.out.println(sound.name()); - - for (int i = 0; i < chord.length; i++) { - // play each note for one second - int note = sound.playSound( chord[i], 1000000); - System.out.println(note); - - Thread.sleep(100); - } - //! [Interesting] - sound.stopSound(); - } - - -} +// Hz = 1000000 / N +#define BUZZER_DO 3800 // 262 Hz - C4 +#define BUZZER_RE 3400 // 294 Hz - D +#define BUZZER_MI 3000 // 330 Hz - E +#define BUZZER_FA 2900 // 349 Hz - F +#define BUZZER_SOL 2550 // 392 Hz - G +#define BUZZER_LA 2270 // 440 Hz - A +#define BUZZER_SI 2000 // 494 Hz - B diff --git a/src/buzzer/javaupm_buzzer.i b/src/buzzer/javaupm_buzzer.i index ce8ed60f..b7041134 100644 --- a/src/buzzer/javaupm_buzzer.i +++ b/src/buzzer/javaupm_buzzer.i @@ -1,12 +1,13 @@ %module javaupm_buzzer %include "../upm.i" + +%include "buzzer_tones.h" +%include "buzzer.hpp" %{ #include "buzzer.hpp" %} -%include "buzzer.hpp" - %pragma(java) jniclasscode=%{ static { try { @@ -16,4 +17,4 @@ System.exit(1); } } -%} \ No newline at end of file +%} diff --git a/src/buzzer/jsupm_buzzer.i b/src/buzzer/jsupm_buzzer.i index 4b7c452c..93768b29 100644 --- a/src/buzzer/jsupm_buzzer.i +++ b/src/buzzer/jsupm_buzzer.i @@ -1,8 +1,9 @@ %module jsupm_buzzer %include "../upm.i" +%include "buzzer_tones.h" +%include "buzzer.hpp" %{ #include "buzzer.hpp" %} -%include "buzzer.hpp" diff --git a/src/buzzer/pyupm_buzzer.i b/src/buzzer/pyupm_buzzer.i index 56de1212..41dad162 100644 --- a/src/buzzer/pyupm_buzzer.i +++ b/src/buzzer/pyupm_buzzer.i @@ -5,6 +5,7 @@ %feature("autodoc", "3"); +%include "buzzer_tones.h" %include "buzzer.hpp" %{ #include "buzzer.hpp"