![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
lab stugg
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem 4180_Lab3_rtos_basic
main.cpp@10:275cc4d62276, 2021-05-02 (annotated)
- Committer:
- mbarros7
- Date:
- Sun May 02 21:12:53 2021 +0000
- Revision:
- 10:275cc4d62276
- Parent:
- 9:1b37fd4bf5ea
lllllll
Who changed what in which revision?
User | Revision | Line number | New 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" |
mbarros7 | 9:1b37fd4bf5ea | 6 | #include "PlatformMutex.h" |
ldeng31 | 7:49bfd42cbf6f | 7 | |
mbarros7 | 9:1b37fd4bf5ea | 8 | |
ycai47 | 8:8eb653dc6571 | 9 | |
mbarros7 | 9:1b37fd4bf5ea | 10 | uLCD_4DGL myLCD(p28, p27, p30); |
mbarros7 | 9:1b37fd4bf5ea | 11 | Mutex lcd_mutex; |
mbarros7 | 9:1b37fd4bf5ea | 12 | RawSerial pc(USBTX, USBRX); |
mbarros7 | 9:1b37fd4bf5ea | 13 | Serial bluemod(p13,p14); |
mbarros7 | 9:1b37fd4bf5ea | 14 | |
mbarros7 | 10:275cc4d62276 | 15 | PwmOut red(p24); |
mbarros7 | 9:1b37fd4bf5ea | 16 | PwmOut green(p22); |
mbarros7 | 9:1b37fd4bf5ea | 17 | PwmOut blue(p21); |
ldeng31 | 7:49bfd42cbf6f | 18 | |
ycai47 | 8:8eb653dc6571 | 19 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
ycai47 | 8:8eb653dc6571 | 20 | |
ycai47 | 8:8eb653dc6571 | 21 | AnalogOut Speaker(p18); |
ycai47 | 8:8eb653dc6571 | 22 | wave_player waver(&Speaker); |
ycai47 | 8:8eb653dc6571 | 23 | bool PLAY = true; |
ycai47 | 8:8eb653dc6571 | 24 | bool *PlayStopPtr = &PLAY; |
ycai47 | 8:8eb653dc6571 | 25 | |
mbarros7 | 9:1b37fd4bf5ea | 26 | // Thread 1 |
mbarros7 | 9:1b37fd4bf5ea | 27 | // print counter into first line and wait for 1 s |
mbarros7 | 9:1b37fd4bf5ea | 28 | void thread1(void const *args) |
mbarros7 | 9:1b37fd4bf5ea | 29 | { |
mbarros7 | 9:1b37fd4bf5ea | 30 | int i; |
mbarros7 | 9:1b37fd4bf5ea | 31 | while(true) { // thread loop |
ycai47 | 8:8eb653dc6571 | 32 | lcd_mutex.lock(); |
ycai47 | 8:8eb653dc6571 | 33 | myLCD.locate(0,0); |
mbarros7 | 9:1b37fd4bf5ea | 34 | myLCD.printf("Weeeee Woooo"); |
ycai47 | 8:8eb653dc6571 | 35 | lcd_mutex.unlock(); |
mbarros7 | 9:1b37fd4bf5ea | 36 | i++; |
mbarros7 | 9:1b37fd4bf5ea | 37 | Thread::wait(1000); |
ycai47 | 8:8eb653dc6571 | 38 | } |
ldeng31 | 7:49bfd42cbf6f | 39 | } |
ldeng31 | 7:49bfd42cbf6f | 40 | |
mbarros7 | 9:1b37fd4bf5ea | 41 | // Thread 2 |
mbarros7 | 9:1b37fd4bf5ea | 42 | // print counter into third line and wait for 0,5s |
mbarros7 | 9:1b37fd4bf5ea | 43 | void thread2(void const *args) |
mbarros7 | 9:1b37fd4bf5ea | 44 | { |
mbarros7 | 9:1b37fd4bf5ea | 45 | int k; |
mbarros7 | 9:1b37fd4bf5ea | 46 | while(true) { // thread loop |
ycai47 | 8:8eb653dc6571 | 47 | lcd_mutex.lock(); |
mbarros7 | 9:1b37fd4bf5ea | 48 | myLCD.locate(0,20); |
mbarros7 | 9:1b37fd4bf5ea | 49 | myLCD.printf("Pull Over."); |
ycai47 | 8:8eb653dc6571 | 50 | lcd_mutex.unlock(); |
ycai47 | 8:8eb653dc6571 | 51 | k++; |
mbarros7 | 9:1b37fd4bf5ea | 52 | Thread::wait(500); // wait 0.5s |
ycai47 | 8:8eb653dc6571 | 53 | } |
ldeng31 | 7:49bfd42cbf6f | 54 | } |
ldeng31 | 7:49bfd42cbf6f | 55 | |
ldeng31 | 7:49bfd42cbf6f | 56 | |
mbarros7 | 9:1b37fd4bf5ea | 57 | void thread4(void const *args) { |
ycai47 | 8:8eb653dc6571 | 58 | while(true) { |
ycai47 | 8:8eb653dc6571 | 59 | FILE *wave_file; |
mbarros7 | 9:1b37fd4bf5ea | 60 | wave_file=fopen("/sd/Siren.wav","r"); |
ycai47 | 8:8eb653dc6571 | 61 | waver.play(wave_file,PlayStopPtr); |
ycai47 | 8:8eb653dc6571 | 62 | fclose(wave_file); |
ycai47 | 8:8eb653dc6571 | 63 | Thread::wait(1000); // wait 1.0s |
ycai47 | 8:8eb653dc6571 | 64 | } |
ldeng31 | 7:49bfd42cbf6f | 65 | } |
emilmont | 1:491820ee784d | 66 | |
emilmont | 1:491820ee784d | 67 | int main() { |
ycai47 | 8:8eb653dc6571 | 68 | |
ycai47 | 8:8eb653dc6571 | 69 | myLCD.cls(); |
ycai47 | 8:8eb653dc6571 | 70 | |
mbarros7 | 9:1b37fd4bf5ea | 71 | Thread t1(thread1); |
mbarros7 | 9:1b37fd4bf5ea | 72 | Thread t2(thread2); |
mbarros7 | 9:1b37fd4bf5ea | 73 | // Thread t3(thread3); |
mbarros7 | 9:1b37fd4bf5ea | 74 | Thread t4(thread4); |
emilmont | 1:491820ee784d | 75 | |
ycai47 | 8:8eb653dc6571 | 76 | Timer t; |
ycai47 | 8:8eb653dc6571 | 77 | t.start(); |
emilmont | 1:491820ee784d | 78 | while (true) { |
ycai47 | 8:8eb653dc6571 | 79 | lcd_mutex.lock(); |
ycai47 | 8:8eb653dc6571 | 80 | myLCD.locate(0,9); |
ycai47 | 8:8eb653dc6571 | 81 | myLCD.printf("The time taken was %0.2f seconds\n", t.read()); |
mbarros7 | 9:1b37fd4bf5ea | 82 | char bred=0; |
mbarros7 | 9:1b37fd4bf5ea | 83 | char bgreen=0; |
mbarros7 | 9:1b37fd4bf5ea | 84 | char bblue=0; |
mbarros7 | 9:1b37fd4bf5ea | 85 | red = green = blue = 0; |
mbarros7 | 9:1b37fd4bf5ea | 86 | while(1) { |
mbarros7 | 9:1b37fd4bf5ea | 87 | if (bluemod.getc()=='!') { |
mbarros7 | 9:1b37fd4bf5ea | 88 | if (bluemod.getc()=='C') { //color data packet |
mbarros7 | 9:1b37fd4bf5ea | 89 | bred = bluemod.getc(); // RGB color values |
mbarros7 | 9:1b37fd4bf5ea | 90 | bgreen = bluemod.getc(); |
mbarros7 | 9:1b37fd4bf5ea | 91 | bblue = bluemod.getc(); |
mbarros7 | 9:1b37fd4bf5ea | 92 | if (bluemod.getc()==char(~('!' + 'C' + bred + bgreen + bblue))) { //checksum OK? |
mbarros7 | 9:1b37fd4bf5ea | 93 | red = bred/255.0; //send new color to RGB LED PWM outputs |
mbarros7 | 9:1b37fd4bf5ea | 94 | green = bgreen/255.0; |
mbarros7 | 9:1b37fd4bf5ea | 95 | blue = bblue/255.0; |
mbarros7 | 9:1b37fd4bf5ea | 96 | } |
mbarros7 | 9:1b37fd4bf5ea | 97 | } |
mbarros7 | 9:1b37fd4bf5ea | 98 | } |
mbarros7 | 9:1b37fd4bf5ea | 99 | } |
mbarros7 | 9:1b37fd4bf5ea | 100 | |
mbarros7 | 9:1b37fd4bf5ea | 101 | |
ycai47 | 8:8eb653dc6571 | 102 | lcd_mutex.unlock(); |
emilmont | 1:491820ee784d | 103 | Thread::wait(500); |
emilmont | 1:491820ee784d | 104 | } |
emilmont | 1:491820ee784d | 105 | } |