music mixer code

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

main.cpp

Committer:
fkhan39
Date:
2016-10-11
Revision:
4:4792f0db029a
Parent:
3:641770225c44
Child:
5:420cda7cbfc5

File content as of revision 4:4792f0db029a:

#include "mbed.h"
#include "rtos.h"
#include "uLCD_4DGL.h"
#include "SimpleRGB.h"

#define SECOND 1000 // used with threads
DigitalOut led(LED1); // test led

uLCD_4DGL lcd(p13,p14,p12); 
/* 1 init/declare

Ticker first;
Ticker second;
Ticker third;
Ticker fourth;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

void one() {      // attached to ticker object. will be called after time given
    led1 = !led1; // flip the value of the led
}                 // nothing returned

void two() {
    led2 = !led2;
}

void three() {
    led3 = !led3;
}

void four() {
    led4 = !led4;
}

*/// - 1


/* 2 init/declare */

// lcd thread
void uLCD_reboot_thread(void const *args) {
    // show reboot process, associated with clip of apple restart
    while (true) {
        // lock, print 'Rebooting!' to uLCD with "progress_bar" underneath
        lcd.text_string("Rebooting", 2, 5, FONT_7X8, WHITE);
        // draw progress bar 8 sec
        std::string progress("|");
        for (int i = 0; i < 8; i++) {
            lcd.text_string(progress, 2, 6, FONT_7X8, WHITE);
            progress += '|';
            wait(SECOND);
        }
        // draw an ascii 'apple'
        lcd.text_string("(_`_)", 2, 5, FONT_7X8, WHITE);
        lcd.text_string("", 2, 6, FONT_7X8, WHITE);
        // unlock, wait for 52
        wait(52*SECOND);
    }
}

// lcd thread
void uLCD_status_thread(void const *args) {
    // show time with smile, frown, sad face at top right
    set_time(1476146921);
    while (true) {
        // lock, update time at top right with smiley at top left
        time_t seconds = time(NULL);
        char buffer[32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        lcd.text_string(buffer, 5, 1, FONT_7X8, RED);
        lcd.text_string(":-)", 1, 1, FONT_7X8, WHITE);
        // unlock, wait for 20 sec
        wait(20*SECOND);
        // lock, change to frown
        lcd.text_string(">:|", 1, 1, FONT_7X8, WHITE);
        // unlock, wait for 20 sec
        wait(20*SECOND);
        // lock, change to sad
        lcd.text_string(":-(", 1, 1, FONT_7X8, WHITE);
        // unlock, wait for 20 sec
        wait(20*SECOND);
    }
}

RGBLed rgbLED(p21, p22, p23);          // object to use the RGB LED
const LightColor green(0.0, 0.8, 0.0); // adjust brightness for green
const LightColor yellow(1.0, 0.8, 0.0);
const LightColor orange(1.0, 0.4, 0.0);
const LightColor red(1.0,0.0,0.0);
const LightColor violet(1.0, 0.0, 0.8);
const LightColor blue(0.0, 0.0, 1.0);

// rgb led thread
void rainbow_led_thread(void const *args) {
    // reboot with white (6 sec), then cycle through apple colors (54 sec)
    while (true) {
        rgbLED.write(1.0, 1.0, 1.0); // bright white
        Thread::wait(2 * SECOND);
        // dim to nothing over 4 seconds
        for (float c = 1.0; c > 0.0; c = c - 0.01) {
            rgbLED.write(c, c, c);
            Thread::wait(40); // 40 * 100 = 4000 = 4 secs
        }
        // rotate through other colors over 54 seconds
        rgbLED = green;
        Thread::wait(9 * SECOND);
        rgbLED = yellow;
        Thread::wait(9 * SECOND);
        rgbLED = orange;
        Thread::wait(9 * SECOND);
        rgbLED = red;
        Thread::wait(9 * SECOND);
        rgbLED = violet;
        Thread::wait(9 * SECOND);
        rgbLED = blue;
        Thread::wait(9 * SECOND);
    }
}
// - 2


int main() {
    /* 1 calls
    led1 = 1, led2 = 1, led3 = 1, led4 = 1;
    
    first.attach(&one, 1.0);   // attach the function and when to call it
    second.attach(&two, 2.0);
    third.attach(&three, 3.0);
    fourth.attach(&four, 4.0);
    */// - 1
    
    
    /* 2 calls */
    //Thread thread2(uLCD_reboot_thread);
    //Thread thread3(uLCD_status_thread);
    Thread thread4(rainbow_led_thread);
    // - 2
    
    
    led = 1;
    
    while(true) {
        /* 2 speaker 'thread' calls
        // speaker plays mac reboot sound, error sound occasionally
        // play reboot sound
        // wait 20
        // error
        // wait 20
        // error
        // wait 20
        */// - 2
        
        
        // thread testing
        led = !led;
        Thread::wait(2000);
        led = !led;
        Thread::wait(1000);
        led = !led;
        Thread::wait(500);
        // - test
        
    }                // interupts will occur automatically
}