alarm

Dependencies:   PinDetect SDFileSystem mbed wave_player

Committer:
jhawkins38
Date:
Tue Dec 02 15:21:13 2014 +0000
Revision:
0:a2ded96ce136
for alarm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhawkins38 0:a2ded96ce136 1 #include "mbed.h"
jhawkins38 0:a2ded96ce136 2 #include "PinDetect.h"
jhawkins38 0:a2ded96ce136 3 #include "SDFileSystem.h"
jhawkins38 0:a2ded96ce136 4 #include <vector>
jhawkins38 0:a2ded96ce136 5 #include <string>
jhawkins38 0:a2ded96ce136 6 #include "wave_player.h"
jhawkins38 0:a2ded96ce136 7
jhawkins38 0:a2ded96ce136 8 DigitalOut myled(LED1);
jhawkins38 0:a2ded96ce136 9 PwmOut alarm(p22);
jhawkins38 0:a2ded96ce136 10 AnalogOut DACout(p18);
jhawkins38 0:a2ded96ce136 11 PinDetect snooze(p23);
jhawkins38 0:a2ded96ce136 12 PinDetect stop(p24);
jhawkins38 0:a2ded96ce136 13 SDFileSystem sd(p5, p6, p7, p8, p12, "sd"); //SD card
jhawkins38 0:a2ded96ce136 14
jhawkins38 0:a2ded96ce136 15
jhawkins38 0:a2ded96ce136 16 volatile bool t = true;
jhawkins38 0:a2ded96ce136 17 volatile bool play = true;
jhawkins38 0:a2ded96ce136 18 volatile float vol = .52;
jhawkins38 0:a2ded96ce136 19 volatile bool insert;
jhawkins38 0:a2ded96ce136 20 volatile int index;
jhawkins38 0:a2ded96ce136 21 wave_player waver(&DACout);
jhawkins38 0:a2ded96ce136 22 int num = 0;
jhawkins38 0:a2ded96ce136 23 vector<string> filenames; //filenames are stored in a vector string
jhawkins38 0:a2ded96ce136 24
jhawkins38 0:a2ded96ce136 25 void read_file_names(char *dir)
jhawkins38 0:a2ded96ce136 26 {
jhawkins38 0:a2ded96ce136 27 DIR *dp;
jhawkins38 0:a2ded96ce136 28 struct dirent *dirp;
jhawkins38 0:a2ded96ce136 29 dp = opendir(dir);
jhawkins38 0:a2ded96ce136 30 //read all directory and file names in current directory into filename vector
jhawkins38 0:a2ded96ce136 31 while((dirp = readdir(dp)) != NULL) {
jhawkins38 0:a2ded96ce136 32 filenames.push_back(string(dirp->d_name));
jhawkins38 0:a2ded96ce136 33 num++;
jhawkins38 0:a2ded96ce136 34 }
jhawkins38 0:a2ded96ce136 35 num= num;
jhawkins38 0:a2ded96ce136 36 }
jhawkins38 0:a2ded96ce136 37
jhawkins38 0:a2ded96ce136 38 void snooze_hit_callback(void) {
jhawkins38 0:a2ded96ce136 39 alarm = 0.0;
jhawkins38 0:a2ded96ce136 40 wait(20);
jhawkins38 0:a2ded96ce136 41 if (insert == true) {
jhawkins38 0:a2ded96ce136 42 play =! play;
jhawkins38 0:a2ded96ce136 43 wait(20);
jhawkins38 0:a2ded96ce136 44 play =! play;
jhawkins38 0:a2ded96ce136 45 }
jhawkins38 0:a2ded96ce136 46 }
jhawkins38 0:a2ded96ce136 47
jhawkins38 0:a2ded96ce136 48 void stop_hit_callback(void){
jhawkins38 0:a2ded96ce136 49 //alarm = 0.0;
jhawkins38 0:a2ded96ce136 50 //state = off;
jhawkins38 0:a2ded96ce136 51 myled = 1;
jhawkins38 0:a2ded96ce136 52 vol = 0.0;
jhawkins38 0:a2ded96ce136 53 //wait(1000);
jhawkins38 0:a2ded96ce136 54 t = false;
jhawkins38 0:a2ded96ce136 55 if (insert == true){
jhawkins38 0:a2ded96ce136 56 play =! play;
jhawkins38 0:a2ded96ce136 57 }
jhawkins38 0:a2ded96ce136 58 }
jhawkins38 0:a2ded96ce136 59 int main() {
jhawkins38 0:a2ded96ce136 60 snooze.mode(PullUp);
jhawkins38 0:a2ded96ce136 61 stop.mode(PullUp);
jhawkins38 0:a2ded96ce136 62 //pb3.mode(PullUp);
jhawkins38 0:a2ded96ce136 63
jhawkins38 0:a2ded96ce136 64 //Delay for initial pullup to take effect
jhawkins38 0:a2ded96ce136 65 wait(.01);
jhawkins38 0:a2ded96ce136 66
jhawkins38 0:a2ded96ce136 67 //Setup Interrupt callback functions for a pb hit
jhawkins38 0:a2ded96ce136 68 snooze.attach_deasserted(&snooze_hit_callback);
jhawkins38 0:a2ded96ce136 69 stop.attach_deasserted(&stop_hit_callback);
jhawkins38 0:a2ded96ce136 70 //pb3.attach_deasserted(&pb3_hit_callback);
jhawkins38 0:a2ded96ce136 71
jhawkins38 0:a2ded96ce136 72 //Start sampling pb inputs using interrupts
jhawkins38 0:a2ded96ce136 73 snooze.setSampleFrequency(); //default is 20KHz sampling
jhawkins38 0:a2ded96ce136 74 stop.setSampleFrequency();
jhawkins38 0:a2ded96ce136 75 //pb3.setSampleFrequency();
jhawkins38 0:a2ded96ce136 76 //pushbuttons now setup and running
jhawkins38 0:a2ded96ce136 77
jhawkins38 0:a2ded96ce136 78
jhawkins38 0:a2ded96ce136 79 while(t == true) {
jhawkins38 0:a2ded96ce136 80 insert = sd.SD_inserted();
jhawkins38 0:a2ded96ce136 81 if ((insert == true) && (play ==true)) {
jhawkins38 0:a2ded96ce136 82 read_file_names("/sd/myMusic");
jhawkins38 0:a2ded96ce136 83 //string str2 = filenames[index].substr(0,filenames[index].length()-4);
jhawkins38 0:a2ded96ce136 84 FILE *wave_file;
jhawkins38 0:a2ded96ce136 85 string filepath = "/sd/mymusic/";
jhawkins38 0:a2ded96ce136 86 wave_file = fopen( (filepath+filenames[index]).c_str(), "r");
jhawkins38 0:a2ded96ce136 87 waver.play(wave_file, &play);
jhawkins38 0:a2ded96ce136 88 if (feof(wave_file)) // when playback ends file will close and play will equal false
jhawkins38 0:a2ded96ce136 89 {
jhawkins38 0:a2ded96ce136 90 fclose(wave_file);
jhawkins38 0:a2ded96ce136 91 //play=!play;
jhawkins38 0:a2ded96ce136 92 }
jhawkins38 0:a2ded96ce136 93
jhawkins38 0:a2ded96ce136 94 myled != myled;
jhawkins38 0:a2ded96ce136 95 }
jhawkins38 0:a2ded96ce136 96 if ((insert == false)&&(play == true)) {
jhawkins38 0:a2ded96ce136 97 // generate a 500Hz tone using PWM hardware output
jhawkins38 0:a2ded96ce136 98
jhawkins38 0:a2ded96ce136 99 for (int j=0; j<26; j=j+2) {
jhawkins38 0:a2ded96ce136 100 alarm.period(1.0/500.0); // 500hz period
jhawkins38 0:a2ded96ce136 101 alarm =vol; //50% duty cycle - max volume
jhawkins38 0:a2ded96ce136 102 wait(.5);
jhawkins38 0:a2ded96ce136 103 //alarm=0.0; // turn off audio
jhawkins38 0:a2ded96ce136 104 alarm.period(0.0/500.0);
jhawkins38 0:a2ded96ce136 105 wait(.5);
jhawkins38 0:a2ded96ce136 106 } // end of for
jhawkins38 0:a2ded96ce136 107
jhawkins38 0:a2ded96ce136 108 // two tone police siren effect - two periods or two frequencies
jhawkins38 0:a2ded96ce136 109 // increase volume - by changing the PWM duty cycle
jhawkins38 0:a2ded96ce136 110 for (int i=0; i<26; i=i+2) {
jhawkins38 0:a2ded96ce136 111 alarm = vol;
jhawkins38 0:a2ded96ce136 112 alarm.period(1.0/969.0);
jhawkins38 0:a2ded96ce136 113 wait(.5);
jhawkins38 0:a2ded96ce136 114 alarm.period(1.0/800.0);
jhawkins38 0:a2ded96ce136 115 wait(.5);
jhawkins38 0:a2ded96ce136 116 } //end of for
jhawkins38 0:a2ded96ce136 117
jhawkins38 0:a2ded96ce136 118 //alarm = 0.0;
jhawkins38 0:a2ded96ce136 119 } //end of if
jhawkins38 0:a2ded96ce136 120
jhawkins38 0:a2ded96ce136 121 }// end of while
jhawkins38 0:a2ded96ce136 122 while (t == false) {
jhawkins38 0:a2ded96ce136 123 alarm = 0.0;
jhawkins38 0:a2ded96ce136 124 //fclose(wave_file);
jhawkins38 0:a2ded96ce136 125 }
jhawkins38 0:a2ded96ce136 126 }// end of main