Implemented polling code to check button press and change speed of button flashing.

Fork of digitalInPolling_sample by William Marsh

Committer:
natgovor
Date:
Thu Feb 01 12:24:02 2018 +0000
Revision:
3:17c6a539bd7c
Parent:
2:cd1fe8c29793
Implemented polling code to check button press and change the speed of button flashing.

Who changed what in which revision?

UserRevisionLine numberNew 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);
WilliamMarshQMUL 2:cd1fe8c29793 12 DigitalOut led(LED1);
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
WilliamMarshQMUL 0:a66a8cb0012c 16
WilliamMarshQMUL 0:a66a8cb0012c 17 enum buttonPos { up, down, bounce }; // Button positions
WilliamMarshQMUL 0:a66a8cb0012c 18 void polling() {
WilliamMarshQMUL 0:a66a8cb0012c 19 buttonPos pos = up ;
WilliamMarshQMUL 0:a66a8cb0012c 20 int bcounter = 0 ;
WilliamMarshQMUL 0:a66a8cb0012c 21 while (true) {
WilliamMarshQMUL 0:a66a8cb0012c 22 switch (pos) {
WilliamMarshQMUL 0:a66a8cb0012c 23 case up :
WilliamMarshQMUL 2:cd1fe8c29793 24 if (!b1.read()) { // now down
WilliamMarshQMUL 0:a66a8cb0012c 25 pressEvent = 1 ; // transition occurred
WilliamMarshQMUL 0:a66a8cb0012c 26 pos = down ;
WilliamMarshQMUL 0:a66a8cb0012c 27 }
WilliamMarshQMUL 0:a66a8cb0012c 28 break ;
WilliamMarshQMUL 0:a66a8cb0012c 29 case down :
WilliamMarshQMUL 2:cd1fe8c29793 30 if (b1 == 1) { // no longer down
WilliamMarshQMUL 0:a66a8cb0012c 31 bcounter = 3 ; // wait four cycles
WilliamMarshQMUL 0:a66a8cb0012c 32 pos = bounce ;
WilliamMarshQMUL 0:a66a8cb0012c 33 }
WilliamMarshQMUL 0:a66a8cb0012c 34 break ;
WilliamMarshQMUL 0:a66a8cb0012c 35 case bounce :
WilliamMarshQMUL 2:cd1fe8c29793 36 if (b1 == 0) { // down again - button has bounced
WilliamMarshQMUL 0:a66a8cb0012c 37 pos = down ; // no event
WilliamMarshQMUL 0:a66a8cb0012c 38 } else if (bcounter == 0) {
WilliamMarshQMUL 0:a66a8cb0012c 39 pos = up ; // delay passed - reset to up
WilliamMarshQMUL 0:a66a8cb0012c 40 } else {
WilliamMarshQMUL 0:a66a8cb0012c 41 bcounter-- ; // continue waiting
WilliamMarshQMUL 0:a66a8cb0012c 42 }
WilliamMarshQMUL 0:a66a8cb0012c 43 break ;
WilliamMarshQMUL 0:a66a8cb0012c 44 }
WilliamMarshQMUL 0:a66a8cb0012c 45 Thread::wait(30);
WilliamMarshQMUL 0:a66a8cb0012c 46 }
WilliamMarshQMUL 0:a66a8cb0012c 47 }
WilliamMarshQMUL 0:a66a8cb0012c 48
WilliamMarshQMUL 2:cd1fe8c29793 49 /* ---- Main function (default thread) ----
WilliamMarshQMUL 2:cd1fe8c29793 50 Note that if this thread completes, nothing else works
WilliamMarshQMUL 2:cd1fe8c29793 51 */
natgovor 3:17c6a539bd7c 52 enum states { flashing, timeToCheck }; // flsh thread states
WilliamMarshQMUL 2:cd1fe8c29793 53 int main() {
natgovor 3:17c6a539bd7c 54 int speed = 200;
natgovor 3:17c6a539bd7c 55 states state = flashing;
natgovor 3:17c6a539bd7c 56 int cycleCounter = 0;
natgovor 3:17c6a539bd7c 57 led = 0; // LED is on by default
natgovor 3:17c6a539bd7c 58
WilliamMarshQMUL 2:cd1fe8c29793 59 pollT.start(callback(polling));
WilliamMarshQMUL 2:cd1fe8c29793 60
WilliamMarshQMUL 0:a66a8cb0012c 61 while(true) {
natgovor 3:17c6a539bd7c 62 Thread::wait(100);
natgovor 3:17c6a539bd7c 63 cycleCounter++;
natgovor 3:17c6a539bd7c 64
natgovor 3:17c6a539bd7c 65 if (cycleCounter == 1) {
natgovor 3:17c6a539bd7c 66 state = timeToCheck;
WilliamMarshQMUL 0:a66a8cb0012c 67 }
natgovor 3:17c6a539bd7c 68
natgovor 3:17c6a539bd7c 69 switch (state) {
natgovor 3:17c6a539bd7c 70 case flashing:
natgovor 3:17c6a539bd7c 71 if (cycleCounter == speed / 100) {
natgovor 3:17c6a539bd7c 72 led = !led;
natgovor 3:17c6a539bd7c 73 cycleCounter = 0;
natgovor 3:17c6a539bd7c 74 }
natgovor 3:17c6a539bd7c 75 break;
natgovor 3:17c6a539bd7c 76 case timeToCheck:
natgovor 3:17c6a539bd7c 77 if (pressEvent) {
natgovor 3:17c6a539bd7c 78 cycleCounter = 0;
natgovor 3:17c6a539bd7c 79 pressEvent = 0 ; // clear the event variable
natgovor 3:17c6a539bd7c 80 if (speed < 1000) {
natgovor 3:17c6a539bd7c 81 speed = speed + 200;
natgovor 3:17c6a539bd7c 82 } else {
natgovor 3:17c6a539bd7c 83 speed = 200;
natgovor 3:17c6a539bd7c 84 }
natgovor 3:17c6a539bd7c 85 }
natgovor 3:17c6a539bd7c 86 state = flashing;
natgovor 3:17c6a539bd7c 87 break;
natgovor 3:17c6a539bd7c 88 }
WilliamMarshQMUL 0:a66a8cb0012c 89 }
WilliamMarshQMUL 0:a66a8cb0012c 90 }