Lab 3 Part 4
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem
main.cpp@14:ae63fb7c2d06, 2021-02-25 (annotated)
- Committer:
- glanier9
- Date:
- Thu Feb 25 20:51:58 2021 +0000
- Revision:
- 14:ae63fb7c2d06
- Parent:
- 13:8987c75dfe41
- Child:
- 15:e406620ee8af
Ditched the interrupt idea. Using a check for every blink cycle. ISSUE: for some reason the logic for the serial check inside thread one is being read even though no serial data is transmitted. The LED ends up turning itself off.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
emilmont | 1:491820ee784d | 1 | #include "mbed.h" |
mbed_official | 11:0309bef74ba8 | 2 | #include "rtos.h" |
glanier9 | 12:94b16c66c09a | 3 | #include "Speaker.h" |
glanier9 | 12:94b16c66c09a | 4 | #include "uLCD_4DGL.h" |
glanier9 | 12:94b16c66c09a | 5 | #include "SDFileSystem.h" |
glanier9 | 12:94b16c66c09a | 6 | #include "wave_player.h" |
glanier9 | 12:94b16c66c09a | 7 | |
glanier9 | 13:8987c75dfe41 | 8 | char setting = '1'; |
glanier9 | 12:94b16c66c09a | 9 | |
glanier9 | 12:94b16c66c09a | 10 | BusOut myled(LED1,LED2,LED3,LED4); |
glanier9 | 12:94b16c66c09a | 11 | |
glanier9 | 12:94b16c66c09a | 12 | Serial blue(p13,p14); |
glanier9 | 12:94b16c66c09a | 13 | |
glanier9 | 12:94b16c66c09a | 14 | AnalogOut DACout(p18); |
glanier9 | 12:94b16c66c09a | 15 | |
glanier9 | 12:94b16c66c09a | 16 | wave_player waver(&DACout); |
glanier9 | 12:94b16c66c09a | 17 | |
glanier9 | 12:94b16c66c09a | 18 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
glanier9 | 12:94b16c66c09a | 19 | |
glanier9 | 12:94b16c66c09a | 20 | Mutex stdio_mutex; |
glanier9 | 14:ae63fb7c2d06 | 21 | Mutex get_mutex; |
glanier9 | 12:94b16c66c09a | 22 | |
glanier9 | 12:94b16c66c09a | 23 | uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; |
glanier9 | 12:94b16c66c09a | 24 | |
glanier9 | 12:94b16c66c09a | 25 | PwmOut rLED(p21); // Light up Red LED with p21 |
glanier9 | 12:94b16c66c09a | 26 | PwmOut gLED(p22); // Light up Green LED with p22 |
glanier9 | 12:94b16c66c09a | 27 | PwmOut bLED(p23); // Light up Blue LED with p23 |
glanier9 | 12:94b16c66c09a | 28 | |
glanier9 | 12:94b16c66c09a | 29 | Thread thread1; |
glanier9 | 12:94b16c66c09a | 30 | Thread thread2; |
glanier9 | 12:94b16c66c09a | 31 | Thread thread3; |
glanier9 | 12:94b16c66c09a | 32 | Thread thread4; |
glanier9 | 12:94b16c66c09a | 33 | |
glanier9 | 14:ae63fb7c2d06 | 34 | Semaphore sem1(0); |
glanier9 | 14:ae63fb7c2d06 | 35 | Semaphore sem2(0); |
glanier9 | 14:ae63fb7c2d06 | 36 | |
glanier9 | 14:ae63fb7c2d06 | 37 | ///* The Terminator 53. Coming to theatres this summer!!!!!! */ |
glanier9 | 14:ae63fb7c2d06 | 38 | //void the_terminator() |
glanier9 | 14:ae63fb7c2d06 | 39 | //{ |
glanier9 | 14:ae63fb7c2d06 | 40 | // |
glanier9 | 14:ae63fb7c2d06 | 41 | //} |
glanier9 | 14:ae63fb7c2d06 | 42 | |
glanier9 | 14:ae63fb7c2d06 | 43 | |
glanier9 | 12:94b16c66c09a | 44 | /* Function Calls */ |
glanier9 | 12:94b16c66c09a | 45 | void top_red_box() |
glanier9 | 12:94b16c66c09a | 46 | { |
glanier9 | 12:94b16c66c09a | 47 | stdio_mutex.lock(); |
glanier9 | 12:94b16c66c09a | 48 | uLCD.filled_rectangle(0, 0, 127, 63, RED); |
glanier9 | 12:94b16c66c09a | 49 | stdio_mutex.unlock(); |
glanier9 | 12:94b16c66c09a | 50 | } |
glanier9 | 12:94b16c66c09a | 51 | void bot_blue_box() |
glanier9 | 12:94b16c66c09a | 52 | { |
glanier9 | 12:94b16c66c09a | 53 | stdio_mutex.lock(); |
glanier9 | 12:94b16c66c09a | 54 | uLCD.filled_rectangle(0, 63, 127, 127, BLUE); |
glanier9 | 12:94b16c66c09a | 55 | stdio_mutex.unlock(); |
glanier9 | 12:94b16c66c09a | 56 | } |
glanier9 | 12:94b16c66c09a | 57 | void top_black_box() |
glanier9 | 12:94b16c66c09a | 58 | { |
glanier9 | 12:94b16c66c09a | 59 | stdio_mutex.lock(); |
glanier9 | 12:94b16c66c09a | 60 | uLCD.filled_rectangle(0, 0, 127, 63, BLACK); |
glanier9 | 12:94b16c66c09a | 61 | stdio_mutex.unlock(); |
glanier9 | 12:94b16c66c09a | 62 | |
glanier9 | 12:94b16c66c09a | 63 | } |
glanier9 | 12:94b16c66c09a | 64 | void bot_black_box() |
glanier9 | 12:94b16c66c09a | 65 | { |
glanier9 | 12:94b16c66c09a | 66 | stdio_mutex.lock(); |
glanier9 | 12:94b16c66c09a | 67 | uLCD.filled_rectangle(0, 63, 127, 127, BLACK); |
glanier9 | 12:94b16c66c09a | 68 | stdio_mutex.unlock(); |
glanier9 | 12:94b16c66c09a | 69 | } |
glanier9 | 12:94b16c66c09a | 70 | |
glanier9 | 12:94b16c66c09a | 71 | |
glanier9 | 12:94b16c66c09a | 72 | /* Threads */ |
glanier9 | 12:94b16c66c09a | 73 | void led_thread() |
glanier9 | 12:94b16c66c09a | 74 | { |
glanier9 | 12:94b16c66c09a | 75 | while(1) { |
glanier9 | 13:8987c75dfe41 | 76 | if(setting == '1') { |
glanier9 | 13:8987c75dfe41 | 77 | bLED = 0; |
glanier9 | 13:8987c75dfe41 | 78 | rLED = 1; |
glanier9 | 13:8987c75dfe41 | 79 | gLED = 0; |
glanier9 | 13:8987c75dfe41 | 80 | Thread::wait(750); |
glanier9 | 13:8987c75dfe41 | 81 | rLED = 0; |
glanier9 | 13:8987c75dfe41 | 82 | bLED = 1; |
glanier9 | 13:8987c75dfe41 | 83 | gLED = 0; |
glanier9 | 13:8987c75dfe41 | 84 | Thread::wait(750); |
glanier9 | 13:8987c75dfe41 | 85 | } |
glanier9 | 13:8987c75dfe41 | 86 | if (setting == '2') { |
glanier9 | 13:8987c75dfe41 | 87 | bLED = 1; |
glanier9 | 13:8987c75dfe41 | 88 | rLED = 1; |
glanier9 | 13:8987c75dfe41 | 89 | gLED = 0; |
glanier9 | 13:8987c75dfe41 | 90 | Thread::wait(750); |
glanier9 | 13:8987c75dfe41 | 91 | rLED = 0; |
glanier9 | 13:8987c75dfe41 | 92 | bLED = 0; |
glanier9 | 13:8987c75dfe41 | 93 | gLED = 1; |
glanier9 | 13:8987c75dfe41 | 94 | Thread::wait(750); |
glanier9 | 13:8987c75dfe41 | 95 | } |
glanier9 | 14:ae63fb7c2d06 | 96 | if (blue.readable() && blue.getc()=='!') { |
glanier9 | 14:ae63fb7c2d06 | 97 | if (blue.getc()=='B') { //button data |
glanier9 | 14:ae63fb7c2d06 | 98 | char bnum = blue.getc(); //button number |
glanier9 | 14:ae63fb7c2d06 | 99 | if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4 |
glanier9 | 14:ae63fb7c2d06 | 100 | setting = bnum;// Change setting based on button input to set start setting. Check for chars |
glanier9 | 14:ae63fb7c2d06 | 101 | rLED = 0; |
glanier9 | 14:ae63fb7c2d06 | 102 | gLED = 0; |
glanier9 | 14:ae63fb7c2d06 | 103 | bLED = 0; |
glanier9 | 14:ae63fb7c2d06 | 104 | myled[bnum-'1']=blue.getc()-'0'; //turn on/off that num LED |
glanier9 | 14:ae63fb7c2d06 | 105 | wait(1); |
glanier9 | 14:ae63fb7c2d06 | 106 | break; |
glanier9 | 14:ae63fb7c2d06 | 107 | } |
glanier9 | 14:ae63fb7c2d06 | 108 | } |
glanier9 | 14:ae63fb7c2d06 | 109 | } |
emilmont | 1:491820ee784d | 110 | } |
emilmont | 1:491820ee784d | 111 | } |
glanier9 | 12:94b16c66c09a | 112 | |
glanier9 | 12:94b16c66c09a | 113 | void speaker_thread() |
glanier9 | 12:94b16c66c09a | 114 | { |
glanier9 | 12:94b16c66c09a | 115 | while(1) { |
glanier9 | 12:94b16c66c09a | 116 | FILE *wave_file; |
glanier9 | 12:94b16c66c09a | 117 | wave_file=fopen("/sd/WEEWOO.wav","r"); |
glanier9 | 12:94b16c66c09a | 118 | waver.play(wave_file); |
glanier9 | 12:94b16c66c09a | 119 | fclose(wave_file); |
glanier9 | 12:94b16c66c09a | 120 | } |
glanier9 | 12:94b16c66c09a | 121 | } |
glanier9 | 12:94b16c66c09a | 122 | |
glanier9 | 12:94b16c66c09a | 123 | void lcd1_thread() |
glanier9 | 12:94b16c66c09a | 124 | { |
glanier9 | 12:94b16c66c09a | 125 | while(1) { |
glanier9 | 12:94b16c66c09a | 126 | top_red_box(); |
glanier9 | 12:94b16c66c09a | 127 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 128 | top_black_box(); |
glanier9 | 12:94b16c66c09a | 129 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 130 | } |
glanier9 | 12:94b16c66c09a | 131 | } |
glanier9 | 12:94b16c66c09a | 132 | |
glanier9 | 12:94b16c66c09a | 133 | void lcd2_thread() |
glanier9 | 12:94b16c66c09a | 134 | { |
glanier9 | 12:94b16c66c09a | 135 | while(1) { |
glanier9 | 12:94b16c66c09a | 136 | bot_black_box(); |
glanier9 | 12:94b16c66c09a | 137 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 138 | bot_blue_box(); |
glanier9 | 12:94b16c66c09a | 139 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 140 | } |
glanier9 | 12:94b16c66c09a | 141 | } |
glanier9 | 12:94b16c66c09a | 142 | |
glanier9 | 12:94b16c66c09a | 143 | int main() |
glanier9 | 12:94b16c66c09a | 144 | { |
glanier9 | 12:94b16c66c09a | 145 | rLED = 0; |
glanier9 | 12:94b16c66c09a | 146 | gLED = 0; |
glanier9 | 12:94b16c66c09a | 147 | bLED = 0; |
glanier9 | 13:8987c75dfe41 | 148 | |
glanier9 | 14:ae63fb7c2d06 | 149 | /* Wait for user to press a setting button */ |
glanier9 | 12:94b16c66c09a | 150 | char bnum=0; |
glanier9 | 12:94b16c66c09a | 151 | while(1) { |
glanier9 | 12:94b16c66c09a | 152 | if (blue.readable() && blue.getc()=='!') { |
glanier9 | 12:94b16c66c09a | 153 | if (blue.getc()=='B') { //button data |
glanier9 | 12:94b16c66c09a | 154 | bnum = blue.getc(); //button number |
glanier9 | 13:8987c75dfe41 | 155 | if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4 |
glanier9 | 13:8987c75dfe41 | 156 | setting = bnum;// Change setting based on button input to set start setting. Check for chars |
glanier9 | 12:94b16c66c09a | 157 | break; //turn on/off that num LED |
glanier9 | 13:8987c75dfe41 | 158 | } |
glanier9 | 12:94b16c66c09a | 159 | } |
glanier9 | 12:94b16c66c09a | 160 | } |
glanier9 | 14:ae63fb7c2d06 | 161 | } |
glanier9 | 13:8987c75dfe41 | 162 | |
glanier9 | 14:ae63fb7c2d06 | 163 | /* Start all threads */ |
glanier9 | 12:94b16c66c09a | 164 | thread1.start(led_thread); |
glanier9 | 12:94b16c66c09a | 165 | thread2.start(speaker_thread); |
glanier9 | 12:94b16c66c09a | 166 | thread3.start(lcd1_thread); |
glanier9 | 12:94b16c66c09a | 167 | thread4.start(lcd2_thread); |
glanier9 | 13:8987c75dfe41 | 168 | |
glanier9 | 14:ae63fb7c2d06 | 169 | /* Infinite Loop, let threads run */ |
emilmont | 1:491820ee784d | 170 | while (true) { |
glanier9 | 14:ae63fb7c2d06 | 171 | ///* Terminate all thread processes */ |
glanier9 | 14:ae63fb7c2d06 | 172 | // thread1.join(); |
glanier9 | 14:ae63fb7c2d06 | 173 | // |
glanier9 | 14:ae63fb7c2d06 | 174 | // thread2.terminate(); |
glanier9 | 14:ae63fb7c2d06 | 175 | // thread3.terminate(); |
glanier9 | 14:ae63fb7c2d06 | 176 | // thread4.terminate(); |
glanier9 | 14:ae63fb7c2d06 | 177 | // |
glanier9 | 14:ae63fb7c2d06 | 178 | // /* Start all thread processes again, this time with new settings */ |
glanier9 | 14:ae63fb7c2d06 | 179 | // thread1.start(led_thread); |
glanier9 | 14:ae63fb7c2d06 | 180 | // thread2.start(speaker_thread); |
glanier9 | 14:ae63fb7c2d06 | 181 | // thread3.start(lcd1_thread); |
glanier9 | 14:ae63fb7c2d06 | 182 | // thread4.start(lcd2_thread); |
emilmont | 1:491820ee784d | 183 | } |
emilmont | 1:491820ee784d | 184 | } |