A simple digital lock with attempted audio guidance

Dependencies:   FATFileSystem TextLCD mbed

Fork of Digital_Lock_with_audio by NITH ece

Revision:
4:74d697416c75
Parent:
2:9bdba6204bb4
Child:
5:d45608f231d3
--- a/main.cpp	Sat May 11 18:37:21 2013 +0000
+++ b/main.cpp	Sun May 12 09:04:10 2013 +0000
@@ -100,6 +100,9 @@
 short block_align;
 short sig_bps;
 } FMT_STRUCT;
+
+
+
 void play_wave(char *wavname)
 {
 unsigned chunk_id,chunk_size,channel;
@@ -123,9 +126,12 @@
 DAC_on=0;
 
 led1=led2=led3=led4=0;
+lcd.cls();
+lcd.printf("Playing wave file '%s'\n",wavname);
 
 wavfile=fopen(wavname,"rb");
 if (!wavfile) {
+    lcd.cls();
     printf("Unable to open wav file '%s'\n",wavname);
     exit(1);
     }
@@ -133,27 +139,74 @@
 fread(&chunk_id,4,1,wavfile);
 fread(&chunk_size,4,1,wavfile);
 while (!feof(wavfile)) {
+
+
+    lcd.cls();
+    lcd.printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size);
     switch (chunk_id) {
       case 0x46464952:
         fread(&data,4,1,wavfile);
+        lcd.cls();
+        lcd.printf("RIFF chunk\n");
+        wait(1);
+        lcd.cls();
+        lcd.printf("  chunk size %d (0x%x)\n",chunk_size,chunk_size);
+        lcd.cls();
+        lcd.printf("  RIFF type 0x%x\n",data);
         break;
       case 0x20746d66:
         fread(&wav_format,sizeof(wav_format),1,wavfile);
+        lcd.cls();
+        lcd.printf("FORMAT chunk\n");
+        wait(1);
+        lcd.cls();
+        lcd.printf("  chunk size %d (0x%x)\n",chunk_size,chunk_size);
+        wait(1);
+        lcd.cls();
+        lcd.printf("  compression code %d\n",wav_format.comp_code);
+        wait(1);
+        lcd.cls();
+        lcd.printf("  %d channels\n",wav_format.num_channels);
+        wait(1);
+        lcd.cls();
+        lcd.printf("  %d samples/sec\n",wav_format.sample_rate);
+        wait(1);
+        lcd.cls();
+        lcd.printf("  %d bytes/sec\n",wav_format.avg_Bps);
+        wait(1);
+        lcd.cls();
+        lcd.printf("  block align %d\n",wav_format.block_align);
+        wait(1);
+        lcd.cls();
+        lcd.printf("  %d bits per sample\n",wav_format.sig_bps);
         if (chunk_size > sizeof(wav_format))
         fseek(wavfile,chunk_size-sizeof(wav_format),SEEK_CUR);
 // create a slice buffer large enough to hold multiple slices
         slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE);
         if (!slice_buf) {
+        lcd.cls();
+        lcd.printf("Unable to malloc slice buffer");
         exit(1);
         }
         break;
       case 0x61746164:
         slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE);
         if (!slice_buf) {
+        lcd.cls();
+        lcd.printf("Unable to malloc slice buffer");
         exit(1);
         }        num_slices=chunk_size/wav_format.block_align;
+        lcd.cls();
+        lcd.printf("DATA chunk\n");
+        lcd.cls();
+        lcd.printf("  chunk size %d (0x%x)\n",chunk_size,chunk_size);
+        lcd.cls();
+        lcd.printf("  %d slices\n",num_slices);
+        lcd.cls();
+        lcd.printf("  Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate));
         samp_int=1000000/(wav_format.sample_rate);
-   
+        lcd.cls();
+        lcd.printf("  programmed interrupt tick interval=%d\n",samp_int);
 // starting up ticker to write samples out -- no printfs until tick.detach is called
         tick.attach_us(&dac_out, samp_int); 
         DAC_on=1; 
@@ -161,6 +214,8 @@
         for (slice=0;slice<num_slices;slice+=SLICE_BUF_SIZE) {
         fread(slice_buf,wav_format.block_align*SLICE_BUF_SIZE,1,wavfile);
         if (feof(wavfile)) {
+        lcd.cls();
+         lcd.printf("Oops -- not enough slices in the wave file\n");
         exit(1);
           }
           data_sptr=(short *)slice_buf;
@@ -187,19 +242,27 @@
         DAC_on=0;
         led2=0;
         tick.detach();
+        lcd.cls();
+        lcd.printf("Ticker detached\n");
         led3=1;
         free(slice_buf);
         break;
       case 0x5453494c:
+      lcd.cls();
+      lcd.printf("INFO chunk, size %d\n",chunk_size);
         fseek(wavfile,chunk_size,SEEK_CUR);
         break;
       default:
+      lcd.cls();
+      lcd.printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size);
         data=fseek(wavfile,chunk_size,SEEK_CUR);
         break;
     }
     fread(&chunk_id,4,1,wavfile);
     fread(&chunk_size,4,1,wavfile);
   }
+  lcd.cls();
+  lcd.printf("Done with wave file\n");
   fclose(wavfile);
   led1=0;
 }
@@ -266,7 +329,8 @@
 //------------------------------------------------to start enter new password which user wants to set
 lcd.cls();
 lcd.printf("enter your new password\n");
-play_wave("/sd/forest.wav");
+
+//play_wave("/sd/forest.wav");
 wait(0.5);
 
   int k;
@@ -290,7 +354,7 @@
 AGAIN:    
     lcd.cls();
     lcd.printf("1-open door\n2-reset password");
-    //play_wave("/sd/forest.wav");
+    play_wave("/sd/forest.wav");
     interrupt.fall(&fallInterrupt);
     interrupt.mode(PullUp);
   
@@ -302,7 +366,7 @@
     lcd.cls();    
     //---------------------------------------------------enter the password
     lcd.printf("enter your password\n");
-    //play_wave("/sd/forest.wav");
+    play_wave("/sd/forest.wav");
     wait(1);
     
     while(1)
@@ -328,7 +392,7 @@
                               wait(1);
                               lcd.cls();
                               lcd.printf("opening door...\n"); 
-                           // play_wave("/sd/forest.wav");
+                               play_wave("/sd/forest.wav");
                               myled = 1;
         myled1 = 0;
         wait(5);