alarm
Dependencies: PinDetect SDFileSystem mbed wave_player
Revision 0:a2ded96ce136, committed 2014-12-02
- Comitter:
- jhawkins38
- Date:
- Tue Dec 02 15:21:13 2014 +0000
- Commit message:
- for alarm
Changed in this revision
diff -r 000000000000 -r a2ded96ce136 PinDetect.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PinDetect.lib Tue Dec 02 15:21:13 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/jhawkins38/code/PinDetect/#cb3afc45028b
diff -r 000000000000 -r a2ded96ce136 SDFileSystem.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SDFileSystem.lib Tue Dec 02 15:21:13 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/jhawkins38/code/SDFileSystem/#b8c141fd8e0e
diff -r 000000000000 -r a2ded96ce136 Speaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Tue Dec 02 15:21:13 2014 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +// a new class to play a note on Speaker based on PwmOut class +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +}; \ No newline at end of file
diff -r 000000000000 -r a2ded96ce136 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Dec 02 15:21:13 2014 +0000 @@ -0,0 +1,126 @@ +#include "mbed.h" +#include "PinDetect.h" +#include "SDFileSystem.h" +#include <vector> +#include <string> +#include "wave_player.h" + +DigitalOut myled(LED1); +PwmOut alarm(p22); +AnalogOut DACout(p18); +PinDetect snooze(p23); +PinDetect stop(p24); +SDFileSystem sd(p5, p6, p7, p8, p12, "sd"); //SD card + + +volatile bool t = true; +volatile bool play = true; + volatile float vol = .52; + volatile bool insert; + volatile int index; +wave_player waver(&DACout); +int num = 0; +vector<string> filenames; //filenames are stored in a vector string + +void read_file_names(char *dir) +{ + DIR *dp; + struct dirent *dirp; + dp = opendir(dir); + //read all directory and file names in current directory into filename vector + while((dirp = readdir(dp)) != NULL) { + filenames.push_back(string(dirp->d_name)); + num++; + } + num= num; +} + +void snooze_hit_callback(void) { + alarm = 0.0; + wait(20); + if (insert == true) { + play =! play; + wait(20); + play =! play; + } +} + +void stop_hit_callback(void){ + //alarm = 0.0; + //state = off; + myled = 1; + vol = 0.0; + //wait(1000); + t = false; + if (insert == true){ + play =! play; + } +} +int main() { +snooze.mode(PullUp); +stop.mode(PullUp); +//pb3.mode(PullUp); + +//Delay for initial pullup to take effect +wait(.01); + +//Setup Interrupt callback functions for a pb hit +snooze.attach_deasserted(&snooze_hit_callback); +stop.attach_deasserted(&stop_hit_callback); +//pb3.attach_deasserted(&pb3_hit_callback); + +//Start sampling pb inputs using interrupts +snooze.setSampleFrequency(); //default is 20KHz sampling +stop.setSampleFrequency(); +//pb3.setSampleFrequency(); +//pushbuttons now setup and running + + +while(t == true) { + insert = sd.SD_inserted(); + if ((insert == true) && (play ==true)) { + read_file_names("/sd/myMusic"); + //string str2 = filenames[index].substr(0,filenames[index].length()-4); + FILE *wave_file; + string filepath = "/sd/mymusic/"; + wave_file = fopen( (filepath+filenames[index]).c_str(), "r"); + waver.play(wave_file, &play); + if (feof(wave_file)) // when playback ends file will close and play will equal false + { + fclose(wave_file); + //play=!play; + } + + myled != myled; + } + if ((insert == false)&&(play == true)) { + // generate a 500Hz tone using PWM hardware output + + for (int j=0; j<26; j=j+2) { + alarm.period(1.0/500.0); // 500hz period + alarm =vol; //50% duty cycle - max volume + wait(.5); + //alarm=0.0; // turn off audio + alarm.period(0.0/500.0); + wait(.5); + } // end of for + + // two tone police siren effect - two periods or two frequencies + // increase volume - by changing the PWM duty cycle + for (int i=0; i<26; i=i+2) { + alarm = vol; + alarm.period(1.0/969.0); + wait(.5); + alarm.period(1.0/800.0); + wait(.5); + } //end of for + + //alarm = 0.0; + } //end of if + +}// end of while + while (t == false) { + alarm = 0.0; + //fclose(wave_file); + } +}// end of main
diff -r 000000000000 -r a2ded96ce136 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Dec 02 15:21:13 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file
diff -r 000000000000 -r a2ded96ce136 wave_player.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wave_player.lib Tue Dec 02 15:21:13 2014 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/jhawkins38/code/wave_player/#69c7fc62c0ef