music mixer code

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

Committer:
kswanson31
Date:
Tue Oct 11 00:14:04 2016 +0000
Revision:
3:641770225c44
Parent:
2:c64bfb83fe76
Child:
4:4792f0db029a
~

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 1:ff69d2d97a2c 3 #include "SimpleRGB.h"
kswanson31 1:ff69d2d97a2c 4
kswanson31 1:ff69d2d97a2c 5 #define SECOND 1000 // used with threads
kswanson31 3:641770225c44 6 DigitalOut led(LED1); // test led
kswanson31 0:51d88b4e2b23 7
kswanson31 3:641770225c44 8 /* 1 init/declare
kswanson31 1:ff69d2d97a2c 9
kswanson31 0:51d88b4e2b23 10 Ticker first;
kswanson31 0:51d88b4e2b23 11 Ticker second;
kswanson31 0:51d88b4e2b23 12 Ticker third;
kswanson31 0:51d88b4e2b23 13 Ticker fourth;
kswanson31 0:51d88b4e2b23 14 DigitalOut led1(LED1);
kswanson31 0:51d88b4e2b23 15 DigitalOut led2(LED2);
kswanson31 0:51d88b4e2b23 16 DigitalOut led3(LED3);
kswanson31 0:51d88b4e2b23 17 DigitalOut led4(LED4);
kswanson31 0:51d88b4e2b23 18
kswanson31 0:51d88b4e2b23 19 void one() { // attached to ticker object. will be called after time given
kswanson31 0:51d88b4e2b23 20 led1 = !led1; // flip the value of the led
kswanson31 0:51d88b4e2b23 21 } // nothing returned
kswanson31 0:51d88b4e2b23 22
kswanson31 0:51d88b4e2b23 23 void two() {
kswanson31 0:51d88b4e2b23 24 led2 = !led2;
kswanson31 0:51d88b4e2b23 25 }
kswanson31 0:51d88b4e2b23 26
kswanson31 0:51d88b4e2b23 27 void three() {
kswanson31 0:51d88b4e2b23 28 led3 = !led3;
kswanson31 0:51d88b4e2b23 29 }
kswanson31 0:51d88b4e2b23 30
fkhan39 2:c64bfb83fe76 31 void four() {
kswanson31 0:51d88b4e2b23 32 led4 = !led4;
kswanson31 0:51d88b4e2b23 33 }
kswanson31 1:ff69d2d97a2c 34
kswanson31 3:641770225c44 35 */// - 1
kswanson31 0:51d88b4e2b23 36
kswanson31 1:ff69d2d97a2c 37
kswanson31 3:641770225c44 38 /* 2 init/declare */
kswanson31 1:ff69d2d97a2c 39
kswanson31 1:ff69d2d97a2c 40 // lcd thread
kswanson31 0:51d88b4e2b23 41 void uLCD_reboot_thread(void const *args) {
kswanson31 0:51d88b4e2b23 42 // show reboot process, associated with clip of apple restart
kswanson31 1:ff69d2d97a2c 43 while (true) {
kswanson31 0:51d88b4e2b23 44 // lock, print 'Rebooting!' to uLCD with "progress_bar" underneath
kswanson31 0:51d88b4e2b23 45 // draw progress bar 8 sec
kswanson31 0:51d88b4e2b23 46 // draw an ascii 'apple'
kswanson31 0:51d88b4e2b23 47 // unlock, wait for 52
kswanson31 0:51d88b4e2b23 48 }
kswanson31 0:51d88b4e2b23 49 }
kswanson31 0:51d88b4e2b23 50
kswanson31 1:ff69d2d97a2c 51 // lcd thread
kswanson31 0:51d88b4e2b23 52 void uLCD_status_thread(void const *args) {
kswanson31 0:51d88b4e2b23 53 // show time with smile, frown, sad face at top right
kswanson31 1:ff69d2d97a2c 54 while (true) {
kswanson31 0:51d88b4e2b23 55 // lock, update time at top right with smiley at top left
kswanson31 0:51d88b4e2b23 56 // unlock, wait for 20 sec
kswanson31 0:51d88b4e2b23 57 // lock, change to frown
kswanson31 0:51d88b4e2b23 58 // unlock, wait for 20 sec
kswanson31 0:51d88b4e2b23 59 // lock, change to sad
kswanson31 0:51d88b4e2b23 60 // unlock, wait for 20 sec
kswanson31 0:51d88b4e2b23 61 }
kswanson31 0:51d88b4e2b23 62 }
kswanson31 0:51d88b4e2b23 63
kswanson31 1:ff69d2d97a2c 64 RGBLed rgbLED(p21, p22, p23); // object to use the RGB LED
kswanson31 1:ff69d2d97a2c 65 const LightColor green(0.0, 0.8, 0.0); // adjust brightness for green
kswanson31 1:ff69d2d97a2c 66 const LightColor yellow(1.0, 0.8, 0.0);
kswanson31 1:ff69d2d97a2c 67 const LightColor orange(1.0, 0.4, 0.0);
kswanson31 1:ff69d2d97a2c 68 const LightColor red(1.0,0.0,0.0);
kswanson31 1:ff69d2d97a2c 69 const LightColor violet(1.0, 0.0, 0.8);
kswanson31 1:ff69d2d97a2c 70 const LightColor blue(0.0, 0.0, 1.0);
kswanson31 1:ff69d2d97a2c 71
kswanson31 1:ff69d2d97a2c 72 // rgb led thread
kswanson31 0:51d88b4e2b23 73 void rainbow_led_thread(void const *args) {
kswanson31 1:ff69d2d97a2c 74 // reboot with white (6 sec), then cycle through apple colors (54 sec)
kswanson31 1:ff69d2d97a2c 75 while (true) {
kswanson31 1:ff69d2d97a2c 76 rgbLED.write(1.0, 1.0, 1.0); // bright white
kswanson31 1:ff69d2d97a2c 77 Thread::wait(2 * SECOND);
kswanson31 1:ff69d2d97a2c 78 // dim to nothing over 4 seconds
kswanson31 1:ff69d2d97a2c 79 for (float c = 1.0; c > 0.0; c = c - 0.01) {
kswanson31 1:ff69d2d97a2c 80 rgbLED.write(c, c, c);
kswanson31 1:ff69d2d97a2c 81 Thread::wait(40); // 40 * 100 = 4000 = 4 secs
kswanson31 1:ff69d2d97a2c 82 }
kswanson31 1:ff69d2d97a2c 83 // rotate through other colors over 54 seconds
kswanson31 1:ff69d2d97a2c 84 rgbLED = green;
kswanson31 1:ff69d2d97a2c 85 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 86 rgbLED = yellow;
kswanson31 1:ff69d2d97a2c 87 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 88 rgbLED = orange;
kswanson31 1:ff69d2d97a2c 89 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 90 rgbLED = red;
kswanson31 1:ff69d2d97a2c 91 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 92 rgbLED = violet;
kswanson31 1:ff69d2d97a2c 93 Thread::wait(9 * SECOND);
kswanson31 1:ff69d2d97a2c 94 rgbLED = blue;
kswanson31 1:ff69d2d97a2c 95 Thread::wait(9 * SECOND);
kswanson31 0:51d88b4e2b23 96 }
kswanson31 0:51d88b4e2b23 97 }
kswanson31 3:641770225c44 98 // - 2
kswanson31 0:51d88b4e2b23 99
kswanson31 1:ff69d2d97a2c 100
kswanson31 0:51d88b4e2b23 101 int main() {
kswanson31 3:641770225c44 102 /* 1 calls
kswanson31 0:51d88b4e2b23 103 led1 = 1, led2 = 1, led3 = 1, led4 = 1;
kswanson31 0:51d88b4e2b23 104
kswanson31 0:51d88b4e2b23 105 first.attach(&one, 1.0); // attach the function and when to call it
kswanson31 0:51d88b4e2b23 106 second.attach(&two, 2.0);
kswanson31 0:51d88b4e2b23 107 third.attach(&three, 3.0);
kswanson31 0:51d88b4e2b23 108 fourth.attach(&four, 4.0);
kswanson31 3:641770225c44 109 */// - 1
kswanson31 0:51d88b4e2b23 110
kswanson31 1:ff69d2d97a2c 111
kswanson31 3:641770225c44 112 /* 2 calls */
kswanson31 3:641770225c44 113 //Thread thread2(uLCD_reboot_thread);
kswanson31 3:641770225c44 114 //Thread thread3(uLCD_status_thread);
kswanson31 1:ff69d2d97a2c 115 Thread thread4(rainbow_led_thread);
kswanson31 3:641770225c44 116 // - 2
kswanson31 1:ff69d2d97a2c 117
kswanson31 1:ff69d2d97a2c 118
kswanson31 3:641770225c44 119 led = 1;
kswanson31 3:641770225c44 120
kswanson31 1:ff69d2d97a2c 121 while(true) {
kswanson31 3:641770225c44 122 /* 2 speaker 'thread' calls
kswanson31 0:51d88b4e2b23 123 // speaker plays mac reboot sound, error sound occasionally
kswanson31 0:51d88b4e2b23 124 // play reboot sound
kswanson31 0:51d88b4e2b23 125 // wait 20
kswanson31 0:51d88b4e2b23 126 // error
kswanson31 0:51d88b4e2b23 127 // wait 20
kswanson31 0:51d88b4e2b23 128 // error
kswanson31 0:51d88b4e2b23 129 // wait 20
kswanson31 3:641770225c44 130 */// - 2
kswanson31 3:641770225c44 131
kswanson31 3:641770225c44 132
kswanson31 3:641770225c44 133 // thread testing
kswanson31 3:641770225c44 134 led = !led;
kswanson31 3:641770225c44 135 Thread::wait(2000);
kswanson31 3:641770225c44 136 led = !led;
kswanson31 3:641770225c44 137 Thread::wait(1000);
kswanson31 3:641770225c44 138 led = !led;
kswanson31 3:641770225c44 139 Thread::wait(500);
kswanson31 3:641770225c44 140 // - test
kswanson31 3:641770225c44 141
kswanson31 0:51d88b4e2b23 142 } // interupts will occur automatically
kswanson31 0:51d88b4e2b23 143 }