István Cserny / Mbed 2 deprecated Lab08_4semaphores

Dependencies:   mbed mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 
00004 Semaphore sem[] = {(1),(0),(0),(0)};          // Set of semaphores
00005 DigitalOut led[] = {(D13),(D12),(D11),(D10)}; // Set of LEDs
00006 
00007 void led_thread(void const* args) {
00008     int i = (int)args-1;                      // Idx of this task
00009     int inext = (i+1)%4;                      // Idx of next task
00010     while (true) {
00011         sem[i].wait();
00012         led[i] = 0;                           // ith LED on
00013         Thread::wait(500+rand()%500);
00014         led[i] = 1;                           // ith LED off
00015         sem[inext].release();                 // Start new task
00016     }
00017 }
00018 
00019 int main (void) {
00020     for(int i=0; i<4; i++) {
00021         led[i]=1;                             // LEDs off
00022     }
00023     Thread t2(led_thread, (void *)2U);
00024     Thread t3(led_thread, (void *)3U);
00025     Thread t4(led_thread, (void *)4U);       
00026     led_thread((void *)1U);
00027 }