update
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed wave_player
Fork of Lab3 by
Revision 4:7ec6407f673f, committed 2016-10-16
- Comitter:
- aismail1997
- Date:
- Sun Oct 16 14:42:22 2016 +0000
- Parent:
- 3:2adc8522c5e8
- Commit message:
- updated version;
Changed in this revision
Part2.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/Part2.cpp Thu Oct 13 19:30:09 2016 +0000 +++ b/Part2.cpp Sun Oct 16 14:42:22 2016 +0000 @@ -1,49 +1,59 @@ #include "mbed.h" #include "rtos.h" #include "uLCD_4DGL.h" -#include "SDFileSystem.h" -#include "wave_player.h" +//#include "SDFileSystem.h" +//#include "wave_player.h" #include <mpr121.h> // mutex to make the lcd lib thread safe -//Mutex lcd_mutex; -SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card -AnalogOut speaker(p18); -wave_player waver(&speaker); +Mutex lcd_mutex; +//SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card +PwmOut speaker(p26); +//wave_player waver(&speaker); uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin -Semaphore lcd_sem(1); +//Semaphore lcd_sem(1); // 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(p21); // 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); DigitalOut led2(LED2); DigitalOut led3(LED3); DigitalOut led4(LED4); +// sonar + +DigitalOut trigger(p20); +//DigitalOut myled(LED1); //monitor trigger +//DigitalOut myled2(LED2); //monitor echo +DigitalIn echo(p19); +int distance = 0; +int correction = 0; +Timer sonar; + + // global variables int light = 0; // to keep track of lighting int sound = 1; // to keep track of sound -int sb_freq = 0.5; // rgb frequency +int sb_freq = 1; // rgb frequency int bright = 1; // time of the day // Light sensor -//AnalogIn photocell(p17); +AnalogIn photocell(p17); //PwmOut myled(LED1); - // Microphone class microphone { @@ -131,7 +141,7 @@ //microphone mymicrophone(p19); // uncomment when using // joystick -//Nav_Switch myNav(p14, p25, p24, p10, p9); //pin order on Sparkfun breakout +//Nav_Switch myNav(p25, p22, p23, p21, p24); //pin order on Sparkfun breakout // Shiftbrite void RGB_LED(int red, int green, int blue) @@ -151,8 +161,8 @@ 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 @@ -169,8 +179,8 @@ 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 } } @@ -179,8 +189,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!" @@ -197,8 +207,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 } } @@ -218,19 +228,35 @@ { // play siren while(true) { // thread loop - FILE * wave_file; + /*FILE * wave_file; wave_file = fopen("/sd/police-siren.wav","r"); - //if (play == 1) { + 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) { + } 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); + } + fclose(wave_file);*/ + //if (play == 1) { + for (int i=0; i<1000; i=i+2) { + speaker.period(1.0/969.0); + speaker = float(i/50.0); + wait(.5); + speaker.period(1.0/800.0); + wait(.5); + sound = 1; + if (play == 0) { + sound = 0; + //wait(1); + break; + } + // } + } + ///wait(1); Thread::wait(1000); // wait 1s } } @@ -238,9 +264,53 @@ // thread reading from - ultrasonic sensor to do XYZ void sonar_thread(void const *args) { + sonar.reset(); + sonar.start(); + while(echo==2) {} + //while (echo==2) {}; + //led2 = 0; +// stop timer + sonar.stop(); +// read timer + correction = sonar.read_us(); +// printf("Approximate software overhead timer delay is %d uS\n\r",correction); + +//Loop to read Sonar distance values, scale, and print + while(1) { +// trigger sonar to send a ping + trigger = 1; + //led1 = 1; + //led2 = 0; + sonar.reset(); + wait_us(10.0); + trigger = 0; + //led1 = 0; +//wait for echo high + while (echo==0) {}; + //led2=echo; +//echo high, so start timer + sonar.start(); +//wait for echo low + while (echo==1) {}; +//stop timer and read value + sonar.stop(); +//subtract software overhead timer delay and scale to cm + distance = (sonar.read_us()-correction)/58.0; + //led2 = 0; +// printf(" %d cm \n\r",distance); +//wait so that any echo(s) return before sending another ping + //wait(0.2); + if (distance < 10) { + play = 0; + } else { + play = 1; + } + Thread::wait(500); + } } + // thread reading from - tactile switch to control RGB/sound/lcd /* void switchthread(void const *args) @@ -251,23 +321,23 @@ //mbedleds = ~(myNav & 0x0F); //update leds with nav switch direction inputs FOR TESTING ONLY if (myNav.up()) play = 1; if (myNav.down()) play = 0; - if (sb_freq > 0.2) { - if (myNav.left()) sb_freq -= 0.1; + if (sb_freq > 0.5) { + if (myNav.left()) sb_freq -= 0.2; } - if (sb_freq < 1) { - if (myNav.right()) sb_freq += 0.1; + if (sb_freq < 3) { + if (myNav.right()) sb_freq += 0.2; } //if(myNav.fire()) mbedleds = 0x0F; //special all leds on case for fire (center button) //or use - if(myNav[4]==0) mbedleds = 0x0F; //can index a switch bit like this Thread::wait(200); } -} - +}*/ +/* // thread reading from - touch keypad same void touchpad_thread(void const *args) { while(true) { // thread loop - int key_code=0; + int key_code = 0; int i=0; int value=mpr121.read(0x00); value +=mpr121.read(0x01)<<8; @@ -275,22 +345,26 @@ i=0; // puts key number out to LEDs for demo for (i=0; i<12; i++) { - if (((value>>i)&0x01)==1) key_code=i+1; + if (((value>>i)&0x01)==1) key_code=i; } - if (key_code == 2) { + if (key_code == 6) { play = 1; - } else if (key_code == 8) { + } else if (key_code == 4) { play = 0; - } else if (key_code == 4) { - sb_freq -= 0.1; - } else if (key_code == 6) { - sb_freq += 0.1; + } else if (key_code == 1) { + if (sb_freq > 0.5) { + sb_freq += -0.2; + } + } else if (key_code == 9) { + if (sb_freq < 3) { + sb_freq += 0.2; + } } } } */ // thread reading from - light sensor to do something -/* + void lightsensor_thread(void const *args) { while(true) { // thread loop @@ -299,11 +373,11 @@ } else { bright = 1; } - wait(0.1); - Thread::wait(1000); // wait 0.25s + //wait(0.1); + Thread::wait(500); // wait 0.25s } } -*/ + //int mymic; /* // thread reading from - microphone to control rgb @@ -326,8 +400,8 @@ */ int main() { - //uLCD.cls(); - //wait(1); + uLCD.cls(); + wait(1); // shiftbrite stuff int red=0; @@ -339,8 +413,6 @@ latch=0; wait(2); - // draw police car base? - // t5 and t6 should not run at the same time // call threads here @@ -350,64 +422,83 @@ //Thread t4(sonar_thread); // start thread4 //Thread t5(switchthread); //start thread5 //Thread t6(touchpad_thread); //start thread6 - //Thread t7(lightsensor_thread); //start thread7 + Thread t7(lightsensor_thread); //start thread7 //Thread t8(mic_thread); //start thread8 + //t1.set_priority(osPriorityHigh); + //t2.set_priority(osPriorityHigh); + //t3.set_priority(osPriorityNormal); + //t5.set_priority(osPriorityHigh); + //t1.yield(); + //t2.yield(); + //t3.yield(); + //t5.yield(); + // running shiftbrite int color = 255; while(1) { - if (bright == 0) { - color = 150; - } else if (bright == 1) { - color = 255; - } + if (bright == 0) { + color = 150; + green = 100; + } else if (bright == 1) { + color = 255; + green = 0; + } if (sound == 0) { - RGB_LED(0,0,0); - Thread::wait(500); + RGB_LED(0,0,0); + Thread::wait(200); } else { - light = 0; - red = color; // flash red light - green = 0; - blue = 0; - RGB_LED(red, green, blue); - wait(0.5); + light = 0; + red = color; // flash red light + //green = 0; + blue = 0; + RGB_LED(red, green, blue); + wait(sb_freq); + //wait(0.5); - RGB_LED(0,0,0); - wait(0.5); + RGB_LED(0,0,0); + wait(sb_freq); + //wait(0.5); - light = 1; - red = color; // flash white light - green = color; - blue = color; - RGB_LED( red, green, blue); - wait(0.5); + light = 1; + red = color; // flash white light + //green = color; + blue = color; + RGB_LED( red, green, blue); + wait(sb_freq); + //wait(0.5); - RGB_LED(0,0,0); - wait(0.5); + RGB_LED(0,0,0); + wait(sb_freq); + //wait(0.5); - light = 2; - red = 0; // flash blue light - green = 0; - blue = color; - RGB_LED( red, green, blue); - wait(0.5); + light = 2; + red = 0; // flash blue light + //green = 0; + blue = color; + RGB_LED( red, green, blue); + wait(sb_freq); + //wait(0.5); - RGB_LED(0,0,0); - wait(0.5); + RGB_LED(0,0,0); + wait(sb_freq); + //wait(0.5); - light = 1; - red = color; // flash white light - green = color; - blue = color; - RGB_LED( red, green, blue); - wait(0.5); + light = 1; + red = color; // flash white light + //green = color; + blue = color; + RGB_LED( red, green, blue); + wait(sb_freq); + //wait(0.5); - RGB_LED(0,0,0); - wait(0.5); - Thread::wait(500); + RGB_LED(0,0,0); + wait(sb_freq); + //wait(0.5); + Thread::wait(500); } } }