lab3_part2

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

Fork of rtos_basic by mbed official

Committer:
ycai47
Date:
Thu Oct 01 20:35:16 2015 +0000
Revision:
8:8eb653dc6571
Parent:
7:49bfd42cbf6f
part 2 done

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:491820ee784d 1 #include "mbed.h"
emilmont 1:491820ee784d 2 #include "rtos.h"
ycai47 8:8eb653dc6571 3 #include "uLCD_4DGL.h"
ycai47 8:8eb653dc6571 4 #include "SDFileSystem.h"
ycai47 8:8eb653dc6571 5 #include "wave_player.h"
ldeng31 7:49bfd42cbf6f 6
ycai47 8:8eb653dc6571 7 uLCD_4DGL myLCD(p9, p10, p11);
ycai47 8:8eb653dc6571 8 Mutex lcd_mutex;
ycai47 8:8eb653dc6571 9
ldeng31 7:49bfd42cbf6f 10 PwmOut RGBLED_r(p23);
ldeng31 7:49bfd42cbf6f 11 PwmOut RGBLED_g(p22);
ldeng31 7:49bfd42cbf6f 12 PwmOut RGBLED_b(p21);
ldeng31 7:49bfd42cbf6f 13
ycai47 8:8eb653dc6571 14 SDFileSystem sd(p5, p6, p7, p8, "sd");
ycai47 8:8eb653dc6571 15
ycai47 8:8eb653dc6571 16 AnalogOut Speaker(p18);
ycai47 8:8eb653dc6571 17 wave_player waver(&Speaker);
ycai47 8:8eb653dc6571 18 bool PLAY = true;
ycai47 8:8eb653dc6571 19 bool *PlayStopPtr = &PLAY;
ycai47 8:8eb653dc6571 20
ycai47 8:8eb653dc6571 21 int j = 0;
ycai47 8:8eb653dc6571 22 int k = 0;
ycai47 8:8eb653dc6571 23
ldeng31 7:49bfd42cbf6f 24 void ulcd1_thread(void const *args) {
ycai47 8:8eb653dc6571 25 while(true) {
ycai47 8:8eb653dc6571 26 lcd_mutex.lock();
ycai47 8:8eb653dc6571 27 myLCD.locate(0,0);
ycai47 8:8eb653dc6571 28 myLCD.printf("Thread1 status count: %d", j);
ycai47 8:8eb653dc6571 29 lcd_mutex.unlock();
ycai47 8:8eb653dc6571 30 j++;
ycai47 8:8eb653dc6571 31 Thread::wait(150);
ycai47 8:8eb653dc6571 32 }
ldeng31 7:49bfd42cbf6f 33 }
ldeng31 7:49bfd42cbf6f 34
ldeng31 7:49bfd42cbf6f 35 void ulcd2_thread(void const *args) {
ycai47 8:8eb653dc6571 36 while(true) {
ycai47 8:8eb653dc6571 37 lcd_mutex.lock();
ycai47 8:8eb653dc6571 38 myLCD.locate(0,5);
ycai47 8:8eb653dc6571 39 myLCD.printf("Thread 2 status count : %d", k);
ycai47 8:8eb653dc6571 40 lcd_mutex.unlock();
ycai47 8:8eb653dc6571 41 k++;
ycai47 8:8eb653dc6571 42 Thread::wait(300);
ycai47 8:8eb653dc6571 43 }
ldeng31 7:49bfd42cbf6f 44 }
ldeng31 7:49bfd42cbf6f 45
ldeng31 7:49bfd42cbf6f 46 void led_thread(void const *args) {
ycai47 8:8eb653dc6571 47 while(true) {
ycai47 8:8eb653dc6571 48 RGBLED_g = 0.0;
ldeng31 7:49bfd42cbf6f 49 RGBLED_r = 1.0;
ycai47 8:8eb653dc6571 50 RGBLED_b = 0.0;
ycai47 8:8eb653dc6571 51 Thread::wait(300);
ycai47 8:8eb653dc6571 52 RGBLED_r = 0.0;
ycai47 8:8eb653dc6571 53 RGBLED_b = 1.0;
ycai47 8:8eb653dc6571 54 Thread::wait(300);
emilmont 1:491820ee784d 55 }
emilmont 1:491820ee784d 56 }
ldeng31 7:49bfd42cbf6f 57
ldeng31 7:49bfd42cbf6f 58 void Speaker_thread(void const *args) {
ycai47 8:8eb653dc6571 59 while(true) {
ycai47 8:8eb653dc6571 60 FILE *wave_file;
ycai47 8:8eb653dc6571 61 wave_file=fopen("/sd/mySound/Siren.wav","r");
ycai47 8:8eb653dc6571 62 waver.play(wave_file,PlayStopPtr);
ycai47 8:8eb653dc6571 63 fclose(wave_file);
ycai47 8:8eb653dc6571 64 Thread::wait(1000); // wait 1.0s
ycai47 8:8eb653dc6571 65 }
ldeng31 7:49bfd42cbf6f 66 }
emilmont 1:491820ee784d 67
emilmont 1:491820ee784d 68 int main() {
ycai47 8:8eb653dc6571 69
ycai47 8:8eb653dc6571 70 myLCD.cls();
ycai47 8:8eb653dc6571 71
ycai47 8:8eb653dc6571 72 Thread thread0(ulcd1_thread);
ycai47 8:8eb653dc6571 73 Thread thread1(ulcd2_thread);
ycai47 8:8eb653dc6571 74 Thread thread2(led_thread);
ycai47 8:8eb653dc6571 75 Thread thread3(Speaker_thread);
emilmont 1:491820ee784d 76
ycai47 8:8eb653dc6571 77 Timer t;
ycai47 8:8eb653dc6571 78 t.start();
emilmont 1:491820ee784d 79 while (true) {
ycai47 8:8eb653dc6571 80 lcd_mutex.lock();
ycai47 8:8eb653dc6571 81 myLCD.locate(0,9);
ycai47 8:8eb653dc6571 82 myLCD.printf("The time taken was %0.2f seconds\n", t.read());
ycai47 8:8eb653dc6571 83 lcd_mutex.unlock();
emilmont 1:491820ee784d 84 Thread::wait(500);
emilmont 1:491820ee784d 85 }
emilmont 1:491820ee784d 86 }