2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Files at this revision

API Documentation at this revision

Comitter:
shimniok
Date:
Fri Dec 28 17:10:43 2018 +0000
Parent:
32:eb673f6f5734
Child:
34:2f7ebc88596d
Commit message:
debounce improvements

Changed in this revision

SimpleShell.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/SimpleShell.lib	Thu Dec 27 15:46:57 2018 +0000
+++ b/SimpleShell.lib	Fri Dec 28 17:10:43 2018 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/shimniok/code/SimpleShell/#8d4132274445
+https://os.mbed.com/users/shimniok/code/SimpleShell/#27e8130a0d8f
--- a/main.cpp	Thu Dec 27 15:46:57 2018 +0000
+++ b/main.cpp	Fri Dec 28 17:10:43 2018 +0000
@@ -32,9 +32,9 @@
 FATFileSystem ffs("log", &bd);
 Serial pc(USBTX, USBRX);
 RawSerial s(UART1TX, UART1RX, 38400);
-InterruptIn lbutton(LBUTTON, PullUp); // left button
-InterruptIn cbutton(CBUTTON, PullUp); // center button
-InterruptIn rbutton(RBUTTON, PullUp); // right button
+InterruptIn lbutton(LBUTTON, PullUp); // button interrupts
+InterruptIn cbutton(CBUTTON, PullUp);
+InterruptIn rbutton(RBUTTON, PullUp);
 
 ///////////////////////////////////////////////////////////////////////////////
 // Idle hook
@@ -71,12 +71,23 @@
 
 //enum button_mask { LEFT = 0x01, CENTER = 0x02, RIGHT = 0x04 };
 EventQueue buttonQueue(16 * EVENTS_EVENT_SIZE);
-int last = 0;
-const int BUTTON_DEBOUNCE_MS = 100;
+const int BUTTON_DEBOUNCE_SAMPLES = 20;
+const int BUTTON_DEBOUNCE_THRESH = 8;
+const int BUTTON_SAMPLE_MS = 0.005;
 
 void button_event() {
-    int now = Kernel::get_ms_count();
-    if ((now - last) > BUTTON_DEBOUNCE_MS) {
+    int samples = 0;
+
+    // disable subsequent interrupts
+    lbutton.fall(NULL);
+ 
+    // sample repeatedly to debounce
+    for (int i=0; i < BUTTON_DEBOUNCE_SAMPLES; i++) {
+        if (lbutton == 0) samples++;
+        wait(BUTTON_SAMPLE_MS);
+    }
+    
+    if (samples > BUTTON_DEBOUNCE_THRESH) {
         if (logger.enabled()) {
             logQueue.call(&logger, &Logger::stop);
             led3 = 0;
@@ -85,7 +96,8 @@
             led3 = 1;
         }
     }
-    last = now;
+    
+    lbutton.fall(buttonQueue.event(button_event));
 }
 
 
@@ -244,7 +256,7 @@
     }
 
     printf("Starting buttons...\n");
-    lbutton.rise(buttonQueue.event(button_event));
+    lbutton.fall(buttonQueue.event(button_event));
     //cbutton.rise(buttonQueue.event(button_event));
     //rbutton.rise(buttonQueue.event(button_event));
     Thread buttonThread(osPriorityNormal, 256, 0, "buttons");