Final Game. Have fun!!! :-)

Dependencies:   4DGL-uLCD-SE PinDetect mbed-rtos mbed

Revision:
4:c136aa81f52a
Parent:
3:0d678aefc1d0
Child:
6:f99b7d1fd770
--- a/main.cpp	Wed May 02 05:18:30 2018 +0000
+++ b/main.cpp	Wed May 02 08:47:48 2018 +0000
@@ -9,15 +9,15 @@
 Serial bluemod(p28,p27); //serial tx, serial rx
 
 //Bus for buttons
-BusIn nibble(p9, p10, p11, p12);
+BusIn nibble(p21, p22, p23, p24);
 
-//Flips the LEDs when you hit one of the buttons
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
+ //Setup RGB led using PWM pins and class
+DigitalOut redLed(p19);
+DigitalOut greenLed(p20);
 
-uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
+uLCD_4DGL uLCD(p13,p14,p12); // main screen for game
+//uLCD_4DGL uLCD1(p13, p14, p12); // second screen for score
+
 Mutex ledMutex;
 
 SDFileSystem sd(p5, p6, p7, p8, "sd");
@@ -31,7 +31,7 @@
 
 
 //***** Constants *****//
-const int songLength = 600; //adjust as needed
+const int songLength = 50; //adjust as needed
 
 //*********************//
 
@@ -58,7 +58,8 @@
 
 
 void mainMenu()
-{      
+{   
+    uLCD.display_control(PORTRAIT_R);
     uLCD.locate(5,0);
     uLCD.printf("Welcome!");
     uLCD.locate(2,2);
@@ -67,11 +68,16 @@
     uLCD.printf("2: DarkHorse\n\n");
     uLCD.printf("3: OneRepublic\n\n");
     uLCD.printf("4: ShakeItOff\n\n");
+   
+    int startGame = 0;
+    playGame = 1;
+    timespace = -6;
+    blueIn = '0';
+    score = 0;
+    selectedSong = 0;
     
-    int startGame = 0;
     while(!startGame){ //Loop until a valid song is selected
         while(bluemod.readable()){
-            
             blueIn = bluemod.getc(); //Read bluetooth input and select song.
             switch(blueIn) {
                 case '1':
@@ -93,16 +99,20 @@
         if(selectedSong >= 1 && selectedSong <= 4) //Start game once song is selected
             startGame = 1;
     }
+    
+    uLCD.cls();
 }
 
 void endScreen()
 {
     uLCD.cls();
+    uLCD.display_control(PORTRAIT_R);
     uLCD.printf("Score: %d\n\n", score);
     
     uLCD.printf("1: Play Again\n");
     uLCD.printf("2: Quit.");
     
+    blueIn = '0';
     while(restartGame < 0)  //Loop waiting for bluetooth input
     {
         while(bluemod.readable()){
@@ -121,167 +131,152 @@
 }
 
 
-/******Interrupt functions go here********/
 void buttonsThread(void const* args) {
     while (playGame) {
         if (timespace > -1) {
-            if ((nibble != 0x8) || (nibble != 0x4) || (nibble != 0x2) || (nibble != 0x1) || (nibble == 0)) { //check if multiple buttons or no buttons got pressed
-                red = 1;
-                green = 0;
-            } else {
                 switch(nibble) {
                     case 0x8:
-                        led4 = 1;
                         if (L4[timespace] == 1) {
                             score = score + 1;
-                            red = 0;
-                            green = 1;
+                            redLed = 0;
+                            greenLed = 1;
                         } else {
-                            red = 1;
-                            green = 0;
+                            redLed = 1;
+                            greenLed = 0;
                         }
                         break;
                     case 0x4:
-                        led3 = 1;
                         if (L3[timespace] == 1) {
                             score = score + 1;
-                            red = 0;
-                            green = 1;
+                            redLed = 0;
+                            greenLed = 1;
                         } else {
-                            red = 1;
-                            green = 0;
+                            redLed = 1;
+                            greenLed = 0;
                         }
                         break;
                     case 0x2:
-                        led2 = 1;
                         if (L2[timespace] == 1) {
                             score = score + 1;
-                            red = 0;
-                            green = 1;
+                            redLed = 0;
+                            greenLed = 1;
                         } else {
-                            red = 1;
-                            green = 0;
+                            redLed = 1;
+                            greenLed = 0;
                         }
                         break;
                     case 0x1:
-                        led1 = 1;
                         if (L1[timespace] == 1) {
                             score = score + 1;
-                            red = 0;
-                            green = 1;
+                            redLed = 0;
+                            greenLed = 1;
                         } else {
-                            red = 1;
-                            green = 0;
+                            redLed = 1;
+                            greenLed = 0;
                         }
                         break;
                     default:
+                        redLed = 1;
                         break;
                 }
-            }
+                while(nibble) {}
         }
-        led1 = 0;
-        led2 = 0;
-        led3 = 0;
-        led4 = 0;
-    }
+    }    
 }
-/*****************************************/
 
 void lcdThread1(void const *args) {
-    while (true) {
-        timespace += 1;
-        if (timespace < 256)  // <- get rid of this so while loop goes to end of song
-        {
-            //delete any circles in the yellow circles
-            uLCD.filled_circle(110, 20, 12, 0x000000);
-            uLCD.filled_circle(110, 50, 12, 0x000000);
-            uLCD.filled_circle(110, 80, 12, 0x000000);
-            uLCD.filled_circle(110, 110, 12, 0x000000);
-            
-            //draw the path of the circles
-            if (timespace >= 0){
-                if (L1[timespace] == 1){
-                    uLCD.filled_circle(86, 20, 10, 0x000000);
-                    uLCD.filled_circle(110, 20, 12, 0x0000FF);}
-                if (L2[timespace] == 1){
-                    uLCD.filled_circle(86, 50, 10, 0x000000);
-                    uLCD.filled_circle(110, 50, 12, 0x00FFFF);}
-                if (L3[timespace] == 1){
-                    uLCD.filled_circle(86, 80, 10, 0x000000);
-                    uLCD.filled_circle(110, 80, 12, 0x00FF00);}
-                if (L4[timespace] == 1){
-                    uLCD.filled_circle(86, 110, 10, 0x000000);
-                    uLCD.filled_circle(110, 110, 12, 0x008000);}
-            }
-            if (timespace >= -1){
-                if (L1[timespace+1] == 1){
-                    uLCD.filled_circle(66, 20, 8, 0x000000);
-                    uLCD.filled_circle(86, 20, 10, 0x0000FF);}
-                if (L2[timespace+1] == 1){
-                    uLCD.filled_circle(66, 50, 8, 0x000000);
-                    uLCD.filled_circle(86, 50, 10, 0x00FFFF);}
-                if (L3[timespace+1] == 1){
-                    uLCD.filled_circle(66, 80, 8, 0x000000);
-                    uLCD.filled_circle(86, 80, 10, 0x00FF00);}
-                if (L4[timespace+1] == 1){
-                    uLCD.filled_circle(66, 110, 8, 0x000000);
-                    uLCD.filled_circle(86, 110, 10, 0x008000);}
-            }
-            if (timespace >= -2){
-                if (L1[timespace+2] == 1){
-                    uLCD.filled_circle(50, 20, 6, 0x000000);
-                    uLCD.filled_circle(66, 20, 8, 0x0000FF);}
-                if (L2[timespace+2] == 1){
-                    uLCD.filled_circle(50, 50, 6, 0x000000);
-                    uLCD.filled_circle(66, 50, 8, 0x00FFFF);}
-                if (L3[timespace+2] == 1){
-                    uLCD.filled_circle(50, 80, 6, 0x000000);
-                    uLCD.filled_circle(66, 80, 8, 0x00FF00);}
-                if (L4[timespace+2] == 1){
-                    uLCD.filled_circle(50, 110, 6, 0x000000);
-                    uLCD.filled_circle(66, 110, 8, 0x008000);}
-            }
-            if (timespace >= -3){
-                if (L1[timespace+3] == 1){
-                    uLCD.filled_circle(38, 20, 4, 0x000000);
-                    uLCD.filled_circle(50, 20, 6, 0x0000FF);}
-                if (L2[timespace+3] == 1){
-                    uLCD.filled_circle(38, 50, 4, 0x000000);
-                    uLCD.filled_circle(50, 50, 6, 0x00FFFF);}
-                if (L3[timespace+3] == 1){
-                    uLCD.filled_circle(38, 80, 4, 0x000000);
-                    uLCD.filled_circle(50, 80, 6, 0x00FF00);}
-                if (L4[timespace+3] == 1){
-                    uLCD.filled_circle(38, 110, 4, 0x000000);
-                    uLCD.filled_circle(50, 110, 6, 0x008000);}
-            }
-            if (timespace >= -4){
-                if (L1[timespace+4] == 1){
-                    uLCD.filled_circle(30, 20, 2, 0x000000);
-                    uLCD.filled_circle(38, 20, 4, 0x0000FF);}
-                if (L2[timespace+4] == 1){
-                    uLCD.filled_circle(30, 50, 2, 0x000000);
-                    uLCD.filled_circle(38, 50, 4, 0x00FFFF);}
-                if (L3[timespace+4] == 1){
-                    uLCD.filled_circle(30, 80, 2, 0x000000);
-                    uLCD.filled_circle(38, 80, 4, 0x00FF00);}
-                if (L4[timespace+4] == 1){
-                    uLCD.filled_circle(30, 110, 2, 0x000000);
-                    uLCD.filled_circle(38, 110, 4, 0x008000);}
-            }
-            if (L1[timespace+5] == 1){
-                uLCD.filled_circle(30, 20, 2, 0x0000FF);}
-            if (L2[timespace+5] == 1){
-                uLCD.filled_circle(30, 50, 2, 0x00FFFF);}
-            if (L3[timespace+5] == 1){
-                uLCD.filled_circle(30, 80, 2, 0x00FF00);}
-            if (L4[timespace+5] == 1){
-                uLCD.filled_circle(30, 110, 2, 0x008000);}
+    
+    while(playGame) {
+        timespace++;
+        //delete any circles in the yellow circles
+        uLCD.filled_circle(110, 20, 12, 0x000000);
+        uLCD.filled_circle(110, 50, 12, 0x000000);
+        uLCD.filled_circle(110, 80, 12, 0x000000);
+        uLCD.filled_circle(110, 110, 12, 0x000000);
+        
+        uLCD.locate(5, 0);
+        uLCD.printf("%d", score);        
+        //draw the path of the circles
+        if (timespace >= 0){
+            if (L1[timespace] == 1){
+                uLCD.filled_circle(86, 20, 10, 0x000000);
+                uLCD.filled_circle(110, 20, 12, 0x0000FF);}
+            if (L2[timespace] == 1){
+                uLCD.filled_circle(86, 50, 10, 0x000000);
+                uLCD.filled_circle(110, 50, 12, 0x00FFFF);}
+            if (L3[timespace] == 1){
+                uLCD.filled_circle(86, 80, 10, 0x000000);
+                uLCD.filled_circle(110, 80, 12, 0x00FF00);}
+            if (L4[timespace] == 1){
+                uLCD.filled_circle(86, 110, 10, 0x000000);
+                uLCD.filled_circle(110, 110, 12, 0x008000);}
+        }
+        if (timespace >= -1){
+            if (L1[timespace+1] == 1){
+                uLCD.filled_circle(66, 20, 8, 0x000000);
+                uLCD.filled_circle(86, 20, 10, 0x0000FF);}
+            if (L2[timespace+1] == 1){
+                uLCD.filled_circle(66, 50, 8, 0x000000);
+                uLCD.filled_circle(86, 50, 10, 0x00FFFF);}
+            if (L3[timespace+1] == 1){
+                uLCD.filled_circle(66, 80, 8, 0x000000);
+                uLCD.filled_circle(86, 80, 10, 0x00FF00);}
+            if (L4[timespace+1] == 1){
+                uLCD.filled_circle(66, 110, 8, 0x000000);
+                uLCD.filled_circle(86, 110, 10, 0x008000);}
         }
-        else //when the game is over
-        {
-            playGame = 0;
+        if (timespace >= -2){
+            if (L1[timespace+2] == 1){
+                uLCD.filled_circle(50, 20, 6, 0x000000);
+                uLCD.filled_circle(66, 20, 8, 0x0000FF);}
+            if (L2[timespace+2] == 1){
+                uLCD.filled_circle(50, 50, 6, 0x000000);
+                uLCD.filled_circle(66, 50, 8, 0x00FFFF);}
+            if (L3[timespace+2] == 1){
+                uLCD.filled_circle(50, 80, 6, 0x000000);
+                uLCD.filled_circle(66, 80, 8, 0x00FF00);}
+            if (L4[timespace+2] == 1){
+                uLCD.filled_circle(50, 110, 6, 0x000000);
+                uLCD.filled_circle(66, 110, 8, 0x008000);}
         }
+        if (timespace >= -3){
+            if (L1[timespace+3] == 1){
+                uLCD.filled_circle(38, 20, 4, 0x000000);
+                uLCD.filled_circle(50, 20, 6, 0x0000FF);}
+            if (L2[timespace+3] == 1){
+                uLCD.filled_circle(38, 50, 4, 0x000000);
+                uLCD.filled_circle(50, 50, 6, 0x00FFFF);}
+            if (L3[timespace+3] == 1){
+                uLCD.filled_circle(38, 80, 4, 0x000000);
+                uLCD.filled_circle(50, 80, 6, 0x00FF00);}
+            if (L4[timespace+3] == 1){
+                uLCD.filled_circle(38, 110, 4, 0x000000);
+                uLCD.filled_circle(50, 110, 6, 0x008000);}
+        }
+        if (timespace >= -4){
+            if (L1[timespace+4] == 1){
+                uLCD.filled_circle(30, 20, 2, 0x000000);
+                uLCD.filled_circle(38, 20, 4, 0x0000FF);}
+            if (L2[timespace+4] == 1){
+                uLCD.filled_circle(30, 50, 2, 0x000000);
+                uLCD.filled_circle(38, 50, 4, 0x00FFFF);}
+            if (L3[timespace+4] == 1){
+                uLCD.filled_circle(30, 80, 2, 0x000000);
+                uLCD.filled_circle(38, 80, 4, 0x00FF00);}
+            if (L4[timespace+4] == 1){
+                uLCD.filled_circle(30, 110, 2, 0x000000);
+                uLCD.filled_circle(38, 110, 4, 0x008000);}
+        }
+        if (L1[timespace+5] == 1){
+            uLCD.filled_circle(30, 20, 2, 0x0000FF);}
+        if (L2[timespace+5] == 1){
+            uLCD.filled_circle(30, 50, 2, 0x00FFFF);}
+        if (L3[timespace+5] == 1){
+            uLCD.filled_circle(30, 80, 2, 0x00FF00);}
+        if (L4[timespace+5] == 1){
+            uLCD.filled_circle(30, 110, 2, 0x008000);}
+        
+        
         
         //draw circles at base
         uLCD.circle(110,20,12, 0xFFFF00);
@@ -289,9 +284,12 @@
         uLCD.circle(110,80,12, 0xFFFF00);
         uLCD.circle(110,110,12, 0xFFFF00);
         
-        Thread::wait(250);
-
+        Thread::wait(100);
+        
+        if(timespace >= songLength)
+            playGame = 0;
     }
+    
 }
 
 //Thread to play song, song is selected in mainMenu()
@@ -318,9 +316,12 @@
     }
         
     waver.play(wav_file);
-    while (true) {
-        Thread::wait(100);
+    
+    while (playGame) {
+        Thread::wait(50);
     }
+    fclose(wav_file);
+    
 }
 
 //Test method for writing notes to a specified file
@@ -387,29 +388,36 @@
             index++;
         }
         
-    } while (c!=EOF);
+    } while ((c!=EOF) && (index < songLength));
     
     fclose(fp);
+    
+    FILE *arr = fopen("/sd/arr.txt", "w");
+    
+    for(int i = 0; i < index - 1; i++)
+        fprintf(arr, "%d%d%d%d\n", L1[i], L2[i], L3[i], L4[i]);
+        
+    fclose(arr);
 }
 
 void readSong(int selection)
 {
     switch(selection) {
-    case 1:
-        readFile("/sd/", "GTShake");
-        break;
-    case 2:
-        readFile("/sd/", "DarkHorse");
-        break;
-    case 3:
-        readFile("/sd/", "OneRepublic");
-        break;
-    case 4:
-        readFile("/sd/", "ShakeItOff");
-        break;
-    default:
-        readFile("/sd/", "ShakeItOff");
-        break;
+        case 1:
+            readFile("/sd/", "GTShake");
+            break;
+        case 2:
+            readFile("/sd/", "DarkHorse");
+            break;
+        case 3:
+            readFile("/sd/", "OneRepublic");
+            break;
+        case 4:
+            readFile("/sd/", "ShakeItOff");
+            break;
+        default:
+            readFile("/sd/", "ShakeItOff");
+            break;
     }
 }
  
@@ -424,21 +432,18 @@
         readSong(selectedSong); //create note arrays based on chosen song
         
         //start threads
+        uLCD.display_control(LANDSCAPE_R);
+        
         Thread thread1(lcdThread1); //thread that displays the circles
         Thread thread2(musicThread); //thread that plays the song
         Thread thread3(buttonsThread); //thread to get user input
-        
-        //declare interrupt
-        //interrupt.mode(PullUp);
-        
+                
         while (playGame) {   
             Thread::wait(10);
         }
         
         //End Threads
-        thread1.terminate();
         thread2.terminate();
-        thread3.terminate();
         
         //reset game state switches
         playGame = 1;