update

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

Fork of Lab3 by Azra Ismail

Part2.cpp

Committer:
aismail1997
Date:
2016-10-13
Revision:
3:2adc8522c5e8
Parent:
2:d832b6c139d4
Child:
4:7ec6407f673f

File content as of revision 3:2adc8522c5e8:

#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(p18);
wave_player waver(&speaker);
uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin
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);
// 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);

// Test leds
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

// 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 bright = 1; // time of the day

// Light sensor
//AnalogIn photocell(p17);
//PwmOut myled(LED1);


// Microphone
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();
}


// 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;
    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();
        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) {
            // 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) {
            // 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();
        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();
        lcd_sem.wait();
        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.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)
{
    // play siren
    while(true) {         // thread loop
        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
    }
}

// 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) {
        // 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);
    }
}

// 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;
        }
        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;
        }
    }
}
*/
// thread reading from - light sensor to do something
/*
void lightsensor_thread(void const *args)
{
    while(true) {         // thread loop
        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
        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);

    // 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?

    // 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 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 if (bright == 1) {
            color = 255;
        }

        if (sound == 0) {
        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(0.5);

        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(0.5);

        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(0.5);

        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(0.5);

        RGB_LED(0,0,0);
        wait(0.5);
        Thread::wait(500);
        }
    }
}