Zhen Liu / Mbed 2 deprecated HW3_P2

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "uLCD_4DGL.h"
00004 
00005 uLCD_4DGL lcd(p28, p27, p30);
00006 
00007 Mutex stdio_mutex; 
00008 
00009 DigitalOut latch(p15);
00010 DigitalOut enable(p16);
00011 DigitalOut led1(LED1);
00012  
00013 SPI spi(p11, p12, p13);
00014 
00015 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
00016 
00017 AnalogOut DACout(p18);
00018 
00019 wave_player waver(&DACout);
00020 
00021 void RGB_LED(int red, int green, int blue) {
00022     unsigned int low_color=0;
00023     unsigned int high_color=0;
00024     high_color=(blue<<4)|((red&0x3C0)>>6);
00025     low_color=(((red&0x3F)<<10)|(green));
00026     spi.write(high_color);
00027     spi.write(low_color);
00028     latch=1;
00029     latch=0;
00030 }
00031 
00032 void shiftbrite(void const *args) {
00033     stdio_mutex.lock();
00034     int red=0;
00035     int green=0;
00036     int blue=0;
00037     int r=1;
00038     int g=1;
00039     int b=1;
00040     while(1) {
00041         if (r == 1){
00042             if (red <= 255) {red += 5;} else {r= 1-r;}
00043         } else {
00044             if (red >= 0) {red -= 5;} else {r= 1-r;}
00045         }
00046         if (b == 1){
00047             if (blue <= 255) {blue += 5;} else {b=!b;}
00048         } else {
00049             if (blue >= 0) {blue -= 5;} else {b=!b;}
00050         }
00051         if (g == 1){
00052             if (green <= 255) {green += 5;} else {g=!g;}
00053         } else {
00054             if (green >= 0) {green -= 5;} else {g=!g;}
00055         } 
00056         RGB_LED(red, green, blue);
00057         stdio_mutex.unlock();
00058         wait(5);
00059     }
00060 }
00061 
00062 void lcd1(void const *args) {
00063         stdio_mutex.lock();
00064         lcd.locate(0,0);
00065         lcd.printf("police light");
00066         stdio_mutex.unlock();
00067         stdio_mutex.lock();
00068         for (int i=0; i<10; i++) {
00069             lcd.locate(1,0);
00070             lcd.printf("Counter: %d", i);
00071         }
00072         stdio_mutex.unlock();
00073         Thread:wait(2000);
00074     //lcd.line(64,64,prevline_x,prevline_y, BLACK);
00075     //lcd.circle(prevcirc_x,prevcirc_y, 10, BLACK);
00076     //float line_x = 30*cos(heading*pi/180) + 64;
00077     //float line_y = 30*sin(heading*pi/180) + 64;
00078     //lcd.circle(64, 64, 30, GREEN);
00079     //lcd.line(64,64,line_x, line_y, BLUE);
00080 }
00081 
00082 void lcd2(void const *args) {
00083         stdio_mutex.lock();
00084         lcd.locate(6,0);
00085         lcd.printf("fire sound");
00086         stdio_mutex.unlock();
00087         Thread::wait(4000);
00088     //lcd.line(64,64,prevline_x,prevline_y, BLACK);
00089     //lcd.circle(prevcirc_x,prevcirc_y, 10, BLACK);
00090     //float line_x = 30*cos(heading*pi/180) + 64;
00091     //float line_y = 30*sin(heading*pi/180) + 64;
00092     //lcd.circle(64, 64, 30, GREEN);
00093     //lcd.line(64,64,line_x, line_y, BLUE);
00094 }
00095 
00096 int main() {
00097     spi.format(16,0);
00098     spi.frequency(500000);
00099     FILE *wave_file;
00100     Thread t1(lcd1, (void *)"Th 1");
00101     Thread t2(lcd2, (void *)"Th 2"); 
00102     Thread t3(shiftbrite, (void *)"Th 3");
00103             
00104     while (true) {
00105         wave_file=fopen("/sd/test.wav","r");
00106         waver.play(wave_file);
00107         fclose(wave_file);
00108         led1 = !led1;
00109         Theard::wait(1000);
00110     }
00111 }