Publish code

Dependencies:   FatFileSystem RPG TextLCD 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 "RPG.h"
00003 #include "TextLCD.h"
00004 #include "wave_player.h"
00005 #include "MSCFileSystem.h"
00006 #include <string.h>
00007 #include <cstdio>
00008 
00009 #define FSNAME "msc"
00010 MSCFileSystem msc(FSNAME);
00011 
00012 RPG rpg1(p21,p22,p23); //Set up RPG
00013 TextLCD lcd1(p9, p10, p11, p12, p13, p14, TextLCD::LCD20x4); // rs, e, d4-d7
00014 TextLCD lcd2(p5, p6, p7, p8, p19, p20, TextLCD::LCD20x4);
00015 AnalogOut mySpeaker(p18);
00016 DigitalIn pb_add(p29);
00017 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
00018 AnalogIn position(p17); //slider
00019 wave_player waver(&mySpeaker);
00020 Serial term(USBTX, USBRX); // tx, rx
00021 int volume = 0;
00022 
00023 int count = 0;
00024 int dirt = 0;
00025 char* songList[50];
00026 char* playList[10]; 
00027 int index = 0; 
00028 int index_play = 0;
00029 int len; 
00030 int main()
00031 {    
00032 
00033     DIR *d;
00034     struct dirent *p;
00035     d = opendir("/" FSNAME);
00036     
00037     if ( d != NULL )
00038     {
00039         while ( (p = readdir(d)) != NULL )
00040         {
00041             songList[index] = (char*)malloc(sizeof(char) * strlen(p->d_name));
00042             term.printf(" - %s\n", p->d_name);
00043             strcpy(songList[index++], p->d_name);
00044         }
00045         songList[index] = (char*)malloc(sizeof(char) * 8);
00046         strcpy(songList[index++], "Playlist");        
00047     }
00048     else
00049     {
00050         error("Could not open directory!");
00051     }      
00052 
00053     pb_add.mode(PullUp);
00054     FILE *wave_file;
00055           
00056     while(1)
00057     {
00058         char * selectedFile;
00059         volume = position*300;
00060         dirt = rpg1.dir(); //Get Dir
00061         count = count + dirt; //Ad Dir to count
00062         if(count > (index-1)){
00063             count = 0;
00064         }
00065         if(count < 0){
00066             count = index-1;
00067         }
00068 
00069         lcd1.cls();
00070         lcd1.printf("Song List: \n"); //Print out Count
00071         lcd1.printf("%s ", songList[count]);
00072         
00073         lcd2.cls();
00074         lcd2.printf("Volume: %d \n", volume); //Print out Count
00075         lcd2.printf("Mode: STOP");
00076         
00077         if(!pb_add)
00078         {
00079             playList[index_play] = (char*)malloc(sizeof(char) * strlen(songList[count]));
00080             strcpy(playList[index_play++], songList[count]);
00081             lcd2.cls();
00082             lcd2.printf("Song Added to PlayList"); //Print out Count
00083             wait(0.5);
00084         }
00085         if (rpg1.pb())
00086         {
00087            if(count == (index-1)&&index_play > 0)
00088            {
00089                 for(int i = 0; i < (index_play);i++)
00090                 {
00091                     len = 5+strlen(playList[i]);
00092                     selectedFile = (char*)malloc(sizeof(char) * len);
00093                     strcpy(selectedFile, "/msc/");
00094                     strcat(selectedFile, playList[i]);
00095                     wave_file=fopen(selectedFile,"r");
00096                     lcd2.cls();
00097                    lcd2.printf("Volume: %d \n", volume); //Print out Count
00098                    lcd2.printf("Mode: PLAY");
00099                    waver.play(wave_file);
00100                    fclose(wave_file);
00101                    free(selectedFile);
00102                    if(!pb_add) //stop the whole play list
00103                         break;
00104                 }
00105            }
00106            else
00107            {
00108                len = 5+strlen(songList[count]);
00109                selectedFile = (char*)malloc(sizeof(char) * len);
00110                strcpy(selectedFile, "/msc/");
00111                strcat(selectedFile, songList[count]);
00112                wave_file=fopen(selectedFile,"r");
00113                lcd2.cls();
00114                lcd2.printf("Volume: %d \n", volume); //Print out Count
00115                lcd2.printf("Mode: PLAY");
00116                waver.play(wave_file);
00117                fclose(wave_file);
00118                free(selectedFile);
00119            }
00120            while(!pb_add){}; //prevent from adding stuff to the play list
00121         }
00122     }   
00123     
00124 }