4180 Final

Dependencies:   mbed wave_player mbed-rtos 4180Final SDFileSystem

Revision:
14:c21ffddc6e13
Parent:
13:aff3ad7d5afc
Child:
15:490eba282a68
Child:
16:e3c304456b15
--- a/main.cpp	Wed Apr 29 03:39:11 2020 +0000
+++ b/main.cpp	Wed Apr 29 10:23:28 2020 +0000
@@ -13,7 +13,10 @@
 #include  "rtos.h"
 
 Thread thread1, thread2; 
-
+//Thread t1(read_mic);
+//Thread t2(update_lcd_score);
+Thread t1;
+Thread t2;
 Nav_Switch myNav(p9, p6, p7, p5, p8); //pin order on Sparkfun breakout
 uLCD_4DGL uLCD(p28, p27, p30); // serial tx, serial rx, reset pin; 
 SDFileSystem sd(p11, p12, p13, p10, "sd");
@@ -26,6 +29,8 @@
 PwmOut myled3(LED3);
 BusOut led_strip1(p21,p22,p23); 
 BusOut led_strip2(p24,p25,p26);
+//Mutexs
+Mutex lcd_mutex;
 
 // Globals
 int rounds = 3;
@@ -96,7 +101,7 @@
 // thread 2
 void mic_thread() {
     while (1) {
-        myled = pot;
+        //myled = pot;
         micScore1 = int(mymic1*500.0);
         micScore2 = int(mymic2*500.0);
         Thread::wait(100);
@@ -363,15 +368,25 @@
          }
     }
 }
-
-void read_mic(){
+// thread that updates the score displayed on lcd
+void update_lcd_score() {
     while(1){
-        mic_reading1 = int(abs((mymic1 - (0.67/3.3)))*500.0); //this value should hover around 15 
-        mic_reading2 = int(abs((mymic2 - (0.67/3.3)))*500.0); 
-        Thread::wait(125);
+        myled = 1;// testing to see if threads are called
+    lcd_mutex.lock();
+    uLCD.color(BLUE);
+    uLCD.locate(0,1);
+    //uLCD.printf("%4d", mic_reading1);
+    uLCD.printf("%4d", score1);
+    uLCD.color(PURPLE);
+    uLCD.locate(0,3);
+    //uLCD.printf("%4d", mic_reading2);
+    uLCD.printf("%4d", score2);
+    lcd_mutex.unlock();
+      
     }
 }
 
+
 void update_leds(){
     switch (loud1){
         case 10: led_strip1 = 1; break;
@@ -397,6 +412,18 @@
     }
 }
 
+void read_mic(){
+    while(1){
+        mic_reading1 = int(abs((mymic1 - (0.67/3.3)))*500.0); //this value should hover around 15 
+        mic_reading2 = int(abs((mymic2 - (0.67/3.3)))*500.0); 
+        update_score();
+        update_leds();
+      
+        Thread::wait(500);
+    }
+}
+
+
 int getScore1() {
     myled3 = ((double) micScore1) / 500.0;
     return micScore1; //int(mymic1*1000.0);
@@ -437,8 +464,12 @@
      // Scores
      uLCD.text_height(4);
      uLCD.text_width(4);
-     Thread t1(read_mic);
-     mic.attach(&read_mic, 1.0/8000.0);
+     wait(0.5);
+     //delete thread methods if threads do not work//
+     t1.start(read_mic);
+     t2.start(update_lcd_score);
+     
+     //mic.attach(&read_mic, 1.0/8000.0);
      while (1) {
         timer = 0;
         // Blinking pause button
@@ -446,27 +477,48 @@
             //mic.attach(&read_mic, 1.0/8000.0);
             //Threread_mic();
             if (myNav.fire()) {
+                lcd_mutex.lock();
                 uLCD.filled_rectangle(60, 68, 65, 82, GREEN);
                 uLCD.filled_rectangle(70, 68, 75, 82, GREEN);
                 wait(.1);
+                //**delete mutexs ad thread methods if threads do not work**//
+                lcd_mutex.lock();
+                t1.terminate();
+                t2.terminate();
+                t1.join();
+                t2.join();
+                //******************************************************//
                 pause();
             }
+            lcd_mutex.lock(); //remove if thread doesnt work
             if (timer % 25 == 0) {
                 //score1 += getScore1(); 
                 //score2 += getScore2(); 
-                update_score();
-                update_leds();
-                uLCD.color(BLUE);
-                uLCD.locate(0,1);
-                uLCD.printf("%4d", mic_reading1);
-                uLCD.color(PURPLE);
-                uLCD.locate(0,3);
-                uLCD.printf("%4d", mic_reading2);
+                
+                //*****uncomment if threads do not work********//
+                //update_score();
+                //update_leds();
+                //uLCD.color(BLUE);
+                //uLCD.locate(0,1);
+                //uLCD.printf("%4d", mic_reading1); // change to score1
+                //uLCD.printf("%4d", mic_reading2); //change to score 2
+                //uLCD.color(PURPLE);
+                //uLCD.locate(0,3);
+                //uLCD.printf("%4d", mic_reading2);
+                //uLCD.printf("%4d", score2);
+                //*******************************************//
+                
                 // Time bar
                 double roundTime = 15.0;  // in secs
                 int length = (int) ( (double)curTime / roundTime * 127.0);
                 uLCD.filled_rectangle(0, 65, length, 85, DGREEN);
                 if (length >= 127) {
+                    t1.terminate();
+                    t2.terminate();
+                    t1.join();
+                    t2.join();
+                    myled = 0;
+                    lcd_mutex.unlock();
                     nextRound();
                 }
                 curTime++;
@@ -478,6 +530,7 @@
                 uLCD.filled_rectangle(60, 68, 65, 82, BLACK);
                 uLCD.filled_rectangle(70, 68, 75, 82, BLACK);
             }
+            lcd_mutex.unlock();
             Thread::wait(100);
             timer++;
          }
@@ -639,6 +692,7 @@
 
 int main() {
      // read in highScoresArr from SD card
+     play();
      FILE *fp = fopen("/sd/finalProj/highscores.txt", "r");
      if(fp == NULL) {
      uLCD.printf("Error Open \n");