Debounce_Library

Dependents:   EJ3_Cafetera EJ1

Revision:
3:df73a4aa4ff8
Parent:
0:16d62113f1d5
--- a/antirrebote.cpp	Thu May 03 23:32:45 2018 +0000
+++ b/antirrebote.cpp	Tue Jun 12 17:11:31 2018 +0000
@@ -1,56 +1,77 @@
 #include "antirrebote.h"
 
-enum{PULS_NA, PULS_FLC1, PULS_A, PULS_FLC2};
+enum {PRESS=0, NO_PRESS=1, S_NO_PRESS, S_DETECT_PULS, S_DEB1, S_PRESS, S_RETENTION, S_DEB2, DEB_TIME = 20};
 
-AntReb::~AntReb(){
-    }
-AntReb::AntReb(){
-    e_PULS = PULS_NA;
-    }
+AntReb::~AntReb()
+{
+}
+AntReb::AntReb()
+{
+    state = S_NO_PRESS;
+    deb_time = DEB_TIME;
+    hab=0;
+    out=0;
+}
 
 void AntReb::setPin(DigitalIn pin)
 {
-    P_PULS =pin.read();
+    Pulsador = pin.read();
+}
+void AntReb::DebTime()
+{
+    if(deb_time>0)
+        deb_time--;
+    else
+        deb_time = DEB_TIME;
 }
 
-pinEstado_t AntReb::antiRebote(){
-    pinEstado_t StateOut;
+pinEstado_t AntReb::antiRebote()
+{
 
-   switch(e_PULS){
+    switch(state) {
+        case S_NO_PRESS:
         default:
-        case PULS_NA:
-           StateOut = APAGADO;
-            if(P_PULS == 0) {
-                e_PULS = PULS_FLC1;
-                }
+            if (Pulsador == PRESS) {
+                 state = S_DETECT_PULS;
+            }
+        break;
+        
+        case S_DETECT_PULS:
+            if (Pulsador == PRESS && deb_time < 20){
+                hab=1;
+                state = S_DEB1;
+            } else if(Pulsador == NO_PRESS)
+                state = S_NO_PRESS;
             break;
-        case PULS_FLC1:
-            StateOut = APAGADO;
-            if(P_PULS == 0) {
-                e_PULS = PULS_A;
-                StateOut= ENCENDIDO;
-            }else if(P_PULS == 1) {
-                e_PULS = PULS_NA;
+
+        case S_DEB1:
+            if (deb_time == 0 && hab == 1) {
+                state = S_PRESS;
+                hab=0;
+
             }
             break;
-        case PULS_A:
-            StateOut =APAGADO;
-            if(P_PULS == 1) {
-                e_PULS = PULS_FLC2;
-            }
+
+        case S_PRESS:
+            out = 1;
+            state= S_RETENTION;
             break;
-        case PULS_FLC2:
-            StateOut = APAGADO;
-            if(P_PULS == 0) {
-                e_PULS = PULS_A;
-            }else if(P_PULS == 1) {
-                e_PULS = PULS_NA;
-            
+
+        case S_RETENTION:
+            out = 0;
+            if (Pulsador == NO_PRESS)
+                state = S_DEB2;
+            break;
+
+        case S_DEB2:
+            if (deb_time == 20)
+                hab=1;
+            else if (deb_time == 0 && hab == 1) {
+                state = S_NO_PRESS;
+                hab=0;
             }
             break;
     }
-    return StateOut;
+    return out;
 }
 
-
-