Purpose: Simple application example with 2 threads and mutual-exclusion (Mutex) to a shared ressource (a bargraph).

Dependencies:   IHM_V2

/media/uploads/jacquesolivierklein/mbed-os-mutex.png

Purpose: Simple application example with 2 threads and mutual-exclusion (Mutex) to a shared ressource (a bargraph).

Target: L432KC / Nboard (from IUT-Cachan) Tested: YES (2019-01-09) Author: Jacques-Olivier Klein - IUT de CACHAN Date: 2018-02-10 rev. 2019-01-09 OS_STACK_SIZE=4096 Libraries: mbed-os rev5345:c966348(03jan2019) + IHMV2 (from IUT-Cachan) rev6:ad91067

Revision:
27:3780dd84a543
Parent:
22:af9dcf379926
Child:
29:8b329367eec8
--- a/main.cpp	Wed Feb 15 10:00:05 2017 +0000
+++ b/main.cpp	Sun Feb 11 08:54:54 2018 +0000
@@ -1,13 +1,66 @@
+// Title:  mbed-os-mutex
+// Author: Jacques-Olivier Klein - IUT de CACHAN
+// Date: 2018-02-10
+
 #include "mbed.h"
+#include "IHM.h"
+
+IHM ihm; 
 
-DigitalOut led1(LED1);
+DigitalOut L0 (PB_3) ;  // led L0
+DigitalOut L1 (PA_7) ;  // led L1
+DigitalOut L2 (PA_6) ;  // led L2
+
+void bargraph_up_counter();
+void bargraph_down_counter();
+
+Mutex bargraph_mutex; 
 
-// main() runs in its own thread in the OS
-// (note the calls to wait below for delays)
-int main() {
-    while (true) {
-        led1 = !led1;
-        wait(0.5);
+Thread Thread_bargraph_up_counter;
+Thread Thread_bargraph_down_counter;
+      
+ 
+int main(void) 
+{   ihm.LCD_clear();
+    ihm.LCD_printf("Mutex-%s %s",__DATE__,__TIME__);
+    printf("\n\rmbed-os-mutex-%s %s\n\r",__DATE__,__TIME__);
+    printf(" DEFAULT_STACK_SIZE:%d\n\r", DEFAULT_STACK_SIZE); 
+
+    Thread_bargraph_up_counter.start(bargraph_up_counter); 
+    Thread_bargraph_down_counter.start(bargraph_down_counter); 
+       
+    while(1){
+        Thread::wait(4000);  
+        L0=!L0;
+        printf("      [pid-%d]Main \n\r",Thread::gettid());
     }
 }
 
+void bargraph_up_counter(){
+    static int up_counter = 1; 
+    while(1){
+        bargraph_mutex.lock();
+         L1=1;       
+        for(up_counter = 1; up_counter != 0xFF; up_counter = (up_counter<<1)|1){
+            ihm.BAR_set(up_counter);
+            Thread::wait(200); 
+        }
+        bargraph_mutex.unlock();
+        L1=0;
+    }
+}
+
+void bargraph_down_counter(){
+    static int down_counter = 1; 
+    while(1){
+        bargraph_mutex.lock();
+        L2=1;
+        for(down_counter = 0x80; down_counter != 0xFF; 
+                down_counter = (down_counter>>1)|0x80){
+            ihm.BAR_set(down_counter);
+            Thread::wait(200); 
+        }
+        bargraph_mutex.unlock();
+        L2=0;
+    }
+}