![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Lab 3 Part 4
Dependencies: mbed wave_player mbed-rtos 4DGL-uLCD-SE SDFileSystem
main.cpp@12:94b16c66c09a, 2021-02-25 (annotated)
- Committer:
- glanier9
- Date:
- Thu Feb 25 18:30:28 2021 +0000
- Revision:
- 12:94b16c66c09a
- Parent:
- 11:0309bef74ba8
- Child:
- 13:8987c75dfe41
Version 1. Logic for Bluetooth not working.
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 | 12:94b16c66c09a | 8 | int 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 | 12:94b16c66c09a | 21 | |
glanier9 | 12:94b16c66c09a | 22 | uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; |
glanier9 | 12:94b16c66c09a | 23 | |
glanier9 | 12:94b16c66c09a | 24 | PwmOut rLED(p21); // Light up Red LED with p21 |
glanier9 | 12:94b16c66c09a | 25 | PwmOut gLED(p22); // Light up Green LED with p22 |
glanier9 | 12:94b16c66c09a | 26 | PwmOut bLED(p23); // Light up Blue LED with p23 |
glanier9 | 12:94b16c66c09a | 27 | |
glanier9 | 12:94b16c66c09a | 28 | Thread thread1; |
glanier9 | 12:94b16c66c09a | 29 | Thread thread2; |
glanier9 | 12:94b16c66c09a | 30 | Thread thread3; |
glanier9 | 12:94b16c66c09a | 31 | Thread thread4; |
glanier9 | 12:94b16c66c09a | 32 | |
glanier9 | 12:94b16c66c09a | 33 | /* Function Calls */ |
glanier9 | 12:94b16c66c09a | 34 | void top_red_box() |
glanier9 | 12:94b16c66c09a | 35 | { |
glanier9 | 12:94b16c66c09a | 36 | stdio_mutex.lock(); |
glanier9 | 12:94b16c66c09a | 37 | uLCD.filled_rectangle(0, 0, 127, 63, RED); |
glanier9 | 12:94b16c66c09a | 38 | stdio_mutex.unlock(); |
glanier9 | 12:94b16c66c09a | 39 | } |
glanier9 | 12:94b16c66c09a | 40 | void bot_blue_box() |
glanier9 | 12:94b16c66c09a | 41 | { |
glanier9 | 12:94b16c66c09a | 42 | stdio_mutex.lock(); |
glanier9 | 12:94b16c66c09a | 43 | uLCD.filled_rectangle(0, 63, 127, 127, BLUE); |
glanier9 | 12:94b16c66c09a | 44 | stdio_mutex.unlock(); |
glanier9 | 12:94b16c66c09a | 45 | } |
glanier9 | 12:94b16c66c09a | 46 | void top_black_box() |
glanier9 | 12:94b16c66c09a | 47 | { |
glanier9 | 12:94b16c66c09a | 48 | stdio_mutex.lock(); |
glanier9 | 12:94b16c66c09a | 49 | uLCD.filled_rectangle(0, 0, 127, 63, BLACK); |
glanier9 | 12:94b16c66c09a | 50 | stdio_mutex.unlock(); |
glanier9 | 12:94b16c66c09a | 51 | |
glanier9 | 12:94b16c66c09a | 52 | } |
glanier9 | 12:94b16c66c09a | 53 | void bot_black_box() |
glanier9 | 12:94b16c66c09a | 54 | { |
glanier9 | 12:94b16c66c09a | 55 | stdio_mutex.lock(); |
glanier9 | 12:94b16c66c09a | 56 | uLCD.filled_rectangle(0, 63, 127, 127, BLACK); |
glanier9 | 12:94b16c66c09a | 57 | stdio_mutex.unlock(); |
glanier9 | 12:94b16c66c09a | 58 | } |
glanier9 | 12:94b16c66c09a | 59 | |
glanier9 | 12:94b16c66c09a | 60 | |
glanier9 | 12:94b16c66c09a | 61 | /* Threads */ |
glanier9 | 12:94b16c66c09a | 62 | void led_thread() |
glanier9 | 12:94b16c66c09a | 63 | { |
glanier9 | 12:94b16c66c09a | 64 | while(1) { |
glanier9 | 12:94b16c66c09a | 65 | bLED = 0; |
glanier9 | 12:94b16c66c09a | 66 | rLED = 1; |
glanier9 | 12:94b16c66c09a | 67 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 68 | rLED = 0; |
glanier9 | 12:94b16c66c09a | 69 | bLED = 1; |
glanier9 | 12:94b16c66c09a | 70 | Thread::wait(750); |
emilmont | 1:491820ee784d | 71 | } |
emilmont | 1:491820ee784d | 72 | } |
glanier9 | 12:94b16c66c09a | 73 | |
glanier9 | 12:94b16c66c09a | 74 | void speaker_thread() |
glanier9 | 12:94b16c66c09a | 75 | { |
glanier9 | 12:94b16c66c09a | 76 | while(1) { |
glanier9 | 12:94b16c66c09a | 77 | FILE *wave_file; |
glanier9 | 12:94b16c66c09a | 78 | printf("\n\n\nHello, wave world!\n"); |
glanier9 | 12:94b16c66c09a | 79 | wave_file=fopen("/sd/WEEWOO.wav","r"); |
glanier9 | 12:94b16c66c09a | 80 | waver.play(wave_file); |
glanier9 | 12:94b16c66c09a | 81 | fclose(wave_file); |
glanier9 | 12:94b16c66c09a | 82 | } |
glanier9 | 12:94b16c66c09a | 83 | } |
glanier9 | 12:94b16c66c09a | 84 | |
glanier9 | 12:94b16c66c09a | 85 | void lcd1_thread() |
glanier9 | 12:94b16c66c09a | 86 | { |
glanier9 | 12:94b16c66c09a | 87 | while(1) { |
glanier9 | 12:94b16c66c09a | 88 | top_red_box(); |
glanier9 | 12:94b16c66c09a | 89 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 90 | top_black_box(); |
glanier9 | 12:94b16c66c09a | 91 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 92 | } |
glanier9 | 12:94b16c66c09a | 93 | } |
glanier9 | 12:94b16c66c09a | 94 | |
glanier9 | 12:94b16c66c09a | 95 | void lcd2_thread() |
glanier9 | 12:94b16c66c09a | 96 | { |
glanier9 | 12:94b16c66c09a | 97 | while(1) { |
glanier9 | 12:94b16c66c09a | 98 | bot_black_box(); |
glanier9 | 12:94b16c66c09a | 99 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 100 | bot_blue_box(); |
glanier9 | 12:94b16c66c09a | 101 | Thread::wait(750); |
glanier9 | 12:94b16c66c09a | 102 | } |
glanier9 | 12:94b16c66c09a | 103 | } |
glanier9 | 12:94b16c66c09a | 104 | |
glanier9 | 12:94b16c66c09a | 105 | int main() |
glanier9 | 12:94b16c66c09a | 106 | { |
glanier9 | 12:94b16c66c09a | 107 | rLED = 0; |
glanier9 | 12:94b16c66c09a | 108 | gLED = 0; |
glanier9 | 12:94b16c66c09a | 109 | bLED = 0; |
glanier9 | 12:94b16c66c09a | 110 | char bnum=0; |
glanier9 | 12:94b16c66c09a | 111 | while(1) { |
glanier9 | 12:94b16c66c09a | 112 | if (blue.readable() && blue.getc()=='!') { |
glanier9 | 12:94b16c66c09a | 113 | if (blue.getc()=='B') { //button data |
glanier9 | 12:94b16c66c09a | 114 | bnum = blue.getc(); //button number |
glanier9 | 12:94b16c66c09a | 115 | if (bnum=='1') //is a number button 1..4 |
glanier9 | 12:94b16c66c09a | 116 | setting = 1; |
glanier9 | 12:94b16c66c09a | 117 | break; //turn on/off that num LED |
glanier9 | 12:94b16c66c09a | 118 | } |
glanier9 | 12:94b16c66c09a | 119 | } |
glanier9 | 12:94b16c66c09a | 120 | } |
glanier9 | 12:94b16c66c09a | 121 | |
glanier9 | 12:94b16c66c09a | 122 | thread1.start(led_thread); |
glanier9 | 12:94b16c66c09a | 123 | thread2.start(speaker_thread); |
glanier9 | 12:94b16c66c09a | 124 | thread3.start(lcd1_thread); |
glanier9 | 12:94b16c66c09a | 125 | thread4.start(lcd2_thread); |
emilmont | 1:491820ee784d | 126 | |
emilmont | 1:491820ee784d | 127 | while (true) { |
glanier9 | 12:94b16c66c09a | 128 | Thread::wait(500) |
glanier9 | 12:94b16c66c09a | 129 | if (blue.readable() && blue.getc()=='!') { |
glanier9 | 12:94b16c66c09a | 130 | if (blue.getc()=='B') { //button data |
glanier9 | 12:94b16c66c09a | 131 | bnum = blue.getc(); //button number |
glanier9 | 12:94b16c66c09a | 132 | if (bnum=='1') //is a number button 1..4 |
glanier9 | 12:94b16c66c09a | 133 | setting = 1; |
glanier9 | 12:94b16c66c09a | 134 | break; //turn on/off that num LED |
glanier9 | 12:94b16c66c09a | 135 | } |
glanier9 | 12:94b16c66c09a | 136 | } |
emilmont | 1:491820ee784d | 137 | } |
emilmont | 1:491820ee784d | 138 | } |