LED with mutex protect

Dependencies:   C12832 LM75B mbed-rtos mbed

Fork of Case_Study_rtos_basic_Mutex_LED by cathal deehy-power

main.cpp

Committer:
cathal66
Date:
2015-02-13
Revision:
13:95aecf2156ed
Parent:
9:b4346bf47dd7

File content as of revision 13:95aecf2156ed:

#include "mbed.h"                       //Include the mbed.h file   
#include "rtos.h"                       //include the RTOS.h file
#include "LM75B.h"
#include "C12832.h"

C12832 lcd(p5, p7, p6, p8, p11);

Serial pc(USBTX,USBRX);

PwmOut LED_red(p23);                    //PWM LED Red
PwmOut LED_blue(p25);                   //PWM LED Blue

DigitalOut led1(LED1);                  //Setup LED1 to the varible name led1
DigitalOut led2(LED2);                  //Setup LED2 to the varible name led2

Mutex LED_B_Mutex;                      //Mutex protect LED_B
Mutex LED_R_Mutex;                      //Mutex protect LED_R
 
void led_dim(void const *args) {        //Function or the thread to be called
    while (true) {                      //Super loop
        LED_R_Mutex.lock();             //Mutex Protect LED_R
        LED_B_Mutex.lock();             //Mutex Protect LED_B
        LED_red = 0.5;                  //Turn on LED and dimm half  
        LED_blue = 1;                   //Turn off LED              
        Thread::wait(800);              //Thread wait
        LED_R_Mutex.unlock();           //Mutex Unlock LED_R  
        LED_B_Mutex.unlock();           //Mutex Unlock LED_R                      
 
        LED_R_Mutex.lock();             //Mutex Protect LED_R
        LED_B_Mutex.lock();             //Mutex Protect LED_B
        LED_red = 1;                    //Turn off LED  
        LED_blue = 0.5;                 //Turn on LED and dimm half 
        Thread::wait(800);              //Thread wait
        LED_R_Mutex.unlock();           //Mutex Unlock LED_R    
        LED_B_Mutex.unlock();           //Mutex Unlock LED_B               
    }                                   //End Super loop
}  
 
void led_R_B_flash(void const *args) {   //Function or the thread to be called
    while (true) {                      //Super loop
    
        LED_R_Mutex.lock();             //Mutex Protect LED_R
        LED_B_Mutex.lock();             //Mutex Protect LED_B
        LED_red = 0;                    //Turn on LED 
        LED_blue = 1;                   //Turn off LED  
        Thread::wait(800);              //Thread wait
        LED_R_Mutex.unlock();           //Mutex Unlock LED_R    
        LED_B_Mutex.unlock();           //Mutex Unlock LED_B                      
 
        LED_R_Mutex.lock();             //Mutex Protect LED_R
        LED_B_Mutex.lock();             //Mutex Protect LED_B
        LED_red = 1;                    //Turn off LED  
        LED_blue = 0;                   //Turn on LED 
        Thread::wait(800);              //Thread wait
        LED_R_Mutex.unlock();           //Mutex Unlock LED_R    
        LED_B_Mutex.unlock();           //Mutex Unlock LED_B                
    }                                   //End Super loop
}                                       //End Function / thread
 
int main() {                            //Main
    Thread thread_LED_Flash(led_R_B_flash);     //Start thread     
    Thread thread_LED_dim(led_dim);             //Start thread    
    
        while (1) {

            printf("Hello_World\r\n");  //printf hello world over serial        
            Thread::wait(5000);         //Thread wait
        }

    
}                                       //End main