14 Key poller with debouncing DEMO

Dependencies:   mbed

Revision:
1:6ee2362ce0fd
Parent:
0:acabda48796c
diff -r acabda48796c -r 6ee2362ce0fd main.cpp
--- a/main.cpp	Sat Jan 07 00:17:15 2012 +0000
+++ b/main.cpp	Sat Jan 07 11:17:04 2012 +0000
@@ -1,14 +1,16 @@
 /* -----------------------------------------------------------------------
-   14 Key poller and debounce demo by Theo Ekelmans, Version 1.0
+   14 Key poller and debounce demo by Theo Ekelmans, Version 1.01
    -----------------------------------------------------------------------
    
    In this demo, banks A, C and D are push buttons, bank B are toggle switches that have a LED inside
    
+   A 4x20 LCD is used in this demo to show key status and debounce info
+   
    The timer used for SampleInterval is set to 10 ms (sample time)
 
    2 * SampleInterval = 20ms for good quality switches, 80ms for high CPU load or really crappy switches :)
    
-   led3 on the MBED 
+   led3 on the MBED is used to indicate a debounce
    
    Config your USB COM port terminal session as a VT100 terminal :)
        
@@ -16,10 +18,7 @@
    1: The SampleCnt is 32 bit, that means every 2^32 times 10ms SampleInterval it will trigger an overflow 
       on the long vars, thats why @ 4000000000 the TimerOverflowRecover resets all counters. 
       During in that brief moment (2 miliseconds every 25 days or so), the debounce is not reliable ;)
-   
-   2: The USB COM port / VT100 terminal session hangs somtimes when you are bashing buttons like a madman, 
-      the LCD however is rock solid.
-   
+  
  */
 
 #include "mbed.h"
@@ -46,8 +45,6 @@
 int  DebounceThreshold = 3; // 2 * SampleInterval = 20ms for good quality switches, 80ms for high CPU load or really crappy switches :)
 float SampleInterval = 0.01; // The timer for debouncing interval: 10 ms sample time
 
-unsigned long PollCount1 = 0;
-unsigned long PollCount2 = 0;
 unsigned long KillBeepAt = 0;
 char strCmd [100] = ""; 
 
@@ -128,14 +125,14 @@
 //-------------------------------------------------------------------------------------------------------
 //This is where you put your payload; 
 //-------------------------------------------------------------------------------------------------------
-void PayLoadPressed(char msg[] ) { lcd.printf("%c%c%c%cA%i%i%i%iB%i%i%i%iC%i%i%i%iD%i%i\r\n@ %ld, %ld bounces\r\nKey + %s @ %ld\r\n                    ", CMD, CurPos, 1, 1,buttonA1.read(),buttonA2.read(),buttonA3.read(),buttonA4.read(),buttonB1.read(),buttonB2.read(),buttonB3.read(),buttonB4.read(),buttonC1.read(),buttonC2.read(),buttonC3.read(),buttonC4.read(),buttonD1.read(),buttonD2.read(), PollCount2, DebounceActivationCounter, msg, SampleCnt);};
-void PayLoadRelease(char msg[] ) { lcd.printf("%c%c%c%cA%i%i%i%iB%i%i%i%iC%i%i%i%iD%i%i\r\n@ %ld, %ld bounces\r\nKey - %s @ %ld\r\n                    ", CMD, CurPos, 1, 1,buttonA1.read(),buttonA2.read(),buttonA3.read(),buttonA4.read(),buttonB1.read(),buttonB2.read(),buttonB3.read(),buttonB4.read(),buttonC1.read(),buttonC2.read(),buttonC3.read(),buttonC4.read(),buttonD1.read(),buttonD2.read(), PollCount2, DebounceActivationCounter, msg, SampleCnt);};
+void PayLoadPressed(char msg[] ) { lcd.printf("%c%c%c%cA%i%i%i%iB%i%i%i%iC%i%i%i%iD%i%i\r\n%ld bounce events\r\nKey + %s @ %ld", CMD, CurPos, 1, 1,buttonA1.read(),buttonA2.read(),buttonA3.read(),buttonA4.read(),buttonB1.read(),buttonB2.read(),buttonB3.read(),buttonB4.read(),buttonC1.read(),buttonC2.read(),buttonC3.read(),buttonC4.read(),buttonD1.read(),buttonD2.read(), DebounceActivationCounter, msg, SampleCnt);};
+void PayLoadRelease(char msg[] ) { lcd.printf("%c%c%c%cA%i%i%i%iB%i%i%i%iC%i%i%i%iD%i%i\r\n%ld bounce events\r\nKey - %s @ %ld", CMD, CurPos, 1, 1,buttonA1.read(),buttonA2.read(),buttonA3.read(),buttonA4.read(),buttonB1.read(),buttonB2.read(),buttonB3.read(),buttonB4.read(),buttonC1.read(),buttonC2.read(),buttonC3.read(),buttonC4.read(),buttonD1.read(),buttonD2.read(), DebounceActivationCounter, msg, SampleCnt);};
 
 //-------------------------------------------------------------------------------------------------------
 //This routine is only needed for the demo and can be removed for runtime
 //-------------------------------------------------------------------------------------------------------
 void DeboucnceActive(char msg[], unsigned long l, unsigned long c) { 
-    lcd.printf("%c%c%c%cA%i%i%i%iB%i%i%i%iC%i%i%i%iD%i%i\r\n@ %ld, %ld bounces\r\nKey - %s @ %ld\r\n%s bounced %ld x    ", CMD, CurPos, 1, 1, buttonA1.read(),buttonA2.read(),buttonA3.read(),buttonA4.read(),buttonB1.read(),buttonB2.read(),buttonB3.read(),buttonB4.read(),buttonC1.read(),buttonC2.read(),buttonC3.read(),buttonC4.read(),buttonD1.read(),buttonD2.read(), PollCount2, DebounceActivationCounter, msg, SampleCnt, msg, (c - l));
+    lcd.printf("%c%c%c%cA%i%i%i%iB%i%i%i%iC%i%i%i%iD%i%i\r\n%ld bounces\r\nKey - %s @ %ld\r\n%s bounced %ld samples", CMD, CurPos, 1, 1, buttonA1.read(),buttonA2.read(),buttonA3.read(),buttonA4.read(),buttonB1.read(),buttonB2.read(),buttonB3.read(),buttonB4.read(),buttonC1.read(),buttonC2.read(),buttonC3.read(),buttonC4.read(),buttonD1.read(),buttonD2.read(), DebounceActivationCounter, msg, SampleCnt, msg, (c - l));
     DebounceActivationCounter ++; 
     led3 = !led3; //Blink on debounce
     Beep = ON;
@@ -143,7 +140,7 @@
     };
 
 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-//                              Wait at least <DebounceThreshold> samples       Toggle status            Payload              Save debounce counter           Debounce active         
+//                              Wait at least <DebounceThreshold> samples       Toggle status             Payload              Save debounce counter           Debounce active         
 //----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 void keyPressedA1 ( void ) {if ((LastSampleA1 + DebounceThreshold) < SampleCnt) {LastButtonA1 = buttonA1; PayLoadPressed("F"); LastSampleA1 = SampleCnt;} else {DeboucnceActive("F", LastSampleA1, SampleCnt);};};
 void keyReleasedA1( void ) {if ((LastSampleA1 + DebounceThreshold) < SampleCnt) {LastButtonA1 = buttonA1; /*no key up event*/  LastSampleA1 = SampleCnt;} else {/*no key up debounce needed either*/         ;};};
@@ -174,7 +171,7 @@
 void keyPressedC4 ( void ) {if ((LastSampleC4 + DebounceThreshold) < SampleCnt) {LastButtonC4 = buttonC4; PayLoadPressed("O"); LastSampleC4 = SampleCnt;} else {DeboucnceActive("O", LastSampleC4, SampleCnt);};};
 void keyReleasedC4( void ) {if ((LastSampleC4 + DebounceThreshold) < SampleCnt) {LastButtonC4 = buttonC4; PayLoadRelease("o"); LastSampleC4 = SampleCnt;} else {DeboucnceActive("o", LastSampleC4, SampleCnt);};};
 
-//Push button switches only need a payload and debouncing on the pressed event, and you can ignore the release event (C4 left on for demo purposes)
+//Push button switches only need a payload and debouncing on the pressed event, and you can ignore the release event (D2 left on for demo purposes)
 void keyPressedD1 ( void ) {if ((LastSampleD1 + DebounceThreshold) < SampleCnt) {LastButtonD1 = buttonD1; PayLoadPressed("Q"); LastSampleD1 = SampleCnt;} else {DeboucnceActive("Q", LastSampleD1, SampleCnt);};};
 void keyReleasedD1( void ) {if ((LastSampleD1 + DebounceThreshold) < SampleCnt) {LastButtonD1 = buttonD1; /*no key up event*/  LastSampleD1 = SampleCnt;} else {/*no key up debounce needed either*/         ;};};
 void keyPressedD2 ( void ) {if ((LastSampleD2 + DebounceThreshold) < SampleCnt) {LastButtonD2 = buttonD2; PayLoadPressed("P"); LastSampleD2 = SampleCnt;} else {DeboucnceActive("P", LastSampleD2, SampleCnt);};};
@@ -213,8 +210,6 @@
 void TimerOverflowRecover (void) {
 
     //Reset all counters
-    PollCount2 = 0;
-    PollCount1 = 0;
     SampleCnt = 0;
     LastSampleA1 = 0;
     LastSampleA2 = 0;
@@ -272,19 +267,13 @@
 
 
 //-------------------------------------------------------------------------------------------------------
-// Main loop
+// Main - no loop needed because the Sample timer does all the work
 //-------------------------------------------------------------------------------------------------------
 int main() {
     
     //Init Serial LCD
     lcd.baud(115200);
     lcd.printf("%c", CLS);
-
-    //Init USB COM port for VT100       
-    usbUART.baud(115200);
-
-    //VT100 clear screen (ESC[2J) , move cursor (ESC[1;1H) to Row 1, Col 1
-    usbUART.printf("%c[2J%c[1;1H", ESC , ESC );
     
     InitButtons();
     
@@ -295,28 +284,6 @@
     Beep = ON; 
     KillBeepAt = SampleCnt + 50;
 
-
-    PollCount1 = 0;
-    while (1) {
+    lcd.printf("Ready !!");
 
-        PollCount1 ++;
-       
-        //Update terminal every 500000'th pass
-        if ( PollCount1 % 500000 == 0 ) {
-        
-            PollCount1 = 0;
-            PollCount2 ++;
-           
-            //Send to VT100 terminal emulator on the PC USB COM port
-            //And because i think scrolling screens are annoying, i added cursor control codes below
-            //VT100 move cursor (ESC[1;1H) to Row 1, Col 1
-            usbUART.printf("%c[1;1H", ESC );
-            usbUART.printf("A%i%i%i%i ",buttonA1.read(),buttonA2.read(),buttonA3.read(),buttonA4.read());
-            usbUART.printf("B%i%i%i%i ",buttonB1.read(),buttonB2.read(),buttonB3.read(),buttonB4.read());
-            usbUART.printf("C%i%i%i%i ",buttonC1.read(),buttonC2.read(),buttonC3.read(),buttonC4.read());
-            usbUART.printf("D%i%i @ %ld, %ld bounces",buttonD1.read(),buttonD2.read(), PollCount2, DebounceActivationCounter);
-
-        }
-        
-    }
 }