Lab 3 Part 4

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

Committer:
glanier9
Date:
Tue Mar 09 15:15:31 2021 +0000
Revision:
16:f31b20cfeebc
Parent:
15:e406620ee8af
Final Version

Who changed what in which revision?

UserRevisionLine numberNew 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 15:e406620ee8af 34 Semaphore semled(0);
glanier9 15:e406620ee8af 35 Semaphore semlcd1(0);
glanier9 15:e406620ee8af 36 Semaphore semlcd2(0);
glanier9 15:e406620ee8af 37 Semaphore sem_check(0);
glanier9 14:ae63fb7c2d06 38
glanier9 14:ae63fb7c2d06 39 ///* The Terminator 53. Coming to theatres this summer!!!!!! */
glanier9 14:ae63fb7c2d06 40 //void the_terminator()
glanier9 14:ae63fb7c2d06 41 //{
glanier9 14:ae63fb7c2d06 42 //
glanier9 14:ae63fb7c2d06 43 //}
glanier9 14:ae63fb7c2d06 44
glanier9 12:94b16c66c09a 45 /* Threads */
glanier9 12:94b16c66c09a 46 void led_thread()
glanier9 12:94b16c66c09a 47 {
glanier9 12:94b16c66c09a 48 while(1) {
glanier9 15:e406620ee8af 49 /* Setting 1: Red and Blue */
glanier9 13:8987c75dfe41 50 if(setting == '1') {
glanier9 13:8987c75dfe41 51 bLED = 0;
glanier9 13:8987c75dfe41 52 rLED = 1;
glanier9 13:8987c75dfe41 53 gLED = 0;
glanier9 15:e406620ee8af 54 //Thread::wait(750);
glanier9 15:e406620ee8af 55 semled.wait();
glanier9 13:8987c75dfe41 56 rLED = 0;
glanier9 13:8987c75dfe41 57 bLED = 1;
glanier9 13:8987c75dfe41 58 gLED = 0;
glanier9 15:e406620ee8af 59 //Thread::wait(750);
glanier9 15:e406620ee8af 60 semled.wait();
glanier9 13:8987c75dfe41 61 }
glanier9 15:e406620ee8af 62
glanier9 15:e406620ee8af 63 /* Setting 2: Purple and Green */
glanier9 15:e406620ee8af 64 else if (setting == '2') {
glanier9 13:8987c75dfe41 65 bLED = 1;
glanier9 13:8987c75dfe41 66 rLED = 1;
glanier9 13:8987c75dfe41 67 gLED = 0;
glanier9 15:e406620ee8af 68 //Thread::wait(750);
glanier9 15:e406620ee8af 69 semled.wait();
glanier9 13:8987c75dfe41 70 rLED = 0;
glanier9 13:8987c75dfe41 71 bLED = 0;
glanier9 13:8987c75dfe41 72 gLED = 1;
glanier9 15:e406620ee8af 73 //Thread::wait(750);
glanier9 15:e406620ee8af 74 semled.wait();
glanier9 13:8987c75dfe41 75 }
glanier9 15:e406620ee8af 76
glanier9 15:e406620ee8af 77 /* Setting 3: Yellow and Cyan*/
glanier9 15:e406620ee8af 78 else if(setting == '3') {
glanier9 15:e406620ee8af 79 bLED = 0;
glanier9 15:e406620ee8af 80 rLED = 1;
glanier9 15:e406620ee8af 81 gLED = 1;
glanier9 15:e406620ee8af 82 //Thread::wait(750);
glanier9 15:e406620ee8af 83 semled.wait();
glanier9 15:e406620ee8af 84 rLED = 0;
glanier9 15:e406620ee8af 85 bLED = 1;
glanier9 15:e406620ee8af 86 gLED = 1;
glanier9 15:e406620ee8af 87 //Thread::wait(750);
glanier9 15:e406620ee8af 88 semled.wait();
glanier9 14:ae63fb7c2d06 89 }
glanier9 15:e406620ee8af 90
glanier9 15:e406620ee8af 91 /* Setting 4: Black and White */
glanier9 15:e406620ee8af 92 else if(setting == '4') {
glanier9 15:e406620ee8af 93 bLED = 0;
glanier9 15:e406620ee8af 94 rLED = 0;
glanier9 15:e406620ee8af 95 gLED = 0;
glanier9 15:e406620ee8af 96 //Thread::wait(750);
glanier9 15:e406620ee8af 97 semled.wait();
glanier9 15:e406620ee8af 98 rLED = 1;
glanier9 15:e406620ee8af 99 bLED = 1;
glanier9 15:e406620ee8af 100 gLED = 1;
glanier9 15:e406620ee8af 101 //Thread::wait(750);
glanier9 15:e406620ee8af 102 semled.wait();
glanier9 15:e406620ee8af 103 }
glanier9 15:e406620ee8af 104
emilmont 1:491820ee784d 105 }
emilmont 1:491820ee784d 106 }
glanier9 12:94b16c66c09a 107
glanier9 12:94b16c66c09a 108 void speaker_thread()
glanier9 12:94b16c66c09a 109 {
glanier9 12:94b16c66c09a 110 while(1) {
glanier9 15:e406620ee8af 111
glanier9 15:e406620ee8af 112 /* Setting 1: WeeWoo Wagon */
glanier9 15:e406620ee8af 113 if(setting == '1') {
glanier9 15:e406620ee8af 114 FILE *wave_file;
glanier9 15:e406620ee8af 115 wave_file=fopen("/sd/WEEWOO.wav","r");
glanier9 15:e406620ee8af 116 waver.play(wave_file);
glanier9 15:e406620ee8af 117 fclose(wave_file);
glanier9 15:e406620ee8af 118 }
glanier9 15:e406620ee8af 119
glanier9 15:e406620ee8af 120 /* Setting 2: */
glanier9 15:e406620ee8af 121 if(setting == '2') {
glanier9 15:e406620ee8af 122 FILE *wave_file;
glanier9 15:e406620ee8af 123 wave_file=fopen("/sd/dank_export.wav","r");
glanier9 15:e406620ee8af 124 waver.play(wave_file);
glanier9 15:e406620ee8af 125 fclose(wave_file);
glanier9 15:e406620ee8af 126 }
glanier9 15:e406620ee8af 127
glanier9 15:e406620ee8af 128 /* Setting 3: */
glanier9 15:e406620ee8af 129 if(setting == '3') {
glanier9 15:e406620ee8af 130 FILE *wave_file;
glanier9 15:e406620ee8af 131 wave_file=fopen("/sd/crickets_exp.wav","r");
glanier9 15:e406620ee8af 132 waver.play(wave_file);
glanier9 15:e406620ee8af 133 fclose(wave_file);
glanier9 15:e406620ee8af 134 }
glanier9 15:e406620ee8af 135
glanier9 15:e406620ee8af 136 /* Setting 4: */
glanier9 15:e406620ee8af 137 if(setting == '4') {
glanier9 15:e406620ee8af 138 FILE *wave_file;
glanier9 15:e406620ee8af 139 wave_file=fopen("/sd/static_exp.wav","r");
glanier9 15:e406620ee8af 140 waver.play(wave_file);
glanier9 15:e406620ee8af 141 fclose(wave_file);
glanier9 15:e406620ee8af 142 }
glanier9 15:e406620ee8af 143
glanier9 12:94b16c66c09a 144 }
glanier9 12:94b16c66c09a 145 }
glanier9 12:94b16c66c09a 146
glanier9 12:94b16c66c09a 147 void lcd1_thread()
glanier9 12:94b16c66c09a 148 {
glanier9 12:94b16c66c09a 149 while(1) {
glanier9 15:e406620ee8af 150
glanier9 15:e406620ee8af 151 /* Setting 1: Red */
glanier9 15:e406620ee8af 152 if(setting == '1') {
glanier9 15:e406620ee8af 153 // Top Red Box
glanier9 16:f31b20cfeebc 154 //stdio_mutex.lock();
glanier9 15:e406620ee8af 155 uLCD.filled_rectangle(0, 0, 127, 63, RED);
glanier9 16:f31b20cfeebc 156 semlcd2.release();
glanier9 16:f31b20cfeebc 157 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 158 Thread::wait(750);
glanier9 15:e406620ee8af 159 semled.release();
glanier9 15:e406620ee8af 160 semlcd2.release();
glanier9 15:e406620ee8af 161 // Top Black Box
glanier9 16:f31b20cfeebc 162 //stdio_mutex.lock();
glanier9 15:e406620ee8af 163 uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
glanier9 16:f31b20cfeebc 164 semlcd2.release();
glanier9 16:f31b20cfeebc 165 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 166 Thread::wait(750);
glanier9 15:e406620ee8af 167 sem_check.release();
glanier9 15:e406620ee8af 168 semlcd1.wait();
glanier9 15:e406620ee8af 169 semled.release();
glanier9 15:e406620ee8af 170 semlcd2.release();
glanier9 15:e406620ee8af 171 }
glanier9 15:e406620ee8af 172
glanier9 15:e406620ee8af 173 /* Setting 2: Purple */
glanier9 15:e406620ee8af 174 else if(setting == '2') {
glanier9 15:e406620ee8af 175 // Top Red Box
glanier9 16:f31b20cfeebc 176 //stdio_mutex.lock();
glanier9 15:e406620ee8af 177 uLCD.filled_rectangle(0, 0, 127, 63, 0xFF00FF);
glanier9 16:f31b20cfeebc 178 semlcd2.release();
glanier9 16:f31b20cfeebc 179 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 180 Thread::wait(750);
glanier9 15:e406620ee8af 181 semled.release();
glanier9 15:e406620ee8af 182 semlcd2.release();
glanier9 15:e406620ee8af 183 // Top Black Box
glanier9 16:f31b20cfeebc 184 //stdio_mutex.lock();
glanier9 15:e406620ee8af 185 uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
glanier9 16:f31b20cfeebc 186 semlcd2.release();
glanier9 16:f31b20cfeebc 187 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 188 Thread::wait(750);
glanier9 15:e406620ee8af 189 sem_check.release();
glanier9 15:e406620ee8af 190 semlcd1.wait();
glanier9 15:e406620ee8af 191 semled.release();
glanier9 15:e406620ee8af 192 semlcd2.release();
glanier9 15:e406620ee8af 193 }
glanier9 15:e406620ee8af 194
glanier9 15:e406620ee8af 195 /* Setting 3: Yellow */
glanier9 15:e406620ee8af 196 else if(setting == '3') {
glanier9 15:e406620ee8af 197 // Top Red Box
glanier9 16:f31b20cfeebc 198 //stdio_mutex.lock();
glanier9 15:e406620ee8af 199 uLCD.filled_rectangle(0, 0, 127, 63, 0xFFFF00);
glanier9 16:f31b20cfeebc 200 semlcd2.release();
glanier9 16:f31b20cfeebc 201 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 202 Thread::wait(750);
glanier9 15:e406620ee8af 203 semled.release();
glanier9 15:e406620ee8af 204 semlcd2.release();
glanier9 15:e406620ee8af 205 // Top Black Box
glanier9 16:f31b20cfeebc 206 //stdio_mutex.lock();
glanier9 15:e406620ee8af 207 uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
glanier9 16:f31b20cfeebc 208 semlcd2.release();
glanier9 16:f31b20cfeebc 209 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 210 Thread::wait(750);
glanier9 15:e406620ee8af 211 sem_check.release();
glanier9 15:e406620ee8af 212 semlcd1.wait();
glanier9 15:e406620ee8af 213 semled.release();
glanier9 15:e406620ee8af 214 semlcd2.release();
glanier9 15:e406620ee8af 215 }
glanier9 15:e406620ee8af 216
glanier9 15:e406620ee8af 217 /* Setting 4: Black and White */
glanier9 15:e406620ee8af 218 else if(setting == '4') {
glanier9 15:e406620ee8af 219 // Top Red Box
glanier9 16:f31b20cfeebc 220 //stdio_mutex.lock();
glanier9 15:e406620ee8af 221 uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
glanier9 16:f31b20cfeebc 222 semlcd2.release();
glanier9 16:f31b20cfeebc 223 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 224 Thread::wait(250);
glanier9 15:e406620ee8af 225 semled.release();
glanier9 15:e406620ee8af 226 semlcd2.release();
glanier9 15:e406620ee8af 227 // Top Black Box
glanier9 16:f31b20cfeebc 228 //stdio_mutex.lock();
glanier9 15:e406620ee8af 229 uLCD.filled_rectangle(0, 0, 127, 63, WHITE);
glanier9 16:f31b20cfeebc 230 semlcd2.release();
glanier9 16:f31b20cfeebc 231 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 232 Thread::wait(250);
glanier9 15:e406620ee8af 233 sem_check.release();
glanier9 15:e406620ee8af 234 semlcd1.wait();
glanier9 15:e406620ee8af 235 semled.release();
glanier9 15:e406620ee8af 236 semlcd2.release();
glanier9 15:e406620ee8af 237 }
glanier9 12:94b16c66c09a 238 }
glanier9 12:94b16c66c09a 239 }
glanier9 12:94b16c66c09a 240
glanier9 12:94b16c66c09a 241 void lcd2_thread()
glanier9 12:94b16c66c09a 242 {
glanier9 12:94b16c66c09a 243 while(1) {
glanier9 15:e406620ee8af 244
glanier9 15:e406620ee8af 245 /* Setting 1: Blue */
glanier9 15:e406620ee8af 246 if(setting == '1') {
glanier9 15:e406620ee8af 247 // Bottom Black Box
glanier9 16:f31b20cfeebc 248 semlcd2.wait();
glanier9 16:f31b20cfeebc 249 //stdio_mutex.lock();
glanier9 15:e406620ee8af 250 uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
glanier9 16:f31b20cfeebc 251 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 252 //Thread::wait(750);
glanier9 15:e406620ee8af 253 semlcd2.wait();
glanier9 15:e406620ee8af 254 // Bottom Blue Box
glanier9 16:f31b20cfeebc 255 //stdio_mutex.lock();
glanier9 16:f31b20cfeebc 256 semlcd2.wait();
glanier9 15:e406620ee8af 257 uLCD.filled_rectangle(0, 63, 127, 127, BLUE);
glanier9 16:f31b20cfeebc 258 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 259 //Thread::wait(750);
glanier9 15:e406620ee8af 260 semlcd2.wait();
glanier9 15:e406620ee8af 261 }
glanier9 15:e406620ee8af 262
glanier9 15:e406620ee8af 263 /* Setting 2: Green */
glanier9 15:e406620ee8af 264 else if(setting == '2') {
glanier9 15:e406620ee8af 265 // Bottom Black Box
glanier9 16:f31b20cfeebc 266 //stdio_mutex.lock();
glanier9 16:f31b20cfeebc 267 semlcd2.wait();
glanier9 15:e406620ee8af 268 uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
glanier9 16:f31b20cfeebc 269 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 270 //Thread::wait(750);
glanier9 15:e406620ee8af 271 semlcd2.wait();
glanier9 15:e406620ee8af 272 // Bottom Blue Box
glanier9 16:f31b20cfeebc 273 //stdio_mutex.lock();
glanier9 16:f31b20cfeebc 274 semlcd2.wait();
glanier9 15:e406620ee8af 275 uLCD.filled_rectangle(0, 63, 127, 127, GREEN);
glanier9 16:f31b20cfeebc 276 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 277 //Thread::wait(750);
glanier9 15:e406620ee8af 278 semlcd2.wait();
glanier9 15:e406620ee8af 279 }
glanier9 15:e406620ee8af 280
glanier9 15:e406620ee8af 281 /* Setting 3: Cyan */
glanier9 15:e406620ee8af 282 else if(setting == '3') {
glanier9 15:e406620ee8af 283 // Bottom Black Box
glanier9 16:f31b20cfeebc 284 //stdio_mutex.lock();
glanier9 16:f31b20cfeebc 285 semlcd2.wait();
glanier9 15:e406620ee8af 286 uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
glanier9 16:f31b20cfeebc 287 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 288 //Thread::wait(750);
glanier9 15:e406620ee8af 289 semlcd2.wait();
glanier9 15:e406620ee8af 290 // Bottom Blue Box
glanier9 16:f31b20cfeebc 291 //stdio_mutex.lock();
glanier9 16:f31b20cfeebc 292 semlcd2.wait();
glanier9 15:e406620ee8af 293 uLCD.filled_rectangle(0, 63, 127, 127, 0x00FFFF);
glanier9 16:f31b20cfeebc 294 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 295 //Thread::wait(750);
glanier9 15:e406620ee8af 296 semlcd2.wait();
glanier9 15:e406620ee8af 297 }
glanier9 15:e406620ee8af 298
glanier9 15:e406620ee8af 299 /* Setting 4: Black and White */
glanier9 15:e406620ee8af 300 else if(setting == '4') {
glanier9 15:e406620ee8af 301 // Bottom Black Box
glanier9 16:f31b20cfeebc 302 //stdio_mutex.lock();
glanier9 16:f31b20cfeebc 303 semlcd2.wait();
glanier9 15:e406620ee8af 304 uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
glanier9 16:f31b20cfeebc 305 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 306 //Thread::wait(750);
glanier9 15:e406620ee8af 307 semlcd2.wait();
glanier9 15:e406620ee8af 308 // Bottom Blue Box
glanier9 16:f31b20cfeebc 309 //stdio_mutex.lock();
glanier9 16:f31b20cfeebc 310 semlcd2.wait();
glanier9 15:e406620ee8af 311 uLCD.filled_rectangle(0, 63, 127, 127, WHITE);
glanier9 16:f31b20cfeebc 312 //stdio_mutex.unlock();
glanier9 15:e406620ee8af 313 //Thread::wait(750);
glanier9 15:e406620ee8af 314 semlcd2.wait();
glanier9 15:e406620ee8af 315 }
glanier9 12:94b16c66c09a 316 }
glanier9 12:94b16c66c09a 317 }
glanier9 12:94b16c66c09a 318
glanier9 15:e406620ee8af 319
glanier9 12:94b16c66c09a 320 int main()
glanier9 12:94b16c66c09a 321 {
glanier9 12:94b16c66c09a 322 rLED = 0;
glanier9 12:94b16c66c09a 323 gLED = 0;
glanier9 12:94b16c66c09a 324 bLED = 0;
glanier9 13:8987c75dfe41 325
glanier9 14:ae63fb7c2d06 326 /* Wait for user to press a setting button */
glanier9 12:94b16c66c09a 327 char bnum=0;
glanier9 12:94b16c66c09a 328 while(1) {
glanier9 12:94b16c66c09a 329 if (blue.readable() && blue.getc()=='!') {
glanier9 12:94b16c66c09a 330 if (blue.getc()=='B') { //button data
glanier9 12:94b16c66c09a 331 bnum = blue.getc(); //button number
glanier9 13:8987c75dfe41 332 if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4
glanier9 13:8987c75dfe41 333 setting = bnum;// Change setting based on button input to set start setting. Check for chars
glanier9 12:94b16c66c09a 334 break; //turn on/off that num LED
glanier9 13:8987c75dfe41 335 }
glanier9 12:94b16c66c09a 336 }
glanier9 12:94b16c66c09a 337 }
glanier9 14:ae63fb7c2d06 338 }
glanier9 13:8987c75dfe41 339
glanier9 14:ae63fb7c2d06 340 /* Start all threads */
glanier9 12:94b16c66c09a 341 thread1.start(led_thread);
glanier9 12:94b16c66c09a 342 thread2.start(speaker_thread);
glanier9 12:94b16c66c09a 343 thread3.start(lcd1_thread);
glanier9 12:94b16c66c09a 344 thread4.start(lcd2_thread);
glanier9 13:8987c75dfe41 345
glanier9 14:ae63fb7c2d06 346 /* Infinite Loop, let threads run */
glanier9 15:e406620ee8af 347 while (1) {
glanier9 15:e406620ee8af 348
glanier9 15:e406620ee8af 349 sem_check.wait();
glanier9 15:e406620ee8af 350
glanier9 15:e406620ee8af 351 if (blue.readable() && blue.getc()=='!') {
glanier9 15:e406620ee8af 352 if (blue.getc()=='B') { //button data
glanier9 15:e406620ee8af 353 char bnum = blue.getc(); //button number
glanier9 15:e406620ee8af 354 if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4
glanier9 15:e406620ee8af 355 setting = bnum;// Change setting based on button input to set start setting. Check for chars
glanier9 15:e406620ee8af 356 }
glanier9 15:e406620ee8af 357 }
glanier9 15:e406620ee8af 358 }
glanier9 15:e406620ee8af 359
glanier9 15:e406620ee8af 360 semlcd1.release();
emilmont 1:491820ee784d 361 }
emilmont 1:491820ee784d 362 }