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