Test program for PwmSound class. Provides tests (demos) for the various sound generating functions in the class. Includes several MML melodies and music clips to test the play() function. Tested on an mbed LPC1768.

Dependencies:   PwmSound mbed

Committer:
paulg
Date:
Sun Mar 30 22:54:52 2014 +0000
Revision:
0:41640fc98a43
Child:
1:46f098444d0c
Test program for PwmSound class.; Initial release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
paulg 0:41640fc98a43 1 /******************************************************************************
paulg 0:41640fc98a43 2 * File: main.cpp
paulg 0:41640fc98a43 3 * Author: Paul Griffith
paulg 0:41640fc98a43 4 * Created: 25 Mar 2014
paulg 0:41640fc98a43 5 * Last Edit: see below
paulg 0:41640fc98a43 6 * Version: see below
paulg 0:41640fc98a43 7 *
paulg 0:41640fc98a43 8 * Description:
paulg 0:41640fc98a43 9 * Test program for PwmSound class.
paulg 0:41640fc98a43 10 *
paulg 0:41640fc98a43 11 * Copyright (c) 2014 Paul Griffith, MIT License
paulg 0:41640fc98a43 12 *
paulg 0:41640fc98a43 13 * Permission is hereby granted, free of charge, to any person obtaining a copy
paulg 0:41640fc98a43 14 * of this software and associated documentation files (the "Software"), to
paulg 0:41640fc98a43 15 * deal in the Software without restriction, including without limitation the
paulg 0:41640fc98a43 16 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
paulg 0:41640fc98a43 17 * sell copies of the Software, and to permit persons to whom the Software is
paulg 0:41640fc98a43 18 * furnished to do so, subject to the following conditions:
paulg 0:41640fc98a43 19 *
paulg 0:41640fc98a43 20 * The above copyright notice and this permission notice shall be included in
paulg 0:41640fc98a43 21 * all copies or substantial portions of the Software.
paulg 0:41640fc98a43 22 *
paulg 0:41640fc98a43 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
paulg 0:41640fc98a43 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
paulg 0:41640fc98a43 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
paulg 0:41640fc98a43 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
paulg 0:41640fc98a43 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
paulg 0:41640fc98a43 28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
paulg 0:41640fc98a43 29 * IN THE SOFTWARE.
paulg 0:41640fc98a43 30 *
paulg 0:41640fc98a43 31 * Modifications:
paulg 0:41640fc98a43 32 * Ver Date By Details
paulg 0:41640fc98a43 33 * 0.00 25Mar14 PG File created.
paulg 0:41640fc98a43 34 * 1.00 30Mar14 PG Initial release.
paulg 0:41640fc98a43 35 *
paulg 0:41640fc98a43 36 ******************************************************************************/
paulg 0:41640fc98a43 37
paulg 0:41640fc98a43 38 #include "mbed.h"
paulg 0:41640fc98a43 39 #include "PwmSound.h"
paulg 0:41640fc98a43 40
paulg 0:41640fc98a43 41 PwmSound mySpeaker(p24);
paulg 0:41640fc98a43 42 Serial pc(USBTX, USBRX);
paulg 0:41640fc98a43 43
paulg 0:41640fc98a43 44 void badArgs(void);
paulg 0:41640fc98a43 45 void nyi(void);
paulg 0:41640fc98a43 46 void pause(void);
paulg 0:41640fc98a43 47
paulg 0:41640fc98a43 48 // a simple melody (8 notes)
paulg 0:41640fc98a43 49
paulg 0:41640fc98a43 50 int melody[8] = {
paulg 0:41640fc98a43 51 37, 32, 32, 34, 32, 0, 36, 37
paulg 0:41640fc98a43 52 };
paulg 0:41640fc98a43 53
paulg 0:41640fc98a43 54 int melodyDurations[] = {
paulg 0:41640fc98a43 55 4, 8, 8, 4, 4, 4, 4, 4
paulg 0:41640fc98a43 56 };
paulg 0:41640fc98a43 57
paulg 0:41640fc98a43 58 // Dragnet theme (9 notes)
paulg 0:41640fc98a43 59
paulg 0:41640fc98a43 60 unsigned char dragnet[2+(9*2)+2] = {
paulg 0:41640fc98a43 61 90, //tempo
paulg 0:41640fc98a43 62 PwmSound::NORMAL, //style
paulg 0:41640fc98a43 63 29, 5, 31, 16, 32, 8, 0, 8, 29, 5, 31, 16, 32, 8, 29, 8, 35, 3,
paulg 0:41640fc98a43 64 0, 0 //terminator
paulg 0:41640fc98a43 65 };
paulg 0:41640fc98a43 66
paulg 0:41640fc98a43 67 int main()
paulg 0:41640fc98a43 68 {
paulg 0:41640fc98a43 69 int i, n;
paulg 0:41640fc98a43 70
paulg 0:41640fc98a43 71 pc.printf("\nPWM Sound Demo 7, %s\n", __DATE__);
paulg 0:41640fc98a43 72
paulg 0:41640fc98a43 73 while(1) {
paulg 0:41640fc98a43 74 pc.printf("0 = 2-tone, 1 = b/g, 2x = melody, 3x = trill, 4x = phone, 5 = Dragnet, 6x-9x = beeps: ");
paulg 0:41640fc98a43 75 n = pc.getc() - '0';
paulg 0:41640fc98a43 76 pc.printf("%d", n);
paulg 0:41640fc98a43 77 if (n == 0) {
paulg 0:41640fc98a43 78 pc.printf("(= to stop)");
paulg 0:41640fc98a43 79 mySpeaker.siren(0);
paulg 0:41640fc98a43 80 }
paulg 0:41640fc98a43 81 else if (n == 1) {
paulg 0:41640fc98a43 82 mySpeaker.tone(440.0, 0.0);
paulg 0:41640fc98a43 83 pc.getc();
paulg 0:41640fc98a43 84 mySpeaker.stop();
paulg 0:41640fc98a43 85 } else if (n == 2) {
paulg 0:41640fc98a43 86 i = pc.getc() - '0';
paulg 0:41640fc98a43 87 pc.printf("%d", i);
paulg 0:41640fc98a43 88 mySpeaker.tempo(240);
paulg 0:41640fc98a43 89 mySpeaker.style( (PwmSound::MusicStyle) (i % 3));
paulg 0:41640fc98a43 90 for (int i = 0; i < 8; i++) {
paulg 0:41640fc98a43 91 mySpeaker.note(melody[i], melodyDurations[i]);
paulg 0:41640fc98a43 92 }
paulg 0:41640fc98a43 93 } else if (n == 3) {
paulg 0:41640fc98a43 94 i = pc.getc() - '0';
paulg 0:41640fc98a43 95 pc.printf("%d", i);
paulg 0:41640fc98a43 96 mySpeaker.trill(i);
paulg 0:41640fc98a43 97 } else if (n == 4) {
paulg 0:41640fc98a43 98 i = pc.getc() - '0';
paulg 0:41640fc98a43 99 pc.printf("%d", i);
paulg 0:41640fc98a43 100 mySpeaker.phone(i);
paulg 0:41640fc98a43 101 } else if (n == 5) {
paulg 0:41640fc98a43 102 mySpeaker.tune(dragnet);
paulg 0:41640fc98a43 103 } else if (n == 6) {
paulg 0:41640fc98a43 104 i = pc.getc() - '0';
paulg 0:41640fc98a43 105 pc.printf("%d", i);
paulg 0:41640fc98a43 106 mySpeaker.bip(i);
paulg 0:41640fc98a43 107 } else if (n == 7) {
paulg 0:41640fc98a43 108 i = pc.getc() - '0';
paulg 0:41640fc98a43 109 pc.printf("%d", i);
paulg 0:41640fc98a43 110 mySpeaker.beep(i);
paulg 0:41640fc98a43 111 } else if (n == 8) {
paulg 0:41640fc98a43 112 i = pc.getc() - '0';
paulg 0:41640fc98a43 113 pc.printf("%d", i);
paulg 0:41640fc98a43 114 mySpeaker.bleep(i);
paulg 0:41640fc98a43 115 } else if (n == 9) {
paulg 0:41640fc98a43 116 i = pc.getc() - '0';
paulg 0:41640fc98a43 117 pc.printf("%d", i);
paulg 0:41640fc98a43 118 mySpeaker.buzz(i);
paulg 0:41640fc98a43 119 } else if (n == 13) { //press = to stop a continuous sound
paulg 0:41640fc98a43 120 mySpeaker.stop();
paulg 0:41640fc98a43 121 } else {
paulg 0:41640fc98a43 122 badArgs();
paulg 0:41640fc98a43 123 }
paulg 0:41640fc98a43 124 pc.printf("\n");
paulg 0:41640fc98a43 125 } //end of while
paulg 0:41640fc98a43 126 }
paulg 0:41640fc98a43 127
paulg 0:41640fc98a43 128 //************************
paulg 0:41640fc98a43 129 // Miscellaneous functions
paulg 0:41640fc98a43 130 //************************
paulg 0:41640fc98a43 131
paulg 0:41640fc98a43 132 void badArgs(void) {
paulg 0:41640fc98a43 133 pc.printf("?? Bad arguments\n");
paulg 0:41640fc98a43 134 }
paulg 0:41640fc98a43 135
paulg 0:41640fc98a43 136 void nyi(void) {
paulg 0:41640fc98a43 137 pc.printf("!! Not yet implemented\n");
paulg 0:41640fc98a43 138 }
paulg 0:41640fc98a43 139
paulg 0:41640fc98a43 140 void pause(void)
paulg 0:41640fc98a43 141 {
paulg 0:41640fc98a43 142 pc.printf("Press any key to continue . . .\n");
paulg 0:41640fc98a43 143 pc.getc();
paulg 0:41640fc98a43 144 }
paulg 0:41640fc98a43 145
paulg 0:41640fc98a43 146 //END of main.cpp