diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt index a9c8b269..f2e5e55f 100644 --- a/examples/c++/CMakeLists.txt +++ b/examples/c++/CMakeLists.txt @@ -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) diff --git a/examples/c++/speaker_pwm.cxx b/examples/c++/speaker_pwm.cxx new file mode 100644 index 00000000..95236890 --- /dev/null +++ b/examples/c++/speaker_pwm.cxx @@ -0,0 +1,55 @@ +/* + * Author: Jon Trulson + * 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 +#include +#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; +} diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index e1687614..5a614b52 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -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) diff --git a/examples/c/speaker_pwm.c b/examples/c/speaker_pwm.c new file mode 100644 index 00000000..b552bfa0 --- /dev/null +++ b/examples/c/speaker_pwm.c @@ -0,0 +1,61 @@ +/* + * Author: Jon Trulson + * 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 +#include +#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; +} diff --git a/examples/java/CMakeLists.txt b/examples/java/CMakeLists.txt index fe248c70..b3873de3 100644 --- a/examples/java/CMakeLists.txt +++ b/examples/java/CMakeLists.txt @@ -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) diff --git a/examples/java/SpeakerPWMSample.java b/examples/java/SpeakerPWMSample.java new file mode 100644 index 00000000..9f256aa9 --- /dev/null +++ b/examples/java/SpeakerPWMSample.java @@ -0,0 +1,46 @@ +/* + * Author: Jon Trulson + * 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] + } + +} diff --git a/examples/javascript/speaker_pwm.js b/examples/javascript/speaker_pwm.js new file mode 100644 index 00000000..f353af48 --- /dev/null +++ b/examples/javascript/speaker_pwm.js @@ -0,0 +1,47 @@ +/* + * Author: Jon Trulson + * 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); +}); diff --git a/examples/python/speaker_pwm.py b/examples/python/speaker_pwm.py new file mode 100755 index 00000000..0e6c6940 --- /dev/null +++ b/examples/python/speaker_pwm.py @@ -0,0 +1,44 @@ +#!/usr/bin/python +# Author: Jon Trulson +# 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() diff --git a/src/speaker/speaker.c b/src/speaker/speaker.c index f2643d10..744d054b 100644 --- a/src/speaker/speaker.c +++ b/src/speaker/speaker.c @@ -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) { diff --git a/src/speaker/speaker.cxx b/src/speaker/speaker.cxx index e3bbebc9..276e0ec8 100644 --- a/src/speaker/speaker.cxx +++ b/src/speaker/speaker.cxx @@ -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); +} diff --git a/src/speaker/speaker.h b/src/speaker/speaker.h index 17f7cc7b..ebebb9b9 100644 --- a/src/speaker/speaker.h +++ b/src/speaker/speaker.h @@ -31,6 +31,8 @@ #include #include +#include + #include #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 diff --git a/src/speaker/speaker.hpp b/src/speaker/speaker.hpp index b666dacf..7da5ff52 100644 --- a/src/speaker/speaker.hpp +++ b/src/speaker/speaker.hpp @@ -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;