This library is push button or dipSw noise cancel and on/off edge recognition

Dependents:   led-sound_checker clockGenerator

Fork of DigitalSw by suu pen

sample program "DigitalSW_hello" https://developer.mbed.org/users/suupen/code/DigitalSW_hello/rev/ffa16c2a347f

Files at this revision

API Documentation at this revision

Comitter:
suupen
Date:
Sat Oct 29 01:46:17 2016 +0000
Parent:
1:cd8bb4f0b08d
Commit message:
sw input use interrupt.; pullup/pulldown setting.

Changed in this revision

DigitalSw.cpp Show annotated file Show diff for this revision Revisions of this file
DigitalSw.h Show annotated file Show diff for this revision Revisions of this file
diff -r cd8bb4f0b08d -r 9198c5c0afcb DigitalSw.cpp
--- a/DigitalSw.cpp	Sun Oct 23 09:46:01 2016 +0000
+++ b/DigitalSw.cpp	Sat Oct 29 01:46:17 2016 +0000
@@ -7,8 +7,8 @@
 /**
  *
  */
-DigitalSw::DigitalSw(PinName sw
-                    ) : _sw(sw)
+DigitalSw::DigitalSw(PinName sw, uint8_t pull
+                    ) : _sw(sw), _pull(pull)
 {
 
     _onEdge = 0;
@@ -16,8 +16,12 @@
     _levelNow = 1;
     _levelOld = 1;
 
-    _sw.mode(PullUp);
-
+    if(_pull == 1) {
+        _sw.mode(PullUp);
+    } else {
+        _sw.mode(PullDown);
+    }
+    
     _sw.rise(this, &DigitalSw::swLevelUp);
     _sw.fall(this, &DigitalSw::swLevelDown);
 
@@ -60,6 +64,7 @@
     uint8_t levelCheck = 0xaa;
 
     while((levelCheck != 0x00) && (levelCheck != 0xff)) {
+        wait_us(100);
         levelCheck = (levelCheck << 1) | _sw.read();
     }
 
@@ -79,6 +84,7 @@
     uint8_t levelCheck = 0xaa;
 
     while((levelCheck != 0x00) && (levelCheck != 0xff)) {
+        wait_us(100);
         levelCheck = (levelCheck << 1) | _sw.read();
     }
 
diff -r cd8bb4f0b08d -r 9198c5c0afcb DigitalSw.h
--- a/DigitalSw.h	Sun Oct 23 09:46:01 2016 +0000
+++ b/DigitalSw.h	Sat Oct 29 01:46:17 2016 +0000
@@ -74,9 +74,10 @@
 
     /** Create a DigitalSw port, connected to the specified sw pins
      *  @param sw pin
+     *  @param (option) Register pull 1:PullUp(default), 0:PullDown
      *  @note  nothing
      */
-    DigitalSw(PinName sw);
+    DigitalSw(PinName sw, uint8_t pull = 1);
 
     /**
      * Reset the edge and level.
@@ -113,6 +114,7 @@
 
 
     InterruptIn _sw;
+    int8_t _pull;
 
     volatile int16_t _onEdge;
     volatile int16_t _offEdge;