Lab 3 Part 4

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

Revision:
15:e406620ee8af
Parent:
14:ae63fb7c2d06
Child:
16:f31b20cfeebc
--- a/main.cpp	Thu Feb 25 20:51:58 2021 +0000
+++ b/main.cpp	Tue Mar 02 15:24:05 2021 +0000
@@ -31,8 +31,10 @@
 Thread thread3;
 Thread thread4;
 
-Semaphore sem1(0);
-Semaphore sem2(0);
+Semaphore semled(0);
+Semaphore semlcd1(0);
+Semaphore semlcd2(0);
+Semaphore sem_check(0);
 
 ///* The Terminator 53. Coming to theatres this summer!!!!!! */
 //void the_terminator()
@@ -40,106 +42,265 @@
 //
 //}
 
-
-/* Function Calls */
-void top_red_box()
-{
-    stdio_mutex.lock();
-    uLCD.filled_rectangle(0, 0, 127, 63, RED);
-    stdio_mutex.unlock();
-}
-void bot_blue_box()
-{
-    stdio_mutex.lock();
-    uLCD.filled_rectangle(0, 63, 127, 127, BLUE);
-    stdio_mutex.unlock();
-}
-void top_black_box()
-{
-    stdio_mutex.lock();
-    uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
-    stdio_mutex.unlock();
-
-}
-void bot_black_box()
-{
-    stdio_mutex.lock();
-    uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
-    stdio_mutex.unlock();
-}
-
-
 /* Threads */
 void led_thread()
 {
     while(1) {
+        /* Setting 1: Red and Blue */
         if(setting == '1') {
             bLED = 0;
             rLED = 1;
             gLED = 0;
-            Thread::wait(750);
+            //Thread::wait(750);
+            semled.wait();
             rLED = 0;
             bLED = 1;
             gLED = 0;
-            Thread::wait(750);
+            //Thread::wait(750);
+            semled.wait();
         }
-        if (setting == '2') {
+
+        /* Setting 2: Purple and Green */
+        else if (setting == '2') {
             bLED = 1;
             rLED = 1;
             gLED = 0;
-            Thread::wait(750);
+            //Thread::wait(750);
+            semled.wait();
             rLED = 0;
             bLED = 0;
             gLED = 1;
-            Thread::wait(750);
+            //Thread::wait(750);
+            semled.wait();
         }
-        if (blue.readable() && blue.getc()=='!') {
-            if (blue.getc()=='B') { //button data
-                char bnum = blue.getc(); //button number
-                if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4
-                    setting = bnum;// Change setting based on button input to set start setting. Check for chars
-                    rLED = 0;
-                    gLED = 0;
-                    bLED = 0;
-                    myled[bnum-'1']=blue.getc()-'0'; //turn on/off that num LED
-                    wait(1);
-                    break;
-                }
-            }
+
+        /* Setting 3: Yellow and Cyan*/
+        else if(setting == '3') {
+            bLED = 0;
+            rLED = 1;
+            gLED = 1;
+            //Thread::wait(750);
+            semled.wait();
+            rLED = 0;
+            bLED = 1;
+            gLED = 1;
+            //Thread::wait(750);
+            semled.wait();
         }
+
+        /* Setting 4: Black and White */
+        else if(setting == '4') {
+            bLED = 0;
+            rLED = 0;
+            gLED = 0;
+            //Thread::wait(750);
+            semled.wait();
+            rLED = 1;
+            bLED = 1;
+            gLED = 1;
+            //Thread::wait(750);
+            semled.wait();
+        }
+
     }
 }
 
 void speaker_thread()
 {
     while(1) {
-        FILE *wave_file;
-        wave_file=fopen("/sd/WEEWOO.wav","r");
-        waver.play(wave_file);
-        fclose(wave_file);
+
+        /* Setting 1: WeeWoo Wagon */
+        if(setting == '1') {
+            FILE *wave_file;
+            wave_file=fopen("/sd/WEEWOO.wav","r");
+            waver.play(wave_file);
+            fclose(wave_file);
+        }
+        
+        /* Setting 2: */
+        if(setting == '2') {
+            FILE *wave_file;
+            wave_file=fopen("/sd/dank_export.wav","r");
+            waver.play(wave_file);
+            fclose(wave_file);
+        }
+        
+        /* Setting 3: */
+        if(setting == '3') {
+            FILE *wave_file;
+            wave_file=fopen("/sd/crickets_exp.wav","r");
+            waver.play(wave_file);
+            fclose(wave_file);
+        }
+        
+        /* Setting 4: */
+        if(setting == '4') {
+            FILE *wave_file;
+            wave_file=fopen("/sd/static_exp.wav","r");
+            waver.play(wave_file);
+            fclose(wave_file);
+        }
+
     }
 }
 
 void lcd1_thread()
 {
     while(1) {
-        top_red_box();
-        Thread::wait(750);
-        top_black_box();
-        Thread::wait(750);
+
+        /* Setting 1: Red */
+        if(setting == '1') {
+            // Top Red Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 0, 127, 63, RED);
+            stdio_mutex.unlock();
+            Thread::wait(750);
+            semled.release();
+            semlcd2.release();
+            // Top Black Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
+            stdio_mutex.unlock();
+            Thread::wait(750);
+            sem_check.release();
+            semlcd1.wait();
+            semled.release();
+            semlcd2.release();
+        }
+
+        /* Setting 2: Purple */
+        else if(setting == '2') {
+            // Top Red Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 0, 127, 63, 0xFF00FF);
+            stdio_mutex.unlock();
+            Thread::wait(750);
+            semled.release();
+            semlcd2.release();
+            // Top Black Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
+            stdio_mutex.unlock();
+            Thread::wait(750);
+            sem_check.release();
+            semlcd1.wait();
+            semled.release();
+            semlcd2.release();
+        }
+
+        /* Setting 3: Yellow */
+        else if(setting == '3') {
+            // Top Red Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 0, 127, 63, 0xFFFF00);
+            stdio_mutex.unlock();
+            Thread::wait(750);
+            semled.release();
+            semlcd2.release();
+            // Top Black Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
+            stdio_mutex.unlock();
+            Thread::wait(750);
+            sem_check.release();
+            semlcd1.wait();
+            semled.release();
+            semlcd2.release();
+        }
+
+        /* Setting 4: Black and White */
+        else if(setting == '4') {
+            // Top Red Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
+            stdio_mutex.unlock();
+            Thread::wait(250);
+            semled.release();
+            semlcd2.release();
+            // Top Black Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 0, 127, 63, WHITE);
+            stdio_mutex.unlock();
+            Thread::wait(250);
+            sem_check.release();
+            semlcd1.wait();
+            semled.release();
+            semlcd2.release();
+        }
     }
 }
 
 void lcd2_thread()
 {
     while(1) {
-        bot_black_box();
-        Thread::wait(750);
-        bot_blue_box();
-        Thread::wait(750);
+
+        /* Setting 1: Blue */
+        if(setting == '1') {
+            // Bottom Black Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
+            stdio_mutex.unlock();
+            //Thread::wait(750);
+            semlcd2.wait();
+            // Bottom Blue Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 63, 127, 127, BLUE);
+            stdio_mutex.unlock();
+            //Thread::wait(750);
+            semlcd2.wait();
+        }
+
+        /* Setting 2: Green */
+        else if(setting == '2') {
+            // Bottom Black Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
+            stdio_mutex.unlock();
+            //Thread::wait(750);
+            semlcd2.wait();
+            // Bottom Blue Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 63, 127, 127, GREEN);
+            stdio_mutex.unlock();
+            //Thread::wait(750);
+            semlcd2.wait();
+        }
+
+        /* Setting 3: Cyan */
+        else if(setting == '3') {
+            // Bottom Black Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
+            stdio_mutex.unlock();
+            //Thread::wait(750);
+            semlcd2.wait();
+            // Bottom Blue Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 63, 127, 127, 0x00FFFF);
+            stdio_mutex.unlock();
+            //Thread::wait(750);
+            semlcd2.wait();
+        }
+
+        /* Setting 4: Black and White */
+        else if(setting == '4') {
+            // Bottom Black Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
+            stdio_mutex.unlock();
+            //Thread::wait(750);
+            semlcd2.wait();
+            // Bottom Blue Box
+            stdio_mutex.lock();
+            uLCD.filled_rectangle(0, 63, 127, 127, WHITE);
+            stdio_mutex.unlock();
+            //Thread::wait(750);
+            semlcd2.wait();
+        }
     }
 }
 
+
 int main()
 {
     rLED = 0;
@@ -167,18 +328,19 @@
     thread4.start(lcd2_thread);
 
     /* Infinite Loop, let threads run */
-    while (true) {
-        ///* Terminate all thread processes */
-//        thread1.join();
-//
-//        thread2.terminate();
-//        thread3.terminate();
-//        thread4.terminate();
-//
-//        /* Start all thread processes again, this time with new settings */
-//        thread1.start(led_thread);
-//        thread2.start(speaker_thread);
-//        thread3.start(lcd1_thread);
-//        thread4.start(lcd2_thread);
+    while (1) {
+
+        sem_check.wait();
+
+        if (blue.readable() && blue.getc()=='!') {
+            if (blue.getc()=='B') { //button data
+                char bnum = blue.getc(); //button number
+                if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4
+                    setting = bnum;// Change setting based on button input to set start setting. Check for chars
+                }
+            }
+        }
+
+        semlcd1.release();
     }
 }