polling fix for lab 2 assessed

Fork of digitalInPolling_sample by William Marsh

Committer:
dhenis
Date:
Fri Feb 02 21:41:51 2018 +0000
Revision:
3:9b120c387adf
Parent:
2:cd1fe8c29793
polling lab2

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);
dhenis 3:9b120c387adf 12 DigitalOut yled(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
dhenis 3:9b120c387adf 17 enum buttonPos { up, down, bounce }; // Button positions // three options of button position
dhenis 3:9b120c387adf 18
WilliamMarshQMUL 0:a66a8cb0012c 19 void polling() {
dhenis 3:9b120c387adf 20 buttonPos pos = up ;// initial position
WilliamMarshQMUL 0:a66a8cb0012c 21 int bcounter = 0 ;
WilliamMarshQMUL 0:a66a8cb0012c 22 while (true) {
WilliamMarshQMUL 0:a66a8cb0012c 23 switch (pos) {
WilliamMarshQMUL 0:a66a8cb0012c 24 case up :
WilliamMarshQMUL 2:cd1fe8c29793 25 if (!b1.read()) { // now down
WilliamMarshQMUL 0:a66a8cb0012c 26 pressEvent = 1 ; // transition occurred
dhenis 3:9b120c387adf 27 pos = down ; // action and update position
WilliamMarshQMUL 0:a66a8cb0012c 28 }
WilliamMarshQMUL 0:a66a8cb0012c 29 break ;
WilliamMarshQMUL 0:a66a8cb0012c 30 case down :
dhenis 3:9b120c387adf 31 if (b1 == 1) { // no longer down. what the default value of b1?- > it depends and doesnt have initial value
WilliamMarshQMUL 0:a66a8cb0012c 32 bcounter = 3 ; // wait four cycles
WilliamMarshQMUL 0:a66a8cb0012c 33 pos = bounce ;
WilliamMarshQMUL 0:a66a8cb0012c 34 }
WilliamMarshQMUL 0:a66a8cb0012c 35 break ;
WilliamMarshQMUL 0:a66a8cb0012c 36 case bounce :
WilliamMarshQMUL 2:cd1fe8c29793 37 if (b1 == 0) { // down again - button has bounced
WilliamMarshQMUL 0:a66a8cb0012c 38 pos = down ; // no event
WilliamMarshQMUL 0:a66a8cb0012c 39 } else if (bcounter == 0) {
WilliamMarshQMUL 0:a66a8cb0012c 40 pos = up ; // delay passed - reset to up
WilliamMarshQMUL 0:a66a8cb0012c 41 } else {
WilliamMarshQMUL 0:a66a8cb0012c 42 bcounter-- ; // continue waiting
WilliamMarshQMUL 0:a66a8cb0012c 43 }
WilliamMarshQMUL 0:a66a8cb0012c 44 break ;
WilliamMarshQMUL 0:a66a8cb0012c 45 }
WilliamMarshQMUL 0:a66a8cb0012c 46 Thread::wait(30);
WilliamMarshQMUL 0:a66a8cb0012c 47 }
WilliamMarshQMUL 0:a66a8cb0012c 48 }
WilliamMarshQMUL 0:a66a8cb0012c 49
WilliamMarshQMUL 2:cd1fe8c29793 50 /* ---- Main function (default thread) ----
WilliamMarshQMUL 2:cd1fe8c29793 51 Note that if this thread completes, nothing else works
WilliamMarshQMUL 2:cd1fe8c29793 52 */
WilliamMarshQMUL 2:cd1fe8c29793 53 int main() {
WilliamMarshQMUL 2:cd1fe8c29793 54 led = 1 ; // Initially off
WilliamMarshQMUL 2:cd1fe8c29793 55 pollT.start(callback(polling));
dhenis 3:9b120c387adf 56 int speed = 200; // global variable
dhenis 3:9b120c387adf 57
WilliamMarshQMUL 2:cd1fe8c29793 58
WilliamMarshQMUL 0:a66a8cb0012c 59 while(true) {
dhenis 3:9b120c387adf 60 if (pressEvent) { // main event
WilliamMarshQMUL 0:a66a8cb0012c 61 pressEvent = 0 ; // clear the event variable
dhenis 3:9b120c387adf 62
dhenis 3:9b120c387adf 63 // led = !led ;
dhenis 3:9b120c387adf 64 // Thread::wait(100) ; // time to apply
dhenis 3:9b120c387adf 65 //
dhenis 3:9b120c387adf 66 speed += 200;
dhenis 3:9b120c387adf 67
dhenis 3:9b120c387adf 68 if(speed>1000){
dhenis 3:9b120c387adf 69 speed =200;
dhenis 3:9b120c387adf 70 }
WilliamMarshQMUL 0:a66a8cb0012c 71 }
dhenis 3:9b120c387adf 72
dhenis 3:9b120c387adf 73 Thread::wait(speed) ;
dhenis 3:9b120c387adf 74 led = !led ;
dhenis 3:9b120c387adf 75
dhenis 3:9b120c387adf 76 //
WilliamMarshQMUL 0:a66a8cb0012c 77 }
WilliamMarshQMUL 0:a66a8cb0012c 78 }