Lab 2, sample 1: polling example

Fork of digitalInPolling_sample by William Marsh

Files at this revision

API Documentation at this revision

Comitter:
WilliamMarshQMUL
Date:
Thu Jan 30 08:04:56 2020 +0000
Parent:
3:8d87cbabe37e
Commit message:
Replace wait with sleep_for

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 8d87cbabe37e -r 82f7ccb294d3 main.cpp
--- a/main.cpp	Tue Jan 15 09:47:06 2019 +0000
+++ b/main.cpp	Thu Jan 30 08:04:56 2020 +0000
@@ -13,12 +13,15 @@
 
 Thread pollT ; // thread to poll
 volatile int pressEvent = 0 ;  // Variabe set by the polling thread
+  // Sharing a global variable between two threads is not safe in general
+  //  but it can be ok if a) update is atomic and b) only one thread writes
 
 enum buttonPos { up, down, bounce }; // Button positions
 void polling() {
     buttonPos pos = up ;
     int bcounter = 0 ;
     while (true) {
+        ThisThread::sleep_for(30) ; // poll every 30ms
         switch (pos) {
             case up :
                 if (!b1.read()) {    // now down 
@@ -42,7 +45,6 @@
                 }
                 break ;
         }
-        wait(0.03);
     }
 }
 
@@ -58,6 +60,6 @@
             pressEvent = 0 ; // clear the event variable
             led = !led ;
         }
-        Thread::wait(0.1) ;
+        ThisThread::sleep_for(100) ; // delay for 100ms 
     }
 }
\ No newline at end of file
diff -r 8d87cbabe37e -r 82f7ccb294d3 mbed-os.lib
--- a/mbed-os.lib	Tue Jan 15 09:47:06 2019 +0000
+++ b/mbed-os.lib	Thu Jan 30 08:04:56 2020 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#c966348d3f9ca80843be7cdc9b748f06ea73ced0
+https://github.com/ARMmbed/mbed-os/#430e64fce8098fd5e54f6de32f3f029f35dc705f