music mixer code

Dependencies:   mbed wave_player mbed-rtos SimpleRGB 4DGL-uLCD-SE PinDetect

Committer:
kswanson31
Date:
Tue Oct 11 03:30:07 2016 +0000
Revision:
8:38419fb4e242
Parent:
7:f3eb2b2c295d
Child:
9:d6343146d23e
Edits to status and reboot threads

Who changed what in which revision?

UserRevisionLine numberNew 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 8:38419fb4e242 10
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 8:38419fb4e242 43 uLCD_4DGL lcd(p9, p10, p11); // lcd used by the following threads
kswanson31 8:38419fb4e242 44 Mutex stdio_mutex; // mutex lock, ensure safe use of lcd
kswanson31 8:38419fb4e242 45
kswanson31 1:ff69d2d97a2c 46 // lcd thread
kswanson31 0:51d88b4e2b23 47 void uLCD_reboot_thread(void const *args) {
kswanson31 0:51d88b4e2b23 48 // show reboot process, associated with clip of apple restart
kswanson31 1:ff69d2d97a2c 49 while (true) {
kswanson31 0:51d88b4e2b23 50 // lock, print 'Rebooting!' to uLCD with "progress_bar" underneath
fkhan39 6:648cd6a6fae2 51 stdio_mutex.lock();
fkhan39 4:4792f0db029a 52 lcd.text_string("Rebooting", 2, 5, FONT_7X8, WHITE);
kswanson31 0:51d88b4e2b23 53 // draw progress bar 8 sec
fkhan39 4:4792f0db029a 54 for (int i = 0; i < 8; i++) {
kswanson31 5:420cda7cbfc5 55 lcd.text_string("|", 2 + i, 6, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 56 // progress[i+1] += arr[i];
kswanson31 5:420cda7cbfc5 57 Thread::wait(SECOND);
fkhan39 4:4792f0db029a 58 }
kswanson31 0:51d88b4e2b23 59 // draw an ascii 'apple'
kswanson31 5:420cda7cbfc5 60 lcd.text_string(" .:' ", 2, 4, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 61 lcd.text_string(" :' ", 2, 5, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 62 lcd.text_string(" .'` `-' ``. ", 2, 6, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 63 lcd.text_string(": .-' ", 2, 7, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 64 lcd.text_string(": : ", 2, 8, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 65 lcd.text_string(" : `-; ", 2, 9, FONT_7X8, WHITE);
kswanson31 5:420cda7cbfc5 66 lcd.text_string(" `.__.-.__.' ", 2, 10, FONT_7X8, WHITE);
kswanson31 0:51d88b4e2b23 67 // unlock, wait for 52
fkhan39 6:648cd6a6fae2 68 stdio_mutex.unlock();
kswanson31 5:420cda7cbfc5 69 Thread::wait(52 * SECOND);
kswanson31 5:420cda7cbfc5 70 // clear screen
kswanson31 8:38419fb4e242 71 stdio_mutex.lock();
kswanson31 5:420cda7cbfc5 72 lcd.cls();
kswanson31 8:38419fb4e242 73 stdio_mutex.unlock();
kswanson31 0:51d88b4e2b23 74 }
kswanson31 0:51d88b4e2b23 75 }
kswanson31 0:51d88b4e2b23 76
kswanson31 1:ff69d2d97a2c 77 // lcd thread
kswanson31 0:51d88b4e2b23 78 void uLCD_status_thread(void const *args) {
kswanson31 0:51d88b4e2b23 79 // show time with smile, frown, sad face at top right
fkhan39 4:4792f0db029a 80 set_time(1476146921);
kswanson31 1:ff69d2d97a2c 81 while (true) {
kswanson31 5:420cda7cbfc5 82 char buffer[32];
fkhan39 4:4792f0db029a 83 time_t seconds = time(NULL);
fkhan39 4:4792f0db029a 84 strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
fkhan39 6:648cd6a6fae2 85 // lock, update time at top right with smiley at top left
fkhan39 6:648cd6a6fae2 86 stdio_mutex.lock();
fkhan39 4:4792f0db029a 87 lcd.text_string(buffer, 5, 1, FONT_7X8, RED);
fkhan39 4:4792f0db029a 88 lcd.text_string(":-)", 1, 1, FONT_7X8, WHITE);
kswanson31 0:51d88b4e2b23 89 // unlock, wait for 20 sec
fkhan39 6:648cd6a6fae2 90 stdio_mutex.unlock();
kswanson31 5:420cda7cbfc5 91 Thread::wait(20 * SECOND);
fkhan39 4:4792f0db029a 92 // lock, change to frown
fkhan39 6:648cd6a6fae2 93 stdio_mutex.lock();
fkhan39 4:4792f0db029a 94 lcd.text_string(">:|", 1, 1, FONT_7X8, WHITE);
fkhan39 4:4792f0db029a 95 // unlock, wait for 20 sec
fkhan39 6:648cd6a6fae2 96 stdio_mutex.unlock();
kswanson31 5:420cda7cbfc5 97 Thread::wait(20 * SECOND);
kswanson31 0:51d88b4e2b23 98 // lock, change to sad
fkhan39 6:648cd6a6fae2 99 stdio_mutex.lock();
fkhan39 4:4792f0db029a 100 lcd.text_string(":-(", 1, 1, FONT_7X8, WHITE);
kswanson31 0:51d88b4e2b23 101 // unlock, wait for 20 sec
fkhan39 6:648cd6a6fae2 102 stdio_mutex.unlock();
kswanson31 8:38419fb4e242 103 Thread::wait(20 * SECOND);
kswanson31 0:51d88b4e2b23 104 }
kswanson31 0:51d88b4e2b23 105 }
kswanson31 0:51d88b4e2b23 106
kswanson31 8:38419fb4e242 107
kswanson31 8:38419fb4e242 108 /* extra credit - video */
fkhan39 7:f3eb2b2c295d 109 void uLCD_video_thread(void const *args) {
kswanson31 8:38419fb4e242 110 lcd.media_init();
kswanson31 8:38419fb4e242 111 lcd.set_sector_address(0x001D, 0x4C42);
kswanson31 8:38419fb4e242 112 lcd.display_video(0,0);
fkhan39 7:f3eb2b2c295d 113 }
kswanson31 8:38419fb4e242 114 // - extra
kswanson31 8:38419fb4e242 115
fkhan39 7:f3eb2b2c295d 116
kswanson31 1:ff69d2d97a2c 117 RGBLed rgbLED(p21, p22, p23); // object to use the RGB LED
kswanson31 8:38419fb4e242 118 const LightColor green(0.0, 1.0, 0.0);
kswanson31 8:38419fb4e242 119 const LightColor yellow(1.0, 1.0, 0.0);
kswanson31 8:38419fb4e242 120 const LightColor orange(1.0, 0.6, 0.0);
kswanson31 1:ff69d2d97a2c 121 const LightColor red(1.0,0.0,0.0);
kswanson31 1:ff69d2d97a2c 122 const LightColor violet(1.0, 0.0, 0.8);
kswanson31 1:ff69d2d97a2c 123 const LightColor blue(0.0, 0.0, 1.0);
kswanson31 1:ff69d2d97a2c 124
kswanson31 1:ff69d2d97a2c 125 // rgb led thread
kswanson31 0:51d88b4e2b23 126 void rainbow_led_thread(void const *args) {
kswanson31 1:ff69d2d97a2c 127 // reboot with white (6 sec), then cycle through apple colors (54 sec)
kswanson31 1:ff69d2d97a2c 128 while (true) {
kswanson31 1:ff69d2d97a2c 129 rgbLED.write(1.0, 1.0, 1.0); // bright white
kswanson31 1:ff69d2d97a2c 130 Thread::wait(2 * SECOND);
kswanson31 1:ff69d2d97a2c 131 // dim to nothing over 4 seconds
kswanson31 1:ff69d2d97a2c 132 for (float c = 1.0; c > 0.0; c = c - 0.01) {
kswanson31 1:ff69d2d97a2c 133 rgbLED.write(c, c, c);
kswanson31 1:ff69d2d97a2c 134 Thread::wait(40); // 40 * 100 = 4000 = 4 secs
kswanson31 1:ff69d2d97a2c 135 }
kswanson31 1:ff69d2d97a2c 136 // rotate through other colors over 54 seconds
kswanson31 1:ff69d2d97a2c 137 rgbLED = green;
kswanson31 1:ff69d2d97a2c 138 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 139 rgbLED = yellow;
kswanson31 1:ff69d2d97a2c 140 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 141 rgbLED = orange;
kswanson31 1:ff69d2d97a2c 142 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 143 rgbLED = red;
kswanson31 1:ff69d2d97a2c 144 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 145 rgbLED = violet;
kswanson31 1:ff69d2d97a2c 146 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 147 rgbLED = blue;
kswanson31 1:ff69d2d97a2c 148 Thread::wait(9 * SECOND);
kswanson31 0:51d88b4e2b23 149 }
kswanson31 0:51d88b4e2b23 150 }
kswanson31 3:641770225c44 151 // - 2
kswanson31 0:51d88b4e2b23 152
kswanson31 1:ff69d2d97a2c 153
kswanson31 0:51d88b4e2b23 154 int main() {
kswanson31 3:641770225c44 155 /* 1 calls
kswanson31 0:51d88b4e2b23 156 led1 = 1, led2 = 1, led3 = 1, led4 = 1;
kswanson31 0:51d88b4e2b23 157
kswanson31 0:51d88b4e2b23 158 first.attach(&one, 1.0); // attach the function and when to call it
kswanson31 0:51d88b4e2b23 159 second.attach(&two, 2.0);
kswanson31 0:51d88b4e2b23 160 third.attach(&three, 3.0);
kswanson31 0:51d88b4e2b23 161 fourth.attach(&four, 4.0);
kswanson31 3:641770225c44 162 */// - 1
kswanson31 0:51d88b4e2b23 163
kswanson31 1:ff69d2d97a2c 164
kswanson31 3:641770225c44 165 /* 2 calls */
kswanson31 5:420cda7cbfc5 166 Thread thread2(uLCD_reboot_thread);
kswanson31 8:38419fb4e242 167 Thread thread3(uLCD_status_thread);
kswanson31 8:38419fb4e242 168 //Thread thread4(rainbow_led_thread);
fkhan39 7:f3eb2b2c295d 169 //Thread thread5(uLCD_video_thread);
kswanson31 3:641770225c44 170 // - 2
kswanson31 1:ff69d2d97a2c 171
kswanson31 1:ff69d2d97a2c 172
kswanson31 3:641770225c44 173 led = 1;
kswanson31 3:641770225c44 174
kswanson31 1:ff69d2d97a2c 175 while(true) {
kswanson31 3:641770225c44 176 /* 2 speaker 'thread' calls
kswanson31 0:51d88b4e2b23 177 // speaker plays mac reboot sound, error sound occasionally
kswanson31 0:51d88b4e2b23 178 // play reboot sound
kswanson31 0:51d88b4e2b23 179 // wait 20
kswanson31 0:51d88b4e2b23 180 // error
kswanson31 0:51d88b4e2b23 181 // wait 20
kswanson31 0:51d88b4e2b23 182 // error
kswanson31 0:51d88b4e2b23 183 // wait 20
kswanson31 3:641770225c44 184 */// - 2
kswanson31 3:641770225c44 185
kswanson31 3:641770225c44 186
kswanson31 3:641770225c44 187 // thread testing
kswanson31 3:641770225c44 188 led = !led;
kswanson31 3:641770225c44 189 Thread::wait(2000);
kswanson31 3:641770225c44 190 led = !led;
kswanson31 3:641770225c44 191 Thread::wait(1000);
kswanson31 3:641770225c44 192 led = !led;
kswanson31 3:641770225c44 193 Thread::wait(500);
kswanson31 3:641770225c44 194 // - test
kswanson31 3:641770225c44 195
kswanson31 0:51d88b4e2b23 196 } // interupts will occur automatically
kswanson31 0:51d88b4e2b23 197 }