SEMAPHORE PART 3 SEMAPHORE FUNCTION : try_acquire_until(ms) use -- replacement of wait_until() function SEMAPHORE ADVANCE LEVEL Jaydeep Shah -- radhey04ec@gmail.com
main.cpp
00001 /*SEMAPHORE PART 3 00002 FUNCTION USE : try_acquire_until(uint32_t miliSecond) 00003 return bool = true /false 00004 00005 REPLACEMENT OF wait_until(uint64_t milisecond) 00006 */ 00007 00008 //USE SERIAL TERMINAL WITH 9600 BAUD RATE 8-N-1 FORMAT 00009 00010 //Created by : JAYDEEP SHAH --radhey04ec@gmail.com 00011 00012 00013 //Library Added ::::: 00014 #include "mbed.h" 00015 00016 DigitalOut led(LED1); // ON BOARD LED 00017 InterruptIn btn(USER_BUTTON); // ON BOARD BUTTON PORT PC_13 00018 //INTERRUPT REGISTERATION 00019 00020 Semaphore updates(0); //SEMAPHORE OBJECT CREATE = SHARE RESOURCE 0 00021 //NO SHARE RESOURCE ONLY ISR 00022 00023 void do_something() { //ISR FUNCTION 00024 // release the semaphore 00025 updates.release(); // NOW V=1 00026 } 00027 00028 int main() { 00029 btn.fall(&do_something); //FUNCTION CALL WHEN FALL EDGE OF BUTTON - ISR 00030 00031 while (1) { //LOOP FOREVER 00032 // wait for the semaphore to be released from the ISR 00033 bool v = updates.try_acquire_until(30000); //RETURN STORE IN V //WAIT FOR 30 SEC 00034 00035 // now this runs on the main thread, and is safe 00036 00037 if(v==1) //THIS ONLY RUN WHEN BUTTON PRESS & SEMAPHORE RELEASE BY 1 00038 { 00039 led = !led; //LED TOGGLE 00040 printf("Toggle LED! V= %d \r\n",v); //SERIAL PRINT V === 0 AGAIN 00041 } 00042 00043 if(v == 0) // This Function run after 30 sec of Power on/RST Continue -- NO matter button press or not 00044 { 00045 printf("\n TIMER RELEASE SEMAPHORE V = %d \r\n",v); 00046 } 00047 00048 } 00049 }
Generated on Fri Jul 15 2022 14:27:02 by 1.7.2