update
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed wave_player
Fork of Lab3 by
Diff: Part2.cpp
- Revision:
- 3:2adc8522c5e8
- Parent:
- 2:d832b6c139d4
- Child:
- 4:7ec6407f673f
diff -r d832b6c139d4 -r 2adc8522c5e8 Part2.cpp --- a/Part2.cpp Thu Oct 13 15:28:12 2016 +0000 +++ b/Part2.cpp Thu Oct 13 19:30:09 2016 +0000 @@ -6,7 +6,7 @@ #include <mpr121.h> // mutex to make the lcd lib thread safe -Mutex lcd_mutex; +//Mutex lcd_mutex; SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card AnalogOut speaker(p18); wave_player waver(&speaker); @@ -16,17 +16,16 @@ // Shiftbrite DigitalOut latch(p15); DigitalOut enable(p16); - SPI spi(p11, p12, p13); // Touchpad // Create the interrupt receiver object on pin 26 -InterruptIn interrupt(p26); +//InterruptIn interrupt(p26); // Setup the i2c bus on pins 9 and 10 -I2C i2c(p9, p10); +//I2C i2c(p9, p10); // Setup the Mpr121: // constructor(i2c object, i2c address of the mpr121) -Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); +//Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); // Test leds DigitalOut led1(LED1); @@ -41,7 +40,7 @@ int bright = 1; // time of the day // Light sensor -AnalogIn photocell(p17); +//AnalogIn photocell(p17); //PwmOut myled(LED1); @@ -129,10 +128,10 @@ return _pins.read(); } -microphone mymicrophone(p19); // uncomment when using +//microphone mymicrophone(p19); // uncomment when using // joystick -Nav_Switch myNav(p14, p25, p24, p10, p9); //pin order on Sparkfun breakout +//Nav_Switch myNav(p14, p25, p24, p10, p9); //pin order on Sparkfun breakout // Shiftbrite void RGB_LED(int red, int green, int blue) @@ -152,26 +151,26 @@ void LCD_thread1(void const *args) { while(true) { // thread loop - lcd_mutex.lock(); - // lcd_sem.wait(); + //lcd_mutex.lock(); + lcd_sem.wait(); //if (light == 0 && sound == 1) { if (light == 0) { // display red siren uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, RED); uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, RED); - //} else if (light == 1 || sound == 0) { - } else if (light == 1) { + //} else if (light == 1 || sound == 0) { + } else if (light == 1) { // display white siren uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, WHITE); uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, WHITE); - //} else if (light == 2 && sound == 1) { - } else if (light == 2) { + //} else if (light == 2 && sound == 1) { + } else if (light == 2) { // display blue siren uLCD.filled_circle(SIZE_X/2, SIZE_Y/2, 10, BLUE); uLCD.filled_rectangle(SIZE_X/2 - 10, SIZE_Y/2, SIZE_X/2 + 10, SIZE_Y/2 + 30, BLUE); } - lcd_mutex.unlock(); - // lcd_sem.release(); + //lcd_mutex.unlock(); + lcd_sem.release(); Thread::wait(5); // wait till thread is done } } @@ -180,8 +179,8 @@ void LCD_thread2(void const *args) { while(true) { // thread loop - lcd_mutex.lock(); - // lcd_sem.wait(); + //lcd_mutex.lock(); + lcd_sem.wait(); if (sound == 1) { // some indication there is audio // text saying "!ALERT!" @@ -198,8 +197,8 @@ // no indication // text removed } - lcd_mutex.unlock(); - // lcd_sem.release(); + //lcd_mutex.unlock(); + lcd_sem.release(); Thread::wait(5); // wait till thread is done } } @@ -213,26 +212,27 @@ } int play = 1; + // thread dealing with speaker void speaker_thread(void const *args) { - FILE *wave_file; - wave_file = fopen("/sd/police_siren.wav","r"); // play siren while(true) { // thread loop - if (play == 1) { - sound = 1; // comment when using mic - waver.play(wave_file); - play = 0; // comment when using joystick or touchpad - } else if (play == 0) { - // how can we stop play midway? - reduce volume? - sound = 0; // comment when using mic - wait(5); // comment when using joystick or touchpad - play = 1; // comment when using joystick or touchpad - } + FILE * wave_file; + wave_file = fopen("/sd/police-siren.wav","r"); + //if (play == 1) { + sound = 1; // comment when using mic + waver.play(wave_file); + //play = 0; // comment when using joystick or touchpad + //} else if (play == 0) { + // how can we stop play midway? - reduce volume? + sound = 0; // comment when using mic + wait(5); // comment when using joystick or touchpad + play = 1; // comment when using joystick or touchpad + //} + fclose(wave_file); Thread::wait(1000); // wait 1s } - fclose(wave_file); } // thread reading from - ultrasonic sensor to do XYZ @@ -242,6 +242,7 @@ } // thread reading from - tactile switch to control RGB/sound/lcd +/* void switchthread(void const *args) { while(true) { @@ -287,8 +288,9 @@ } } } - +*/ // thread reading from - light sensor to do something +/* void lightsensor_thread(void const *args) { while(true) { // thread loop @@ -301,8 +303,9 @@ Thread::wait(1000); // wait 0.25s } } - -int mymic; +*/ +//int mymic; +/* // thread reading from - microphone to control rgb void mic_thread(void const *args) { @@ -320,10 +323,11 @@ Thread::wait(1000); // wait 0.25s } } - +*/ int main() { - uLCD.cls(); + //uLCD.cls(); + //wait(1); // shiftbrite stuff int red=0; @@ -342,7 +346,7 @@ // call threads here Thread t1(LCD_thread1); //start thread1 Thread t2(LCD_thread2); //start thread2 - //Thread t3(speaker_thread); //start thread3 + Thread t3(speaker_thread); //start thread3 //Thread t4(sonar_thread); // start thread4 //Thread t5(switchthread); //start thread5 //Thread t6(touchpad_thread); //start thread6 @@ -351,56 +355,59 @@ // running shiftbrite int color = 255; + while(1) { if (bright == 0) { color = 150; - } else { + } else if (bright == 1) { color = 255; } + if (sound == 0) { - RGB_LED(0,0,0); - Thread::wait(500); + RGB_LED(0,0,0); + Thread::wait(500); } else { - light = 0; - red = color; // flash red light - green = 0; - blue = 0; - RGB_LED(red, green, blue); - wait(sb_freq); + light = 0; + red = color; // flash red light + green = 0; + blue = 0; + RGB_LED(red, green, blue); + wait(0.5); - RGB_LED(0,0,0); - wait(sb_freq); + RGB_LED(0,0,0); + wait(0.5); - light = 1; - red = color; // flash white light - green = color; - blue = color; - RGB_LED( red, green, blue); - wait(sb_freq); + light = 1; + red = color; // flash white light + green = color; + blue = color; + RGB_LED( red, green, blue); + wait(0.5); - RGB_LED(0,0,0); - wait(sb_freq); + RGB_LED(0,0,0); + wait(0.5); - light = 2; - red = 0; // flash blue light - green = 0; - blue = color; - RGB_LED( red, green, blue); - wait(sb_freq); + light = 2; + red = 0; // flash blue light + green = 0; + blue = color; + RGB_LED( red, green, blue); + wait(0.5); - RGB_LED(0,0,0); - wait(sb_freq); + RGB_LED(0,0,0); + wait(0.5); - light = 1; - red = color; // flash white light - green = color; - blue = color; - RGB_LED( red, green, blue); - wait(sb_freq); + light = 1; + red = color; // flash white light + green = color; + blue = color; + RGB_LED( red, green, blue); + wait(0.5); - RGB_LED(0,0,0); - Thread::wait(500); + RGB_LED(0,0,0); + wait(0.5); + Thread::wait(500); } } }