![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Implemented polling code to check button press and change speed of button flashing.
Fork of digitalInPolling_sample by
Diff: main.cpp
- Revision:
- 2:cd1fe8c29793
- Parent:
- 1:4709af498799
- Child:
- 3:17c6a539bd7c
diff -r 4709af498799 -r cd1fe8c29793 main.cpp --- a/main.cpp Tue Jan 24 18:24:25 2017 +0000 +++ b/main.cpp Tue Jan 16 18:02:44 2018 +0000 @@ -1,5 +1,4 @@ #include "mbed.h" -#include "rtos.h" // Labs 2: Example program for polling an input // -------------------------------------------- @@ -7,13 +6,12 @@ // - The thread monitors the position of the button // - When the button transitions up and down, a press event is signaled // - Button bounce is guarded against -// A second thread checks for the press event and toggles the LED +// A second thread (the default one) checks for the press event and toggles the LED -DigitalIn button(PTD0, PullUp); -DigitalOut led(LED_RED); +DigitalIn b1(PTD0, PullUp); +DigitalOut led(LED1); Thread pollT ; // thread to poll -Thread flashT ; // thread to flash light volatile int pressEvent = 0 ; // Variabe set by the polling thread enum buttonPos { up, down, bounce }; // Button positions @@ -23,19 +21,19 @@ while (true) { switch (pos) { case up : - if (button == 0) { // now down + if (!b1.read()) { // now down pressEvent = 1 ; // transition occurred pos = down ; } break ; case down : - if (button == 1) { // no longer down + if (b1 == 1) { // no longer down bcounter = 3 ; // wait four cycles pos = bounce ; } break ; case bounce : - if (button == 0) { // down again - button has bounced + if (b1 == 0) { // down again - button has bounced pos = down ; // no event } else if (bcounter == 0) { pos = up ; // delay passed - reset to up @@ -48,8 +46,13 @@ } } -// Toggle the LED every time the button is pressed -void flash() { +/* ---- Main function (default thread) ---- + Note that if this thread completes, nothing else works + */ +int main() { + led = 1 ; // Initially off + pollT.start(callback(polling)); + while(true) { if (pressEvent) { pressEvent = 0 ; // clear the event variable @@ -57,9 +60,4 @@ } Thread::wait(100) ; } -} - -int main() { - pollT.start(&polling) ; // start the polling thread running - flashT.start(&flash) ; // start the flashing thread running } \ No newline at end of file