4180 / Mbed 2 deprecated Ipod

Dependencies:   4DGL-uLCD-SE 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 "Speaker.h"
00003 #include "SDFileSystem.h"
00004 #include "TextLCD.h"
00005 #include "PinDetect.h"
00006 #include "wave_player.h"
00007 #include "uLCD_4DGL.h"
00008 #include <vector>
00009 #include <string>
00010 
00011 //Pin setup
00012 DigitalOut myled(LED1);
00013 PinDetect pb1(p26);
00014 PinDetect pb2(p25);
00015 PinDetect pb3(p24);
00016 PinDetect pb4(p23);
00017 DigitalIn sdd(p12);
00018 DigitalOut myLED1(LED1);
00019 DigitalOut myLED2(LED2);
00020 DigitalOut myLED3(LED3);
00021 DigitalOut myLED4(LED4);
00022 TextLCD panel(p14, p15, p16, p17, p19, p20);
00023 uLCD_4DGL lcd(p28,p27,p29);
00024 AnalogOut DACout(p18);
00025 wave_player waver(&DACout);
00026 SDFileSystem sd(p5, p6, p7, p8, "sd");
00027 
00028 //Globals
00029 vector <string> filenames;
00030 int current = 0;
00031 bool play = false, sd_insert = false;
00032 static int veclen = 5;
00033 FILE *wave_file;
00034 
00035 void sd_check(){
00036     int sdPre = sdd.read();
00037     while (sdPre == 0){
00038         lcd.locate(0,0);
00039         lcd.printf("Insert SD card");
00040         sdPre = sdd.read();
00041         wait (.5);
00042     }
00043     lcd.cls();
00044 }
00045 
00046 void write_screen()
00047 {
00048     lcd.locate(0,0);
00049     string name;
00050     int j = 0;
00051     vector<string>::iterator it;
00052     //lcd.cls();
00053     for(it = filenames.begin(); it < filenames.end(); it++) {
00054         name = (*it).c_str();
00055         name.erase((name.length()-4),4);
00056         if(j++ == current){
00057             lcd.printf("~");
00058         }
00059         else{
00060             lcd.printf(" ");
00061         }
00062         if(name.length() > 17){
00063             name.erase(17,(17-name.length()));
00064             lcd.printf("%s",name);
00065         }
00066         else{
00067             lcd.printf("%s\n",name);
00068         }
00069     }
00070     /*for(int j = 0; j > veclen; j++){
00071         lcd.locate(0,j);
00072         if(j == current){
00073             lcd.printf("~");
00074         }
00075         else{
00076             lcd.printf("~");   
00077         }
00078     }*/
00079 }
00080 
00081 void write_panel()
00082 {
00083     panel.cls();
00084     if(play == true){
00085     string name;
00086     vector<string>::iterator it;
00087     it = filenames.begin() + current;
00088     name = (*it).c_str();
00089     name.erase((name.length()-4),4);
00090     if(name.length() > 16){
00091         name.erase(16,(16-name.length()));
00092         panel.printf("%s",name);
00093     }
00094     else{
00095         panel.printf("%s\n",name);
00096     }
00097     }
00098     else{
00099         panel.printf("Stopped\n");
00100     }
00101     for(int i = 0; i < 16; i++){
00102         if(i < waver.volume){
00103             panel.printf("*");
00104         }
00105         else
00106         {    
00107         panel.printf(" ");
00108         }
00109     }
00110 }
00111 void play_file()
00112 {
00113     bool* play_point = &play;
00114     string file_name("/sd/");
00115     file_name += filenames[current];
00116     wave_file = fopen(file_name.c_str(),"r");
00117     waver.play(wave_file, play_point);
00118     fclose(wave_file);
00119     play = false;
00120     write_panel();
00121 }
00122 
00123 //Push buttons
00124 void pb1_hit_callback (void)
00125 {
00126     if(play == false){
00127         current = (current-1) % veclen;
00128         if(current < 0){
00129             current += veclen;
00130         }
00131         write_screen();
00132     }
00133 }
00134 
00135 void pb2_hit_callback (void)
00136 {
00137     if(play == false){
00138         current = (1+current) % veclen;
00139         write_screen();
00140     }    
00141 }
00142 
00143 void pb3_hit_callback (void)
00144 {
00145     play = !play;
00146     write_panel();
00147 }
00148 
00149 void pb4_hit_callback (void)
00150 {
00151     waver.volume--;
00152     if(waver.volume < 0){
00153         waver.volume = 16;
00154     }
00155     write_panel();
00156 
00157 }
00158 
00159 //Set up
00160 void push_set()
00161 {
00162     // Use internal pullups for the three pushbuttons
00163     pb1.mode(PullUp);
00164     pb2.mode(PullUp);
00165     pb3.mode(PullUp);
00166     pb4.mode(PullUp);
00167     // Delay for initial pullup to take effect
00168     wait(.01);
00169     // Setup Interrupt callback functions for a pb hit
00170     pb1.attach_deasserted(&pb1_hit_callback);
00171     pb2.attach_deasserted(&pb2_hit_callback);
00172     pb3.attach_deasserted(&pb3_hit_callback);
00173     pb4.attach_deasserted(&pb4_hit_callback);
00174     // Start sampling pb inputs using interrupts
00175     pb1.setSampleFrequency();
00176     pb2.setSampleFrequency();
00177     pb3.setSampleFrequency();
00178     pb4.setSampleFrequency();
00179 }
00180 
00181 //File reads
00182 void read_file_names(char *dir)
00183 {
00184     DIR *dp;
00185     struct dirent *dirp;
00186     dp = opendir(dir);
00187     //read all directory and file names in current directory into filename vector
00188     while((dirp = readdir(dp)) != NULL) {
00189         filenames.push_back(string(dirp->d_name));
00190     }
00191 }
00192 
00193 int main()
00194 {
00195     lcd.cls();
00196     panel.cls();
00197     sd_check();
00198     push_set();
00199     read_file_names("/sd/");
00200     write_screen();
00201     write_panel();
00202     while(true)
00203     {     
00204         wait(.01);
00205         if(play == true)
00206         {
00207             play_file();
00208         }
00209     }
00210 }