mirror of
https://github.com/eclipse/upm.git
synced 2025-07-02 01:41:12 +03:00
buzzer: C implementation; C++ wraps C
Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
@ -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)
|
||||
|
142
src/buzzer/buzzer.c
Normal file
142
src/buzzer/buzzer.c
Normal file
@ -0,0 +1,142 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation.
|
||||
*
|
||||
* based on original C++ driver by
|
||||
* 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 <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <upm_utilities.h>
|
||||
#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;
|
||||
}
|
@ -1,4 +1,8 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation.
|
||||
*
|
||||
* based on original C++ driver by
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* 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");
|
||||
}
|
||||
|
||||
|
115
src/buzzer/buzzer.h
Normal file
115
src/buzzer/buzzer.h
Normal file
@ -0,0 +1,115 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation.
|
||||
*
|
||||
* based on original C++ driver by
|
||||
* 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 <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <upm.h>
|
||||
|
||||
#include <mraa/pwm.h>
|
||||
#include <buzzer_tones.h>
|
||||
|
||||
#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
|
@ -1,4 +1,8 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation.
|
||||
*
|
||||
* based on original C++ driver by
|
||||
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||
* Copyright (c) 2014 Intel Corporation.
|
||||
*
|
||||
@ -24,69 +28,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <mraa/pwm.h>
|
||||
|
||||
#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 <buzzer.h>
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
|
37
src/buzzer/buzzer_tones.h
Normal file
37
src/buzzer/buzzer_tones.h
Normal file
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation.
|
||||
*
|
||||
* based on original C++ driver by
|
||||
* 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
|
||||
|
||||
// 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
|
@ -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);
|
||||
}
|
||||
}
|
||||
%}
|
||||
%}
|
||||
|
@ -1,8 +1,9 @@
|
||||
%module jsupm_buzzer
|
||||
%include "../upm.i"
|
||||
|
||||
%include "buzzer_tones.h"
|
||||
%include "buzzer.hpp"
|
||||
%{
|
||||
#include "buzzer.hpp"
|
||||
%}
|
||||
|
||||
%include "buzzer.hpp"
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
%feature("autodoc", "3");
|
||||
|
||||
%include "buzzer_tones.h"
|
||||
%include "buzzer.hpp"
|
||||
%{
|
||||
#include "buzzer.hpp"
|
||||
|
Reference in New Issue
Block a user