Finalized, Viva(ed).

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002  
00003 // Labs 2: Example program for polling an input
00004 // --------------------------------------------
00005 // The program uses a thread to poll a digital input
00006 //   - The thread monitors the position of the button
00007 //   - When the button transitions up and down, a press event is signaled 
00008 //   - Button bounce is guarded against
00009 // A second thread (the default one) checks for the press event and toggles the LED
00010  
00011 DigitalIn b1(PTD0, PullUp);
00012 DigitalOut led(LED1);
00013 
00014  
00015 Thread pollT; // thread to poll
00016 Thread flashL; //Flash the LED 100ms 
00017 
00018 volatile int pressEvent = 0 ;  // Variabe set by the polling thread
00019 float waiting_period[5] = {0.2, 0.4, 0.6, 0.8, 1.0};
00020 unsigned int i = 0, count = 0;  
00021 enum buttonPos { up, down, bounce }; // Button positions
00022 
00023 void polling() {
00024     buttonPos pos = up ;
00025     int bcounter = 0 ;
00026     while (true) {
00027         switch (pos) {
00028             case up :
00029                 if (!b1.read()) {    // now down 
00030                     pressEvent = 1 ;  // transition occurred
00031                     pos = down ;
00032                 }
00033                 break ;
00034             case down : 
00035                 if (b1 == 1) { // no longer down
00036                     bcounter = 3 ; // wait four cycles
00037                     pos = bounce ;
00038                 }
00039                 break ;
00040             case bounce :
00041                 if (b1 == 0) { // down again - button has bounced
00042                     pos = down ;   // no event
00043                 } else if (bcounter == 0) {
00044                     pos = up ;     // delay passed - reset to up
00045                 } else {
00046                     bcounter-- ;   // continue waiting
00047                 }
00048                 break ;
00049         }
00050         wait(0.03);
00051     }
00052 }
00053 
00054 
00055 
00056  
00057 /*  ---- Main function (default thread) ----
00058     Note that if this thread completes, nothing else works
00059  */
00060 int main() {
00061     led = 1 ;  // Initially off
00062     pollT.start(callback(polling)); 
00063  
00064     while(true) {
00065          //led = !led ;
00066          wait(0.1);
00067          count++; 
00068          if(count >= waiting_period[i]*10) {
00069             led = !led; 
00070             count = 0 ;
00071         }
00072          //wait(waiting_period[i]); 
00073          if (pressEvent) {
00074             //Thread::wait(waiting_period[i]-count*100);
00075             //wait(waiting_period[i]-count);
00076             i++;  
00077             i = i%5;
00078             //count = 0;
00079             pressEvent = 0 ; // clear the event variable 
00080         }
00081      //wait(0.1);   
00082     }
00083 }