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

main.cpp

Committer:
radhey04ec
Date:
2020-07-12
Revision:
0:ee291bda0e6e

File content as of revision 0:ee291bda0e6e:

//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
    }
  }
}