Peiling Yi
/
digitalInPolling_modify2
lab2-part1
Fork of digitalInPolling_sample by
main.cpp@1:4709af498799, 2017-01-24 (annotated)
- Committer:
- WilliamMarshQMUL
- Date:
- Tue Jan 24 18:24:25 2017 +0000
- Revision:
- 1:4709af498799
- Parent:
- 0:a66a8cb0012c
- Child:
- 2:cd1fe8c29793
Correct the detect of button position
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WilliamMarshQMUL | 0:a66a8cb0012c | 1 | #include "mbed.h" |
WilliamMarshQMUL | 0:a66a8cb0012c | 2 | #include "rtos.h" |
WilliamMarshQMUL | 0:a66a8cb0012c | 3 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 4 | // Labs 2: Example program for polling an input |
WilliamMarshQMUL | 0:a66a8cb0012c | 5 | // -------------------------------------------- |
WilliamMarshQMUL | 0:a66a8cb0012c | 6 | // The program uses a thread to poll a digital input |
WilliamMarshQMUL | 0:a66a8cb0012c | 7 | // - The thread monitors the position of the button |
WilliamMarshQMUL | 0:a66a8cb0012c | 8 | // - When the button transitions up and down, a press event is signaled |
WilliamMarshQMUL | 0:a66a8cb0012c | 9 | // - Button bounce is guarded against |
WilliamMarshQMUL | 0:a66a8cb0012c | 10 | // A second thread checks for the press event and toggles the LED |
WilliamMarshQMUL | 0:a66a8cb0012c | 11 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 12 | DigitalIn button(PTD0, PullUp); |
WilliamMarshQMUL | 0:a66a8cb0012c | 13 | DigitalOut led(LED_RED); |
WilliamMarshQMUL | 0:a66a8cb0012c | 14 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 15 | Thread pollT ; // thread to poll |
WilliamMarshQMUL | 0:a66a8cb0012c | 16 | Thread flashT ; // thread to flash light |
WilliamMarshQMUL | 0:a66a8cb0012c | 17 | volatile int pressEvent = 0 ; // Variabe set by the polling thread |
WilliamMarshQMUL | 0:a66a8cb0012c | 18 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 19 | enum buttonPos { up, down, bounce }; // Button positions |
WilliamMarshQMUL | 0:a66a8cb0012c | 20 | void polling() { |
WilliamMarshQMUL | 0:a66a8cb0012c | 21 | buttonPos pos = up ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 22 | int bcounter = 0 ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 23 | while (true) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 24 | switch (pos) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 25 | case up : |
WilliamMarshQMUL | 1:4709af498799 | 26 | if (button == 0) { // now down |
WilliamMarshQMUL | 0:a66a8cb0012c | 27 | pressEvent = 1 ; // transition occurred |
WilliamMarshQMUL | 0:a66a8cb0012c | 28 | pos = down ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 29 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 30 | break ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 31 | case down : |
WilliamMarshQMUL | 1:4709af498799 | 32 | if (button == 1) { // no longer down |
WilliamMarshQMUL | 0:a66a8cb0012c | 33 | bcounter = 3 ; // wait four cycles |
WilliamMarshQMUL | 0:a66a8cb0012c | 34 | pos = bounce ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 35 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 36 | break ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 37 | case bounce : |
WilliamMarshQMUL | 1:4709af498799 | 38 | if (button == 0) { // down again - button has bounced |
WilliamMarshQMUL | 0:a66a8cb0012c | 39 | pos = down ; // no event |
WilliamMarshQMUL | 0:a66a8cb0012c | 40 | } else if (bcounter == 0) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 41 | pos = up ; // delay passed - reset to up |
WilliamMarshQMUL | 0:a66a8cb0012c | 42 | } else { |
WilliamMarshQMUL | 0:a66a8cb0012c | 43 | bcounter-- ; // continue waiting |
WilliamMarshQMUL | 0:a66a8cb0012c | 44 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 45 | break ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 46 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 47 | Thread::wait(30); |
WilliamMarshQMUL | 0:a66a8cb0012c | 48 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 49 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 50 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 51 | // Toggle the LED every time the button is pressed |
WilliamMarshQMUL | 0:a66a8cb0012c | 52 | void flash() { |
WilliamMarshQMUL | 0:a66a8cb0012c | 53 | while(true) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 54 | if (pressEvent) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 55 | pressEvent = 0 ; // clear the event variable |
WilliamMarshQMUL | 0:a66a8cb0012c | 56 | led = !led ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 57 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 58 | Thread::wait(100) ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 59 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 60 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 61 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 62 | int main() { |
WilliamMarshQMUL | 0:a66a8cb0012c | 63 | pollT.start(&polling) ; // start the polling thread running |
WilliamMarshQMUL | 0:a66a8cb0012c | 64 | flashT.start(&flash) ; // start the flashing thread running |
WilliamMarshQMUL | 0:a66a8cb0012c | 65 | } |