Lab 3 Part 4

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

Committer:
glanier9
Date:
Thu Feb 25 19:18:25 2021 +0000
Revision:
13:8987c75dfe41
Parent:
12:94b16c66c09a
Child:
14:ae63fb7c2d06
Trying interrupts

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 InterruptIn data(p14);
glanier9 13:8987c75dfe41 9
glanier9 13:8987c75dfe41 10 char setting = '1';
glanier9 12:94b16c66c09a 11
glanier9 12:94b16c66c09a 12 BusOut myled(LED1,LED2,LED3,LED4);
glanier9 12:94b16c66c09a 13
glanier9 12:94b16c66c09a 14 Serial blue(p13,p14);
glanier9 12:94b16c66c09a 15
glanier9 12:94b16c66c09a 16 AnalogOut DACout(p18);
glanier9 12:94b16c66c09a 17
glanier9 12:94b16c66c09a 18 wave_player waver(&DACout);
glanier9 12:94b16c66c09a 19
glanier9 12:94b16c66c09a 20 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
glanier9 12:94b16c66c09a 21
glanier9 12:94b16c66c09a 22 Mutex stdio_mutex;
glanier9 12:94b16c66c09a 23
glanier9 12:94b16c66c09a 24 uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
glanier9 12:94b16c66c09a 25
glanier9 12:94b16c66c09a 26 PwmOut rLED(p21); // Light up Red LED with p21
glanier9 12:94b16c66c09a 27 PwmOut gLED(p22); // Light up Green LED with p22
glanier9 12:94b16c66c09a 28 PwmOut bLED(p23); // Light up Blue LED with p23
glanier9 12:94b16c66c09a 29
glanier9 12:94b16c66c09a 30 Thread thread1;
glanier9 12:94b16c66c09a 31 Thread thread2;
glanier9 12:94b16c66c09a 32 Thread thread3;
glanier9 12:94b16c66c09a 33 Thread thread4;
glanier9 12:94b16c66c09a 34
glanier9 12:94b16c66c09a 35 /* Function Calls */
glanier9 12:94b16c66c09a 36 void top_red_box()
glanier9 12:94b16c66c09a 37 {
glanier9 12:94b16c66c09a 38 stdio_mutex.lock();
glanier9 12:94b16c66c09a 39 uLCD.filled_rectangle(0, 0, 127, 63, RED);
glanier9 12:94b16c66c09a 40 stdio_mutex.unlock();
glanier9 12:94b16c66c09a 41 }
glanier9 12:94b16c66c09a 42 void bot_blue_box()
glanier9 12:94b16c66c09a 43 {
glanier9 12:94b16c66c09a 44 stdio_mutex.lock();
glanier9 12:94b16c66c09a 45 uLCD.filled_rectangle(0, 63, 127, 127, BLUE);
glanier9 12:94b16c66c09a 46 stdio_mutex.unlock();
glanier9 12:94b16c66c09a 47 }
glanier9 12:94b16c66c09a 48 void top_black_box()
glanier9 12:94b16c66c09a 49 {
glanier9 12:94b16c66c09a 50 stdio_mutex.lock();
glanier9 12:94b16c66c09a 51 uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
glanier9 12:94b16c66c09a 52 stdio_mutex.unlock();
glanier9 12:94b16c66c09a 53
glanier9 12:94b16c66c09a 54 }
glanier9 12:94b16c66c09a 55 void bot_black_box()
glanier9 12:94b16c66c09a 56 {
glanier9 12:94b16c66c09a 57 stdio_mutex.lock();
glanier9 12:94b16c66c09a 58 uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
glanier9 12:94b16c66c09a 59 stdio_mutex.unlock();
glanier9 12:94b16c66c09a 60 }
glanier9 12:94b16c66c09a 61
glanier9 13:8987c75dfe41 62 /* Interrupt */
glanier9 13:8987c75dfe41 63 void set()
glanier9 13:8987c75dfe41 64 {
glanier9 13:8987c75dfe41 65 char bnum;
glanier9 13:8987c75dfe41 66 if (blue.readable() && blue.getc()=='!') {
glanier9 13:8987c75dfe41 67 if (blue.getc()=='B') { //button data
glanier9 13:8987c75dfe41 68 bnum = blue.getc(); //button number
glanier9 13:8987c75dfe41 69 if ((bnum>='1')&&(bnum<='4')) //is a number button 1..4
glanier9 13:8987c75dfe41 70 setting = bnum;// Set new setting
glanier9 13:8987c75dfe41 71 }
glanier9 13:8987c75dfe41 72 }
glanier9 13:8987c75dfe41 73 }
glanier9 12:94b16c66c09a 74
glanier9 12:94b16c66c09a 75 /* Threads */
glanier9 12:94b16c66c09a 76 void led_thread()
glanier9 12:94b16c66c09a 77 {
glanier9 13:8987c75dfe41 78 //char bnum = setting;
glanier9 12:94b16c66c09a 79 while(1) {
glanier9 13:8987c75dfe41 80 if(setting == '1') {
glanier9 13:8987c75dfe41 81 bLED = 0;
glanier9 13:8987c75dfe41 82 rLED = 1;
glanier9 13:8987c75dfe41 83 gLED = 0;
glanier9 13:8987c75dfe41 84 Thread::wait(750);
glanier9 13:8987c75dfe41 85 rLED = 0;
glanier9 13:8987c75dfe41 86 bLED = 1;
glanier9 13:8987c75dfe41 87 gLED = 0;
glanier9 13:8987c75dfe41 88 Thread::wait(750);
glanier9 13:8987c75dfe41 89 }
glanier9 13:8987c75dfe41 90 if (setting == '2') {
glanier9 13:8987c75dfe41 91 bLED = 1;
glanier9 13:8987c75dfe41 92 rLED = 1;
glanier9 13:8987c75dfe41 93 gLED = 0;
glanier9 13:8987c75dfe41 94 Thread::wait(750);
glanier9 13:8987c75dfe41 95 rLED = 0;
glanier9 13:8987c75dfe41 96 bLED = 0;
glanier9 13:8987c75dfe41 97 gLED = 1;
glanier9 13:8987c75dfe41 98 Thread::wait(750);
glanier9 13:8987c75dfe41 99 }
emilmont 1:491820ee784d 100 }
emilmont 1:491820ee784d 101 }
glanier9 12:94b16c66c09a 102
glanier9 12:94b16c66c09a 103 void speaker_thread()
glanier9 12:94b16c66c09a 104 {
glanier9 12:94b16c66c09a 105 while(1) {
glanier9 12:94b16c66c09a 106 FILE *wave_file;
glanier9 12:94b16c66c09a 107 printf("\n\n\nHello, wave world!\n");
glanier9 12:94b16c66c09a 108 wave_file=fopen("/sd/WEEWOO.wav","r");
glanier9 12:94b16c66c09a 109 waver.play(wave_file);
glanier9 12:94b16c66c09a 110 fclose(wave_file);
glanier9 12:94b16c66c09a 111 }
glanier9 12:94b16c66c09a 112 }
glanier9 12:94b16c66c09a 113
glanier9 12:94b16c66c09a 114 void lcd1_thread()
glanier9 12:94b16c66c09a 115 {
glanier9 12:94b16c66c09a 116 while(1) {
glanier9 12:94b16c66c09a 117 top_red_box();
glanier9 12:94b16c66c09a 118 Thread::wait(750);
glanier9 12:94b16c66c09a 119 top_black_box();
glanier9 12:94b16c66c09a 120 Thread::wait(750);
glanier9 12:94b16c66c09a 121 }
glanier9 12:94b16c66c09a 122 }
glanier9 12:94b16c66c09a 123
glanier9 12:94b16c66c09a 124 void lcd2_thread()
glanier9 12:94b16c66c09a 125 {
glanier9 12:94b16c66c09a 126 while(1) {
glanier9 12:94b16c66c09a 127 bot_black_box();
glanier9 12:94b16c66c09a 128 Thread::wait(750);
glanier9 12:94b16c66c09a 129 bot_blue_box();
glanier9 12:94b16c66c09a 130 Thread::wait(750);
glanier9 12:94b16c66c09a 131 }
glanier9 12:94b16c66c09a 132 }
glanier9 12:94b16c66c09a 133
glanier9 12:94b16c66c09a 134 int main()
glanier9 12:94b16c66c09a 135 {
glanier9 12:94b16c66c09a 136 rLED = 0;
glanier9 12:94b16c66c09a 137 gLED = 0;
glanier9 12:94b16c66c09a 138 bLED = 0;
glanier9 13:8987c75dfe41 139
glanier9 12:94b16c66c09a 140 char bnum=0;
glanier9 12:94b16c66c09a 141 while(1) {
glanier9 12:94b16c66c09a 142 if (blue.readable() && blue.getc()=='!') {
glanier9 12:94b16c66c09a 143 if (blue.getc()=='B') { //button data
glanier9 12:94b16c66c09a 144 bnum = blue.getc(); //button number
glanier9 13:8987c75dfe41 145 if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4
glanier9 13:8987c75dfe41 146 setting = bnum;// Change setting based on button input to set start setting. Check for chars
glanier9 12:94b16c66c09a 147 break; //turn on/off that num LED
glanier9 13:8987c75dfe41 148 }
glanier9 12:94b16c66c09a 149 }
glanier9 12:94b16c66c09a 150 }
glanier9 13:8987c75dfe41 151
glanier9 12:94b16c66c09a 152 }
glanier9 13:8987c75dfe41 153
glanier9 13:8987c75dfe41 154
glanier9 13:8987c75dfe41 155 //data.rise(&set);
glanier9 12:94b16c66c09a 156
glanier9 12:94b16c66c09a 157 thread1.start(led_thread);
glanier9 12:94b16c66c09a 158 thread2.start(speaker_thread);
glanier9 12:94b16c66c09a 159 thread3.start(lcd1_thread);
glanier9 12:94b16c66c09a 160 thread4.start(lcd2_thread);
glanier9 13:8987c75dfe41 161
emilmont 1:491820ee784d 162 while (true) {
emilmont 1:491820ee784d 163 }
emilmont 1:491820ee784d 164 }