LED with mutex protect

Dependencies:   C12832 LM75B mbed-rtos mbed

Fork of Case_Study_rtos_basic by cathal deehy-power

Committer:
cathal66
Date:
Fri Feb 13 00:23:28 2015 +0000
Revision:
10:8635af078e7c
Parent:
9:b4346bf47dd7
Child:
11:530ae198e5f2
Race condition

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 8:0f773a5937f0 10 PwmOut LED_red(p23);
cathal66 8:0f773a5937f0 11 PwmOut LED_blue(p25);
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 9:b4346bf47dd7 16 Mutex LED_B_Mutex;
cathal66 9:b4346bf47dd7 17 Mutex LED_R_Mutex;
cathal66 8:0f773a5937f0 18
cathal66 9:b4346bf47dd7 19 void led_dim(void const *args) { //Function or the thread to be called
cathal66 9:b4346bf47dd7 20 while (true) { //Super loop
cathal66 10:8635af078e7c 21 LED_B_Mutex.lock();
cathal66 9:b4346bf47dd7 22 LED_R_Mutex.lock();
cathal66 10:8635af078e7c 23 LED_red = 1;
cathal66 10:8635af078e7c 24 LED_blue = 0.5;
cathal66 10:8635af078e7c 25 Thread::wait(800);
cathal66 10:8635af078e7c 26 LED_B_Mutex.unlock();
cathal66 10:8635af078e7c 27 LED_R_Mutex.unlock();
cathal66 10:8635af078e7c 28
cathal66 9:b4346bf47dd7 29 LED_B_Mutex.lock();
cathal66 10:8635af078e7c 30 LED_R_Mutex.lock();
cathal66 9:b4346bf47dd7 31 LED_red = 0.5;
cathal66 9:b4346bf47dd7 32 LED_blue = 1;
cathal66 9:b4346bf47dd7 33 Thread::wait(800);
cathal66 10:8635af078e7c 34 LED_B_Mutex.unlock();
cathal66 10:8635af078e7c 35 LED_R_Mutex.unlock();
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 9:b4346bf47dd7 42 LED_R_Mutex.lock();
cathal66 9:b4346bf47dd7 43 LED_B_Mutex.lock();
cathal66 9:b4346bf47dd7 44 LED_red = 0;
cathal66 9:b4346bf47dd7 45 LED_blue = 1;
cathal66 9:b4346bf47dd7 46 Thread::wait(800);
cathal66 9:b4346bf47dd7 47 LED_R_Mutex.unlock();
cathal66 9:b4346bf47dd7 48 LED_B_Mutex.unlock();
cathal66 9:b4346bf47dd7 49
cathal66 9:b4346bf47dd7 50 LED_R_Mutex.lock();
cathal66 9:b4346bf47dd7 51 LED_B_Mutex.lock();
cathal66 9:b4346bf47dd7 52 LED_red = 1;
cathal66 9:b4346bf47dd7 53 LED_blue = 0;
cathal66 9:b4346bf47dd7 54 Thread::wait(800);
cathal66 9:b4346bf47dd7 55 LED_R_Mutex.unlock();
cathal66 9:b4346bf47dd7 56 LED_B_Mutex.unlock();
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 8:0f773a5937f0 61 Thread thread_LED_Flash(led_R_B_flash);
cathal66 9:b4346bf47dd7 62 Thread thread_LED_dim(led_dim);
emilmont 1:491820ee784d 63
cathal66 8:0f773a5937f0 64 while (1) {
cathal66 9:b4346bf47dd7 65
cathal66 9:b4346bf47dd7 66 printf("Hello_World\r\n");
cathal66 9:b4346bf47dd7 67 Thread::wait(5000);
cathal66 8:0f773a5937f0 68 }
cathal66 8:0f773a5937f0 69
cathal66 8:0f773a5937f0 70
cathal66 7:10edddb7f1a8 71 } //End main