hw3p2

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

main.cpp

Committer:
lzzcd001
Date:
2015-02-24
Revision:
0:0f7c4e464135

File content as of revision 0:0f7c4e464135:

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

uLCD_4DGL lcd(p28, p27, p30);

Mutex stdio_mutex; 

DigitalOut latch(p15);
DigitalOut enable(p16);
DigitalOut led1(LED1);
 
SPI spi(p11, p12, p13);

SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card

AnalogOut DACout(p18);

wave_player waver(&DACout);

void RGB_LED(int red, int green, int blue) {
    unsigned int low_color=0;
    unsigned int high_color=0;
    high_color=(blue<<4)|((red&0x3C0)>>6);
    low_color=(((red&0x3F)<<10)|(green));
    spi.write(high_color);
    spi.write(low_color);
    latch=1;
    latch=0;
}

void shiftbrite(void const *args) {
    stdio_mutex.lock();
    int red=0;
    int green=0;
    int blue=0;
    int r=1;
    int g=1;
    int b=1;
    while(1) {
        if (r == 1){
            if (red <= 255) {red += 5;} else {r= 1-r;}
        } else {
            if (red >= 0) {red -= 5;} else {r= 1-r;}
        }
        if (b == 1){
            if (blue <= 255) {blue += 5;} else {b=!b;}
        } else {
            if (blue >= 0) {blue -= 5;} else {b=!b;}
        }
        if (g == 1){
            if (green <= 255) {green += 5;} else {g=!g;}
        } else {
            if (green >= 0) {green -= 5;} else {g=!g;}
        } 
        RGB_LED(red, green, blue);
        stdio_mutex.unlock();
        wait(5);
    }
}

void lcd1(void const *args) {
        stdio_mutex.lock();
        lcd.locate(0,0);
        lcd.printf("police light");
        stdio_mutex.unlock();
        stdio_mutex.lock();
        for (int i=0; i<10; i++) {
            lcd.locate(1,0);
            lcd.printf("Counter: %d", i);
        }
        stdio_mutex.unlock();
        Thread:wait(2000);
    //lcd.line(64,64,prevline_x,prevline_y, BLACK);
    //lcd.circle(prevcirc_x,prevcirc_y, 10, BLACK);
    //float line_x = 30*cos(heading*pi/180) + 64;
    //float line_y = 30*sin(heading*pi/180) + 64;
    //lcd.circle(64, 64, 30, GREEN);
    //lcd.line(64,64,line_x, line_y, BLUE);
}

void lcd2(void const *args) {
        stdio_mutex.lock();
        lcd.locate(6,0);
        lcd.printf("fire sound");
        stdio_mutex.unlock();
        Thread::wait(4000);
    //lcd.line(64,64,prevline_x,prevline_y, BLACK);
    //lcd.circle(prevcirc_x,prevcirc_y, 10, BLACK);
    //float line_x = 30*cos(heading*pi/180) + 64;
    //float line_y = 30*sin(heading*pi/180) + 64;
    //lcd.circle(64, 64, 30, GREEN);
    //lcd.line(64,64,line_x, line_y, BLUE);
}

int main() {
    spi.format(16,0);
    spi.frequency(500000);
    FILE *wave_file;
    Thread t1(lcd1, (void *)"Th 1");
    Thread t2(lcd2, (void *)"Th 2"); 
    Thread t3(shiftbrite, (void *)"Th 3");
            
    while (true) {
        wave_file=fopen("/sd/test.wav","r");
        waver.play(wave_file);
        fclose(wave_file);
        led1 = !led1;
        Theard::wait(1000);
    }
}