![](/media/cache/profiles/IvyS.JPG.50x50_q85.jpg)
Lab 2 Button Testing.
Fork of digitalInPolling_sample by
Revision 2:cd1fe8c29793, committed 2018-01-16
- Comitter:
- WilliamMarshQMUL
- Date:
- Tue Jan 16 18:02:44 2018 +0000
- Parent:
- 1:4709af498799
- Commit message:
- Corrected for mbed 5
Changed in this revision
--- 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Jan 16 18:02:44 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#96d9a00d0a1d25095b330095fa81c40f7741777c
--- a/mbed-rtos.lib Tue Jan 24 18:24:25 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- a/mbed.bld Tue Jan 24 18:24:25 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/ad3be0349dc5 \ No newline at end of file