SEMAPHORE PART 2, FUNCTION : try_acquire() use and replacement of wait() function JAYDEEP SHAH--radhey04ec@gmil.com

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //SEMAPHORE PART 2
00002 
00003 //INSTEAD OF wait() -- > use try_acquire()  = return bool = true if acquire
00004 //NEW FUNCTION INTRO IN MBED
00005 
00006 //USE SERIAL TERMINAL WITH 9600 BAUD RATE  8-N-1 FORMAT
00007 
00008 //Created by : JAYDEEP SHAH  --radhey04ec@gmail.com
00009 
00010 
00011 //Library Added :::::
00012 #include "mbed.h"
00013 
00014 DigitalOut led(LED1);  // ON BOARD LED
00015 InterruptIn btn(USER_BUTTON);  // ON BOARD BUTTON  PORT PC_13 
00016 //INTERRUPT REGISTERATION
00017 
00018 Semaphore updates(0);  //SEMAPHORE OBJECT CREATE = SHARE RESOURCE 0
00019 //NO SHARE RESOURCE ONLY ISR
00020 
00021 void do_something() {        //ISR FUNCTION
00022   // release the semaphore
00023   updates.release();   // NOW V=1
00024 }
00025 
00026 int main() {
00027   btn.fall(&do_something);
00028 
00029   while (1) {
00030     // wait for the semaphore to be released from the ISR
00031     bool v = updates.try_acquire();    //RETURN STORE IN V
00032 
00033     // now this runs on the main thread, and is safe
00034     if(v == 1)
00035     
00036 {
00037       led = !led;    //LED TOGGLE
00038       printf("Toggle LED!\r\n");  //SERIAL PRINT
00039     }
00040   }
00041 }