ECE 4180 Final

Dependencies:   mbed wave_player mbed-rtos C12832_lcd 4DGL-uLCD-SE LCD_fonts SDFileSystem

Revision:
21:cbcbb3480cad
Parent:
20:7d56cdcbc9a5
--- a/main.cpp	Fri Dec 06 18:00:11 2019 +0000
+++ b/main.cpp	Sun Dec 08 02:18:30 2019 +0000
@@ -36,6 +36,7 @@
 volatile bool homescreen = true;
 volatile bool songchange = false;
 volatile bool playing = false;
+Serial pc(USBTX, USBRX);
 uLCD_4DGL uLCD(p28,p27,p30);
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
 AnalogIn joy_pot(p16);
@@ -77,9 +78,12 @@
 
 unsigned short **currentBubbles = NULL;
 int bubbleNdx = 0;
-volatile int bubblesDrawn = 0;
-volatile int bubblesMissed = 0;
-
+volatile float bubblesDrawn = 0;
+volatile float bubblesMissed = 0;
+volatile float bubblesHit = 0;
+int key_code;
+int prev_key_code = 0;
+volatile bool touchpadPressed = false;
 
 
 // Thread 1
@@ -180,12 +184,12 @@
             //lcd_mutex.lock();
 //            uLCD.printf("%d", bubblesMissed);
 //            lcd_mutex.unlock();
-            if (bubblesMissed <= 1) {
-                myRGBled = green;
-            } else if (bubblesMissed <= 3) {
+            if (bubblesHit <= 1) {
+                myRGBled = red;
+            } else if (bubblesHit <= 3) {
                 myRGBled = blue;
             } else {
-                myRGBled = red;
+                myRGBled = green;
             }
             Thread::wait(300);
             
@@ -237,6 +241,9 @@
 
 
 void showScore () {
+    
+    lcd_mutex.lock();
+    uLCD.cls();
     uLCD.color(0x00FF00);        // Change text color and set up menu
     uLCD.text_bold(ON);
     uLCD.locate(1,1);
@@ -245,7 +252,7 @@
     uLCD.printf("Thanks for playing Tap Tap Revolution!");
 //    winSound();
     wait(1.5);
-    
+    uLCD.cls();
     uLCD.text_width(1); //4X size text
     uLCD.text_height(1);
     uLCD.text_bold(OFF);
@@ -255,36 +262,54 @@
     uLCD.color(0x00FF00);
     uLCD.locate(2,10);
     
-     float scorePercent = ((bubblesDrawn - bubblesMissed) / bubblesDrawn) * 100;
-    uLCD.printf("%d %", scorePercent);
-    wait(1.2);   
+    float scorePercent = ((bubblesHit) / bubblesDrawn) * 100.0;
+    uLCD.printf("Bubbles Hit %4.2f \n", bubblesHit);
+    uLCD.printf("Bubbles Drawn %4.2f \n", bubblesDrawn);
+    uLCD.printf("Your Score: %4.2f % \n", scorePercent);
+    bubblesHit = 0;
+    bubblesDrawn = 0;
+    //uLCD.printf("%d %", scorePercent);
+    wait(10);   
     uLCD.cls();
     uLCD.locate(5,0);
-    
+    lcd_mutex.unlock();
     Thread::wait(2000);
 }
-
+//touchpad interrupt code
+void fallInterrupt()
+{
+    prev_key_code = key_code;
+    key_code=0;
+    int i=0;
+    int value=mpr121.read(0x00);
+    value +=mpr121.read(0x01)<<8;
+    i=0;
+    //acquires value
+    for (i=0; i<12; i++) {
+        if (((value>>i)&0x01)==1) key_code=i+1;
+    }
+    touchpadPressed = true;
+}
 int main() {
 //<<<<<<< working copy
 //    
-//    uLCD.baudrate(3000000); // set baud rate to max
+    uLCD.baudrate(3000000); // set baud rate to max
 //    
 //=======
 //    interrupt.fall(&fallInterrupt);
 //    interrupt.mode(PullUp);  //for touch keypad
 //    lcd_mutex.lock();
 //    uLCD.baudrate(3000000);
-//    uLCD.cls();
-//    uLCD.media_init();
-//    uLCD.set_sector_address(0x0077, 0x4002);
-//    uLCD.display_video(0,0);
+    uLCD.cls();
+    uLCD.media_init();
+    uLCD.set_sector_address(0x0077, 0x4002);
+    uLCD.display_video(0,0);
 //    wait(4);
 //    lcd_mutex.unlock();
 //>>>>>>> merge rev
-    
     // seed random
     srand (time(NULL));
-    
+
     thread1.start(homescreen_thread);
     thread2.start(joystick_thread);
     thread3.start(pbcontrol_thread);
@@ -296,7 +321,8 @@
     bubbleLanes[0] = new Lane(20,15);
     bubbleLanes[1] = new Lane(60,15);
     bubbleLanes[2] = new Lane(100,15);
-    
+    interrupt.fall(&fallInterrupt);
+    interrupt.mode(PullUp);
     //startup sound?
     
     while(true) {
@@ -359,14 +385,25 @@
                 bubbleLanes[0]->draw();
                 bubbleLanes[1]->draw();
                 bubbleLanes[2]->draw();
+                uLCD.locate(0,0);
+                uLCD.printf("%4.2f", bubblesHit);
                 lcd_mutex.unlock();
                 
+                
                 // TODO: check for hits with capacitive touchpad
                 // if there is a hit, delete the bubble from the dynamic array
                 // replace "true" in the next 3 lines with your logic of when to check for a hit
-//                if (true) bubbleLanes[0]->checkHit();
-//                if (true) bubbleLanes[1]->checkHit();
-//                if (true) bubbleLanes[2]->checkHit();
+                //if touchpad has been pressed, checks if bubble in right spot
+                if(touchpadPressed){
+                //lcd_mutex.lock();
+                //uLCD.printf("%d", key_code);
+                //lcd_mutex.unlock();
+                //pc.printf("Pressed/n");
+                if (key_code == 12) bubblesHit += bubbleLanes[0]->checkHit();
+                if (key_code == 8) bubblesHit += bubbleLanes[1]->checkHit();
+                if (key_code == 4) bubblesHit += bubbleLanes[2]->checkHit();
+                }
+                touchpadPressed = false;
                 
                 // move down all the bubbles, deleting bubbles that are out of bounds if required
                 bubbleLanes[0]->moveDown();
@@ -382,7 +419,7 @@
             }
             
             // uncomment this when gameplay works
-            //showScore();
+            showScore();
             
             // return to homescreen when
             songselect = false;