Craig Raslawski / Mbed 2 deprecated RTOS_threadingWorking

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

Fork of RTOS_threading by James Plager

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "uLCD_4DGL.h"
00004 #include "ShiftBrite.h"
00005 #include "SDFileSystem.h"
00006 #include "wave_player.h"
00007 
00008 Mutex mutex;    //handles writing to the LCD
00009 Mutex mutex2;   //used for color changing
00010 DigitalOut led1(LED1);
00011 DigitalOut led2(LED2);
00012 DigitalOut led3(LED3);
00013 DigitalOut led4(LED4);
00014 Thread thread1;
00015 Thread thread2;
00016 Thread thread3;
00017 Thread thread4;
00018 DigitalOut latch(p15);
00019 DigitalOut enable(p16);
00020 SPI spi(p11, p12, p13);
00021 //uLCD_4DGL uLCD(p28,p27,p29); //(p27, p28, p30);    //tx, rx, rst
00022 uLCD_4DGL uLCD(p28, p27, p30); 
00023 ShiftBrite myBrite(p15,p16,spi); //latch, enable, spi
00024 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00025 AnalogOut DACout(p18);      //must be p18
00026 RawSerial BT(p9, p10);  //bluetooth pinout
00027 int red = 200;
00028 int blue = 0;
00029 int green = 0;
00030 FILE *wave_file = NULL;        //global bc its gotta be changed by Main while running in child thread
00031 FILE *wave_file2 = NULL;
00032 unsigned int redHex = 0xFF0000;
00033 unsigned int grnHex = 0x00FF00;
00034 unsigned int bluHex = 0x0000FF;
00035 
00036 
00037 wave_player waver(&DACout);
00038 
00039 void LCD_thread1() {
00040     while(1){    
00041         mutex.lock();
00042         uLCD.filled_circle(64, 64, 12, 0xFF0000);
00043         mutex.unlock();
00044         wait(.5);
00045         mutex.lock();
00046         uLCD.filled_circle(64, 64, 12, bluHex);
00047         mutex.unlock();
00048         wait(.5);
00049     }     
00050 }
00051 void LCD_thread2() {    //update a timer on the display every 100ms
00052     uLCD.cls();
00053     //float time = 0.0;
00054     int count =0;
00055     while(1) {
00056         Thread::wait(60);
00057         //time = time + 0.25;
00058         mutex.lock();
00059         uLCD.locate(0,0);
00060         count++;
00061         uLCD.printf("Counting! %i \n", count);
00062         mutex.unlock();
00063     }
00064 }
00065 
00066 void LED_thread() {
00067     //flash red & blue for police siren
00068     while(1){
00069         mutex2.lock();
00070         //myBrite.Write(red,green,blue);
00071         myBrite.Write(150, green, 0);
00072         mutex2.unlock();
00073         wait(.5);
00074         mutex2.lock();
00075         myBrite.Write(0,green,150);
00076         //myBrite.Write(blue,green,red);  // change LEDs so Red=OFF, Blue=ON
00077         mutex2.unlock();
00078         wait(.5); 
00079     }
00080 } 
00081 
00082 void sound_thread(){
00083     wave_file=fopen("/sd/Police_Siren.wav","r");    
00084     if (wave_file == NULL){
00085            led1=0;
00086            led2=led3=led4 = 1;
00087     }
00088     waver.play(wave_file);
00089     fclose(wave_file);  
00090 }
00091 
00092 void banker_thread(){
00093     wave_file=fopen("/sd/banker_calling.wav","r");
00094     if (wave_file == NULL) {
00095         led1=led2=led3=1;
00096         led4=0;
00097     }
00098     waver.play(wave_file);
00099     fclose(wave_file);
00100 }
00101 
00102 int main() {
00103     thread1.start(LED_thread);  //police lights work
00104     thread2.start(LCD_thread1);
00105     thread3.start(LCD_thread2);
00106     thread4.start(sound_thread);
00107     // use mutex to lock getc(), printf(), scanf()
00108     // don't unlock until you've checked that it's readable() 
00109     
00110     char bnum=0;
00111     
00112     while(1) {
00113         led3=0;
00114         led4=1;
00115         if (BT.getc()=='!') {
00116             if (BT.getc()=='B') { //button data
00117                 bnum = BT.getc(); //button number
00118                 if (bnum == '1') {  //turn Green LED on
00119                     green = 250; 
00120                     led1 = 1;
00121                     led2=led3=led4=0;
00122                 }
00123                 if (bnum == '2') { // revert to normal operation
00124                     green = 0;
00125                     bluHex = 0x0000FF;
00126                     led1=led3=led4=0;
00127                     led2=1;
00128                     if(wave_file2 != NULL){
00129                         thread4.terminate();
00130                         fclose(wave_file2);
00131                         thread4.start(sound_thread);
00132                     }
00133                 }
00134                 if (bnum == '3') {  // change sound file playing  
00135                     led2=led1=led4=0;
00136                     led3=1;
00137                     if ( wave_file != NULL) {
00138                         led2=1;              // debug
00139                         thread4.terminate(); //stop police siren from playing
00140                         fclose(wave_file);
00141                         led2=0;
00142                         thread4.start(banker_thread);
00143                     }
00144                     
00145                     
00146                     //CHANGING THIS BLOCK TO ALLOW BANKER INTERRUPTION
00147                     /*
00148                     FILE *wave_file2;
00149                     wave_file2=fopen("/sd/banker_calling.wav","r");
00150                     if (wave_file2 == NULL){
00151                         led1=led2=0;
00152                         led3=led4=1;
00153                     }
00154                     waver.play(wave_file2);
00155                     fclose(wave_file2);
00156                     */
00157                         
00158                     //thread4.start(sound_thread); //return to siren
00159                 }
00160                 if (bnum == '4') {  // change LCD colors
00161                     bluHex = 0x00FF00;  //change the lights to flash red/grn
00162                     led2=led3=led1=0;
00163                     led4=1;
00164                 }
00165             }
00166             led4=0;
00167             led3=1;
00168         }
00169     } 
00170 
00171 }