SEMAPHORE PART 2, FUNCTION : try_acquire() use and replacement of wait() function JAYDEEP SHAH--radhey04ec@gmil.com
Diff: main.cpp
- Revision:
- 0:ee291bda0e6e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sun Jul 12 09:41:04 2020 +0000 @@ -0,0 +1,41 @@ +//SEMAPHORE PART 2 + +//INSTEAD OF wait() -- > use try_acquire() = return bool = true if acquire +//NEW FUNCTION INTRO IN MBED + +//USE SERIAL TERMINAL WITH 9600 BAUD RATE 8-N-1 FORMAT + +//Created by : JAYDEEP SHAH --radhey04ec@gmail.com + + +//Library Added ::::: +#include "mbed.h" + +DigitalOut led(LED1); // ON BOARD LED +InterruptIn btn(USER_BUTTON); // ON BOARD BUTTON PORT PC_13 +//INTERRUPT REGISTERATION + +Semaphore updates(0); //SEMAPHORE OBJECT CREATE = SHARE RESOURCE 0 +//NO SHARE RESOURCE ONLY ISR + +void do_something() { //ISR FUNCTION + // release the semaphore + updates.release(); // NOW V=1 +} + +int main() { + btn.fall(&do_something); + + while (1) { + // wait for the semaphore to be released from the ISR + bool v = updates.try_acquire(); //RETURN STORE IN V + + // now this runs on the main thread, and is safe + if(v == 1) + +{ + led = !led; //LED TOGGLE + printf("Toggle LED!\r\n"); //SERIAL PRINT + } + } +} \ No newline at end of file