wefwe

Dependencies:   mbed C12832 DogM163 FatFileSystem

Revision:
26:304ecc4066a8
Parent:
20:ec1c4f02d50e
Child:
27:6e5cc34fb952
--- a/main.cpp	Mon Aug 29 15:06:56 2016 +0000
+++ b/main.cpp	Sat Apr 20 17:07:46 2019 +0000
@@ -1,129 +1,149 @@
 #include "mbed.h"
+#include "string.h"
+#include "stdio.h"
+#include "math.h"
+#include "C12832.h"
+#include "WavPlayer.h"
 #include "SDFileSystem.h"
+#include <string>
+using namespace std;
 
-Timer timer;
-DigitalIn button(p21, PullUp);
-SDFileSystem sd(p5, p6, p7, p20, "sd", p22, SDFileSystem::SWITCH_NEG_NO, 25000000);
-char buffer[4096];
+//Communication\interfaces
+Serial pc(USBTX,USBRX); //USB serial
+C12832 lcd(p5, p7, p6, p8, p11); //LCD
+SDFileSystem sd(p11, p12, p13, p15, "sd"); //SDCard
+DigitalIn sdDetect(p14); //CardDetect
+
+//in- and outputs
+DigitalIn PauseBtn(p21);
+DigitalIn StopBtn(p22);
 
-void writeTest()
-{
-    //Test write performance by creating a 1MB file
-    printf("Testing %iB write performance...", sizeof(buffer));
-    FileHandle* file = sd.open("Test File.bin", O_WRONLY | O_CREAT | O_TRUNC);
-    if (file != NULL) {
-        timer.start();
-        for (int i = 0; i < (1048576 / sizeof(buffer)); i++) {
-            if (file->write(buffer, sizeof(buffer)) != sizeof(buffer)) {
-                timer.stop();
-                printf("write error!\n");
-                timer.reset();
-                return;
-            }
-        }
-        timer.stop();
-        if (file->close())
-            printf("failed to close file!\n");
-        else
-            printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
-        timer.reset();
-    } else {
-        printf("failed to create file!\n");
-    }
-}
+//joystick
+BusIn joy(p19,p17,p18,p20);
+DigitalIn joyBtn(p14);
+
+//DAC output
+WavPlayer waver; //set up DAC to use wave player library
+
+
+//timer
+Timer dur;
 
-void readTest()
-{
-    //Test read performance by reading the 1MB file created by writeTest()
-    printf("Testing %iB read performance...", sizeof(buffer));
-    FileHandle* file = sd.open("Test File.bin", O_RDONLY);
-    if (file != NULL) {
-        timer.start();
-        int iterations = 0;
-        while (file->read(buffer, sizeof(buffer)) == sizeof(buffer))
-            iterations++;
-        timer.stop();
-        if (iterations != (1048576 / sizeof(buffer)))
-            printf("read error!\n");
-        else if (file->close())
-            printf("failed to close file!\n");
-        else if (sd.remove("Test File.bin"))
-            printf("failed to delete file!\n");
-        else
-            printf("done!\n\tResult: %.2fKB/s\n", 1024 / (timer.read_us() / 1000000.0));
-        timer.reset();
-    } else {
-        printf("failed to open file!\n");
-    }
-}
+//func
+void read_file_names(char *dir); //SDCard read file names
+void menu();
+void PlaySong(int m);
+void PauseSong();
+void StopSong();
+
+//variables
+int m = 1; //index of choosen songs
+int i = 0; //index of files from folder
+int Paused = 0; //Is the song paused?
+int Stopped = 0; //Is the song stopped?
+char* songs[5];
 
 int main()
 {
-    //Configure CRC, large frames, and write validation
-    sd.crc(true);
-    sd.large_frames(true);
-    sd.write_validation(true);
-
-    //Fill the buffer with random data for the write test
-    srand(time(NULL));
-    for (int i = 0; i < sizeof(buffer); i++)
-        buffer[i] = rand();
-
-    while(1) {
-        //Simple button debouncing
+    while(sdDetect == 0) {
+        lcd.locate(0,0);
+        lcd.printf("Insert SD Card!");
         wait(0.5);
+    }
+    menu();
+}
 
-        //Print the start message
-        printf("\nPress the button to perform tests: ");
-
-        //Wait for the button to be pressed
-        while(button);
-
-        //Make sure a card is present
-        if (!sd.card_present()) {
-            printf("\nNo card present!\n");
-            continue;
-        }
-
-        //Try to mount the SD card
-        printf("\nMounting SD card...");
-        if (sd.mount() != 0) {
-            printf("failed!\n");
-            continue;
+void menu()
+{
+    lcd.cls();
+    sd.disk_initialize();
+    read_file_names("/sd/Music");
+    while(1) {
+        lcd.locate(0,0);
+        lcd.printf("Please select a song");
+        if(joy == 1) {
+            m++;
+            if(m == 5) {
+                m = 0;
+            }
+        } else if(joy == 2) {
+            m--;
+            if(m == -1) {
+                m = 4;
+            }
         }
-        printf("success!\n");
-
-        //Display the card type
-        printf("\tCard type: ");
-        SDFileSystem::CardType cardType = sd.card_type();
-        if (cardType == SDFileSystem::CARD_NONE)
-            printf("None\n");
-        else if (cardType == SDFileSystem::CARD_MMC)
-            printf("MMC\n");
-        else if (cardType == SDFileSystem::CARD_SD)
-            printf("SD\n");
-        else if (cardType == SDFileSystem::CARD_SDHC)
-            printf("SDHC\n");
-        else
-            printf("Unknown\n");
+        lcd.locate(0,15);
+        lcd.printf("%s", (songs[m]));
+        if(joyBtn == 1) {
+            PlaySong(m);
+        }
+        wait(0.1);
+    }
+}
 
-        //Display the card capacity
-        printf("\tSectors: %u\n", sd.disk_sectors());
-        printf("\tCapacity: %.1fMB\n", sd.disk_sectors() / 2048.0);
-
-        /*//Format the card
-        printf("Formatting SD card...");
-        if (sd.format() != 0) {
-            printf("failed!\n");
-            continue;
-        }
-        printf("success!\n");*/
-
-        //Perform a read/write test
-        writeTest();
-        readTest();
-
-        //Unmount the SD card
-        sd.unmount();
+void read_file_names(char *dir) // function that reads in file names from sd cards
+{
+    DIR *dp;
+    struct dirent *dirp;
+    dp = opendir(dir);
+    //read all directory and file names in current directory into filename vector
+    while((dirp = readdir(dp)) != NULL) {
+        songs[i] = dirp->d_name;
+        i++;
     }
 }
+
+void PlaySong(int m)
+{
+    string songname = songs[m];
+    string a = "/sd/Music/";
+    string fname = a + songname; //retrieves the file name
+    FILE *wave_file;
+    dur.start();
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("Now playing");
+    wave_file = fopen(fname.c_str(),"r"); //opens the music file
+    waver.open(&wave_file);
+    waver.play(); //plays the music file
+    lcd.locate(0,10);
+    lcd.printf("%s", (songs[m]));
+    while(Stopped == 0) {
+        if(StopBtn == 1) {
+            StopSong();
+        }
+        if(PauseBtn == 1) {
+            PauseSong();
+        }
+        lcd.locate(0,20);
+        lcd.printf("%2.f s", dur.read());
+    }
+    fclose(wave_file);
+    menu();
+}
+
+void PauseSong()
+{
+    while(1) {
+        if(Paused == 0) {
+            string songname = songs[m];
+            unsigned index = songname.find(".wav");
+            songname = songname.substr(0,index);
+            lcd.printf(songname.c_str());
+            dur.stop();
+            Paused = 1;
+        } else if(StopBtn == 1) {
+            StopSong();
+        } else {
+            Paused = 0;
+            dur.start();
+        }
+    }
+}
+
+void StopSong()
+{
+    lcd.cls();
+    dur.reset();
+    Stopped = 1;
+}