polling for lab2
Fork of digitalInPolling_sample by
Embed:
(wiki syntax)
Show/hide line numbers
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); //Set press count 00013 00014 Thread pollT ; // thread to poll 00015 volatile int pressEvent = 0 ; // Variabe set by the polling thread 00016 volatile int timecount =200; 00017 00018 00019 enum buttonPos { up, down, bounce }; // Button positions 00020 void polling() { 00021 buttonPos pos = up ; 00022 int bcounter = 0 ; 00023 while (true) { 00024 switch (pos) { 00025 case up : 00026 if (!b1.read()) { // now down 00027 pressEvent = 1 ; // transition occurred 00028 pos = down ; 00029 } 00030 break ; 00031 case down : 00032 if (b1 == 1) { // no longer down 00033 bcounter = 3 ; // wait four cycles 00034 pos = bounce ; 00035 } 00036 break ; 00037 case bounce : 00038 if (b1 == 0) { // down again - button has bounced 00039 pos = down ; // no event 00040 } else if (bcounter == 0) { 00041 pos = up ; // delay passed - reset to up 00042 } else { 00043 bcounter-- ; // continue waiting 00044 } 00045 break ; 00046 } 00047 Thread::wait(30); 00048 } 00049 } 00050 00051 /* ---- Main function (default thread) ---- 00052 Note that if this thread completes, nothing else works 00053 */ 00054 00055 00056 00057 int main() { 00058 led = 1 ; // Initially off 00059 pollT.start(callback(polling)); 00060 00061 00062 while(true) { 00063 00064 led = !led ; 00065 00066 if (pressEvent) { 00067 pressEvent = 0 ; 00068 00069 if (timecount < 1000) { 00070 00071 timecount+=200; 00072 } 00073 else { 00074 00075 timecount = 200; 00076 } 00077 } 00078 Thread::wait(timecount) ; 00079 } 00080 00081 00082 } 00083
Generated on Mon Jul 18 2022 17:47:03 by
1.7.2
