Lab4 4180 photocell controlled cursor game

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PwmSpeaker.cpp Source File

PwmSpeaker.cpp

00001 #include "PwmSpeaker.h"
00002 
00003 PwmSpeaker::PwmSpeaker(PinName pin) : _pin(pin) {
00004     // _pin(pin) means pass pin to the Speaker Constructor
00005 }
00006 
00007 // class method to play a note based on PwmOut class
00008 void PwmSpeaker::playNote(float frequency, float duration, float volume) {
00009     _pin.period(1.0/frequency);
00010     _pin = volume/2.0; //50% duty cycle - max volume
00011     wait(duration);
00012     _pin = 0.0; //turn off note
00013 }
00014 
00015 // generate a short 150Hz tone using PWM hardware output
00016 // something like this can be used for a button click effect for feedback
00017     //for (i=0; i<10; i++) {
00018 //        speaker.period(1.0/150.0); // 500hz period
00019 //        speaker =0.25; //25% duty cycle - mid range volume
00020 //        wait(.02);
00021 //        speaker=0.0; // turn off audio
00022 //        wait(0.5);
00023 //    }
00024 // sweep up in frequency by changing the PWM period
00025     //for (i=0; i<8000; i=i+100) {
00026 //        speaker.period(1.0/float(i));
00027 //        speaker=0.25;
00028 //        wait(.1);
00029 //    }
00030 //    wait(2);
00031 // two tone police siren effect -  two periods or two frequencies
00032 // increase volume - by changing the PWM duty cycle
00033 //    for (i=0; i<26; i=i+2) {
00034 //        speaker.period(1.0/969.0);
00035 //        speaker = float(i)/50.0;
00036 //        wait(.5);
00037 //        speaker.period(1.0/800.0);
00038 //        wait(.5);
00039 //    }
00040 //// decrease volume
00041 //    for (i=25; i>=0; i=i-2) {
00042 //        speaker.period(1.0/969.0);
00043 //        speaker = float(i)/50.0;
00044 //        wait(.5);
00045 //        speaker.period(1.0/800.0);
00046 //        wait(.5);
00047 //    }
00048 //    speaker =0.0;
00049 //    wait(2);
00050 //}