LED with mutex protect

Dependencies:   C12832 LM75B mbed-rtos mbed

Fork of Case_Study_rtos_basic_Mutex_LED by cathal deehy-power

Committer:
cathal66
Date:
Fri Feb 13 14:22:19 2015 +0000
Revision:
13:95aecf2156ed
Parent:
9:b4346bf47dd7
Mutex protection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cathal66 7:10edddb7f1a8 1 #include "mbed.h" //Include the mbed.h file
cathal66 7:10edddb7f1a8 2 #include "rtos.h" //include the RTOS.h file
cathal66 8:0f773a5937f0 3 #include "LM75B.h"
cathal66 8:0f773a5937f0 4 #include "C12832.h"
cathal66 8:0f773a5937f0 5
cathal66 8:0f773a5937f0 6 C12832 lcd(p5, p7, p6, p8, p11);
cathal66 8:0f773a5937f0 7
cathal66 8:0f773a5937f0 8 Serial pc(USBTX,USBRX);
cathal66 8:0f773a5937f0 9
cathal66 13:95aecf2156ed 10 PwmOut LED_red(p23); //PWM LED Red
cathal66 13:95aecf2156ed 11 PwmOut LED_blue(p25); //PWM LED Blue
cathal66 8:0f773a5937f0 12
cathal66 7:10edddb7f1a8 13 DigitalOut led1(LED1); //Setup LED1 to the varible name led1
cathal66 7:10edddb7f1a8 14 DigitalOut led2(LED2); //Setup LED2 to the varible name led2
cathal66 8:0f773a5937f0 15
cathal66 13:95aecf2156ed 16 Mutex LED_B_Mutex; //Mutex protect LED_B
cathal66 13:95aecf2156ed 17 Mutex LED_R_Mutex; //Mutex protect LED_R
cathal66 8:0f773a5937f0 18
cathal66 13:95aecf2156ed 19 void led_dim(void const *args) { //Function or the thread to be called
cathal66 9:b4346bf47dd7 20 while (true) { //Super loop
cathal66 13:95aecf2156ed 21 LED_R_Mutex.lock(); //Mutex Protect LED_R
cathal66 13:95aecf2156ed 22 LED_B_Mutex.lock(); //Mutex Protect LED_B
cathal66 13:95aecf2156ed 23 LED_red = 0.5; //Turn on LED and dimm half
cathal66 13:95aecf2156ed 24 LED_blue = 1; //Turn off LED
cathal66 13:95aecf2156ed 25 Thread::wait(800); //Thread wait
cathal66 13:95aecf2156ed 26 LED_R_Mutex.unlock(); //Mutex Unlock LED_R
cathal66 13:95aecf2156ed 27 LED_B_Mutex.unlock(); //Mutex Unlock LED_R
cathal66 8:0f773a5937f0 28
cathal66 13:95aecf2156ed 29 LED_R_Mutex.lock(); //Mutex Protect LED_R
cathal66 13:95aecf2156ed 30 LED_B_Mutex.lock(); //Mutex Protect LED_B
cathal66 13:95aecf2156ed 31 LED_red = 1; //Turn off LED
cathal66 13:95aecf2156ed 32 LED_blue = 0.5; //Turn on LED and dimm half
cathal66 13:95aecf2156ed 33 Thread::wait(800); //Thread wait
cathal66 13:95aecf2156ed 34 LED_R_Mutex.unlock(); //Mutex Unlock LED_R
cathal66 13:95aecf2156ed 35 LED_B_Mutex.unlock(); //Mutex Unlock LED_B
cathal66 8:0f773a5937f0 36 } //End Super loop
cathal66 8:0f773a5937f0 37 }
cathal66 8:0f773a5937f0 38
cathal66 8:0f773a5937f0 39 void led_R_B_flash(void const *args) { //Function or the thread to be called
cathal66 8:0f773a5937f0 40 while (true) { //Super loop
cathal66 8:0f773a5937f0 41
cathal66 13:95aecf2156ed 42 LED_R_Mutex.lock(); //Mutex Protect LED_R
cathal66 13:95aecf2156ed 43 LED_B_Mutex.lock(); //Mutex Protect LED_B
cathal66 13:95aecf2156ed 44 LED_red = 0; //Turn on LED
cathal66 13:95aecf2156ed 45 LED_blue = 1; //Turn off LED
cathal66 13:95aecf2156ed 46 Thread::wait(800); //Thread wait
cathal66 13:95aecf2156ed 47 LED_R_Mutex.unlock(); //Mutex Unlock LED_R
cathal66 13:95aecf2156ed 48 LED_B_Mutex.unlock(); //Mutex Unlock LED_B
cathal66 9:b4346bf47dd7 49
cathal66 13:95aecf2156ed 50 LED_R_Mutex.lock(); //Mutex Protect LED_R
cathal66 13:95aecf2156ed 51 LED_B_Mutex.lock(); //Mutex Protect LED_B
cathal66 13:95aecf2156ed 52 LED_red = 1; //Turn off LED
cathal66 13:95aecf2156ed 53 LED_blue = 0; //Turn on LED
cathal66 13:95aecf2156ed 54 Thread::wait(800); //Thread wait
cathal66 13:95aecf2156ed 55 LED_R_Mutex.unlock(); //Mutex Unlock LED_R
cathal66 13:95aecf2156ed 56 LED_B_Mutex.unlock(); //Mutex Unlock LED_B
cathal66 7:10edddb7f1a8 57 } //End Super loop
cathal66 7:10edddb7f1a8 58 } //End Function / thread
emilmont 1:491820ee784d 59
cathal66 7:10edddb7f1a8 60 int main() { //Main
cathal66 13:95aecf2156ed 61 Thread thread_LED_Flash(led_R_B_flash); //Start thread
cathal66 13:95aecf2156ed 62 Thread thread_LED_dim(led_dim); //Start thread
emilmont 1:491820ee784d 63
cathal66 8:0f773a5937f0 64 while (1) {
cathal66 9:b4346bf47dd7 65
cathal66 13:95aecf2156ed 66 printf("Hello_World\r\n"); //printf hello world over serial
cathal66 13:95aecf2156ed 67 Thread::wait(5000); //Thread wait
cathal66 8:0f773a5937f0 68 }
cathal66 8:0f773a5937f0 69
cathal66 8:0f773a5937f0 70
cathal66 7:10edddb7f1a8 71 } //End main