guiiiii

Dependencies:   mbed wave_player mbed-rtos SDFileSystem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "SDFileSystem.h"
00004 #include "wave_player.h"
00005 #include <string>
00006 
00007 SDFileSystem sd(p5, p6, p7, p8, "sd");  // the pinout on the mbed Cool Components workshop board
00008 Serial pc (USBTX,USBRX);
00009 DigitalOut led(LED1);
00010 AnalogOut DACout(p18); // speaker
00011 wave_player waver(&DACout);
00012 //#define MAXLINE 100
00013 //char myline [MAXLINE];
00014 //int i, inch ;
00015 string dir;
00016 //string song = "africa-toto";
00017 string song;
00018 bool play;
00019 Mutex speaker_lock;
00020 
00021 
00022 
00023 void speaker_thread(void const* args)
00024 {
00025     while(1) {
00026         // check helper function for new song in GUI
00027         // grab file here and put together string
00028         // string song_title = "/sd/" + "" + ".wav";
00029         
00030         
00031         if(play == true)
00032         {
00033             //speaker_lock.lock();
00034             FILE *wave_file;
00035             dir = "/sd/" + song + ".wav";
00036             wave_file=fopen(dir.c_str(),"r");
00037             waver.play(wave_file);
00038             fclose(wave_file);
00039             //speaker_lock.unlock();
00040         }
00041         Thread::wait(1000);
00042     }
00043 }
00044 
00045 
00046 
00047 
00048 
00049 
00050 
00051 
00052 
00053 
00054 
00055 
00056 int main()
00057 {
00058     
00059     char c;
00060     Thread speaker(speaker_thread);
00061     //led = 1;
00062     while(1) {
00063         wait(0.2);
00064         
00065         while(!pc.readable()) {
00066             Thread::wait(1);
00067         }
00068         if (pc.getc() == '!') {
00069             c = pc.getc();
00070             switch(c) {
00071                 case '1':
00072                     song = "africa-toto";
00073                     //led = 1;
00074                     break;
00075                 case '2':
00076                     song = "around_the_world-atc";
00077                     break;
00078                 case '3':
00079                     song = "beautiful_life-ace_of_base";
00080                     break;
00081                 case '4':
00082                     song = "dont_speak-no_doubt";
00083                     break;
00084                 case '5':
00085                     song = "my-love";
00086                     break;
00087                 case '6':
00088                     song = "Song1_test";
00089                     //speaker.terminate();
00090                     break;
00091                 case 'P':
00092                     // wave_player plays
00093                     play = true;
00094                     break;
00095                 case 'S':
00096                     // wave_player stops
00097                     play = false;
00098                     //led!=led;
00099                     //speaker.terminate();
00100                     break;
00101                 default:
00102                     break;
00103             }
00104             
00105             if(play==true){led=1;}
00106             else{led=0;}
00107         }
00108         Thread::wait(200);
00109     }
00110 }
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118 //============================================================================//
00119 /*
00120 int main() {
00121     printf("\n\r= = = = = = = = = =");
00122     printf("\n\rHello World!\n\r");
00123     mkdir("/sd/mydir", 0777); // Test that previous run had created permanent folder
00124     // write
00125     FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); // Commenting out, in a later run, from here ..
00126     if(fp == NULL) {
00127         error("Could not open file for write\n");
00128     }
00129     fprintf(fp, "Hello from SD Card World!");
00130     fclose(fp);
00131     printf("Created file in sdcard\n\r"); //.. to here shows file really is permanent .. .. since it still reads correctly.
00132     // read
00133     printf("Reading file\n");
00134     string inputString;
00135     FILE *fp1 = fopen("/sd/mydir/sdtest.txt", "r");
00136     if (fp1 == NULL) {
00137        printf("Error Open \n");
00138     } else {
00139         printf("\r");
00140         while(fscanf(fp1, "%s", inputString)!=EOF) {
00141             printf("%s ", inputString.c_str());
00142         }
00143     }
00144     fclose(fp1);                              // close the file
00145     printf("\n\rFinished reading the file");
00146     printf("\n\r= = = = = = = = = =");
00147 
00148     return 0;
00149 }
00150 */