Roomba Final Project with working RTOS

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

Fork of RTOS_threadingWorking by Craig Raslawski

Revision:
13:72e2a45b7847
Parent:
12:7e05acc6502b
Child:
14:088c1b04b4c1
--- a/main.cpp	Sun Feb 26 19:52:01 2017 +0000
+++ b/main.cpp	Mon Feb 27 06:31:12 2017 +0000
@@ -18,7 +18,8 @@
 DigitalOut latch(p15);
 DigitalOut enable(p16);
 SPI spi(p11, p12, p13);
-uLCD_4DGL uLCD(p28,p27,p29); //(p27, p28, p30);    //tx, rx, rst
+//uLCD_4DGL uLCD(p28,p27,p29); //(p27, p28, p30);    //tx, rx, rst
+uLCD_4DGL uLCD(p28, p27, p30); 
 ShiftBrite myBrite(p15,p16,spi); //latch, enable, spi
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
 AnalogOut DACout(p18);      //must be p18
@@ -26,7 +27,7 @@
 int red = 200;
 int blue = 0;
 int green = 0;
-FILE *wave_file;        //global bc its gotta be changed by Main while running in child thread
+FILE *wave_file = NULL;        //global bc its gotta be changed by Main while running in child thread
 unsigned int redHex = 0xFF0000;
 unsigned int grnHex = 0x00FF00;
 unsigned int bluHex = 0x0000FF;
@@ -41,20 +42,20 @@
         mutex.unlock();
         wait(.5);
         mutex.lock();
-        uLCD.filled_circle(64, 64, 12, 0x0000FF);
+        uLCD.filled_circle(64, 64, 12, bluHex);
         mutex.unlock();
         wait(.5);
     }     
 }
 void LCD_thread2() {    //update a timer on the display every 100ms
     uLCD.cls();
-    float time = 0.0;
+    //float time = 0.0;
     int count =0;
     while(1) {
         Thread::wait(60);
         //time = time + 0.25;
+        mutex.lock();
         uLCD.locate(0,0);
-        mutex.lock();
         count++;
         uLCD.printf("Counting! %i \n", count);
         mutex.unlock();
@@ -75,6 +76,7 @@
         wait(.5); 
     }
 } 
+/*
 void BT_thread() {
     // use mutex to lock getc(), printf(), scanf()
     // don't unlock until you've checked that it's readable() 
@@ -102,8 +104,7 @@
         }
         mutex.unlock();
     }  
-    
-}
+}*/
 void sound_thread(){
     //FILE *wave_file;
     wave_file=fopen("/sd/Police_Siren.wav","r");    
@@ -120,28 +121,37 @@
     thread4.start(sound_thread);
     // use mutex to lock getc(), printf(), scanf()
     // don't unlock until you've checked that it's readable() 
+    
     char bnum=0;
+    //
     while(1) {
-        mutex.lock();
+        //mutex.lock();
+        led3=0;
+        led4=1;
         if (BT.getc()=='!') {
             if (BT.getc()=='B') { //button data
                 bnum = BT.getc(); //button number
+                mutex.unlock();
                 if (bnum == '1') {  //turn Green LED on
                     green = 250; 
                     led1 = 1;
-                    led2=led3=led4=0; 
+                    led2=led3=led4=0;
                 }
-                if (bnum == '2') { // turn Green LED off
+                if (bnum == '2') { // revert to normal operation
                     green = 0;
+                    bluHex = 0x0000FF;
                     led1=led3=led4=0;
                     led2=1;
                 }
-                if (bnum == '3') {  // change sound file playing
-                    green = 250;  
+                if (bnum == '3') {  // change sound file playing  
                     led2=led1=led4=0;
                     led3=1;
-                    fclose(wave_file);        //stop police siren from playing
-                    
+                    if ( wave_file != NULL) {
+                        led2=1;                     // debug
+                        //fclose(wave_file);        //stop police siren from playing
+                        thread4.terminate();
+                        led2=0;
+                    }
                     FILE *wave_file2;
                     wave_file2=fopen("/sd/banker_calling.wav","r");
                     if (wave_file2 == NULL){
@@ -149,9 +159,10 @@
                         led3=led4=1;
                     }
                     waver.play(wave_file2);
-                    fclose(wave_file2);
-                    
-
+                    fclose(wave_file2);     
+                    thread4.start(sound_thread);    
+                    //wave_file=fopen("/sd/Police_Siren.wav","r");
+                    //waver.play(wave_file); 
                 }
                 if (bnum == '4') {  // change LCD colors
                     bluHex = 0x00FF00;  //change the lights to flash red/grn
@@ -159,7 +170,11 @@
                     led4=1;
                 }
             }
+            //mutex.unlock();
+            led4=0;
+            led3=1;
         }
+        //mutex.unlock();
     } 
-    
-}
+
+}
\ No newline at end of file