music mixer code

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

Committer:
fkhan39
Date:
Mon Oct 10 22:47:38 2016 +0000
Revision:
2:c64bfb83fe76
Parent:
1:ff69d2d97a2c
Child:
3:641770225c44
fixed page's typo (couldn't spell four, for, fourth)

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