hw3p2

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

Committer:
lzzcd001
Date:
Tue Feb 24 14:34:00 2015 +0000
Revision:
0:0f7c4e464135
hw3p2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lzzcd001 0:0f7c4e464135 1 #include "mbed.h"
lzzcd001 0:0f7c4e464135 2 #include "rtos.h"
lzzcd001 0:0f7c4e464135 3 #include "uLCD_4DGL.h"
lzzcd001 0:0f7c4e464135 4
lzzcd001 0:0f7c4e464135 5 uLCD_4DGL lcd(p28, p27, p30);
lzzcd001 0:0f7c4e464135 6
lzzcd001 0:0f7c4e464135 7 Mutex stdio_mutex;
lzzcd001 0:0f7c4e464135 8
lzzcd001 0:0f7c4e464135 9 DigitalOut latch(p15);
lzzcd001 0:0f7c4e464135 10 DigitalOut enable(p16);
lzzcd001 0:0f7c4e464135 11 DigitalOut led1(LED1);
lzzcd001 0:0f7c4e464135 12
lzzcd001 0:0f7c4e464135 13 SPI spi(p11, p12, p13);
lzzcd001 0:0f7c4e464135 14
lzzcd001 0:0f7c4e464135 15 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card
lzzcd001 0:0f7c4e464135 16
lzzcd001 0:0f7c4e464135 17 AnalogOut DACout(p18);
lzzcd001 0:0f7c4e464135 18
lzzcd001 0:0f7c4e464135 19 wave_player waver(&DACout);
lzzcd001 0:0f7c4e464135 20
lzzcd001 0:0f7c4e464135 21 void RGB_LED(int red, int green, int blue) {
lzzcd001 0:0f7c4e464135 22 unsigned int low_color=0;
lzzcd001 0:0f7c4e464135 23 unsigned int high_color=0;
lzzcd001 0:0f7c4e464135 24 high_color=(blue<<4)|((red&0x3C0)>>6);
lzzcd001 0:0f7c4e464135 25 low_color=(((red&0x3F)<<10)|(green));
lzzcd001 0:0f7c4e464135 26 spi.write(high_color);
lzzcd001 0:0f7c4e464135 27 spi.write(low_color);
lzzcd001 0:0f7c4e464135 28 latch=1;
lzzcd001 0:0f7c4e464135 29 latch=0;
lzzcd001 0:0f7c4e464135 30 }
lzzcd001 0:0f7c4e464135 31
lzzcd001 0:0f7c4e464135 32 void shiftbrite(void const *args) {
lzzcd001 0:0f7c4e464135 33 stdio_mutex.lock();
lzzcd001 0:0f7c4e464135 34 int red=0;
lzzcd001 0:0f7c4e464135 35 int green=0;
lzzcd001 0:0f7c4e464135 36 int blue=0;
lzzcd001 0:0f7c4e464135 37 int r=1;
lzzcd001 0:0f7c4e464135 38 int g=1;
lzzcd001 0:0f7c4e464135 39 int b=1;
lzzcd001 0:0f7c4e464135 40 while(1) {
lzzcd001 0:0f7c4e464135 41 if (r == 1){
lzzcd001 0:0f7c4e464135 42 if (red <= 255) {red += 5;} else {r= 1-r;}
lzzcd001 0:0f7c4e464135 43 } else {
lzzcd001 0:0f7c4e464135 44 if (red >= 0) {red -= 5;} else {r= 1-r;}
lzzcd001 0:0f7c4e464135 45 }
lzzcd001 0:0f7c4e464135 46 if (b == 1){
lzzcd001 0:0f7c4e464135 47 if (blue <= 255) {blue += 5;} else {b=!b;}
lzzcd001 0:0f7c4e464135 48 } else {
lzzcd001 0:0f7c4e464135 49 if (blue >= 0) {blue -= 5;} else {b=!b;}
lzzcd001 0:0f7c4e464135 50 }
lzzcd001 0:0f7c4e464135 51 if (g == 1){
lzzcd001 0:0f7c4e464135 52 if (green <= 255) {green += 5;} else {g=!g;}
lzzcd001 0:0f7c4e464135 53 } else {
lzzcd001 0:0f7c4e464135 54 if (green >= 0) {green -= 5;} else {g=!g;}
lzzcd001 0:0f7c4e464135 55 }
lzzcd001 0:0f7c4e464135 56 RGB_LED(red, green, blue);
lzzcd001 0:0f7c4e464135 57 stdio_mutex.unlock();
lzzcd001 0:0f7c4e464135 58 wait(5);
lzzcd001 0:0f7c4e464135 59 }
lzzcd001 0:0f7c4e464135 60 }
lzzcd001 0:0f7c4e464135 61
lzzcd001 0:0f7c4e464135 62 void lcd1(void const *args) {
lzzcd001 0:0f7c4e464135 63 stdio_mutex.lock();
lzzcd001 0:0f7c4e464135 64 lcd.locate(0,0);
lzzcd001 0:0f7c4e464135 65 lcd.printf("police light");
lzzcd001 0:0f7c4e464135 66 stdio_mutex.unlock();
lzzcd001 0:0f7c4e464135 67 stdio_mutex.lock();
lzzcd001 0:0f7c4e464135 68 for (int i=0; i<10; i++) {
lzzcd001 0:0f7c4e464135 69 lcd.locate(1,0);
lzzcd001 0:0f7c4e464135 70 lcd.printf("Counter: %d", i);
lzzcd001 0:0f7c4e464135 71 }
lzzcd001 0:0f7c4e464135 72 stdio_mutex.unlock();
lzzcd001 0:0f7c4e464135 73 Thread:wait(2000);
lzzcd001 0:0f7c4e464135 74 //lcd.line(64,64,prevline_x,prevline_y, BLACK);
lzzcd001 0:0f7c4e464135 75 //lcd.circle(prevcirc_x,prevcirc_y, 10, BLACK);
lzzcd001 0:0f7c4e464135 76 //float line_x = 30*cos(heading*pi/180) + 64;
lzzcd001 0:0f7c4e464135 77 //float line_y = 30*sin(heading*pi/180) + 64;
lzzcd001 0:0f7c4e464135 78 //lcd.circle(64, 64, 30, GREEN);
lzzcd001 0:0f7c4e464135 79 //lcd.line(64,64,line_x, line_y, BLUE);
lzzcd001 0:0f7c4e464135 80 }
lzzcd001 0:0f7c4e464135 81
lzzcd001 0:0f7c4e464135 82 void lcd2(void const *args) {
lzzcd001 0:0f7c4e464135 83 stdio_mutex.lock();
lzzcd001 0:0f7c4e464135 84 lcd.locate(6,0);
lzzcd001 0:0f7c4e464135 85 lcd.printf("fire sound");
lzzcd001 0:0f7c4e464135 86 stdio_mutex.unlock();
lzzcd001 0:0f7c4e464135 87 Thread::wait(4000);
lzzcd001 0:0f7c4e464135 88 //lcd.line(64,64,prevline_x,prevline_y, BLACK);
lzzcd001 0:0f7c4e464135 89 //lcd.circle(prevcirc_x,prevcirc_y, 10, BLACK);
lzzcd001 0:0f7c4e464135 90 //float line_x = 30*cos(heading*pi/180) + 64;
lzzcd001 0:0f7c4e464135 91 //float line_y = 30*sin(heading*pi/180) + 64;
lzzcd001 0:0f7c4e464135 92 //lcd.circle(64, 64, 30, GREEN);
lzzcd001 0:0f7c4e464135 93 //lcd.line(64,64,line_x, line_y, BLUE);
lzzcd001 0:0f7c4e464135 94 }
lzzcd001 0:0f7c4e464135 95
lzzcd001 0:0f7c4e464135 96 int main() {
lzzcd001 0:0f7c4e464135 97 spi.format(16,0);
lzzcd001 0:0f7c4e464135 98 spi.frequency(500000);
lzzcd001 0:0f7c4e464135 99 FILE *wave_file;
lzzcd001 0:0f7c4e464135 100 Thread t1(lcd1, (void *)"Th 1");
lzzcd001 0:0f7c4e464135 101 Thread t2(lcd2, (void *)"Th 2");
lzzcd001 0:0f7c4e464135 102 Thread t3(shiftbrite, (void *)"Th 3");
lzzcd001 0:0f7c4e464135 103
lzzcd001 0:0f7c4e464135 104 while (true) {
lzzcd001 0:0f7c4e464135 105 wave_file=fopen("/sd/test.wav","r");
lzzcd001 0:0f7c4e464135 106 waver.play(wave_file);
lzzcd001 0:0f7c4e464135 107 fclose(wave_file);
lzzcd001 0:0f7c4e464135 108 led1 = !led1;
lzzcd001 0:0f7c4e464135 109 Theard::wait(1000);
lzzcd001 0:0f7c4e464135 110 }
lzzcd001 0:0f7c4e464135 111 }