チャタリング防止できそうなものです。

Dependents:   2019NHK_A_manual_red 2019NHK_A_manual_red 2019NHK_A_manual_blue

Revision:
2:866aa77e2fd6
Parent:
0:443871490643
Child:
3:dce990775412
--- a/chatteringremoval.cpp	Fri Aug 30 06:31:54 2019 +0000
+++ b/chatteringremoval.cpp	Mon Sep 02 01:39:35 2019 +0000
@@ -1,10 +1,27 @@
 #include"chatteringremoval.h"
 
-chatteringremoval::chatteringremoval(PinName pin):
+chatteringremoval::chatteringremoval(PinName pin,float time):
     d_in(pin)
 {
     d_in.mode(PullUp);
+    _time = time;
+    value = true;
     flag = false;
+    flag_ = false;
+    mode = true;
+    thread.start(callback(this, &chatteringremoval::threadloop));
+}
+
+chatteringremoval::chatteringremoval(float time):
+    d_in(NC)
+{
+    _time = time;
+    value = true;
+    b = true;
+    flag = false;
+    flag_ = false;
+    mode = false;
+    thread.start(callback(this, &chatteringremoval::threadloop));
 }
 
 unsigned int chatteringremoval::getCount()
@@ -12,43 +29,52 @@
     return count;
 }
 
-void chatteringremoval::setInvalidationtime_s(unsigned int time)
-{
-    _time = time * 1000 * 1000;    
-    thread.start(callback(this, &chatteringremoval::threadloop));
-}
-
-void chatteringremoval::setInvalidationtime_ms(unsigned int time)
-{
-    _time = time * 1000 ;  
-    thread.start(callback(this, &chatteringremoval::threadloop));
-}
-
-void chatteringremoval::setInvalidationtime_us(unsigned int time)
-{
-    _time = time;    
-    thread.start(callback(this, &chatteringremoval::threadloop));
-}
-
 void chatteringremoval::countreset()
 {
     count = 0;    
 }
+
+void chatteringremoval::valuecount()
+{
+    if(value) flag_ = false;
+    if(!value && !flag_){
+        flag_ = true;
+        count++;    
+    }
+}
+
+void chatteringremoval::assignvalue(bool data)
+{
+    _data = data;
+}
+
 void chatteringremoval::threadloop()
 {
     while(true){
-        bool b = d_in.read();
-        
-        if(!b && !flag){
-            flag = true;
-            count++;   
-            t.reset();
-            t.start(); 
-        }
+        checkchattering();
+        valuecount();
+    }    
+}
+void chatteringremoval::checkchattering()
+{
+    if(mode) b = d_in.read();
+    else b = _data;
+     
+    if(!b && !flag){
+        flag = true;
+        value = 0;  
+        t.reset();
+        t.start(); 
+    }
+    
+    
+    if((t.read_us() >= _time*1000*1000) && b){
+        value = 1;
+        flag = false;
+    }    
+}
 
-        
-        if(t.read_us() >= _time && b){
-            flag = false;
-        }
-    }    
+bool chatteringremoval::getValue()
+{
+    return value;
 }
\ No newline at end of file