noop din
/
digitalInPolling_sample
polling for lab2
Fork of digitalInPolling_sample by
main.cpp@3:d35e20a7b695, 2018-02-01 (annotated)
- Committer:
- toh2018
- Date:
- Thu Feb 01 17:25:02 2018 +0000
- Revision:
- 3:d35e20a7b695
- Parent:
- 2:cd1fe8c29793
Polling for lab2
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 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 3 | // Labs 2: Example program for polling an input |
WilliamMarshQMUL | 0:a66a8cb0012c | 4 | // -------------------------------------------- |
WilliamMarshQMUL | 0:a66a8cb0012c | 5 | // The program uses a thread to poll a digital input |
WilliamMarshQMUL | 0:a66a8cb0012c | 6 | // - The thread monitors the position of the button |
WilliamMarshQMUL | 0:a66a8cb0012c | 7 | // - When the button transitions up and down, a press event is signaled |
WilliamMarshQMUL | 0:a66a8cb0012c | 8 | // - Button bounce is guarded against |
WilliamMarshQMUL | 2:cd1fe8c29793 | 9 | // A second thread (the default one) checks for the press event and toggles the LED |
WilliamMarshQMUL | 0:a66a8cb0012c | 10 | |
WilliamMarshQMUL | 2:cd1fe8c29793 | 11 | DigitalIn b1(PTD0, PullUp); |
toh2018 | 3:d35e20a7b695 | 12 | DigitalOut led(LED1); //Set press count |
WilliamMarshQMUL | 0:a66a8cb0012c | 13 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 14 | Thread pollT ; // thread to poll |
WilliamMarshQMUL | 0:a66a8cb0012c | 15 | volatile int pressEvent = 0 ; // Variabe set by the polling thread |
toh2018 | 3:d35e20a7b695 | 16 | volatile int timecount =200; |
toh2018 | 3:d35e20a7b695 | 17 | |
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 | 2:cd1fe8c29793 | 26 | if (!b1.read()) { // 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 | 2:cd1fe8c29793 | 32 | if (b1 == 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 | 2:cd1fe8c29793 | 38 | if (b1 == 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 | 2:cd1fe8c29793 | 51 | /* ---- Main function (default thread) ---- |
WilliamMarshQMUL | 2:cd1fe8c29793 | 52 | Note that if this thread completes, nothing else works |
WilliamMarshQMUL | 2:cd1fe8c29793 | 53 | */ |
toh2018 | 3:d35e20a7b695 | 54 | |
toh2018 | 3:d35e20a7b695 | 55 | |
toh2018 | 3:d35e20a7b695 | 56 | |
WilliamMarshQMUL | 2:cd1fe8c29793 | 57 | int main() { |
WilliamMarshQMUL | 2:cd1fe8c29793 | 58 | led = 1 ; // Initially off |
WilliamMarshQMUL | 2:cd1fe8c29793 | 59 | pollT.start(callback(polling)); |
toh2018 | 3:d35e20a7b695 | 60 | |
WilliamMarshQMUL | 2:cd1fe8c29793 | 61 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 62 | while(true) { |
toh2018 | 3:d35e20a7b695 | 63 | |
toh2018 | 3:d35e20a7b695 | 64 | led = !led ; |
toh2018 | 3:d35e20a7b695 | 65 | |
toh2018 | 3:d35e20a7b695 | 66 | if (pressEvent) { |
toh2018 | 3:d35e20a7b695 | 67 | pressEvent = 0 ; |
toh2018 | 3:d35e20a7b695 | 68 | |
toh2018 | 3:d35e20a7b695 | 69 | if (timecount < 1000) { |
toh2018 | 3:d35e20a7b695 | 70 | |
toh2018 | 3:d35e20a7b695 | 71 | timecount+=200; |
WilliamMarshQMUL | 0:a66a8cb0012c | 72 | } |
toh2018 | 3:d35e20a7b695 | 73 | else { |
toh2018 | 3:d35e20a7b695 | 74 | |
toh2018 | 3:d35e20a7b695 | 75 | timecount = 200; |
toh2018 | 3:d35e20a7b695 | 76 | } |
toh2018 | 3:d35e20a7b695 | 77 | } |
toh2018 | 3:d35e20a7b695 | 78 | Thread::wait(timecount) ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 79 | } |
toh2018 | 3:d35e20a7b695 | 80 | |
toh2018 | 3:d35e20a7b695 | 81 | |
toh2018 | 3:d35e20a7b695 | 82 | } |
toh2018 | 3:d35e20a7b695 | 83 |