Lawrence Quizon / Mbed OS mini_piano_player

Dependencies:   USBMSD_BD SDFileSystem max32630fthr USBDevice

Revision:
10:97f600e6eae2
Parent:
9:17de551d2208
Child:
11:df2fffa042b8
--- a/main.cpp	Tue Jul 23 02:48:39 2019 +0000
+++ b/main.cpp	Tue Jul 23 03:34:18 2019 +0000
@@ -40,9 +40,35 @@
 // File system declaration
 FATFileSystem fs("fs");
 
-// USB MSD 
-USBMSD_BD msd(&bd);  
+// USB MSD
+USBMSD_BD msd(&bd);
+
+void clearSerialStream()
+{
+    char c;
+    while(daplink.readable())
+    {
+        c = daplink.getc();
+        wait_ms(1);   
+    }   
+}
 
+bool getInput(int maxSize,char *inputArray)
+{
+    int i;
+    char c;
+    clearSerialStream();
+    for(i=0; i<maxSize && c!='\r'; i++) {
+        c = daplink.getc();
+        daplink.putc(c);
+        inputArray[i] = c;
+    }
+    if(i == maxSize){
+        return 0;
+    } else {
+        return 1;
+    }
+}
 
 // main() runs in its own thread in the OS
 // (note the calls to Thread::wait below for delays)
@@ -114,7 +140,7 @@
 
         // Seek to beginning of number
         fseek(f, pos, SEEK_SET);
-    
+
         // Store number
         fprintf(f, "    %d\r\n", number);
     }
@@ -171,7 +197,7 @@
 //    fflush(stdout);
 //    err = fs.unmount();
 //    printf("%s\r\n", (err < 0 ? "Fail :(" : "OK"));
-    
+
 
     printf("Starting MSD... ");
     msd.disk_initialize();
@@ -329,7 +355,8 @@
             i--; //rewrite current note
             continue;
         }
-skipoctaveparse:
+        
+        skipoctaveparse:
 
         song[i].pitch = (pitchname)pn_det;
 
@@ -380,36 +407,40 @@
 
     //input iterator vars
     int i;
-    char c;
-    //input holder arrays
+
+    char *title = new char[24];
+    title[0] = '/';
+    title[1] = 'f';
+    title[2] = 's';
+    title[3] = '/';
+    FILE *txtfile;
     char *buffer = new char[1024]; //ver 2 update: placed in stack.
+    while(1) {
+        printf("Please input filename: ");
+        char *inputptr = title+4;
+        if(!getInput(24,inputptr))
+        {
+            printf("Filenames cannot be more than 20 characters.\033[A\r\n");
+        }
     
-    FILE *txtfile = fopen("/fs/furelise.txt","r");
-    
+        //input holder arrays
+        txtfile = fopen(title,"r");
+        if(txtfile == NULL) {
+            printf("File not found. Please append filetype at the end of filename.\033[A\r\n");
+            continue;
+        }
+        break;
+    }
+
     //find file size
     fseek(txtfile,0L,SEEK_END);
     int size = ftell(txtfile);
     fseek(txtfile,0,SEEK_SET);
-    
-    if(txtfile == NULL)
-    {
-        printf("File not found.\r\n");
-        while(1)
-        {
-            bLED = 0;
-            rLED = !rLED;
-            wait_ms(500);
-        }
-    }
-
     fread(buffer,1,size,txtfile);
-    
     printf("Printing file...\r\n");
-    for(i=0;i<size;i++)
-    {
+    for(i=0; i<size; i++) {
         printf("%c",buffer[i]);
     }
-    
     //open file
     buffer[i] = ' '; //set EOF marker.
     buffer[i+1] = '\0';
@@ -438,18 +469,10 @@
     //FORMAT:  { NUM_BEATS, PITCH }
     char bpminp[5];
     int PlayingFreq;
-
-
     printf("Please enter desired BPM:\r\n");
-
-    for(i=0; i<5 && c!='\r'; i++) {
-        c = daplink.getc();
-        daplink.putc(c);
-        bpminp[i] = c;
-    }
+    getInput(5,bpminp);
 
     printf("BPM Recieved.\r\n");
-    c=NULL; //reset C for next time.
     int BPM = strtol(bpminp,NULL,0);
     float SPB = 60*4/(float)BPM;
     printf("BPM: %i",BPM);