music mixer code
Dependencies: mbed wave_player mbed-rtos SimpleRGB 4DGL-uLCD-SE PinDetect
main.cpp@7:f3eb2b2c295d, 2016-10-11 (annotated)
- Committer:
- fkhan39
- Date:
- Tue Oct 11 02:47:49 2016 +0000
- Revision:
- 7:f3eb2b2c295d
- Parent:
- 6:648cd6a6fae2
- Child:
- 8:38419fb4e242
extra credit video!;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kswanson31 | 0:51d88b4e2b23 | 1 | #include "mbed.h" |
kswanson31 | 0:51d88b4e2b23 | 2 | #include "rtos.h" |
kswanson31 | 5:420cda7cbfc5 | 3 | #include <string> |
fkhan39 | 4:4792f0db029a | 4 | #include "uLCD_4DGL.h" |
kswanson31 | 1:ff69d2d97a2c | 5 | #include "SimpleRGB.h" |
kswanson31 | 1:ff69d2d97a2c | 6 | |
kswanson31 | 1:ff69d2d97a2c | 7 | #define SECOND 1000 // used with threads |
kswanson31 | 3:641770225c44 | 8 | DigitalOut led(LED1); // test led |
kswanson31 | 0:51d88b4e2b23 | 9 | |
kswanson31 | 5:420cda7cbfc5 | 10 | uLCD_4DGL lcd(p9,p10,p11); |
fkhan39 | 6:648cd6a6fae2 | 11 | Mutex stdio_mutex; |
kswanson31 | 3:641770225c44 | 12 | /* 1 init/declare |
kswanson31 | 1:ff69d2d97a2c | 13 | |
kswanson31 | 0:51d88b4e2b23 | 14 | Ticker first; |
kswanson31 | 0:51d88b4e2b23 | 15 | Ticker second; |
kswanson31 | 0:51d88b4e2b23 | 16 | Ticker third; |
kswanson31 | 0:51d88b4e2b23 | 17 | Ticker fourth; |
kswanson31 | 0:51d88b4e2b23 | 18 | DigitalOut led1(LED1); |
kswanson31 | 0:51d88b4e2b23 | 19 | DigitalOut led2(LED2); |
kswanson31 | 0:51d88b4e2b23 | 20 | DigitalOut led3(LED3); |
kswanson31 | 0:51d88b4e2b23 | 21 | DigitalOut led4(LED4); |
kswanson31 | 0:51d88b4e2b23 | 22 | |
kswanson31 | 0:51d88b4e2b23 | 23 | void one() { // attached to ticker object. will be called after time given |
kswanson31 | 0:51d88b4e2b23 | 24 | led1 = !led1; // flip the value of the led |
kswanson31 | 0:51d88b4e2b23 | 25 | } // nothing returned |
kswanson31 | 0:51d88b4e2b23 | 26 | |
kswanson31 | 0:51d88b4e2b23 | 27 | void two() { |
kswanson31 | 0:51d88b4e2b23 | 28 | led2 = !led2; |
kswanson31 | 0:51d88b4e2b23 | 29 | } |
kswanson31 | 0:51d88b4e2b23 | 30 | |
kswanson31 | 0:51d88b4e2b23 | 31 | void three() { |
kswanson31 | 0:51d88b4e2b23 | 32 | led3 = !led3; |
kswanson31 | 0:51d88b4e2b23 | 33 | } |
kswanson31 | 0:51d88b4e2b23 | 34 | |
fkhan39 | 2:c64bfb83fe76 | 35 | void four() { |
kswanson31 | 0:51d88b4e2b23 | 36 | led4 = !led4; |
kswanson31 | 0:51d88b4e2b23 | 37 | } |
kswanson31 | 1:ff69d2d97a2c | 38 | |
kswanson31 | 3:641770225c44 | 39 | */// - 1 |
kswanson31 | 0:51d88b4e2b23 | 40 | |
kswanson31 | 1:ff69d2d97a2c | 41 | |
kswanson31 | 3:641770225c44 | 42 | /* 2 init/declare */ |
kswanson31 | 1:ff69d2d97a2c | 43 | |
kswanson31 | 1:ff69d2d97a2c | 44 | // lcd thread |
kswanson31 | 0:51d88b4e2b23 | 45 | void uLCD_reboot_thread(void const *args) { |
kswanson31 | 0:51d88b4e2b23 | 46 | // show reboot process, associated with clip of apple restart |
kswanson31 | 1:ff69d2d97a2c | 47 | while (true) { |
kswanson31 | 0:51d88b4e2b23 | 48 | // lock, print 'Rebooting!' to uLCD with "progress_bar" underneath |
fkhan39 | 6:648cd6a6fae2 | 49 | stdio_mutex.lock(); |
fkhan39 | 4:4792f0db029a | 50 | lcd.text_string("Rebooting", 2, 5, FONT_7X8, WHITE); |
kswanson31 | 0:51d88b4e2b23 | 51 | // draw progress bar 8 sec |
fkhan39 | 4:4792f0db029a | 52 | for (int i = 0; i < 8; i++) { |
kswanson31 | 5:420cda7cbfc5 | 53 | lcd.text_string("|", 2 + i, 6, FONT_7X8, WHITE); |
kswanson31 | 5:420cda7cbfc5 | 54 | // progress[i+1] += arr[i]; |
kswanson31 | 5:420cda7cbfc5 | 55 | Thread::wait(SECOND); |
fkhan39 | 4:4792f0db029a | 56 | } |
kswanson31 | 0:51d88b4e2b23 | 57 | // draw an ascii 'apple' |
kswanson31 | 5:420cda7cbfc5 | 58 | lcd.text_string(" .:' ", 2, 4, FONT_7X8, WHITE); |
kswanson31 | 5:420cda7cbfc5 | 59 | lcd.text_string(" :' ", 2, 5, FONT_7X8, WHITE); |
kswanson31 | 5:420cda7cbfc5 | 60 | lcd.text_string(" .'` `-' ``. ", 2, 6, FONT_7X8, WHITE); |
kswanson31 | 5:420cda7cbfc5 | 61 | lcd.text_string(": .-' ", 2, 7, FONT_7X8, WHITE); |
kswanson31 | 5:420cda7cbfc5 | 62 | lcd.text_string(": : ", 2, 8, FONT_7X8, WHITE); |
kswanson31 | 5:420cda7cbfc5 | 63 | lcd.text_string(" : `-; ", 2, 9, FONT_7X8, WHITE); |
kswanson31 | 5:420cda7cbfc5 | 64 | lcd.text_string(" `.__.-.__.' ", 2, 10, FONT_7X8, WHITE); |
kswanson31 | 0:51d88b4e2b23 | 65 | // unlock, wait for 52 |
fkhan39 | 6:648cd6a6fae2 | 66 | stdio_mutex.unlock(); |
kswanson31 | 5:420cda7cbfc5 | 67 | Thread::wait(52 * SECOND); |
kswanson31 | 5:420cda7cbfc5 | 68 | // clear screen |
kswanson31 | 5:420cda7cbfc5 | 69 | lcd.cls(); |
kswanson31 | 0:51d88b4e2b23 | 70 | } |
kswanson31 | 0:51d88b4e2b23 | 71 | } |
kswanson31 | 0:51d88b4e2b23 | 72 | |
kswanson31 | 1:ff69d2d97a2c | 73 | // lcd thread |
kswanson31 | 0:51d88b4e2b23 | 74 | void uLCD_status_thread(void const *args) { |
kswanson31 | 0:51d88b4e2b23 | 75 | // show time with smile, frown, sad face at top right |
fkhan39 | 4:4792f0db029a | 76 | set_time(1476146921); |
kswanson31 | 1:ff69d2d97a2c | 77 | while (true) { |
kswanson31 | 5:420cda7cbfc5 | 78 | char buffer[32]; |
fkhan39 | 4:4792f0db029a | 79 | time_t seconds = time(NULL); |
fkhan39 | 4:4792f0db029a | 80 | strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
fkhan39 | 6:648cd6a6fae2 | 81 | // lock, update time at top right with smiley at top left |
fkhan39 | 6:648cd6a6fae2 | 82 | stdio_mutex.lock(); |
fkhan39 | 4:4792f0db029a | 83 | lcd.text_string(buffer, 5, 1, FONT_7X8, RED); |
fkhan39 | 4:4792f0db029a | 84 | lcd.text_string(":-)", 1, 1, FONT_7X8, WHITE); |
kswanson31 | 0:51d88b4e2b23 | 85 | // unlock, wait for 20 sec |
fkhan39 | 6:648cd6a6fae2 | 86 | stdio_mutex.unlock(); |
kswanson31 | 5:420cda7cbfc5 | 87 | Thread::wait(20 * SECOND); |
fkhan39 | 4:4792f0db029a | 88 | // lock, change to frown |
fkhan39 | 6:648cd6a6fae2 | 89 | stdio_mutex.lock(); |
fkhan39 | 4:4792f0db029a | 90 | lcd.text_string(">:|", 1, 1, FONT_7X8, WHITE); |
fkhan39 | 4:4792f0db029a | 91 | // unlock, wait for 20 sec |
fkhan39 | 6:648cd6a6fae2 | 92 | stdio_mutex.unlock(); |
kswanson31 | 5:420cda7cbfc5 | 93 | Thread::wait(20 * SECOND); |
kswanson31 | 0:51d88b4e2b23 | 94 | // lock, change to sad |
fkhan39 | 6:648cd6a6fae2 | 95 | stdio_mutex.lock(); |
fkhan39 | 4:4792f0db029a | 96 | lcd.text_string(":-(", 1, 1, FONT_7X8, WHITE); |
kswanson31 | 0:51d88b4e2b23 | 97 | // unlock, wait for 20 sec |
fkhan39 | 6:648cd6a6fae2 | 98 | stdio_mutex.unlock(); |
kswanson31 | 5:420cda7cbfc5 | 99 | Thread::wait(20 * SECOND); |
fkhan39 | 6:648cd6a6fae2 | 100 | lcd.cls(); |
kswanson31 | 0:51d88b4e2b23 | 101 | } |
kswanson31 | 0:51d88b4e2b23 | 102 | } |
kswanson31 | 0:51d88b4e2b23 | 103 | |
fkhan39 | 7:f3eb2b2c295d | 104 | void uLCD_video_thread(void const *args) { |
fkhan39 | 7:f3eb2b2c295d | 105 | uLCD.media_init(); |
fkhan39 | 7:f3eb2b2c295d | 106 | uLCD.set_sector_address(0x001D, 0x4C42); |
fkhan39 | 7:f3eb2b2c295d | 107 | uLCD.display_video(0,0); |
fkhan39 | 7:f3eb2b2c295d | 108 | } |
fkhan39 | 7:f3eb2b2c295d | 109 | |
kswanson31 | 1:ff69d2d97a2c | 110 | RGBLed rgbLED(p21, p22, p23); // object to use the RGB LED |
kswanson31 | 1:ff69d2d97a2c | 111 | const LightColor green(0.0, 0.8, 0.0); // adjust brightness for green |
kswanson31 | 1:ff69d2d97a2c | 112 | const LightColor yellow(1.0, 0.8, 0.0); |
kswanson31 | 1:ff69d2d97a2c | 113 | const LightColor orange(1.0, 0.4, 0.0); |
kswanson31 | 1:ff69d2d97a2c | 114 | const LightColor red(1.0,0.0,0.0); |
kswanson31 | 1:ff69d2d97a2c | 115 | const LightColor violet(1.0, 0.0, 0.8); |
kswanson31 | 1:ff69d2d97a2c | 116 | const LightColor blue(0.0, 0.0, 1.0); |
kswanson31 | 1:ff69d2d97a2c | 117 | |
kswanson31 | 1:ff69d2d97a2c | 118 | // rgb led thread |
kswanson31 | 0:51d88b4e2b23 | 119 | void rainbow_led_thread(void const *args) { |
kswanson31 | 1:ff69d2d97a2c | 120 | // reboot with white (6 sec), then cycle through apple colors (54 sec) |
kswanson31 | 1:ff69d2d97a2c | 121 | while (true) { |
kswanson31 | 1:ff69d2d97a2c | 122 | rgbLED.write(1.0, 1.0, 1.0); // bright white |
kswanson31 | 1:ff69d2d97a2c | 123 | Thread::wait(2 * SECOND); |
kswanson31 | 1:ff69d2d97a2c | 124 | // dim to nothing over 4 seconds |
kswanson31 | 1:ff69d2d97a2c | 125 | for (float c = 1.0; c > 0.0; c = c - 0.01) { |
kswanson31 | 1:ff69d2d97a2c | 126 | rgbLED.write(c, c, c); |
kswanson31 | 1:ff69d2d97a2c | 127 | Thread::wait(40); // 40 * 100 = 4000 = 4 secs |
kswanson31 | 1:ff69d2d97a2c | 128 | } |
kswanson31 | 1:ff69d2d97a2c | 129 | // rotate through other colors over 54 seconds |
kswanson31 | 1:ff69d2d97a2c | 130 | rgbLED = green; |
kswanson31 | 1:ff69d2d97a2c | 131 | Thread::wait(9 * SECOND); |
kswanson31 | 1:ff69d2d97a2c | 132 | rgbLED = yellow; |
kswanson31 | 1:ff69d2d97a2c | 133 | Thread::wait(9 * SECOND); |
kswanson31 | 1:ff69d2d97a2c | 134 | rgbLED = orange; |
kswanson31 | 1:ff69d2d97a2c | 135 | Thread::wait(9 * SECOND); |
kswanson31 | 1:ff69d2d97a2c | 136 | rgbLED = red; |
kswanson31 | 1:ff69d2d97a2c | 137 | Thread::wait(9 * SECOND); |
kswanson31 | 1:ff69d2d97a2c | 138 | rgbLED = violet; |
kswanson31 | 1:ff69d2d97a2c | 139 | Thread::wait(9 * SECOND); |
kswanson31 | 1:ff69d2d97a2c | 140 | rgbLED = blue; |
kswanson31 | 1:ff69d2d97a2c | 141 | Thread::wait(9 * SECOND); |
kswanson31 | 0:51d88b4e2b23 | 142 | } |
kswanson31 | 0:51d88b4e2b23 | 143 | } |
kswanson31 | 3:641770225c44 | 144 | // - 2 |
kswanson31 | 0:51d88b4e2b23 | 145 | |
kswanson31 | 1:ff69d2d97a2c | 146 | |
kswanson31 | 0:51d88b4e2b23 | 147 | int main() { |
kswanson31 | 3:641770225c44 | 148 | /* 1 calls |
kswanson31 | 0:51d88b4e2b23 | 149 | led1 = 1, led2 = 1, led3 = 1, led4 = 1; |
kswanson31 | 0:51d88b4e2b23 | 150 | |
kswanson31 | 0:51d88b4e2b23 | 151 | first.attach(&one, 1.0); // attach the function and when to call it |
kswanson31 | 0:51d88b4e2b23 | 152 | second.attach(&two, 2.0); |
kswanson31 | 0:51d88b4e2b23 | 153 | third.attach(&three, 3.0); |
kswanson31 | 0:51d88b4e2b23 | 154 | fourth.attach(&four, 4.0); |
kswanson31 | 3:641770225c44 | 155 | */// - 1 |
kswanson31 | 0:51d88b4e2b23 | 156 | |
kswanson31 | 1:ff69d2d97a2c | 157 | |
kswanson31 | 3:641770225c44 | 158 | /* 2 calls */ |
kswanson31 | 5:420cda7cbfc5 | 159 | Thread thread2(uLCD_reboot_thread); |
kswanson31 | 3:641770225c44 | 160 | //Thread thread3(uLCD_status_thread); |
kswanson31 | 1:ff69d2d97a2c | 161 | Thread thread4(rainbow_led_thread); |
fkhan39 | 7:f3eb2b2c295d | 162 | //Thread thread5(uLCD_video_thread); |
kswanson31 | 3:641770225c44 | 163 | // - 2 |
kswanson31 | 1:ff69d2d97a2c | 164 | |
kswanson31 | 1:ff69d2d97a2c | 165 | |
kswanson31 | 3:641770225c44 | 166 | led = 1; |
kswanson31 | 3:641770225c44 | 167 | |
kswanson31 | 1:ff69d2d97a2c | 168 | while(true) { |
kswanson31 | 3:641770225c44 | 169 | /* 2 speaker 'thread' calls |
kswanson31 | 0:51d88b4e2b23 | 170 | // speaker plays mac reboot sound, error sound occasionally |
kswanson31 | 0:51d88b4e2b23 | 171 | // play reboot sound |
kswanson31 | 0:51d88b4e2b23 | 172 | // wait 20 |
kswanson31 | 0:51d88b4e2b23 | 173 | // error |
kswanson31 | 0:51d88b4e2b23 | 174 | // wait 20 |
kswanson31 | 0:51d88b4e2b23 | 175 | // error |
kswanson31 | 0:51d88b4e2b23 | 176 | // wait 20 |
kswanson31 | 3:641770225c44 | 177 | */// - 2 |
kswanson31 | 3:641770225c44 | 178 | |
kswanson31 | 3:641770225c44 | 179 | |
kswanson31 | 3:641770225c44 | 180 | // thread testing |
kswanson31 | 3:641770225c44 | 181 | led = !led; |
kswanson31 | 3:641770225c44 | 182 | Thread::wait(2000); |
kswanson31 | 3:641770225c44 | 183 | led = !led; |
kswanson31 | 3:641770225c44 | 184 | Thread::wait(1000); |
kswanson31 | 3:641770225c44 | 185 | led = !led; |
kswanson31 | 3:641770225c44 | 186 | Thread::wait(500); |
kswanson31 | 3:641770225c44 | 187 | // - test |
kswanson31 | 3:641770225c44 | 188 | |
kswanson31 | 0:51d88b4e2b23 | 189 | } // interupts will occur automatically |
kswanson31 | 0:51d88b4e2b23 | 190 | } |