bluetooth

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

main.cpp

Committer:
mus3
Date:
19 months ago
Revision:
3:0bc0b28da20d
Parent:
2:3de25ad12458

File content as of revision 3:0bc0b28da20d:

#include "uLCD_4DGL.h"
#include "mbed.h"
#include "rtos.h"
#include "SongPlayer.h"

uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
AnalogOut DACout(p18);
DigitalOut myled(LED1);
PwmOut RGBLED_r(p23);
PwmOut RGBLED_g(p24);
PwmOut RGBLED_b(p25);
Serial blue(p13,p14);

float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
                 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
                };
float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
                     0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
                    };

// mutex to make the lcd lib thread safe
Mutex lcd_mutex;

// global variables
int i = 0;
int ix = 0;
int iy = 90;
char color = 'g';
int flight_dir = 8;

void drawairplane(int x, int y) {
    uLCD.filled_rectangle(x, y+7, x+27, y+13, WHITE);
    uLCD.filled_rectangle(x, y, x+4, y+6, BLUE);
    uLCD.filled_rectangle(x, y+14, x+4, y+20, BLUE);
    uLCD.filled_rectangle(x+16, y-6, x+20, y+6, BLUE);
    uLCD.filled_rectangle(x+16, y+14, x+20, y+26, BLUE);
    uLCD.filled_rectangle(x+20, y+9, x+27, y+11, BLUE);
    uLCD.filled_rectangle(x+15, y+9, x+17, y+11, BLUE);
    uLCD.filled_rectangle(x+10, y+9, x+12, y+11, BLUE);
    uLCD.filled_rectangle(x+5, y+9, x+7, y+11, BLUE);
}

// Thread 1
// RGB LED
void thread1(void const *args)
{
    while(true) {         // thread loop
        /*
        if (i < 10) {
            RGBLED_r = 0.0;
            RGBLED_g = (float) i / 10;
            RGBLED_b = 0.0;
        } else if (10 <= i && i < 20) {
            RGBLED_r = 0.0;
            RGBLED_g = (float) (20 - i) / 10;
            RGBLED_b = 0.0;
        } else if (20 <= i && i < 30) {
            RGBLED_r = (float) (i - 20) / 10;
            RGBLED_g = (float) (i - 20) / 10;
            RGBLED_b = (float) (i - 20) / 10;
        } else if (30 <= i && i < 40) {
            RGBLED_r = (float) (40 - i) / 10;
            RGBLED_g = (float) (40 - i) / 10;
            RGBLED_b = (float) (40 - i) / 10;
        } else {
            i = -1;
        }
        i++;
        */
        if (color == 'r') {
            RGBLED_r = 0.8;
            RGBLED_g = 0.0;
            RGBLED_b = 0.0;
        } else if (color == 'g') {
            RGBLED_r = 0.0;
            RGBLED_g = 0.8;
            RGBLED_b = 0.0;
        } else if (color == 'b') {
            RGBLED_r = 0.0;
            RGBLED_g = 0.0;
            RGBLED_b = 0.8;
        } else {
            RGBLED_r = 0.0;
            RGBLED_g = 0.8;
            RGBLED_b = 0.0;
        }
        Thread::wait(200);    // wait 0.2s
    }
}


void thread2(void const *args)
{
    while (true) {
        SongPlayer mySpeaker(p21);
        mySpeaker.PlaySong(note,duration);
        Thread::wait(6000);    // wait 6s
    }
}


void thread3(void const *args)
{
    while (true) {
        time_t seconds = time(NULL);
        lcd_mutex.lock();
        uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
        char buffer[32];
        strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
        uLCD.locate(0,0);
        uLCD.printf(buffer);
        lcd_mutex.unlock();
        Thread::wait(1000);    // wait 1s
    }
}

void thread4(void const *args)
{
    while (true) {
        lcd_mutex.lock();
        uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
        drawairplane(ix, iy);
        lcd_mutex.unlock();
        if (flight_dir == 8) {
            ix += 1;
        } else if (flight_dir == 7) {
            ix -= 1;
        } else if (flight_dir == 6) {
            iy += 1;
        } else if (flight_dir == 5) {
            iy -= 1;
        }
        if (ix > 100) {
            ix = 0;
        } else if (ix < 0) {
            ix = 99;
        } else if (iy > 113) {
            iy = 64;
        } else if (iy < 64) {
            iy = 113;
        }
        Thread::wait(500);    // wait 0.75s
    }
}


int main() {
    Thread t1(thread1); //start thread1
    Thread t2(thread2); //start thread2
    Thread t3(thread3); //start thread3
    Thread t4(thread4); //start thread3
    set_time(1256729737);
    while(1) {
        char bnum=0;
        char bhit=0;
        if (!blue.readable()) {
            continue;
        }
        if (blue.getc()=='!') {
            if (blue.getc()=='B') { //button data packet
                bnum = blue.getc(); //button number
                bhit = blue.getc(); //1=hit, 0=release
                if (blue.getc()==char(~('!' + 'B' + bnum + bhit))) { //checksum OK?
                    myled = bnum - '0'; //current button number will appear on LEDs
                    switch (bnum) {
                        case '1': //number button 1
                            if (bhit=='1') {
                                //add hit code here
                                color = 'r';
                            } //else {
                                //add release code here
                            //}
                            break;
                        case '2': //number button 2
                            if (bhit=='1') {
                                //add hit code here
                                color = 'g';
                            } //else {
                                //add release code here
                            //}
                            break;
                        case '3': //number button 3
                            if (bhit=='1') {
                                //add hit code here
                                color = 'b';
                            } //else {
                                //add release code here
                            //}
                            break;
                        case '4': //number button 4
                            if (bhit=='1') {
                                //add hit code here
                            } //else {
                                //add release code here
                            //}
                            break;
                        case '5': //button 5 up arrow
                            if (bhit=='1') {
                                //add hit code here
                                flight_dir = 5;
                            } //else {
                                //add release code here
                            //}
                            break;
                        case '6': //button 6 down arrow
                            if (bhit=='1') {
                                //add hit code here
                                flight_dir = 6;
                            } //else {
                                //add release code here
                            //}
                            break;
                        case '7': //button 7 left arrow
                            if (bhit=='1') {
                                //add hit code here
                                flight_dir = 7;
                            } //else {
                                //add release code here
                            //}
                            break;
                        case '8': //button 8 right arrow
                            if (bhit=='1') {
                                //add hit code here
                                flight_dir = 8;
                            } //else {
                                //add release code here
                            //}
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
    Thread::wait(200);    // wait 0.2s
}