#1

Dependencies:   mbed SB1602E

Fork of mbed-cloud-connect-sensor-laser-distance by Andrea Corrado

Revision:
9:f2bdb2a79528
diff -r 0f74264cc38a -r f2bdb2a79528 DebounceIn.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebounceIn.h	Mon Dec 10 07:22:31 2018 +0000
@@ -0,0 +1,57 @@
+#include "mbed.h"
+
+class DebounceIn : public DigitalIn
+{
+    public:
+    Ticker ticker;
+    int    shadow;
+    int    counter;
+    int    samples;
+    
+public:
+
+    /* Setzt die Entprellzeit (in µs), default sind 1000µs (1ms) */
+    void set_debounce_us(int i) {
+     ticker.attach_us(callback(this,&DebounceIn::cb), i);
+    //ticker.attach_us(this, &DebounceIn::cb, i);
+    //ticker.attach_us(&DebounceIn::cb, i);
+    }
+
+    /* Definiert die Anzahl der Samples um den 'Schatten'-Zustand des DigitalIn-Pins zu schalten */
+    void set_samples(int i) {
+        samples = i;
+    }
+
+    /* Liest den Wert des entprellten Pins */
+    int Read(void) {
+        return shadow;
+    }
+
+//#ifdef MBED_OPERATORS
+       operator int() { return Read(); }
+//#endif
+
+    /* Konstruktor */
+    DebounceIn(PinName pin) : DigitalIn(pin) {
+        counter = 0;
+        samples = 10;
+        // shadow = 0;
+        set_debounce_us(1000);
+    }
+
+    
+
+protected:
+
+    void cb() {
+        if (!DigitalIn::read()) {
+            if (counter < samples) counter++;
+            if (counter == samples) shadow = 1;
+        } else {
+            if (counter > 0) counter--;
+            if (counter == 0) shadow = 0;
+        }
+    }
+
+
+};
\ No newline at end of file