update
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed wave_player
Fork of Lab3 by
Diff: Part2.cpp
- Revision:
- 0:8af0a4200bd0
- Child:
- 1:3af15e979565
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Part2.cpp Thu Oct 13 10:06:25 2016 +0000 @@ -0,0 +1,301 @@ +#include "mbed.h" +#include "rtos.h" +#include "uLCD_4DGL.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(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); + +// Shiftbrite stuff +DigitalOut latch(p15); +DigitalOut enable(p16); + +SPI spi(p11, p12, p13); + +// Create the interrupt receiver object on pin 26 +InterruptIn interrupt(p26); +// Setup the i2c bus on pins 9 and 10 +I2C i2c(p9, p10); +// Setup the Mpr121: +// constructor(i2c object, i2c address of the mpr121) +Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); + + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +// 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; + +//AnalogIn photocell(p15); +PwmOut myled(LED1); + +class microphone +{ +public : + microphone(PinName pin); + float read(); + operator float (); +private : + AnalogIn _pin; +}; +microphone::microphone (PinName pin): + _pin(pin) +{ +} + +float microphone::read() +{ + return _pin.read(); +} + +inline microphone::operator float () +{ + return _pin.read(); +} + +//microphone mymicrophone(p16); + +void RGB_LED(int red, int green, int blue) +{ + unsigned int low_color=0; + unsigned int high_color=0; + high_color=(blue<<4)|((red&0x3C0)>>6); + low_color=(((red&0x3F)<<10)|(green)); + spi.write(high_color); + spi.write(low_color); + latch=1; + latch=0; +} + +// create threads +// first thread dealing with LCD +void LCD_thread1(void const *args) +{ + while(true) { // thread loop + lcd_mutex.lock(); + 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 ) { + // 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) { + // 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(); + Thread::wait(5); // wait till thread is done + } +} + +// second thread dealing with LCD +void LCD_thread2(void const *args) +{ + while(true) { // thread loop + lcd_mutex.lock(); + if (sound == 1) { + // some indication there is audio + // text saying "!ALERT!" + uLCD.color(0xFF0000); + uLCD.locate(6,1); + uLCD.set_font_size(7, 7); + uLCD.printf("!ALERT !"); + } else if (sound == 0) { + uLCD.color(0x000000); + uLCD.locate(6,1); + uLCD.set_font_size(7, 7); + uLCD.printf("!ALERT !"); + // no indication + // text removed + + } + lcd_mutex.unlock(); + Thread::wait(5); // wait till thread is done + } +} + +// 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 + 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); + Thread::wait(1000); // wait 1s + //speaker=0.0; // off + } + //Thread::wait(100); // wait 1s + + fclose(wave_file);*/ +} + +// thread reading from - ultrasonic sensor to do XYZ + + +// 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*/ + } +} + +// thread reading from - touch keypad same +void touchpad_thread(void const *args) +{ + while(true) { // thread loop + int key_code=0; + int i=0; + int value=mpr121.read(0x00); + value +=mpr121.read(0x01)<<8; + // LED demo mod + 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; + } + 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 + } +} + +// thread reading from - light sensor to do something +void lightsensor_thread(void const *args) +{ + while(true) { // thread loop + /* myled = photocell; + wait(0.1); + Thread::wait(1000); // wait 0.25s*/ + } +} + +// 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 + }*/ +} + +int main() +{ + //uLCD.cls(); + wait(1); + // shiftbrite stuff + + int red=0; + int green=0; + int blue=0; + spi.format(16,0); + spi.frequency(500000); + enable=0; + latch=0; + wait(2); + // draw police car base + + // call threads here + Thread t1(LCD_thread1); //start thread1 + Thread t2(LCD_thread2); //start thread2 + //Thread t4(speaker_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 + + while(1) { + + if (sound == 0) { + RGB_LED(0,0,0); + //Thread::wait(500); + } else { + light = 0; + red = 200; // flash red light + green = 0; + blue = 0; + RGB_LED(red, green, blue); + wait(0.5); + + RGB_LED(0,0,0); + wait(0.5); + + light = 1; + red = 200; // flash white light + green = 200; + blue = 200; + RGB_LED( red, green, blue); + wait(0.5); + + RGB_LED(0,0,0); + wait(0.5); + + light = 2; + red = 0; // flash blue light + green = 0; + blue = 200; + RGB_LED( red, green, blue); + wait(0.5); + + RGB_LED(0,0,0); + wait(0.5); + + light = 1; + red = 200; // flash white light + green = 200; + blue = 200; + RGB_LED( red, green, blue); + wait(0.5); + + RGB_LED(0,0,0); + } + Thread::wait(500); + } + + + +}