update
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
Diff: Part2.cpp
- Revision:
- 1:3af15e979565
- Parent:
- 0:8af0a4200bd0
- Child:
- 2:d832b6c139d4
--- a/Part2.cpp Thu Oct 13 10:06:25 2016 +0000 +++ b/Part2.cpp Thu Oct 13 13:22:29 2016 +0000 @@ -7,20 +7,19 @@ // mutex to make the lcd lib thread safe Mutex lcd_mutex; -//SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card -//AnalogOut speaker(p26); -//wave_player waver(&speaker); -uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin; -//BusIn joy(p15,p12,p13,p16); -BusOut leds(LED1,LED2,LED3,LED4); -DigitalIn joyfire(p14); +SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card +AnalogOut speaker(p18); +wave_player waver(&speaker); +uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin +Semaphore lcd_sem(1); -// Shiftbrite stuff +// Shiftbrite DigitalOut latch(p15); DigitalOut enable(p16); SPI spi(p11, p12, p13); +// Touchpad // Create the interrupt receiver object on pin 26 InterruptIn interrupt(p26); // Setup the i2c bus on pins 9 and 10 @@ -29,7 +28,7 @@ // constructor(i2c object, i2c address of the mpr121) Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); - +// Test leds DigitalOut led1(LED1); DigitalOut led2(LED2); DigitalOut led3(LED3); @@ -38,14 +37,15 @@ // global variables int light = 0; // to keep track of lighting int sound = 0; // to keep track of sound -// led lights -//int red; -//int green; -//int blue; +int sb_freq = 0.5; // rgb frequency +int bright = 1; // time of the day -//AnalogIn photocell(p15); -PwmOut myled(LED1); +// Light sensor +AnalogIn photocell(p17); +//PwmOut myled(LED1); + +// Microphone class microphone { public : @@ -55,6 +55,7 @@ private : AnalogIn _pin; }; + microphone::microphone (PinName pin): _pin(pin) { @@ -70,8 +71,70 @@ return _pin.read(); } -//microphone mymicrophone(p16); + +// joystick +class Nav_Switch +{ +public: + Nav_Switch(PinName up,PinName down,PinName left,PinName right,PinName fire); + int read(); +//boolean functions to test each switch + bool up(); + bool down(); + bool left(); + bool right(); + bool fire(); +//automatic read on RHS + operator int (); +//index to any switch array style + bool operator[](int index) { + return _pins[index]; + }; +private: + BusIn _pins; +}; +Nav_Switch::Nav_Switch (PinName up,PinName down,PinName left,PinName right,PinName fire): + _pins(up, down, left, right, fire) +{ + _pins.mode(PullUp); //needed if pullups not on board or a bare nav switch is used - delete otherwise + wait(0.001); //delays just a bit for pullups to pull inputs high +} +inline bool Nav_Switch::up() +{ + return !(_pins[0]); +} +inline bool Nav_Switch::down() +{ + return !(_pins[1]); +} +inline bool Nav_Switch::left() +{ + return !(_pins[2]); +} +inline bool Nav_Switch::right() +{ + return !(_pins[3]); +} +inline bool Nav_Switch::fire() +{ + return !(_pins[4]); +} +inline int Nav_Switch::read() +{ + return _pins.read(); +} +inline Nav_Switch::operator int () +{ + return _pins.read(); +} + +microphone mymicrophone(p19); // uncomment when using + +// joystick +Nav_Switch myNav(p14, p25, p24, p10, p9); //pin order on Sparkfun breakout + +// Shiftbrite void RGB_LED(int red, int green, int blue) { unsigned int low_color=0; @@ -90,11 +153,12 @@ { while(true) { // thread loop lcd_mutex.lock(); + // lcd_sem.wait(); if (light == 0 && sound == 1) { // 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 ) { + } else if (light == 1 || sound == 0) { // 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); @@ -104,6 +168,7 @@ 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(); Thread::wait(5); // wait till thread is done } } @@ -113,6 +178,7 @@ { while(true) { // thread loop lcd_mutex.lock(); + // lcd_sem.wait(); if (sound == 1) { // some indication there is audio // text saying "!ALERT!" @@ -121,61 +187,75 @@ uLCD.set_font_size(7, 7); uLCD.printf("!ALERT !"); } else if (sound == 0) { - uLCD.color(0x000000); + //uLCD.color(0x000000); uLCD.locate(6,1); uLCD.set_font_size(7, 7); - uLCD.printf("!ALERT !"); + uLCD.textbackground_color(BLACK); + uLCD.printf(" "); // no indication // text removed - } lcd_mutex.unlock(); + // lcd_sem.release(); Thread::wait(5); // wait till thread is done } } +// thread playing video +void video_thread(void const *args) +{ + uLCD.media_init(); + uLCD.set_sector_address(0x001D, 0x4C42); + uLCD.display_video(0,0); +} + +int play = 1; // thread dealing with speaker - void speaker_thread(void const *args) { - -/* FILE *wave_file; + FILE *wave_file; wave_file = fopen("/sd/police_siren.wav","r"); // play siren while(true) { // thread loop - sound = 1; - //wait(5); - waver.play(wave_file); - - // generate a 500Hz tone using PWM hardware output - //speaker.period(1.0/500.0); // 500hz period - //speaker =0.5; //50% duty cycle - max volume - //wait(3); - //speaker=0.0; // turn off audio - //wait(2); - speaker = 0; - wait(5); + 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 + } Thread::wait(1000); // wait 1s - //speaker=0.0; // off } - //Thread::wait(100); // wait 1s - - fclose(wave_file);*/ + fclose(wave_file); } // thread reading from - ultrasonic sensor to do XYZ +void sonar_thread(void const *args) +{ +} // thread reading from - tactile switch to control RGB/sound/lcd void switchthread(void const *args) { - while(true) { // thread loop - /* if (joyfire) { - leds = 0xf; - } else { - leds = joy; - } - Thread::wait(1000); // wait 0.25s*/ + while(true) { + // control sound & RGB frequency + //with pullups a button hit is a "0" - "~" inverts data to leds + //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 < 1) { + if (myNav.right()) sb_freq += 0.1; + } + //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); } } @@ -193,11 +273,15 @@ for (i=0; i<12; i++) { if (((value>>i)&0x01)==1) key_code=i+1; } - led4=key_code & 0x01; - led3=(key_code>>1) & 0x01; - led2=(key_code>>2) & 0x01; - led1=(key_code>>3) & 0x01; - Thread::wait(1000); // wait 0.25s + if (key_code == 2) { + play = 1; + } else if (key_code == 8) { + play = 0; + } else if (key_code == 4) { + sb_freq -= 0.1; + } else if (key_code == 6) { + sb_freq += 0.1; + } } } @@ -205,30 +289,40 @@ void lightsensor_thread(void const *args) { while(true) { // thread loop - /* myled = photocell; - wait(0.1); - Thread::wait(1000); // wait 0.25s*/ + if (photocell*3.3 < 1.34) { + bright = 0; + } else { + bright = 1; + } + wait(0.1); + Thread::wait(1000); // wait 0.25s } } +int mymic; // thread reading from - microphone to control rgb void mic_thread(void const *args) { - /* while(true) { // thread loop - //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display - leds = int(abs((mymicrophone - (0.67/3.3)))*500.0); - //Use an 8kHz audio sample rate (phone quality audio); - wait(1.0/8000.0); - Thread::wait(1000); // wait 0.25s - }*/ + while(true) { // thread loop + //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display + mymic = int(abs((mymicrophone - (0.67/3.3)))*500.0); + //Use an 8kHz audio sample rate (phone quality audio); + if (mymic > 0.5) { + sound = 1; + //wait(7); // song is around 8 seconds long + } else { + sound = 0; + } + //wait(1.0/8000.0); + Thread::wait(1000); // wait 0.25s + } } int main() { - //uLCD.cls(); - wait(1); + uLCD.cls(); + // shiftbrite stuff - int red=0; int green=0; int blue=0; @@ -237,65 +331,73 @@ enable=0; latch=0; wait(2); - // draw police car base + + // draw police car base? + + // t5 and t6 should not run at the same time // call threads here Thread t1(LCD_thread1); //start thread1 Thread t2(LCD_thread2); //start thread2 - //Thread t4(speaker_thread); //start thread4 + //Thread t3(speaker_thread); //start thread3 + //Thread t4(sonar_thread); // start thread4 //Thread t5(switchthread); //start thread5 //Thread t6(touchpad_thread); //start thread6 //Thread t7(lightsensor_thread); //start thread7 //Thread t8(mic_thread); //start thread8 + // running shiftbrite + int color = 255; while(1) { + if (bright == 0) { + color = 150; + } else { + color = 255; + } if (sound == 0) { RGB_LED(0,0,0); - //Thread::wait(500); + Thread::wait(500); } else { light = 0; - red = 200; // flash red light + red = color; // flash red light green = 0; blue = 0; RGB_LED(red, green, blue); - wait(0.5); + wait(sb_freq); RGB_LED(0,0,0); - wait(0.5); + wait(sb_freq); light = 1; - red = 200; // flash white light - green = 200; - blue = 200; + red = color; // flash white light + green = color; + blue = color; RGB_LED( red, green, blue); - wait(0.5); + wait(sb_freq); RGB_LED(0,0,0); - wait(0.5); + wait(sb_freq); light = 2; red = 0; // flash blue light green = 0; - blue = 200; + blue = color; RGB_LED( red, green, blue); - wait(0.5); + wait(sb_freq); RGB_LED(0,0,0); - wait(0.5); + wait(sb_freq); light = 1; - red = 200; // flash white light - green = 200; - blue = 200; + red = color; // flash white light + green = color; + blue = color; RGB_LED( red, green, blue); - wait(0.5); + wait(sb_freq); RGB_LED(0,0,0); + Thread::wait(500); } - Thread::wait(500); } - - - }