mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
speaker: Add pwm functionality and 5 new examples to demonstrate
These changes add the ability to intialize a speaker instance to use a PWM pin instead of a standard GPIO pin in order to emit sounds. The current GPIO functionality is fairly constrained by the implementation -- it only permits playing cerain hardcoded "notes". The PWM support allows one to emit specific frequencies (currently between 50-32Khz) using a PWM pin. Of course, you may still be constrained by the limits of your PWM hardware in the end. There are a few new functions provided to support this PWM mode. To use the PWM mode of speaker, use the speaker_init_pwm() initialization routine instead of speaker_init() (for C). For C++ and the SWIG languages, pass "true" as the second argument to the constructor to enable PWM mode. The default is "false", to match current default behavior (GPIO). Functions that are not related to a given mode (GPIO vs. PWM) will do nothing if not usable in that mode. Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
parent
0749f130e1
commit
dac31a0347
@ -374,3 +374,4 @@ add_custom_example (nmea_gps_i2c_example-cxx nmea_gps_i2c.cxx nmea_gps)
|
||||
add_custom_example (mcp2515-txrx-example-cxx mcp2515-txrx.cxx mcp2515)
|
||||
add_custom_example (ads1015-example-cxx ads1015.cxx ads1x15)
|
||||
add_custom_example (le910-example-cxx le910.cxx uartat)
|
||||
add_custom_example (speaker_pwm-example-cxx speaker_pwm.cxx speaker)
|
||||
|
55
examples/c++/speaker_pwm.cxx
Normal file
55
examples/c++/speaker_pwm.cxx
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2017 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 "speaker.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main ()
|
||||
{
|
||||
//! [Interesting]
|
||||
// Instantiate a Speaker (PWM) pin D3 in PWM mode
|
||||
upm::Speaker* speaker = new upm::Speaker(3, true);
|
||||
|
||||
// emit a series of frequencies for 500ms each
|
||||
speaker->emit(50, 500);
|
||||
speaker->emit(75, 500);
|
||||
speaker->emit(125, 500);
|
||||
speaker->emit(250, 500);
|
||||
speaker->emit(500, 500);
|
||||
speaker->emit(1000, 500);
|
||||
speaker->emit(2000, 500);
|
||||
speaker->emit(3000, 500);
|
||||
speaker->emit(5000, 500);
|
||||
speaker->emit(10000, 500);
|
||||
|
||||
cout << "Exiting" << endl;
|
||||
|
||||
delete speaker;
|
||||
|
||||
//! [Interesting]
|
||||
return 0;
|
||||
}
|
@ -159,3 +159,4 @@ add_custom_example (md-stepper-example-c md-stepper.c md)
|
||||
add_custom_example (button_intr-example-c button_intr.c button)
|
||||
add_custom_example (mcp2515-txrx-example-c mcp2515-txrx.c mcp2515)
|
||||
add_custom_example (le910-example-c le910.c uartat)
|
||||
add_custom_example (speaker_pwm-example-c speaker_pwm.c speaker)
|
||||
|
61
examples/c/speaker_pwm.c
Normal file
61
examples/c/speaker_pwm.c
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2017 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 "speaker.h"
|
||||
|
||||
int main ()
|
||||
{
|
||||
//! [Interesting]
|
||||
|
||||
// Instantiate a Speaker (PWM) on digital pin D3
|
||||
speaker_context speaker = speaker_init_pwm(26);
|
||||
|
||||
if (!speaker)
|
||||
{
|
||||
printf("speaker_init() failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// emit a series of frequencies for 500ms each
|
||||
speaker_emit(speaker, 50, 500);
|
||||
speaker_emit(speaker, 75, 500);
|
||||
speaker_emit(speaker, 125, 500);
|
||||
speaker_emit(speaker, 250, 500);
|
||||
speaker_emit(speaker, 500, 500);
|
||||
speaker_emit(speaker, 1000, 500);
|
||||
speaker_emit(speaker, 2000, 500);
|
||||
speaker_emit(speaker, 3000, 500);
|
||||
speaker_emit(speaker, 5000, 500);
|
||||
speaker_emit(speaker, 10000, 500);
|
||||
|
||||
printf("Exiting\n");
|
||||
|
||||
speaker_close(speaker);
|
||||
|
||||
//! [Interesting]
|
||||
|
||||
return 0;
|
||||
}
|
@ -186,3 +186,4 @@ endif()
|
||||
add_example_with_path(NMEAGPS_I2C_Example nmea_gps nmea_gps)
|
||||
add_example_with_path(MCP2515_TXRX_Example mcp2515 mcp2515)
|
||||
add_example_with_path(LE910_Example uartat uartat)
|
||||
add_example_with_path(SpeakerPWMSample speaker speaker)
|
||||
|
46
examples/java/SpeakerPWMSample.java
Normal file
46
examples/java/SpeakerPWMSample.java
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2017 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.
|
||||
*/
|
||||
|
||||
public class SpeakerPWMSample {
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
// ! [Interesting]
|
||||
// Instantiate a Speaker (PWM) pin D3 in PWM mode
|
||||
upm_speaker.Speaker speaker = new upm_speaker.Speaker(3, true);
|
||||
|
||||
// emit a series of frequencies for 500ms each
|
||||
speaker.emit(50, 500);
|
||||
speaker.emit(75, 500);
|
||||
speaker.emit(125, 500);
|
||||
speaker.emit(250, 500);
|
||||
speaker.emit(500, 500);
|
||||
speaker.emit(1000, 500);
|
||||
speaker.emit(2000, 500);
|
||||
speaker.emit(3000, 500);
|
||||
speaker.emit(5000, 500);
|
||||
speaker.emit(10000, 500);
|
||||
// ! [Interesting]
|
||||
}
|
||||
|
||||
}
|
47
examples/javascript/speaker_pwm.js
Normal file
47
examples/javascript/speaker_pwm.js
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2017 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.
|
||||
*/
|
||||
|
||||
//Load Speaker module
|
||||
var Speaker = require('jsupm_speaker');
|
||||
// Instantiate a Speaker (PWM) pin D3 in PWM mode
|
||||
var speaker = new Speaker.Speaker(3, true);
|
||||
|
||||
// emit a series of frequencies for 500ms each
|
||||
speaker.emit(50, 500);
|
||||
speaker.emit(75, 500);
|
||||
speaker.emit(125, 500);
|
||||
speaker.emit(250, 500);
|
||||
speaker.emit(500, 500);
|
||||
speaker.emit(1000, 500);
|
||||
speaker.emit(2000, 500);
|
||||
speaker.emit(3000, 500);
|
||||
speaker.emit(5000, 500);
|
||||
speaker.emit(10000, 500);
|
||||
|
||||
// Print message when exiting
|
||||
process.on('SIGINT', function()
|
||||
{
|
||||
console.log("Exiting...");
|
||||
process.exit(0);
|
||||
});
|
44
examples/python/speaker_pwm.py
Executable file
44
examples/python/speaker_pwm.py
Executable file
@ -0,0 +1,44 @@
|
||||
#!/usr/bin/python
|
||||
# Author: Jon Trulson <jtrulson@ics.com>
|
||||
# Copyright (c) 2017 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.
|
||||
|
||||
import time, sys, signal, atexit
|
||||
from upm import pyupm_speaker as upmspeaker
|
||||
|
||||
def main():
|
||||
# Instantiate a Speaker (PWM) pin D3 in PWM mode
|
||||
speaker = upmspeaker.Speaker(3, True)
|
||||
|
||||
# emit a series of frequencies for 500ms each
|
||||
speaker.emit(50, 500);
|
||||
speaker.emit(75, 500);
|
||||
speaker.emit(125, 500);
|
||||
speaker.emit(250, 500);
|
||||
speaker.emit(500, 500);
|
||||
speaker.emit(1000, 500);
|
||||
speaker.emit(2000, 500);
|
||||
speaker.emit(3000, 500);
|
||||
speaker.emit(5000, 500);
|
||||
speaker.emit(10000, 500);
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -62,7 +62,7 @@ static noteData_t note_list[7] = { // index, note
|
||||
// forward decl
|
||||
static void speaker_sound(const speaker_context dev, int note_delay);
|
||||
|
||||
speaker_context speaker_init(int pin)
|
||||
static speaker_context _common_init()
|
||||
{
|
||||
// make sure MRAA is initialized
|
||||
int mraa_rv;
|
||||
@ -81,6 +81,17 @@ speaker_context speaker_init(int pin)
|
||||
// zero out context
|
||||
memset((void *)dev, 0, sizeof(struct _speaker_context));
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
speaker_context speaker_init(int pin)
|
||||
{
|
||||
speaker_context dev = NULL;
|
||||
if (!(dev = _common_init()))
|
||||
return NULL;
|
||||
|
||||
dev->is_pwm = false;
|
||||
|
||||
if (!(dev->gpio = mraa_gpio_init(pin)))
|
||||
{
|
||||
printf("%s: mraa_gpio_init() failed.\n", __FUNCTION__);
|
||||
@ -98,6 +109,28 @@ speaker_context speaker_init(int pin)
|
||||
return dev;
|
||||
}
|
||||
|
||||
// initialization of PWM uasage
|
||||
speaker_context speaker_init_pwm(int pin)
|
||||
{
|
||||
speaker_context dev = NULL;
|
||||
if (!(dev = _common_init()))
|
||||
return NULL;
|
||||
|
||||
dev->is_pwm = true;
|
||||
|
||||
if (!(dev->pwm = mraa_pwm_init(pin)))
|
||||
{
|
||||
printf("%s: mraa_pwm_init() failed.\n", __FUNCTION__);
|
||||
speaker_close(dev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// set the default frequency to 1Khz
|
||||
dev->default_freq = 1000;
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
void speaker_close(speaker_context dev)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
@ -105,13 +138,114 @@ void speaker_close(speaker_context dev)
|
||||
if (dev->gpio)
|
||||
mraa_gpio_close(dev->gpio);
|
||||
|
||||
if (dev->pwm)
|
||||
{
|
||||
speaker_off(dev);
|
||||
mraa_pwm_close(dev->pwm);
|
||||
}
|
||||
|
||||
free(dev);
|
||||
}
|
||||
|
||||
upm_result_t speaker_set_frequency(const speaker_context dev,
|
||||
unsigned int freq)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
if (!dev->is_pwm)
|
||||
return UPM_ERROR_NO_RESOURCES;
|
||||
|
||||
if (freq < 50 || freq > 32000)
|
||||
{
|
||||
printf("%s: freq must be between 50 and 32000.\n", __FUNCTION__);
|
||||
|
||||
return UPM_ERROR_OPERATION_FAILED;
|
||||
}
|
||||
|
||||
float period = 1.0 / (float)freq;
|
||||
|
||||
// printf("PERIOD: %f (freq %d)\n", period, freq);
|
||||
|
||||
if (period >= 0.001) // ms range
|
||||
{
|
||||
if (mraa_pwm_period(dev->pwm, period))
|
||||
{
|
||||
printf("%s: mraa_pwm_period() failed.\n", __FUNCTION__);
|
||||
|
||||
return UPM_ERROR_OPERATION_FAILED;
|
||||
}
|
||||
}
|
||||
else // us range. With a max of 32KHz, no less than 3.125 us.
|
||||
{
|
||||
if (mraa_pwm_period_us(dev->pwm, (int)(period * 1000000)))
|
||||
{
|
||||
printf("%s: mraa_pwm_period_us() failed.\n", __FUNCTION__);
|
||||
|
||||
return UPM_ERROR_OPERATION_FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
// save it for later if needed
|
||||
dev->default_freq = freq;
|
||||
|
||||
// A 10% duty cycle enables better results at high frequencies
|
||||
mraa_pwm_write(dev->pwm, 0.1);
|
||||
|
||||
return UPM_SUCCESS;
|
||||
}
|
||||
|
||||
upm_result_t speaker_emit(const speaker_context dev, unsigned int freq,
|
||||
unsigned int emit_ms)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
if (!dev->is_pwm)
|
||||
return UPM_ERROR_NO_RESOURCES;
|
||||
|
||||
if (speaker_set_frequency(dev, freq))
|
||||
return UPM_ERROR_OPERATION_FAILED;
|
||||
|
||||
upm_clock_t clock;
|
||||
upm_clock_init(&clock);
|
||||
|
||||
mraa_pwm_enable(dev->pwm, 1);
|
||||
while (upm_elapsed_ms(&clock) < emit_ms)
|
||||
; // loop until finished
|
||||
|
||||
mraa_pwm_enable(dev->pwm, 0);
|
||||
|
||||
return UPM_SUCCESS;
|
||||
}
|
||||
|
||||
void speaker_on(const speaker_context dev)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
if (!dev->is_pwm)
|
||||
return;
|
||||
|
||||
speaker_set_frequency(dev, dev->default_freq);
|
||||
|
||||
mraa_pwm_enable(dev->pwm, 1);
|
||||
}
|
||||
|
||||
void speaker_off(const speaker_context dev)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
if (!dev->is_pwm)
|
||||
return;
|
||||
|
||||
mraa_pwm_enable(dev->pwm, 0);
|
||||
}
|
||||
|
||||
void speaker_play_all(const speaker_context dev)
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
if (dev->is_pwm)
|
||||
return;
|
||||
|
||||
speaker_play_sound(dev, 'c', false, "low");
|
||||
upm_delay_us(200000);
|
||||
speaker_play_sound(dev, 'd', false, "low");
|
||||
@ -133,6 +267,9 @@ void speaker_play_sound(const speaker_context dev, char letter, bool sharp,
|
||||
{
|
||||
assert(dev != NULL);
|
||||
|
||||
if (dev->is_pwm)
|
||||
return;
|
||||
|
||||
int index = 0;
|
||||
switch (letter)
|
||||
{
|
||||
|
@ -34,12 +34,17 @@
|
||||
|
||||
using namespace upm;
|
||||
|
||||
Speaker::Speaker(int pin) :
|
||||
m_speaker(speaker_init(pin))
|
||||
Speaker::Speaker(int pin, bool usePWM) :
|
||||
m_speaker(nullptr)
|
||||
{
|
||||
if (!m_speaker)
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": speaker_init() failed.");
|
||||
if (usePWM)
|
||||
m_speaker = speaker_init_pwm(pin);
|
||||
else
|
||||
m_speaker = speaker_init(pin);
|
||||
|
||||
if (!m_speaker)
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": speaker_init()/speaker_init_pwm() failed.");
|
||||
}
|
||||
|
||||
Speaker::~Speaker()
|
||||
@ -57,3 +62,26 @@ void Speaker::playSound(char letter, bool sharp, std::string vocalWeight)
|
||||
speaker_play_sound(m_speaker, letter, sharp, vocalWeight.c_str());
|
||||
}
|
||||
|
||||
void Speaker::emit(unsigned int freq, unsigned int emit_ms)
|
||||
{
|
||||
if (speaker_emit(m_speaker, freq, emit_ms))
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": speaker_emit() failed.");
|
||||
}
|
||||
|
||||
void Speaker::setFrequency(unsigned int freq)
|
||||
{
|
||||
if (speaker_set_frequency(m_speaker, freq))
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": speaker_set_frequency() failed.");
|
||||
}
|
||||
|
||||
void Speaker::on()
|
||||
{
|
||||
speaker_on(m_speaker);
|
||||
}
|
||||
|
||||
void Speaker::off()
|
||||
{
|
||||
speaker_off(m_speaker);
|
||||
}
|
||||
|
@ -31,6 +31,8 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <mraa/gpio.h>
|
||||
#include <mraa/pwm.h>
|
||||
|
||||
#include <upm.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@ -49,17 +51,39 @@ extern "C" {
|
||||
* Device context
|
||||
*/
|
||||
typedef struct _speaker_context {
|
||||
// gpio version
|
||||
mraa_gpio_context gpio;
|
||||
|
||||
// pwm version
|
||||
mraa_pwm_context pwm;
|
||||
|
||||
// are using the original GPIO mechanism or PWM?
|
||||
bool is_pwm;
|
||||
// default frequency, or last frequency specified
|
||||
unsigned int default_freq;
|
||||
} *speaker_context;
|
||||
|
||||
/**
|
||||
* Speaker GPIO init
|
||||
* Speaker GPIO init. In this mode, only the speaker_play_all()
|
||||
* and speaker_play_sound() function will work. The other
|
||||
* functions only support PWM mode (see speaker_init_pwm()).
|
||||
*
|
||||
* @param pin Digital pin to use
|
||||
* @return Device context
|
||||
*/
|
||||
speaker_context speaker_init(int pin);
|
||||
|
||||
/**
|
||||
* Speaker PWM init. In this mode a PWM pin is used to emit tones
|
||||
* (within the capabilities of your PWM hardware).
|
||||
* speaker_play_all() and speaker_play_sound() will not operate in
|
||||
* this mode. The default frequency is set to 1KHz.
|
||||
*
|
||||
* @param pin Digital PWM capable pin to use
|
||||
* @return Device context
|
||||
*/
|
||||
speaker_context speaker_init_pwm(int pin);
|
||||
|
||||
/**
|
||||
* Speaker close function
|
||||
*
|
||||
@ -68,14 +92,16 @@ extern "C" {
|
||||
void speaker_close(speaker_context dev);
|
||||
|
||||
/**
|
||||
* Plays all alto notes (lowest notes)
|
||||
* Plays all alto notes (lowest notes). This function only
|
||||
* operates in GPIO mode.
|
||||
*
|
||||
* @param dev Device context
|
||||
*/
|
||||
void speaker_play_all(const speaker_context dev);
|
||||
|
||||
/**
|
||||
* Plays a sound and a note whether it's sharp or not
|
||||
* Plays a sound and a note whether it's sharp or not. This
|
||||
* function only operates in GPIO mode.
|
||||
*
|
||||
* @param dev Device context
|
||||
* @param letter Character name of the note
|
||||
@ -88,6 +114,58 @@ extern "C" {
|
||||
void speaker_play_sound(const speaker_context dev, char letter, bool sharp,
|
||||
const char *vocal_weight);
|
||||
|
||||
/**
|
||||
* Emit a specific frequency for a given period of time and
|
||||
* return. This function only operates when in PWM mode (ie: the
|
||||
* speaker context was initialized with speaker_init_pwm()). The
|
||||
* frequency is limited to between 50-32000Hz. In addition, the
|
||||
* allowable frequencies may be restricted further by the
|
||||
* capabilities of your PWM hardware.
|
||||
*
|
||||
* @param dev Device context
|
||||
* @param freq The frequency to emit. Must be between 50 and 32000Hz
|
||||
* inclusive.
|
||||
* @param emit_ms The number of milliseconds to emit the frequency.
|
||||
* @return UPM result
|
||||
*/
|
||||
upm_result_t speaker_emit(const speaker_context dev, unsigned int freq,
|
||||
unsigned int emit_ms);
|
||||
|
||||
/**
|
||||
* Set a default frequency to be used with speaker_on() and
|
||||
* speaker_off(). This function only operates when in PWM mode
|
||||
* (ie: the speaker context was initialized with
|
||||
* speaker_init_pwm()). The frequency is limited to between
|
||||
* 50-32000Hz. In addition, the allowable frequencies may be
|
||||
* restricted further by the capabilities of your PWM hardware.
|
||||
*
|
||||
* @param dev Device context
|
||||
* @param freq The frequency to emit. Must be between 50 and 32000Hz
|
||||
* inclusive.
|
||||
* @return UPM result
|
||||
*/
|
||||
upm_result_t speaker_set_frequency(const speaker_context dev,
|
||||
unsigned int freq);
|
||||
|
||||
/**
|
||||
* Turn the speaker on, and emit the frequency last specified with
|
||||
* speaker_set_frequency() or speaker_emit(). This function only
|
||||
* operates when in PWM mode (ie: the speaker context was
|
||||
* initialized with speaker_init_pwm()).
|
||||
*
|
||||
* @param dev Device context
|
||||
*/
|
||||
void speaker_on(const speaker_context dev);
|
||||
|
||||
/**
|
||||
* Turn the speaker off. This function only operates when in PWM
|
||||
* mode (ie: the speaker context was initialized with
|
||||
* speaker_init_pwm()).
|
||||
*
|
||||
* @param dev Device context
|
||||
*/
|
||||
void speaker_off(const speaker_context dev);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -55,6 +55,14 @@ namespace upm {
|
||||
* This sensor can generate different tones and sounds depending on the
|
||||
* frequency of the input signal.
|
||||
*
|
||||
* It can operate in one of two modes: GPIO (default) and PWM.
|
||||
*
|
||||
* Depending on which mode is selected, some methods may not be
|
||||
* usable. In GPIO mode, the playAll() and playSound() methods
|
||||
* are supported. In PWM mode, setFrequency(), emit(), on() and
|
||||
* off() are supported. Calling a method not appropriate for the
|
||||
* mode will have no effect.
|
||||
*
|
||||
* @image html speaker.jpg
|
||||
* @snippet speaker.cxx Interesting
|
||||
*/
|
||||
@ -64,13 +72,16 @@ namespace upm {
|
||||
* Speaker constructor
|
||||
*
|
||||
* @param pin Digital pin to use
|
||||
* @param usePWM If true, PWM mode will be used, otherwise
|
||||
* GPIO mode (default) is used.
|
||||
*/
|
||||
Speaker(int pin);
|
||||
Speaker(int pin, bool usePWM=false);
|
||||
|
||||
/**
|
||||
* Speaker destructor
|
||||
*/
|
||||
~Speaker();
|
||||
|
||||
virtual ~Speaker();
|
||||
/**
|
||||
* Plays all alto notes (lowest notes)
|
||||
*
|
||||
@ -89,6 +100,48 @@ namespace upm {
|
||||
*/
|
||||
void playSound(char letter, bool sharp, std::string vocalWeight);
|
||||
|
||||
/**
|
||||
* Emit a specific frequency for a given period of time and
|
||||
* return. This function only operates when in PWM mode. The
|
||||
* frequency is limited to between 50-32000Hz. In addition,
|
||||
* the allowable frequencies may be restricted further by the
|
||||
* capabilities of your PWM hardware.
|
||||
*
|
||||
* @param freq The frequency to emit. Must be between 50 and 32000Hz
|
||||
* inclusive.
|
||||
* @param emit_ms The number of milliseconds to emit the frequency.
|
||||
*/
|
||||
void emit(unsigned int freq, unsigned int emit_ms);
|
||||
|
||||
/**
|
||||
* Set a default frequency to be used with on() and off().
|
||||
* This function only operates when in PWM mode.
|
||||
* The frequency is limited to between 50-32000Hz. In
|
||||
* addition, the allowable frequencies may be restricted
|
||||
* further by the capabilities of your PWM hardware.
|
||||
*
|
||||
* @param freq The frequency to emit. Must be between 50 and 32000Hz
|
||||
* inclusive.
|
||||
*/
|
||||
void setFrequency(unsigned int freq);
|
||||
|
||||
/**
|
||||
* Turn the speaker on, and emit the frequency last specified
|
||||
* with setFrequency() or emit(). This function only operates
|
||||
* when in PWM mode.
|
||||
*
|
||||
* @param dev Device context
|
||||
*/
|
||||
void on();
|
||||
|
||||
/**
|
||||
* Turn the speaker off. This function only operates when in
|
||||
* PWM mode.
|
||||
*
|
||||
* @param dev Device context
|
||||
*/
|
||||
void off();
|
||||
|
||||
protected:
|
||||
speaker_context m_speaker;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user