music mixer code

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

Committer:
fkhan39
Date:
Tue Oct 11 02:23:07 2016 +0000
Revision:
6:648cd6a6fae2
Parent:
5:420cda7cbfc5
Child:
7:f3eb2b2c295d
added mutex;

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 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
kswanson31 1:ff69d2d97a2c 104 RGBLed rgbLED(p21, p22, p23); // object to use the RGB LED
kswanson31 1:ff69d2d97a2c 105 const LightColor green(0.0, 0.8, 0.0); // adjust brightness for green
kswanson31 1:ff69d2d97a2c 106 const LightColor yellow(1.0, 0.8, 0.0);
kswanson31 1:ff69d2d97a2c 107 const LightColor orange(1.0, 0.4, 0.0);
kswanson31 1:ff69d2d97a2c 108 const LightColor red(1.0,0.0,0.0);
kswanson31 1:ff69d2d97a2c 109 const LightColor violet(1.0, 0.0, 0.8);
kswanson31 1:ff69d2d97a2c 110 const LightColor blue(0.0, 0.0, 1.0);
kswanson31 1:ff69d2d97a2c 111
kswanson31 1:ff69d2d97a2c 112 // rgb led thread
kswanson31 0:51d88b4e2b23 113 void rainbow_led_thread(void const *args) {
kswanson31 1:ff69d2d97a2c 114 // reboot with white (6 sec), then cycle through apple colors (54 sec)
kswanson31 1:ff69d2d97a2c 115 while (true) {
kswanson31 1:ff69d2d97a2c 116 rgbLED.write(1.0, 1.0, 1.0); // bright white
kswanson31 1:ff69d2d97a2c 117 Thread::wait(2 * SECOND);
kswanson31 1:ff69d2d97a2c 118 // dim to nothing over 4 seconds
kswanson31 1:ff69d2d97a2c 119 for (float c = 1.0; c > 0.0; c = c - 0.01) {
kswanson31 1:ff69d2d97a2c 120 rgbLED.write(c, c, c);
kswanson31 1:ff69d2d97a2c 121 Thread::wait(40); // 40 * 100 = 4000 = 4 secs
kswanson31 1:ff69d2d97a2c 122 }
kswanson31 1:ff69d2d97a2c 123 // rotate through other colors over 54 seconds
kswanson31 1:ff69d2d97a2c 124 rgbLED = green;
kswanson31 1:ff69d2d97a2c 125 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 126 rgbLED = yellow;
kswanson31 1:ff69d2d97a2c 127 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 128 rgbLED = orange;
kswanson31 1:ff69d2d97a2c 129 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 130 rgbLED = red;
kswanson31 1:ff69d2d97a2c 131 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 132 rgbLED = violet;
kswanson31 1:ff69d2d97a2c 133 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 134 rgbLED = blue;
kswanson31 1:ff69d2d97a2c 135 Thread::wait(9 * SECOND);
kswanson31 0:51d88b4e2b23 136 }
kswanson31 0:51d88b4e2b23 137 }
kswanson31 3:641770225c44 138 // - 2
kswanson31 0:51d88b4e2b23 139
kswanson31 1:ff69d2d97a2c 140
kswanson31 0:51d88b4e2b23 141 int main() {
kswanson31 3:641770225c44 142 /* 1 calls
kswanson31 0:51d88b4e2b23 143 led1 = 1, led2 = 1, led3 = 1, led4 = 1;
kswanson31 0:51d88b4e2b23 144
kswanson31 0:51d88b4e2b23 145 first.attach(&one, 1.0); // attach the function and when to call it
kswanson31 0:51d88b4e2b23 146 second.attach(&two, 2.0);
kswanson31 0:51d88b4e2b23 147 third.attach(&three, 3.0);
kswanson31 0:51d88b4e2b23 148 fourth.attach(&four, 4.0);
kswanson31 3:641770225c44 149 */// - 1
kswanson31 0:51d88b4e2b23 150
kswanson31 1:ff69d2d97a2c 151
kswanson31 3:641770225c44 152 /* 2 calls */
kswanson31 5:420cda7cbfc5 153 Thread thread2(uLCD_reboot_thread);
kswanson31 3:641770225c44 154 //Thread thread3(uLCD_status_thread);
kswanson31 1:ff69d2d97a2c 155 Thread thread4(rainbow_led_thread);
kswanson31 3:641770225c44 156 // - 2
kswanson31 1:ff69d2d97a2c 157
kswanson31 1:ff69d2d97a2c 158
kswanson31 3:641770225c44 159 led = 1;
kswanson31 3:641770225c44 160
kswanson31 1:ff69d2d97a2c 161 while(true) {
kswanson31 3:641770225c44 162 /* 2 speaker 'thread' calls
kswanson31 0:51d88b4e2b23 163 // speaker plays mac reboot sound, error sound occasionally
kswanson31 0:51d88b4e2b23 164 // play reboot sound
kswanson31 0:51d88b4e2b23 165 // wait 20
kswanson31 0:51d88b4e2b23 166 // error
kswanson31 0:51d88b4e2b23 167 // wait 20
kswanson31 0:51d88b4e2b23 168 // error
kswanson31 0:51d88b4e2b23 169 // wait 20
kswanson31 3:641770225c44 170 */// - 2
kswanson31 3:641770225c44 171
kswanson31 3:641770225c44 172
kswanson31 3:641770225c44 173 // thread testing
kswanson31 3:641770225c44 174 led = !led;
kswanson31 3:641770225c44 175 Thread::wait(2000);
kswanson31 3:641770225c44 176 led = !led;
kswanson31 3:641770225c44 177 Thread::wait(1000);
kswanson31 3:641770225c44 178 led = !led;
kswanson31 3:641770225c44 179 Thread::wait(500);
kswanson31 3:641770225c44 180 // - test
kswanson31 3:641770225c44 181
kswanson31 0:51d88b4e2b23 182 } // interupts will occur automatically
kswanson31 0:51d88b4e2b23 183 }