Debounce a mechanical switch by periodic sampling.

Dependents:   RedButton WS_7_Seg_mit_LM1635 WS_7_Seg_mit_LM1635 Lichtschalter_M0 ... more

Files at this revision

API Documentation at this revision

Comitter:
huliyang
Date:
Mon May 18 13:25:07 2015 +0000
Parent:
1:e59480cf6c72
Commit message:
Debouncer: re-order fields for better alignment & packing

Changed in this revision

Debouncer.cpp Show annotated file Show diff for this revision Revisions of this file
Debouncer.h Show annotated file Show diff for this revision Revisions of this file
--- a/Debouncer.cpp	Mon May 18 13:08:33 2015 +0000
+++ b/Debouncer.cpp	Mon May 18 13:25:07 2015 +0000
@@ -68,14 +68,14 @@
 //! \param pin Pin to poll.
 //! \param mode Pin mode, e.g. \a PullUp or \a PullDown.
 Debouncer::Debouncer(PinName pin, PinMode mode)
-    : in(pin, mode), now(mode == PullUp), previous(mode == PullUp ? -1 : 0)
+    : in(pin, mode), previous(mode == PullUp ? -1 : 0), now(mode == PullUp)
 {
     samples(); period();
 }
 
 //! Set the number of identical samples needed to regard the input as stable.
 //! \note Limited to at most 32 samples.
-Debouncer &Debouncer::samples(int n)
+Debouncer &Debouncer::samples(uint8_t n)
 {
     _samples = n;
     return *this;
--- a/Debouncer.h	Mon May 18 13:08:33 2015 +0000
+++ b/Debouncer.h	Mon May 18 13:25:07 2015 +0000
@@ -29,14 +29,14 @@
 private:
     FunctionPointer fun_fall, fun_rise;
     DigitalIn in;
+    uint32_t previous;
+    uint8_t _samples;
     bool now;
-    int _samples;
-    uint32_t previous;
     void tick(void);
 
 public:
     Debouncer(PinName pin, PinMode mode = PullUp);
-    Debouncer &samples(int n = 8);
+    Debouncer &samples(uint8_t n = 8);
     Debouncer &period(float seconds = 3.90625e-3);
     operator bool(void);