diff Button.cpp @ 1:284066a427c8

added flanks detection
author ark
date 2017-01-25 23:12 +0100
parents 968a3e3eb8ef
children b76b999b88c7
line wrap: on
line diff
--- a/Button.cpp	Sun Jan 22 23:39:37 2017 +0100
+++ b/Button.cpp	Wed Jan 25 23:12:16 2017 +0100
@@ -20,7 +20,7 @@
     this->pin = pin;
     this->isPullup = pu;
     pinMode(this->pin, this->isPullup?INPUT_PULLUP:INPUT);
-    this->isPressed = this->readState();
+    this->isOn = this->readState();
     this->lastChangeMs = 0;
 }
 
@@ -32,17 +32,18 @@
 void Button::update(unsigned long ms) {
     bool s = this->readState();
     // process state change
-    if(this->isPressed != s) {
+    if(this->isOn != s) {
         // if last state change is older than threshold, accept new state
         // change is not a bounce.
         if(ms - this->lastChangeMs >= Button::BOUNCE_THRESHOLD) {
-            this->isPressed = s;
+            this->isPressed = this->isOn;
+            this->isReleased = !this->isOn;
+            this->isOn = s;
             this->lastChangeMs = ms;
         }
-        else {
-            Serial.print("ignoring bounce at ");
-            Serial.println(ms);
-        }
+    } else {
+        this->isPressed = false;
+        this->isReleased = false;
     }
 }