buzzer: added buzzer with example

Signed-off-by: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
This commit is contained in:
Yevgeniy Kiveisha 2014-05-28 05:56:44 -07:00
parent b26cbd09f1
commit 725fc4661c
8 changed files with 193 additions and 0 deletions

View File

@ -2,12 +2,15 @@ add_executable (compass compass.cxx)
add_executable (groveled groveled.cxx) add_executable (groveled groveled.cxx)
add_executable (grovetemp grovetemp.cxx) add_executable (grovetemp grovetemp.cxx)
add_executable (lcm-lcd lcm-lcd.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/hmc5883l)
include_directories (${PROJECT_SOURCE_DIR}/src/grove) include_directories (${PROJECT_SOURCE_DIR}/src/grove)
include_directories (${PROJECT_SOURCE_DIR}/src/lcm1602) 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 (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (groveled grove ${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 (grovetemp grove ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (lcm-lcd lcm1602 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (lcm-lcd lcm1602 ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (buzzer-sound buzzer ${CMAKE_THREAD_LIBS_INIT})

66
examples/buzzer-sound.cxx Normal file
View File

@ -0,0 +1,66 @@
/*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* 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 <unistd.h>
#include <iostream>
#include <signal.h>
#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;
}

View File

@ -1,3 +1,4 @@
add_subdirectory (hmc5883l) add_subdirectory (hmc5883l)
add_subdirectory (grove) add_subdirectory (grove)
add_subdirectory (lcm1602) add_subdirectory (lcm1602)
add_subdirectory (buzzer)

View File

@ -0,0 +1,4 @@
set (libname "buzzer")
add_library (buzzer SHARED buzzer.cxx)
include_directories (${MAA_INCLUDE_DIR})
target_link_libraries (buzzer ${MAA_LIBRARIES})

51
src/buzzer/buzzer.cxx Normal file
View File

@ -0,0 +1,51 @@
/*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* 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 <iostream>
#include <unistd.h>
#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;
}

53
src/buzzer/buzzer.h Normal file
View File

@ -0,0 +1,53 @@
/*
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
* 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 <string>
#include <maa/pwm.h>
#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;
};
}

View File

@ -0,0 +1,7 @@
%module jsupm_buzzer
%{
#include "buzzer.h"
%}
%include "buzzer.h"

View File

@ -0,0 +1,8 @@
%module pyupm_buzzer
%feature("autodoc", "3");
%include "buzzer.h"
%{
#include "buzzer.h"
%}