Purpose: Simple application example with 2 threads and mutual-exclusion (Mutex) to a shared ressource (a bargraph).
Dependencies: IHM_V2
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
main.cpp@27:3780dd84a543, 2018-02-11 (annotated)
- Committer:
- jacquesolivierklein
- Date:
- Sun Feb 11 08:54:54 2018 +0000
- Revision:
- 27:3780dd84a543
- Parent:
- 22:af9dcf379926
- Child:
- 29:8b329367eec8
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jacquesolivierklein | 27:3780dd84a543 | 1 | // Title: mbed-os-mutex |
jacquesolivierklein | 27:3780dd84a543 | 2 | // Author: Jacques-Olivier Klein - IUT de CACHAN |
jacquesolivierklein | 27:3780dd84a543 | 3 | // Date: 2018-02-10 |
jacquesolivierklein | 27:3780dd84a543 | 4 | |
Jonathan Austin |
0:2757d7abb7d9 | 5 | #include "mbed.h" |
jacquesolivierklein | 27:3780dd84a543 | 6 | #include "IHM.h" |
jacquesolivierklein | 27:3780dd84a543 | 7 | |
jacquesolivierklein | 27:3780dd84a543 | 8 | IHM ihm; |
Jonathan Austin |
0:2757d7abb7d9 | 9 | |
jacquesolivierklein | 27:3780dd84a543 | 10 | DigitalOut L0 (PB_3) ; // led L0 |
jacquesolivierklein | 27:3780dd84a543 | 11 | DigitalOut L1 (PA_7) ; // led L1 |
jacquesolivierklein | 27:3780dd84a543 | 12 | DigitalOut L2 (PA_6) ; // led L2 |
jacquesolivierklein | 27:3780dd84a543 | 13 | |
jacquesolivierklein | 27:3780dd84a543 | 14 | void bargraph_up_counter(); |
jacquesolivierklein | 27:3780dd84a543 | 15 | void bargraph_down_counter(); |
jacquesolivierklein | 27:3780dd84a543 | 16 | |
jacquesolivierklein | 27:3780dd84a543 | 17 | Mutex bargraph_mutex; |
Jonathan Austin |
0:2757d7abb7d9 | 18 | |
jacquesolivierklein | 27:3780dd84a543 | 19 | Thread Thread_bargraph_up_counter; |
jacquesolivierklein | 27:3780dd84a543 | 20 | Thread Thread_bargraph_down_counter; |
jacquesolivierklein | 27:3780dd84a543 | 21 | |
jacquesolivierklein | 27:3780dd84a543 | 22 | |
jacquesolivierklein | 27:3780dd84a543 | 23 | int main(void) |
jacquesolivierklein | 27:3780dd84a543 | 24 | { ihm.LCD_clear(); |
jacquesolivierklein | 27:3780dd84a543 | 25 | ihm.LCD_printf("Mutex-%s %s",__DATE__,__TIME__); |
jacquesolivierklein | 27:3780dd84a543 | 26 | printf("\n\rmbed-os-mutex-%s %s\n\r",__DATE__,__TIME__); |
jacquesolivierklein | 27:3780dd84a543 | 27 | printf(" DEFAULT_STACK_SIZE:%d\n\r", DEFAULT_STACK_SIZE); |
jacquesolivierklein | 27:3780dd84a543 | 28 | |
jacquesolivierklein | 27:3780dd84a543 | 29 | Thread_bargraph_up_counter.start(bargraph_up_counter); |
jacquesolivierklein | 27:3780dd84a543 | 30 | Thread_bargraph_down_counter.start(bargraph_down_counter); |
jacquesolivierklein | 27:3780dd84a543 | 31 | |
jacquesolivierklein | 27:3780dd84a543 | 32 | while(1){ |
jacquesolivierklein | 27:3780dd84a543 | 33 | Thread::wait(4000); |
jacquesolivierklein | 27:3780dd84a543 | 34 | L0=!L0; |
jacquesolivierklein | 27:3780dd84a543 | 35 | printf(" [pid-%d]Main \n\r",Thread::gettid()); |
Jonathan Austin |
0:2757d7abb7d9 | 36 | } |
Jonathan Austin |
0:2757d7abb7d9 | 37 | } |
Jonathan Austin |
1:846c97078558 | 38 | |
jacquesolivierklein | 27:3780dd84a543 | 39 | void bargraph_up_counter(){ |
jacquesolivierklein | 27:3780dd84a543 | 40 | static int up_counter = 1; |
jacquesolivierklein | 27:3780dd84a543 | 41 | while(1){ |
jacquesolivierklein | 27:3780dd84a543 | 42 | bargraph_mutex.lock(); |
jacquesolivierklein | 27:3780dd84a543 | 43 | L1=1; |
jacquesolivierklein | 27:3780dd84a543 | 44 | for(up_counter = 1; up_counter != 0xFF; up_counter = (up_counter<<1)|1){ |
jacquesolivierklein | 27:3780dd84a543 | 45 | ihm.BAR_set(up_counter); |
jacquesolivierklein | 27:3780dd84a543 | 46 | Thread::wait(200); |
jacquesolivierklein | 27:3780dd84a543 | 47 | } |
jacquesolivierklein | 27:3780dd84a543 | 48 | bargraph_mutex.unlock(); |
jacquesolivierklein | 27:3780dd84a543 | 49 | L1=0; |
jacquesolivierklein | 27:3780dd84a543 | 50 | } |
jacquesolivierklein | 27:3780dd84a543 | 51 | } |
jacquesolivierklein | 27:3780dd84a543 | 52 | |
jacquesolivierklein | 27:3780dd84a543 | 53 | void bargraph_down_counter(){ |
jacquesolivierklein | 27:3780dd84a543 | 54 | static int down_counter = 1; |
jacquesolivierklein | 27:3780dd84a543 | 55 | while(1){ |
jacquesolivierklein | 27:3780dd84a543 | 56 | bargraph_mutex.lock(); |
jacquesolivierklein | 27:3780dd84a543 | 57 | L2=1; |
jacquesolivierklein | 27:3780dd84a543 | 58 | for(down_counter = 0x80; down_counter != 0xFF; |
jacquesolivierklein | 27:3780dd84a543 | 59 | down_counter = (down_counter>>1)|0x80){ |
jacquesolivierklein | 27:3780dd84a543 | 60 | ihm.BAR_set(down_counter); |
jacquesolivierklein | 27:3780dd84a543 | 61 | Thread::wait(200); |
jacquesolivierklein | 27:3780dd84a543 | 62 | } |
jacquesolivierklein | 27:3780dd84a543 | 63 | bargraph_mutex.unlock(); |
jacquesolivierklein | 27:3780dd84a543 | 64 | L2=0; |
jacquesolivierklein | 27:3780dd84a543 | 65 | } |
jacquesolivierklein | 27:3780dd84a543 | 66 | } |