Detected Iteration Above Threshold in Unit Time. Check if Frequency (iteration count in Unit time) above Threshold. CATION. max interval is 0xfff0: 65,520ms. max iter threshold is 0xffff. (threfore, using 131KB with span[0xffff].)

Dependencies:   myTimer

Revision:
0:7672973d6bed
Child:
1:6ab4e8504d60
diff -r 000000000000 -r 7672973d6bed DetectFreqAboveTH.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DetectFreqAboveTH.cpp	Thu Oct 08 05:35:04 2015 +0000
@@ -0,0 +1,49 @@
+#include "DetectFreqAboveTH.h"
+
+
+
+DetectFreqAboveTH::DetectFreqAboveTH(int time_ms, int iter)
+{
+    unitTime= time_ms;
+    iterThreshold= iter;
+    span= new unsigned short[iter];
+//init
+    for(id= 0; id < iterThreshold; id++)
+        span[id]= 0xffff;
+    this->timer.start(true);
+    id= 0;
+}
+
+DetectFreqAboveTH::~DetectFreqAboveTH()
+{
+    delete [] span;
+}
+
+
+bool DetectFreqAboveTH::chkAbove()
+{
+    int tmpSpan= this->timer.read_ms(); //reset
+
+    if(tmpSpan > 0xfff0)    // 65,520ms.
+        tmpSpan= 0xffff;
+
+    span[id]= tmpSpan;
+    id++;
+    id %= iterThreshold;
+    if(tmpSpan == 0xffff)
+        return false;
+
+    unsigned int sum= 0, tmp;
+    for(int i= 0; i < iterThreshold; i++) {
+        tmp= span[i];
+        if(tmp == 0xffff)   // under threshold iter count.
+            return false;
+        sum += tmp;
+        if(sum >= unitTime)
+            return false;
+    }
+    // sumが単位時間未満なので、発生回数(/単位時間)は閾値以上。
+    return true;
+}
+
+// EOF
\ No newline at end of file