Running multiple threads on mbed using RTOS

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

main.cpp

Committer:
nliu96
Date:
2016-02-28
Revision:
3:7d53a2744a2b
Parent:
2:f4ad69c44c7e
Child:
4:5fdadaef5b1f

File content as of revision 3:7d53a2744a2b:

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

SDFileSystem sd(p5, p6, p7, p8, "sd"); 
Serial pc(USBTX, USBRX);
Mutex ulcd_mutex;
uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
DigitalOut myled(LED1);
DigitalOut latch(p15);
DigitalOut enable(p17);
//Analog Out Jack
AnalogOut DACout(p18);
//On Board Speaker
PwmOut PWMout(p21);
SPI spi(p5, p6, p7);
wave_player waver(&DACout,&PWMout);
int playing = 1;


void ulcd2_thread(void const *args) {
    int count = 0;
    while(true) {
        ulcd_mutex.lock();
        pc.printf("Activated mutex lock on uLCD screen: ulcd2_thread");
        uLCD.cls();
        uLCD.printf("\nECE 4180 L3: RTOS\n"); //Default Green on black text
        uLCD.printf("\nWes Schon Nick Liu");
        uLCD.printf("\n\nThread 2 count: %d", count);
        if (playing == 1)
            uLCD.printf("\n\nPlaying wav file...");
        else
            uLCD.printf("\n\nWav file finished.");    
        uLCD.text_width(4); //4X size text
        uLCD.text_height(4);
        Thread::wait(3000);
        ulcd_mutex.unlock();
        count++;
        }
    }
void wav_thread(void const *args) {
    USBHostMSD msd("usb");
    FILE *wave_file;
    //setup PWM hardware for a Class D style audio output
    PWMout.period(1.0/400000.0);
    //printf("\n\n\nHello, wave world!\n");
    // wait until connected to a USB device
    while(!msd.connect()) {
        Thread::wait(500);
    }
    //open wav file and play it
    wave_file=fopen("/usb/sample.wav","r");
    waver.play(wave_file);
    fclose(wave_file);
    //end of program
    Thread::wait(500);
    playing = 0;
    }

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


void led_thread(void const *args) {
    int red=0;
    int green=0;
    int blue=0;
    spi.format(16,0);
    spi.frequency(500000);
    enable=0;
    latch=0;
    wait(2);
    for (red = 0; red<50; red = red+10) {
        for (blue = 0; blue<50; blue = blue+10) {
            for (green = 0; green<50; green = green+10)

            {
                RGB_LED( red, green, blue);
                wait(.25);
            }
        }
    }

    }

int main() {
    Thread thread(ulcd2_thread);
//    Thread thread2(wav_thread);
    Thread thread3(led_thread);
    int count2 = 0;
    while(1) {
        myled = 1;
        Thread::wait(200);
        myled = 0;
        Thread::wait(200);
        ulcd_mutex.lock();
        uLCD.cls();
    //SD card needed with image and video files for last two demos
        uLCD.cls();
        uLCD.media_init();
        uLCD.printf("\n\nThread 1 count: %d", count2);
        if (playing == 1)
            uLCD.printf("\n\nPlaying wav file...");
        else
            uLCD.printf("\n\nWav file finished.");  
        Thread::wait(200);
        uLCD.set_sector_address(0x0000, 0x0000);
        uLCD.display_video(0,0);
        Thread::wait(100);
        ulcd_mutex.unlock();
        count2++;
    }
}