mirror of
https://github.com/eclipse/upm.git
synced 2025-03-24 01:10:22 +03:00
buzzer: C implementation; C++ wraps C
Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
parent
f623414c04
commit
7b9fbd8738
@ -31,18 +31,21 @@
|
|||||||
int
|
int
|
||||||
main(int argc, char **argv) {
|
main(int argc, char **argv) {
|
||||||
//! [Interesting]
|
//! [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
|
// create Buzzer instance
|
||||||
upm::Buzzer* sound = new upm::Buzzer(5);
|
upm::Buzzer* sound = new upm::Buzzer(5);
|
||||||
// print sensor name
|
// print sensor name
|
||||||
std::cout << sound->name() << std::endl;
|
std::cout << sound->name() << std::endl;
|
||||||
|
|
||||||
// play sound (DO, RE, MI, etc...), pausing for 0.1 seconds between notes
|
// play each sound (DO, RE, MI, etc...) for .5 seconds, pausing
|
||||||
for (int chord_ind = 0; chord_ind < 7; chord_ind++) {
|
// for 0.1 seconds between notes
|
||||||
// play each note for one second
|
for (int chord_ind = 0; chord_ind < 7; chord_ind++)
|
||||||
std::cout << sound->playSound(chord[chord_ind], 1000000) << std::endl;
|
{
|
||||||
usleep(100000);
|
std::cout << sound->playSound(chord[chord_ind], 500000) << std::endl;
|
||||||
|
upm_delay_ms(100);
|
||||||
}
|
}
|
||||||
//! [Interesting]
|
//! [Interesting]
|
||||||
|
|
||||||
|
@ -131,6 +131,7 @@ add_example (md)
|
|||||||
add_example (linefinder)
|
add_example (linefinder)
|
||||||
add_example (uln200xa)
|
add_example (uln200xa)
|
||||||
add_example (mma7660)
|
add_example (mma7660)
|
||||||
|
add_example (buzzer)
|
||||||
|
|
||||||
# Custom examples
|
# Custom examples
|
||||||
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
||||||
|
68
examples/c/buzzer.c
Normal file
68
examples/c/buzzer.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* 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 <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include <upm_utilities.h>
|
||||||
|
#include <buzzer.h>
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
@ -22,28 +22,29 @@
|
|||||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import upm_buzzer.Buzzer;
|
||||||
public class BuzzerSample {
|
public class BuzzerSample {
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
// ! [Interesting]
|
// ! [Interesting]
|
||||||
int chord[] = {
|
int chord[] = {
|
||||||
upm_buzzer.javaupm_buzzer.DO,
|
upm_buzzer.javaupm_buzzer.BUZZER_DO,
|
||||||
upm_buzzer.javaupm_buzzer.RE,
|
upm_buzzer.javaupm_buzzer.BUZZER_RE,
|
||||||
upm_buzzer.javaupm_buzzer.MI,
|
upm_buzzer.javaupm_buzzer.BUZZER_MI,
|
||||||
upm_buzzer.javaupm_buzzer.FA,
|
upm_buzzer.javaupm_buzzer.BUZZER_FA,
|
||||||
upm_buzzer.javaupm_buzzer.SOL,
|
upm_buzzer.javaupm_buzzer.BUZZER_SOL,
|
||||||
upm_buzzer.javaupm_buzzer.LA,
|
upm_buzzer.javaupm_buzzer.BUZZER_LA,
|
||||||
upm_buzzer.javaupm_buzzer.SI};
|
upm_buzzer.javaupm_buzzer.BUZZER_SI};
|
||||||
|
|
||||||
// Instantiate a buzzer on digital pin D5
|
// Instantiate a buzzer on digital pin D5
|
||||||
upm_buzzer.Buzzer sound = new upm_buzzer.Buzzer(5);
|
Buzzer sound = new Buzzer(5);
|
||||||
|
|
||||||
// print sensor name
|
// print sensor name
|
||||||
System.out.println(sound.name());
|
System.out.println(sound.name());
|
||||||
|
|
||||||
for (int i = 0; i < chord.length; i++) {
|
for (int i = 0; i < chord.length; i++) {
|
||||||
// play each note for one second
|
// play each note for one half second
|
||||||
int note = sound.playSound(chord[i], 1000000);
|
int note = sound.playSound(chord[i], 500000);
|
||||||
System.out.println(note);
|
System.out.println(note);
|
||||||
|
|
||||||
Thread.sleep(100);
|
Thread.sleep(100);
|
||||||
@ -52,4 +53,4 @@ public class BuzzerSample {
|
|||||||
sound.stopSound();
|
sound.stopSound();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@ add_example(Apds9002 apds9002)
|
|||||||
add_example(BISS0001Sample biss0001)
|
add_example(BISS0001Sample biss0001)
|
||||||
add_example(BMPX8XSample bmpx8x)
|
add_example(BMPX8XSample bmpx8x)
|
||||||
add_example(BuzzerSample buzzer)
|
add_example(BuzzerSample buzzer)
|
||||||
add_example(Buzzer_soundSample buzzer)
|
|
||||||
add_example(CJQ4435Sample cjq4435)
|
add_example(CJQ4435Sample cjq4435)
|
||||||
add_example(DS1307Sample ds1307)
|
add_example(DS1307Sample ds1307)
|
||||||
add_example(ENC03RSample enc03r)
|
add_example(ENC03RSample enc03r)
|
||||||
|
@ -26,15 +26,13 @@ var upmBuzzer = require("jsupm_buzzer");
|
|||||||
// Initialize on GPIO 5
|
// Initialize on GPIO 5
|
||||||
var myBuzzer = new upmBuzzer.Buzzer(5);
|
var myBuzzer = new upmBuzzer.Buzzer(5);
|
||||||
var chords = [];
|
var chords = [];
|
||||||
chords.push(upmBuzzer.DO);
|
chords.push(upmBuzzer.BUZZER_DO);
|
||||||
chords.push(upmBuzzer.RE);
|
chords.push(upmBuzzer.BUZZER_RE);
|
||||||
chords.push(upmBuzzer.MI);
|
chords.push(upmBuzzer.BUZZER_MI);
|
||||||
chords.push(upmBuzzer.FA);
|
chords.push(upmBuzzer.BUZZER_FA);
|
||||||
chords.push(upmBuzzer.SOL);
|
chords.push(upmBuzzer.BUZZER_SOL);
|
||||||
chords.push(upmBuzzer.LA);
|
chords.push(upmBuzzer.BUZZER_LA);
|
||||||
chords.push(upmBuzzer.SI);
|
chords.push(upmBuzzer.BUZZER_SI);
|
||||||
chords.push(upmBuzzer.DO);
|
|
||||||
chords.push(upmBuzzer.SI);
|
|
||||||
var chordIndex = 0;
|
var chordIndex = 0;
|
||||||
|
|
||||||
// Print sensor name
|
// Print sensor name
|
||||||
@ -44,8 +42,8 @@ function melody()
|
|||||||
{
|
{
|
||||||
if (chords.length != 0)
|
if (chords.length != 0)
|
||||||
{
|
{
|
||||||
//Play sound for one second
|
//Play sound for one half second
|
||||||
console.log( myBuzzer.playSound(chords[chordIndex], 1000000) );
|
console.log( myBuzzer.playSound(chords[chordIndex], 500000) );
|
||||||
chordIndex++;
|
chordIndex++;
|
||||||
//Reset the sound to start from the beginning.
|
//Reset the sound to start from the beginning.
|
||||||
if (chordIndex > chords.length - 1)
|
if (chordIndex > chords.length - 1)
|
||||||
|
@ -28,17 +28,17 @@ def main():
|
|||||||
# Create the buzzer object using GPIO pin 5
|
# Create the buzzer object using GPIO pin 5
|
||||||
buzzer = upmBuzzer.Buzzer(5)
|
buzzer = upmBuzzer.Buzzer(5)
|
||||||
|
|
||||||
chords = [upmBuzzer.DO, upmBuzzer.RE, upmBuzzer.MI, upmBuzzer.FA,
|
chords = [upmBuzzer.BUZZER_DO, upmBuzzer.BUZZER_RE, upmBuzzer.BUZZER_MI,
|
||||||
upmBuzzer.SOL, upmBuzzer.LA, upmBuzzer.SI, upmBuzzer.DO,
|
upmBuzzer.BUZZER_FA, upmBuzzer.BUZZER_SOL, upmBuzzer.BUZZER_LA,
|
||||||
upmBuzzer.SI];
|
upmBuzzer.BUZZER_SI];
|
||||||
|
|
||||||
# Print sensor name
|
# Print sensor name
|
||||||
print(buzzer.name())
|
print(buzzer.name())
|
||||||
|
|
||||||
# Play sound (DO, RE, MI, etc.), pausing for 0.1 seconds between notes
|
# Play sound (DO, RE, MI, etc.), pausing for 0.1 seconds between notes
|
||||||
for chord_ind in range (0,7):
|
for chord_ind in range (0,7):
|
||||||
# play each note for one second
|
# play each note for a half second
|
||||||
print(buzzer.playSound(chords[chord_ind], 1000000))
|
print(buzzer.playSound(chords[chord_ind], 500000))
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
print("exiting application")
|
print("exiting application")
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
set (libname "buzzer")
|
upm_mixed_module_init (NAME buzzer
|
||||||
set (libdescription "Buzzer")
|
DESCRIPTION "Buzzer"
|
||||||
set (module_src ${libname}.cxx)
|
C_HDR buzzer.h buzzer_tones.h
|
||||||
set (module_hpp ${libname}.hpp)
|
C_SRC buzzer.c
|
||||||
upm_module_init()
|
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>
|
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||||
* Copyright (c) 2014 Intel Corporation.
|
* Copyright (c) 2014 Intel Corporation.
|
||||||
*
|
*
|
||||||
@ -32,46 +36,40 @@
|
|||||||
using namespace upm;
|
using namespace upm;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Buzzer::Buzzer(int pinNumber) {
|
Buzzer::Buzzer(int pinNumber) : m_buzzer(buzzer_init(pinNumber))
|
||||||
m_pwm_context = mraa_pwm_init(pinNumber);
|
{
|
||||||
if(m_pwm_context == 0)
|
if (!m_buzzer)
|
||||||
{
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
": buzzer_init() failed");
|
||||||
": mraa_pwm_init() failed, invalid pin?");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_name = "Buzzer";
|
|
||||||
mraa_pwm_enable (m_pwm_context, 1);
|
|
||||||
Buzzer::setVolume(1.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buzzer::setVolume(float vol){
|
Buzzer::~Buzzer()
|
||||||
m_volume = vol;
|
{
|
||||||
|
buzzer_close(m_buzzer);
|
||||||
}
|
}
|
||||||
|
|
||||||
float Buzzer::getVolume(){
|
void Buzzer::setVolume(float vol)
|
||||||
return m_volume;
|
{
|
||||||
|
buzzer_set_volume(m_buzzer, vol);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Buzzer::playSound(int note, int delay) {
|
float Buzzer::getVolume()
|
||||||
mraa_pwm_period_us(m_pwm_context, note);
|
{
|
||||||
mraa_pwm_write(m_pwm_context, m_volume * 0.5);
|
return buzzer_get_volume(m_buzzer);
|
||||||
if(delay){
|
}
|
||||||
usleep(delay);
|
|
||||||
Buzzer::stopSound();
|
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;
|
return note;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Buzzer::stopSound(){
|
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);
|
if (buzzer_stop_sound(m_buzzer))
|
||||||
mraa_pwm_write(m_pwm_context, 0);
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
}
|
": buzzer_stop_sound() failed");
|
||||||
|
|
||||||
Buzzer::~Buzzer() {
|
|
||||||
Buzzer::stopSound();
|
|
||||||
mraa_pwm_enable(m_pwm_context, 0);
|
|
||||||
mraa_pwm_close(m_pwm_context);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>
|
* Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
|
||||||
* Copyright (c) 2014 Intel Corporation.
|
* Copyright (c) 2014 Intel Corporation.
|
||||||
*
|
*
|
||||||
@ -24,69 +28,62 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <mraa/pwm.h>
|
#include <buzzer.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
|
|
||||||
|
|
||||||
namespace upm {
|
namespace upm {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Buzzer library
|
* @brief Buzzer library
|
||||||
* @defgroup buzzer libupm-buzzer
|
* @defgroup buzzer libupm-buzzer
|
||||||
* @ingroup seeed pwm sound gsk
|
* @ingroup seeed pwm sound gsk
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @library buzzer
|
* @library buzzer
|
||||||
* @sensor buzzer
|
* @sensor buzzer
|
||||||
* @comname Grove Buzzer
|
* @comname Grove Buzzer
|
||||||
* @type sound
|
* @type sound
|
||||||
* @man seeed
|
* @man seeed
|
||||||
* @con pwm
|
* @con pwm
|
||||||
* @kit gsk
|
* @kit gsk
|
||||||
*
|
*
|
||||||
* @brief API for the Buzzer component
|
* @brief API for the Buzzer component
|
||||||
*
|
*
|
||||||
* This module defines the Buzzer interface for libbuzzer.
|
* This module defines the Buzzer interface for libbuzzer.
|
||||||
* This sensor can make different tones when connected to
|
* This sensor can make different tones when connected to
|
||||||
* a pin capable of analog pulse-width modulation. It emits
|
* a pin capable of analog pulse-width modulation. It emits
|
||||||
* sound using a piezoelectric material that vibrates at different
|
* sound using a piezoelectric material that vibrates at different
|
||||||
* frequencies based on the input voltage.
|
* frequencies based on the input voltage.
|
||||||
*
|
*
|
||||||
* @image html buzzer.jpg
|
* @image html buzzer.jpg
|
||||||
* @snippet buzzer-sound.cxx Interesting
|
* @snippet buzzer-sound.cxx Interesting
|
||||||
*/
|
*/
|
||||||
class Buzzer {
|
|
||||||
|
class Buzzer {
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Instantiates a Buzzer object.
|
* Instantiates a Buzzer object.
|
||||||
*
|
*
|
||||||
* @param pinNumber Buzzer pin number
|
* @param pinNumber Buzzer pin number
|
||||||
*/
|
*/
|
||||||
Buzzer (int pinNumber);
|
Buzzer(int pinNumber);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Buzzer object destructor.
|
* Buzzer object destructor.
|
||||||
*/
|
*/
|
||||||
~Buzzer ();
|
~Buzzer();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays a tone for a certain amount of time or indefinitely. When delay
|
* Plays a tone for a certain amount of time or indefinitely. When delay
|
||||||
* is not used, the sound can be stopped by calling stopSound().
|
* is not used, the sound can be stopped by calling stopSound().
|
||||||
*
|
*
|
||||||
* @param note Note to play (DO, RE, MI, etc.) or frequency
|
* @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
|
* @param delay Time in microseconds for which to play the
|
||||||
* 0, the sound is played indefinitely
|
* sound; if the value is 0, the sound is played indefinitely
|
||||||
*
|
*
|
||||||
* @return Note played
|
* @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()
|
* Stops the sound currently playing. Should be called when playSound()
|
||||||
@ -120,8 +117,6 @@ class Buzzer {
|
|||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
private:
|
buzzer_context m_buzzer;
|
||||||
mraa_pwm_context m_pwm_context;
|
};
|
||||||
float m_volume;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
* Copyright (c) 2015 Intel Corporation.
|
* 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
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* 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
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
#pragma once
|
||||||
|
|
||||||
public class Buzzer_soundSample {
|
// Hz = 1000000 / N
|
||||||
|
#define BUZZER_DO 3800 // 262 Hz - C4
|
||||||
public static void main(String[] args) throws InterruptedException {
|
#define BUZZER_RE 3400 // 294 Hz - D
|
||||||
//! [Interesting]
|
#define BUZZER_MI 3000 // 330 Hz - E
|
||||||
int chord[] = {
|
#define BUZZER_FA 2900 // 349 Hz - F
|
||||||
upm_buzzer.javaupm_buzzer.DO,
|
#define BUZZER_SOL 2550 // 392 Hz - G
|
||||||
upm_buzzer.javaupm_buzzer.RE,
|
#define BUZZER_LA 2270 // 440 Hz - A
|
||||||
upm_buzzer.javaupm_buzzer.MI,
|
#define BUZZER_SI 2000 // 494 Hz - B
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +1,13 @@
|
|||||||
%module javaupm_buzzer
|
%module javaupm_buzzer
|
||||||
%include "../upm.i"
|
%include "../upm.i"
|
||||||
|
|
||||||
|
|
||||||
|
%include "buzzer_tones.h"
|
||||||
|
%include "buzzer.hpp"
|
||||||
%{
|
%{
|
||||||
#include "buzzer.hpp"
|
#include "buzzer.hpp"
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%include "buzzer.hpp"
|
|
||||||
|
|
||||||
%pragma(java) jniclasscode=%{
|
%pragma(java) jniclasscode=%{
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
@ -16,4 +17,4 @@
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
%module jsupm_buzzer
|
%module jsupm_buzzer
|
||||||
%include "../upm.i"
|
%include "../upm.i"
|
||||||
|
|
||||||
|
%include "buzzer_tones.h"
|
||||||
|
%include "buzzer.hpp"
|
||||||
%{
|
%{
|
||||||
#include "buzzer.hpp"
|
#include "buzzer.hpp"
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%include "buzzer.hpp"
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
%feature("autodoc", "3");
|
%feature("autodoc", "3");
|
||||||
|
|
||||||
|
%include "buzzer_tones.h"
|
||||||
%include "buzzer.hpp"
|
%include "buzzer.hpp"
|
||||||
%{
|
%{
|
||||||
#include "buzzer.hpp"
|
#include "buzzer.hpp"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user