Lab 3 Part 4

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

main.cpp

Committer:
glanier9
Date:
2021-02-25
Revision:
14:ae63fb7c2d06
Parent:
13:8987c75dfe41
Child:
15:e406620ee8af

File content as of revision 14:ae63fb7c2d06:

#include "mbed.h"
#include "rtos.h"
#include "Speaker.h"
#include "uLCD_4DGL.h"
#include "SDFileSystem.h"
#include "wave_player.h"

char setting = '1';

BusOut myled(LED1,LED2,LED3,LED4);

Serial blue(p13,p14);

AnalogOut DACout(p18);

wave_player waver(&DACout);

SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card

Mutex stdio_mutex;
Mutex get_mutex;

uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;

PwmOut  rLED(p21);  // Light up Red LED with p21
PwmOut  gLED(p22);  // Light up Green LED with p22
PwmOut  bLED(p23);  // Light up Blue LED with p23

Thread thread1;
Thread thread2;
Thread thread3;
Thread thread4;

Semaphore sem1(0);
Semaphore sem2(0);

///* The Terminator 53. Coming to theatres this summer!!!!!! */
//void the_terminator()
//{
//
//}


/* Function Calls */
void top_red_box()
{
    stdio_mutex.lock();
    uLCD.filled_rectangle(0, 0, 127, 63, RED);
    stdio_mutex.unlock();
}
void bot_blue_box()
{
    stdio_mutex.lock();
    uLCD.filled_rectangle(0, 63, 127, 127, BLUE);
    stdio_mutex.unlock();
}
void top_black_box()
{
    stdio_mutex.lock();
    uLCD.filled_rectangle(0, 0, 127, 63, BLACK);
    stdio_mutex.unlock();

}
void bot_black_box()
{
    stdio_mutex.lock();
    uLCD.filled_rectangle(0, 63, 127, 127, BLACK);
    stdio_mutex.unlock();
}


/* Threads */
void led_thread()
{
    while(1) {
        if(setting == '1') {
            bLED = 0;
            rLED = 1;
            gLED = 0;
            Thread::wait(750);
            rLED = 0;
            bLED = 1;
            gLED = 0;
            Thread::wait(750);
        }
        if (setting == '2') {
            bLED = 1;
            rLED = 1;
            gLED = 0;
            Thread::wait(750);
            rLED = 0;
            bLED = 0;
            gLED = 1;
            Thread::wait(750);
        }
        if (blue.readable() && blue.getc()=='!') {
            if (blue.getc()=='B') { //button data
                char bnum = blue.getc(); //button number
                if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4
                    setting = bnum;// Change setting based on button input to set start setting. Check for chars
                    rLED = 0;
                    gLED = 0;
                    bLED = 0;
                    myled[bnum-'1']=blue.getc()-'0'; //turn on/off that num LED
                    wait(1);
                    break;
                }
            }
        }
    }
}

void speaker_thread()
{
    while(1) {
        FILE *wave_file;
        wave_file=fopen("/sd/WEEWOO.wav","r");
        waver.play(wave_file);
        fclose(wave_file);
    }
}

void lcd1_thread()
{
    while(1) {
        top_red_box();
        Thread::wait(750);
        top_black_box();
        Thread::wait(750);
    }
}

void lcd2_thread()
{
    while(1) {
        bot_black_box();
        Thread::wait(750);
        bot_blue_box();
        Thread::wait(750);
    }
}

int main()
{
    rLED = 0;
    gLED = 0;
    bLED = 0;

    /* Wait for user to press a setting button */
    char bnum=0;
    while(1) {
        if (blue.readable() && blue.getc()=='!') {
            if (blue.getc()=='B') { //button data
                bnum = blue.getc(); //button number
                if ((bnum>='1')&&(bnum<='4')) { //is a number button 1..4
                    setting = bnum;// Change setting based on button input to set start setting. Check for chars
                    break; //turn on/off that num LED
                }
            }
        }
    }

    /* Start all threads */
    thread1.start(led_thread);
    thread2.start(speaker_thread);
    thread3.start(lcd1_thread);
    thread4.start(lcd2_thread);

    /* Infinite Loop, let threads run */
    while (true) {
        ///* Terminate all thread processes */
//        thread1.join();
//
//        thread2.terminate();
//        thread3.terminate();
//        thread4.terminate();
//
//        /* Start all thread processes again, this time with new settings */
//        thread1.start(led_thread);
//        thread2.start(speaker_thread);
//        thread3.start(lcd1_thread);
//        thread4.start(lcd2_thread);
    }
}