SEMAPHORE PART 2, FUNCTION : try_acquire() use and replacement of wait() function JAYDEEP SHAH--radhey04ec@gmil.com
main.cpp@0:ee291bda0e6e, 2020-07-12 (annotated)
- Committer:
- radhey04ec
- Date:
- Sun Jul 12 09:41:04 2020 +0000
- Revision:
- 0:ee291bda0e6e
SEMAPHORE PART 2 COMMITED; try_acquire() FUNCTION USE; ; JAYDEEP SHAH -- radhey04ec@gmail.com
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
radhey04ec | 0:ee291bda0e6e | 1 | //SEMAPHORE PART 2 |
radhey04ec | 0:ee291bda0e6e | 2 | |
radhey04ec | 0:ee291bda0e6e | 3 | //INSTEAD OF wait() -- > use try_acquire() = return bool = true if acquire |
radhey04ec | 0:ee291bda0e6e | 4 | //NEW FUNCTION INTRO IN MBED |
radhey04ec | 0:ee291bda0e6e | 5 | |
radhey04ec | 0:ee291bda0e6e | 6 | //USE SERIAL TERMINAL WITH 9600 BAUD RATE 8-N-1 FORMAT |
radhey04ec | 0:ee291bda0e6e | 7 | |
radhey04ec | 0:ee291bda0e6e | 8 | //Created by : JAYDEEP SHAH --radhey04ec@gmail.com |
radhey04ec | 0:ee291bda0e6e | 9 | |
radhey04ec | 0:ee291bda0e6e | 10 | |
radhey04ec | 0:ee291bda0e6e | 11 | //Library Added ::::: |
radhey04ec | 0:ee291bda0e6e | 12 | #include "mbed.h" |
radhey04ec | 0:ee291bda0e6e | 13 | |
radhey04ec | 0:ee291bda0e6e | 14 | DigitalOut led(LED1); // ON BOARD LED |
radhey04ec | 0:ee291bda0e6e | 15 | InterruptIn btn(USER_BUTTON); // ON BOARD BUTTON PORT PC_13 |
radhey04ec | 0:ee291bda0e6e | 16 | //INTERRUPT REGISTERATION |
radhey04ec | 0:ee291bda0e6e | 17 | |
radhey04ec | 0:ee291bda0e6e | 18 | Semaphore updates(0); //SEMAPHORE OBJECT CREATE = SHARE RESOURCE 0 |
radhey04ec | 0:ee291bda0e6e | 19 | //NO SHARE RESOURCE ONLY ISR |
radhey04ec | 0:ee291bda0e6e | 20 | |
radhey04ec | 0:ee291bda0e6e | 21 | void do_something() { //ISR FUNCTION |
radhey04ec | 0:ee291bda0e6e | 22 | // release the semaphore |
radhey04ec | 0:ee291bda0e6e | 23 | updates.release(); // NOW V=1 |
radhey04ec | 0:ee291bda0e6e | 24 | } |
radhey04ec | 0:ee291bda0e6e | 25 | |
radhey04ec | 0:ee291bda0e6e | 26 | int main() { |
radhey04ec | 0:ee291bda0e6e | 27 | btn.fall(&do_something); |
radhey04ec | 0:ee291bda0e6e | 28 | |
radhey04ec | 0:ee291bda0e6e | 29 | while (1) { |
radhey04ec | 0:ee291bda0e6e | 30 | // wait for the semaphore to be released from the ISR |
radhey04ec | 0:ee291bda0e6e | 31 | bool v = updates.try_acquire(); //RETURN STORE IN V |
radhey04ec | 0:ee291bda0e6e | 32 | |
radhey04ec | 0:ee291bda0e6e | 33 | // now this runs on the main thread, and is safe |
radhey04ec | 0:ee291bda0e6e | 34 | if(v == 1) |
radhey04ec | 0:ee291bda0e6e | 35 | |
radhey04ec | 0:ee291bda0e6e | 36 | { |
radhey04ec | 0:ee291bda0e6e | 37 | led = !led; //LED TOGGLE |
radhey04ec | 0:ee291bda0e6e | 38 | printf("Toggle LED!\r\n"); //SERIAL PRINT |
radhey04ec | 0:ee291bda0e6e | 39 | } |
radhey04ec | 0:ee291bda0e6e | 40 | } |
radhey04ec | 0:ee291bda0e6e | 41 | } |