Lab 3 Part 4

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

main.cpp

Committer:
glanier9
Date:
2021-02-25
Revision:
13:8987c75dfe41
Parent:
12:94b16c66c09a
Child:
14:ae63fb7c2d06

File content as of revision 13:8987c75dfe41:

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

InterruptIn data(p14);

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;

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;

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

/* Interrupt */
void set()
{
    char bnum;
    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;// Set new setting
        }
    }
}

/* Threads */
void led_thread()
{
    //char bnum = setting;
    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);
        }
    }
}

void speaker_thread()
{
    while(1) {
        FILE *wave_file;
        printf("\n\n\nHello, wave world!\n");
        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;

    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
                }
            }
        }

    }
    
    
    //data.rise(&set);

    thread1.start(led_thread);
    thread2.start(speaker_thread);
    thread3.start(lcd1_thread);
    thread4.start(lcd2_thread);

    while (true) {
    }
}