2018 revision to classic DataBus AVC code.

Dependencies:   LSM303DLM Servo SerialGraphicLCD L3G4200D IncrementalEncoder SimpleShell

Revision:
33:74c514aea0a1
Parent:
32:eb673f6f5734
Child:
35:c42e7e29c3bd
--- 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");