James Hawkins / Mbed 2 deprecated Alarm

Dependencies:   PinDetect SDFileSystem mbed wave_player

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 #include "SDFileSystem.h"
00004 #include <vector>
00005 #include <string>
00006 #include "wave_player.h"
00007 
00008 DigitalOut myled(LED1);
00009 PwmOut alarm(p22);
00010 AnalogOut DACout(p18);
00011 PinDetect snooze(p23);
00012 PinDetect stop(p24);
00013 SDFileSystem sd(p5, p6, p7, p8, p12, "sd"); //SD card
00014 
00015 
00016 volatile bool t = true;
00017 volatile bool play = true;
00018  volatile float vol = .52;
00019  volatile bool insert;
00020  volatile int index;
00021 wave_player waver(&DACout);
00022 int num = 0;
00023 vector<string> filenames; //filenames are stored in a vector string
00024 
00025 void read_file_names(char *dir)
00026 {
00027     DIR *dp;
00028     struct dirent *dirp;
00029     dp = opendir(dir);
00030     //read all directory and file names in current directory into filename vector
00031     while((dirp = readdir(dp)) != NULL) {
00032         filenames.push_back(string(dirp->d_name));
00033         num++;
00034     }
00035     num= num;
00036 }
00037 
00038 void snooze_hit_callback(void) {
00039     alarm = 0.0;
00040     wait(20);
00041     if (insert == true) {
00042         play =! play;
00043         wait(20);
00044         play =! play;
00045     }
00046 }
00047 
00048 void stop_hit_callback(void){
00049     //alarm = 0.0;
00050     //state = off;
00051     myled = 1;
00052    vol = 0.0;
00053    //wait(1000);
00054    t = false;
00055    if (insert == true){
00056        play =! play;
00057        }
00058 }
00059 int main() {
00060 snooze.mode(PullUp);
00061 stop.mode(PullUp);
00062 //pb3.mode(PullUp);
00063 
00064 //Delay for initial pullup to take effect
00065 wait(.01);
00066 
00067 //Setup Interrupt callback functions for a pb hit
00068 snooze.attach_deasserted(&snooze_hit_callback);
00069 stop.attach_deasserted(&stop_hit_callback);
00070 //pb3.attach_deasserted(&pb3_hit_callback);
00071 
00072 //Start sampling pb inputs using interrupts
00073 snooze.setSampleFrequency(); //default is 20KHz sampling
00074 stop.setSampleFrequency(); 
00075 //pb3.setSampleFrequency();
00076 //pushbuttons now setup and running
00077     
00078 
00079 while(t == true) {
00080      insert = sd.SD_inserted();
00081     if ((insert == true) && (play ==true)) {
00082             read_file_names("/sd/myMusic");
00083             //string str2 = filenames[index].substr(0,filenames[index].length()-4);
00084             FILE *wave_file;
00085             string filepath = "/sd/mymusic/";
00086             wave_file = fopen( (filepath+filenames[index]).c_str(), "r");
00087             waver.play(wave_file, &play);
00088            if (feof(wave_file)) // when playback ends file will close and play will equal false
00089             {
00090             fclose(wave_file);
00091             //play=!play;
00092             } 
00093              
00094              myled != myled;
00095         }
00096     if ((insert == false)&&(play == true)) {
00097         // generate a 500Hz tone using PWM hardware output
00098     
00099         for (int j=0; j<26; j=j+2) {
00100             alarm.period(1.0/500.0); // 500hz period
00101             alarm =vol; //50% duty cycle - max volume
00102             wait(.5);
00103             //alarm=0.0; // turn off audio
00104             alarm.period(0.0/500.0);
00105             wait(.5);
00106             } // end of for
00107     
00108         // two tone police siren effect -  two periods or two frequencies
00109         // increase volume - by changing the PWM duty cycle
00110         for (int i=0; i<26; i=i+2) {
00111             alarm = vol;
00112             alarm.period(1.0/969.0);
00113             wait(.5);
00114             alarm.period(1.0/800.0);
00115             wait(.5);
00116           } //end of for
00117     
00118         //alarm = 0.0;
00119     } //end of if
00120 
00121 }// end of while
00122     while (t == false) {
00123         alarm = 0.0;
00124         //fclose(wave_file);
00125         }
00126 }// end of main