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:
Jon Trulson
2017-02-01 17:03:10 -07:00
parent 0749f130e1
commit dac31a0347
12 changed files with 563 additions and 11 deletions

View File

@ -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;