Debaunced Input like InterruptIn.

Dependents:   LcdClock idd_hw2_martincowell_bicyclehid IDD_FALL15_HW2_JMARQUIS_Keypadentry

Files at this revision

API Documentation at this revision

Comitter:
togayan
Date:
Sat Feb 22 23:55:10 2014 +0000
Commit message:
1st release.

Changed in this revision

DebouncedEdgeIn.cpp Show annotated file Show diff for this revision Revisions of this file
DebouncedEdgeIn.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 471239ea932b DebouncedEdgeIn.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedEdgeIn.cpp	Sat Feb 22 23:55:10 2014 +0000
@@ -0,0 +1,78 @@
+#include "DebouncedEdgeIn.h"
+#include "mbed.h"
+
+/*
+ * Constructor
+ */
+DebouncedEdgeIn::DebouncedEdgeIn(PinName in)
+    : _in(in),
+      _ticker(),
+      _rise(),
+      _fall(),
+      // reset all the flags and counters
+      _samples(0),
+      _output(0),
+      _output_last(0)
+{
+    // Attach ticker
+    _ticker.attach(this, &DebouncedEdgeIn::_sample, 0.005);
+}
+
+DebouncedEdgeIn::~DebouncedEdgeIn() {
+    _ticker.detach();
+}
+
+void DebouncedEdgeIn::rise(void (*fptr)(void))
+{
+    _rise.attach(fptr);
+}
+
+void DebouncedEdgeIn::fall(void (*fptr)(void))
+{
+    _fall.attach(fptr);
+}
+
+void DebouncedEdgeIn::mode(PinMode pull) {
+    _in.mode(pull);
+}
+
+// return the debounced status
+int DebouncedEdgeIn::read(void)
+{
+    return(_output);
+}
+
+// shorthand for read()
+DebouncedEdgeIn::operator int()
+{
+    return read();
+}
+
+void DebouncedEdgeIn::_sample(void)
+{
+    // take a sample
+    _samples = _samples >> 1; // shift left
+
+    if (_in) {
+        _samples |= 0x80;
+    }
+
+    // examine the sample window, look for steady state
+    if (_samples == 0x00) {
+        _output = 0;
+    } else if (_samples == 0xFF) {
+        _output = 1;
+    }
+
+    // Rising edge detection
+    if ((_output == 1) && (_output_last == 0)) {
+        _rise.call();
+    }
+    // Falling edge detection
+    else if ((_output == 0) && (_output_last == 1)) {
+        _fall.call();
+    }
+
+    // update the output
+    _output_last = _output;
+}
diff -r 000000000000 -r 471239ea932b DebouncedEdgeIn.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedEdgeIn.h	Sat Feb 22 23:55:10 2014 +0000
@@ -0,0 +1,70 @@
+#ifndef DEBAUNCED_EDGE_IN_H
+#define DEBAUNCED_EDGE_IN_H
+
+#include "mbed.h"
+#include "FunctionPointer.h"
+
+class DebouncedEdgeIn
+{
+public:
+    DebouncedEdgeIn(PinName in);
+    virtual ~DebouncedEdgeIn();
+
+    int read (void);
+    operator int();
+
+    /** Attach a function to call when a rising edge occurs on the input
+     *
+     *  @param fptr A pointer to a void function, or 0 to set as none
+     */
+    void rise(void (*fptr)(void));
+
+    /** Attach a member function to call when a rising edge occurs on the input
+     *
+     *  @param tptr pointer to the object to call the member function on
+     *  @param mptr pointer to the member function to be called
+     */
+    template<typename T>
+    void rise(T* tptr, void (T::*mptr)(void)) {
+        _rise.attach(tptr, mptr);
+    }
+
+    /** Attach a function to call when a falling edge occurs on the input
+     *
+     *  @param fptr A pointer to a void function, or 0 to set as none
+     */
+    void fall(void (*fptr)(void));
+
+    /** Attach a member function to call when a falling edge occurs on the input
+     *
+     *  @param tptr pointer to the object to call the member function on
+     *  @param mptr pointer to the member function to be called
+     */
+    template<typename T>
+    void fall(T* tptr, void (T::*mptr)(void)) {
+        _fall.attach(tptr, mptr);
+    }
+
+    /** Set the input pin mode
+     *
+     *  @param mode PullUp, PullDown, PullNone
+     */
+    void mode(PinMode pull);
+    
+private :
+    // objects
+    DigitalIn _in;
+    Ticker _ticker;
+    FunctionPointer _rise;
+    FunctionPointer _fall;
+
+    // function to take a sample, and update flags
+    void _sample(void);
+
+    // counters and flags
+    int _samples;
+    int _output;
+    int _output_last;
+};
+
+#endif // DEBAUNCED_EDGE_IN_H