Haozun Sun
/
digitalInPolling_hzsun
polling
main.cpp@5:6ea498e4d496, 2020-02-12 (annotated)
- Committer:
- hzsun
- Date:
- Wed Feb 12 19:42:16 2020 +0000
- Revision:
- 5:6ea498e4d496
- Parent:
- 4:82f7ccb294d3
polling
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 | |
hzsun | 5:6ea498e4d496 | 3 | // Labs 2: polling |
WilliamMarshQMUL | 0:a66a8cb0012c | 4 | // -------------------------------------------- |
WilliamMarshQMUL | 0:a66a8cb0012c | 5 | |
hzsun | 5:6ea498e4d496 | 6 | DigitalIn b1(PTD0, PullUp); // initialize the button |
hzsun | 5:6ea498e4d496 | 7 | DigitalOut led(LED1); // initialize the led |
WilliamMarshQMUL | 0:a66a8cb0012c | 8 | |
hzsun | 5:6ea498e4d496 | 9 | Thread pollT ; // thread to poll |
hzsun | 5:6ea498e4d496 | 10 | volatile int pressEvent = 0 ; // Variabe set by the polling thread |
hzsun | 5:6ea498e4d496 | 11 | |
WilliamMarshQMUL | 0:a66a8cb0012c | 12 | |
hzsun | 5:6ea498e4d496 | 13 | enum buttonPos { up, down, bounce }; // Button positions |
hzsun | 5:6ea498e4d496 | 14 | void polling() { |
hzsun | 5:6ea498e4d496 | 15 | buttonPos pos = up ; //initialize the button position |
hzsun | 5:6ea498e4d496 | 16 | int bcounter = 0 ; //initialize the counter |
WilliamMarshQMUL | 0:a66a8cb0012c | 17 | while (true) { |
hzsun | 5:6ea498e4d496 | 18 | ThisThread::sleep_for(30) ; // poll every 30ms |
WilliamMarshQMUL | 0:a66a8cb0012c | 19 | switch (pos) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 20 | case up : |
hzsun | 5:6ea498e4d496 | 21 | if (!b1.read()) { // now down |
hzsun | 5:6ea498e4d496 | 22 | pressEvent = 1 ; // transition occurred |
hzsun | 5:6ea498e4d496 | 23 | pos = down ; // change to down position |
WilliamMarshQMUL | 0:a66a8cb0012c | 24 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 25 | break ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 26 | case down : |
hzsun | 5:6ea498e4d496 | 27 | if (b1 == 1) { // no longer down |
hzsun | 5:6ea498e4d496 | 28 | bcounter = 3 ; // wait four cycles |
hzsun | 5:6ea498e4d496 | 29 | pos = bounce ; //change to bounce position |
WilliamMarshQMUL | 0:a66a8cb0012c | 30 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 31 | break ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 32 | case bounce : |
hzsun | 5:6ea498e4d496 | 33 | if (b1 == 0) { // down again - button has bounced |
hzsun | 5:6ea498e4d496 | 34 | pos = down ; // no event |
WilliamMarshQMUL | 0:a66a8cb0012c | 35 | } else if (bcounter == 0) { |
hzsun | 5:6ea498e4d496 | 36 | pos = up ; // delay passed - reset to up |
WilliamMarshQMUL | 0:a66a8cb0012c | 37 | } else { |
hzsun | 5:6ea498e4d496 | 38 | bcounter-- ; // continue waiting |
WilliamMarshQMUL | 0:a66a8cb0012c | 39 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 40 | break ; |
WilliamMarshQMUL | 0:a66a8cb0012c | 41 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 42 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 43 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 44 | |
hzsun | 5:6ea498e4d496 | 45 | /* ---- Main function (---- |
WilliamMarshQMUL | 2:cd1fe8c29793 | 46 | */ |
WilliamMarshQMUL | 2:cd1fe8c29793 | 47 | int main() { |
hzsun | 5:6ea498e4d496 | 48 | led = 0 ; // Initially off |
WilliamMarshQMUL | 2:cd1fe8c29793 | 49 | pollT.start(callback(polling)); |
hzsun | 5:6ea498e4d496 | 50 | int i=0; //create a counter for controling the speed of LED |
hzsun | 5:6ea498e4d496 | 51 | int flashspeed[]={200, 400, 600, 800, 1000}; //input the value for chaging the speed of LED |
WilliamMarshQMUL | 0:a66a8cb0012c | 52 | while(true) { |
WilliamMarshQMUL | 0:a66a8cb0012c | 53 | if (pressEvent) { |
hzsun | 5:6ea498e4d496 | 54 | pressEvent = 0 ; // clear the event variable |
hzsun | 5:6ea498e4d496 | 55 | i++ ; // counter add one |
hzsun | 5:6ea498e4d496 | 56 | if (i>4){ // when counter is bigger than four change back to zero |
hzsun | 5:6ea498e4d496 | 57 | i=0; |
hzsun | 5:6ea498e4d496 | 58 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 59 | } |
hzsun | 5:6ea498e4d496 | 60 | led =!led; // led blinks |
hzsun | 5:6ea498e4d496 | 61 | ThisThread::sleep_for(flashspeed[i]) ; // delay for 200ms 400ms 600ms 800ms 1s then come back to 200ms |
WilliamMarshQMUL | 0:a66a8cb0012c | 62 | } |
WilliamMarshQMUL | 0:a66a8cb0012c | 63 | } |