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

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