Evan Zhang / Mbed 2 deprecated StepperMotorSong4180

Dependencies:   mbed mbed-rtos SDFileSystem11

Revision:
8:202142fffa4e
Parent:
7:dd65856c3982
Child:
9:3e69e012c73a
diff -r dd65856c3982 -r 202142fffa4e main.cpp
--- a/main.cpp	Sun May 02 20:07:22 2021 +0000
+++ b/main.cpp	Mon May 03 17:27:51 2021 +0000
@@ -19,7 +19,10 @@
 #include <cstdlib>
 
 Thread motor;
+Thread tpause;
+
 SDFileSystem sd(p5, p6, p7, p8, "sd");
+Serial blue(p13,p14);
 Serial pc(USBTX, USBRX); // tx, rx
 
 class Note { //I use this to create a note object which contains
@@ -35,10 +38,13 @@
         }
     };
         
-
+volatile int pause = 0;
 
 DigitalOut led1(LED2); //Used simply for debugging, could add functionailty
                         // So the led lights up when the corresponding motor is playing
+DigitalOut led2(LED3);
+DigitalOut led3(LED4);
+
 DigitalOut f1(p21);     //Flipper functions used to generate tones
 DigitalOut f2(p22);
 DigitalOut f3(p23);
@@ -57,6 +63,10 @@
 float motor2stop;
 float motor3stop;
 
+char *targmatrix = "/sd/Zelda/notesmatrix.txt"; // testing
+char *targstart = "/sd/Zelda/notesstart.txt";
+char *targfinish = "/sd/Zelda/notesfinish.txt";
+
 Timer motor1timer; //Used to keep track of the time
  void flip1() //These flip the output pins, generating a pulse
 {
@@ -76,7 +86,78 @@
     return 440.0*powf(2.0, (midi-69)/12);
 }
 
+// bluetooth pause thread
+
+void Bpause(void const *arguments){
+    while(1){
+        char bnum = 0;
+        char bhit = 0;
+        if (blue.getc()=='!'){
+            if (blue.getc()=='B'){
+            bnum = blue.getc(); //button number
+            bhit = blue.getc(); //1=hit, 0=release
+            if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? 
+                switch (bnum) {
+                    case '7': //button 7 left arrow play
+                            pause = 0;
+                            break;
+                    case '8': //button 8 right arrow pause
+                            pause = 1;
+                            break;
+                    default:
+                            break;
+                    }
+            }
+        }
+        }
+    Thread::wait(500);
+    }
+}
+
+// bluetooth input parsing function
+
+int bluehelp(){
+    char bnum = 0;
+    char bhit = 0;
+    if (blue.getc()=='!'){
+    if (blue.getc()=='B'){
+        bnum = blue.getc(); //button number
+        bhit = blue.getc(); //1=hit, 0=release
+        if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK? 
+            switch (bnum) {
+                        case '1': //number button 1 zelda
+                                targmatrix = "/sd/Zelda/notesmatrix.txt";
+                                targstart = "/sd/Zelda/notesstart.txt";
+                                targfinish = "/sd/Zelda/notesfinish.txt";
+                                return 1;
+                        case '2': //number button 2 meg
+                                targmatrix = "/sd/meglovania/notesmatrix.txt";
+                                targstart = "/sd/meglovania/notesstart.txt";
+                                targfinish = "/sd/meglovania/notesfinish.txt";
+                                return 1;
+                        case '3': //number button 3 wii
+                                targmatrix = "/sd/wii/notesmatrix.txt";
+                                targstart = "/sd/wii/notesstart.txt";
+                                targfinish = "/sd/wii/notesfinish.txt";
+                                return 1;
+                        case '4': //number button 4 fight
+                                targmatrix = "/sd/FightSong/notesmatrix.txt";
+                                targstart = "/sd/FightSong/notesstart.txt";
+                                targfinish = "/sd/FightSong/notesfinish.txt";
+                                return 1;
+                        case '5': //number 5 reset
+                                return 5;
+                        default:
+                            break;
+                        }
+        }
+    }
+    }
+    return -1;    
+}
+
 int main() {
+start:
     f1 = 0; //Initialize everything
     f2 = 0;
     f3 = 0;
@@ -86,6 +167,8 @@
     motor1stop = 0;
     motor2stop = 0;
     motor3stop = 0;
+    
+    Thread Thread(Bpause); // may not work
 
      bool holdNotes = 0; //Stop reading notes file once we have the next note
      bool holdStart = 0; //Stop reading start times once we have parsed the starrt
@@ -105,15 +188,30 @@
 
     pc.printf("Hello World!");
     string output = "";
+    
+    // select the song with bluetooth
+songselect:
+    led2 = 1;
+    int songret = bluehelp();
+    if (songret != 1)
+    {
+        led2 = !led2;
+        goto songselect;
+    }
+    // blue helper function should have selected a song for us by here
+    pc.printf(targmatrix);
+    FILE *fpn = fopen(targmatrix, "r"); //Fpn is a pointer to the notes file that contains the note values
+    FILE *fpstart = fopen(targstart, "r"); //Fpstart is a pointer to the file on the sd card that contains the start times
+    FILE *fpfinish = fopen(targfinish,"r"); //Fpfinish is a pointer to the file on the sd card that says how long each note should last
+    unsigned char c;
 
-    FILE *fpn = fopen("/sd/notesmatrix.txt", "r"); //Fpn is a pointer to the notes file that contains the note values
-    FILE *fpstart = fopen("/sd/notesstart.txt", "r"); //Fpstart is a pointer to the file on the sd card that contains the start times
-    FILE *fpfinish = fopen("/sd/notesfinish.txt","r"); //Fpfinish is a pointer to the file on the sd card that says how long each note should last
-    unsigned char c;
-    //pc.printf("I'm here\n");
     Note myNote = Note(0, 0, 0);        //Create a blank note
      motor1timer.start(); //Start the timer
+     led3 = 1;
     while (!feof(fpn)){                        // while not end of file, keep goig
+        while(pause){
+            Thread::wait(100);    
+        }
         while(!holdNotes) { //Make sure we haven't completed the note section yet
            c = fgetc(fpn);                         // get a character/byte from the file
            if(c == ','){                //If the character is a comma, we have parsed the full note
@@ -215,11 +313,20 @@
     } //We have now finished assigning the note to be played
     led1 = !led1;      
     
+    //if(!feof(fpn)) goto start;
     
     } //End of while loop for playing    
     motor1stop = 0;
     motor2stop = 0;
     motor3stop = 0;
-      
-   
+    // close all the files before trying reopen
+    pc.printf("attempt to close files\n");
+    fclose(fpn);
+    fclose(fpstart);
+    fclose(fpfinish);
+    pc.printf("files closed");
+    //while(bluehelp() != 5){
+    //    Thread::wait(100);
+    //    }
+    //goto start;
 }
\ No newline at end of file