From 725fc4661c5dd168ba6be1bc6aadd1647ea1e1f1 Mon Sep 17 00:00:00 2001 From: Yevgeniy Kiveisha Date: Wed, 28 May 2014 05:56:44 -0700 Subject: [PATCH] buzzer: added buzzer with example Signed-off-by: Yevgeniy Kiveisha --- examples/CMakeLists.txt | 3 +++ examples/buzzer-sound.cxx | 66 +++++++++++++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 1 + src/buzzer/CMakeLists.txt | 4 +++ src/buzzer/buzzer.cxx | 51 ++++++++++++++++++++++++++++++++++++ src/buzzer/buzzer.h | 53 +++++++++++++++++++++++++++++++++++++ src/buzzer/jsupm_buzzer.i | 7 +++++ src/buzzer/pyupm_buzzer.i | 8 ++++++ 8 files changed, 193 insertions(+) create mode 100644 examples/buzzer-sound.cxx create mode 100644 src/buzzer/CMakeLists.txt create mode 100644 src/buzzer/buzzer.cxx create mode 100644 src/buzzer/buzzer.h create mode 100644 src/buzzer/jsupm_buzzer.i create mode 100644 src/buzzer/pyupm_buzzer.i diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6ec50c6..beede27 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -2,12 +2,15 @@ add_executable (compass compass.cxx) add_executable (groveled groveled.cxx) add_executable (grovetemp grovetemp.cxx) add_executable (lcm-lcd lcm-lcd.cxx) +add_executable (buzzer-sound buzzer-sound.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) include_directories (${PROJECT_SOURCE_DIR}/src/lcm1602) +include_directories (${PROJECT_SOURCE_DIR}/src/buzzer) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (grovetemp grove ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (lcm-lcd lcm1602 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (buzzer-sound buzzer ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/buzzer-sound.cxx b/examples/buzzer-sound.cxx new file mode 100644 index 0000000..e189e11 --- /dev/null +++ b/examples/buzzer-sound.cxx @@ -0,0 +1,66 @@ +/* + * 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" + +int running = 0; + +void +sig_handler(int signo) +{ + printf("got signal\n"); + if (signo == SIGINT) { + printf("exiting application\n"); + running = 1; + } +} + +int +main(int argc, char **argv) { + int chord[] = { DO, RE, MI, FA, SOL, LA, SI, DO, SI }; + // create Buzzer instance + upm::Buzzer* sound = new upm::Buzzer(5); + // print sensor name + std::cout << sound->name() << std::endl; + // play sound (DO, RE, ME, etc...) + + signal(SIGINT, sig_handler); + + while (!running) { + for (int chord_ind = 0; chord_ind < 9; chord_ind++) { + std::cout << sound->playSound(chord[chord_ind]) << std::endl; + usleep(1000); + } + } + + std::cout << "exiting application" << std::endl; + + delete sound; + + return 0; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 81eb5cf..6fe8eac 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory (hmc5883l) add_subdirectory (grove) add_subdirectory (lcm1602) +add_subdirectory (buzzer) diff --git a/src/buzzer/CMakeLists.txt b/src/buzzer/CMakeLists.txt new file mode 100644 index 0000000..86dfbab --- /dev/null +++ b/src/buzzer/CMakeLists.txt @@ -0,0 +1,4 @@ +set (libname "buzzer") +add_library (buzzer SHARED buzzer.cxx) +include_directories (${MAA_INCLUDE_DIR}) +target_link_libraries (buzzer ${MAA_LIBRARIES}) diff --git a/src/buzzer/buzzer.cxx b/src/buzzer/buzzer.cxx new file mode 100644 index 0000000..8800f46 --- /dev/null +++ b/src/buzzer/buzzer.cxx @@ -0,0 +1,51 @@ +/* + * 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 "buzzer.h" + +using namespace upm; + +Buzzer::Buzzer (int pinNumber) { + m_pwm_context = maa_pwm_init (pinNumber); + m_name = "Buzzer"; +} + +int Buzzer::playSound (int note) { + maa_pwm_enable (m_pwm_context, 1); + maa_pwm_period_us (m_pwm_context, note); + maa_pwm_write (m_pwm_context, 50.0); + usleep (10000); + maa_pwm_enable (m_pwm_context, 0); + + return 0; +} + +Buzzer::~Buzzer() { + maa_pwm_close(m_pwm_context); + std::cout << "executed maa_pwm_close" << std::endl; +} + diff --git a/src/buzzer/buzzer.h b/src/buzzer/buzzer.h new file mode 100644 index 0000000..c3ce06c --- /dev/null +++ b/src/buzzer/buzzer.h @@ -0,0 +1,53 @@ +/* + * 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 + +#define DO 3830000 // 261 Hz +#define RE 3400000 // 294 Hz +#define MI 3038000 // 329 Hz +#define FA 2864000 // 349 Hz +#define SOL 2550000 // 392 Hz +#define LA 2272000 // 440 Hz +#define SI 2028000 // 493 Hz + +namespace upm { + +class Buzzer { + public: + Buzzer (int pinNumber); + ~Buzzer (); + int playSound (int note); + std::string name() + { + return m_name; + } + protected: + std::string m_name; + private: + maa_pwm_context m_pwm_context; +}; +} diff --git a/src/buzzer/jsupm_buzzer.i b/src/buzzer/jsupm_buzzer.i new file mode 100644 index 0000000..b85f008 --- /dev/null +++ b/src/buzzer/jsupm_buzzer.i @@ -0,0 +1,7 @@ +%module jsupm_buzzer + +%{ + #include "buzzer.h" +%} + +%include "buzzer.h" diff --git a/src/buzzer/pyupm_buzzer.i b/src/buzzer/pyupm_buzzer.i new file mode 100644 index 0000000..2bdfe12 --- /dev/null +++ b/src/buzzer/pyupm_buzzer.i @@ -0,0 +1,8 @@ +%module pyupm_buzzer + +%feature("autodoc", "3"); + +%include "buzzer.h" +%{ + #include "buzzer.h" +%} -- 2.7.4