Index: /trunk/FACT++/src/ratecontrol.cc
===================================================================
--- /trunk/FACT++/src/ratecontrol.cc	(revision 13955)
+++ /trunk/FACT++/src/ratecontrol.cc	(revision 13956)
@@ -16,4 +16,5 @@
 
 #include "HeadersFTM.h"
+#include "HeadersRateScan.h"
 #include "HeadersRateControl.h"
 
@@ -41,4 +42,6 @@
     DimDescribedState fDimFTM;
     DimDescribedState fDimRS;
+
+    DimDescribedService fDimThreshold;
 
     float  fTargetRate;
@@ -218,8 +221,8 @@
     }
 
-    void ProcessCamera(const FTM::DimTriggerRates &sdata)
+    int ProcessCamera(const FTM::DimTriggerRates &sdata)
     {
         if (fCounter++==0)
-            return;
+            return GetCurrentState();
 
         // Caluclate Median and deviation
@@ -241,5 +244,5 @@
         {
             Warn("The median or the deviation of all board rates is zero... cannot calibrate.");
-            return;
+            return GetCurrentState();
         }
 
@@ -283,5 +286,6 @@
         {
             fThresholds.assign(160, fThresholdMin);
-            return;
+            fDimThreshold.Update(fThresholdMin);
+            return RateControl::State::kGlobalThresholdSet;
         }
 
@@ -292,5 +296,8 @@
         const uint16_t diff = fThresholdMin+int16_t(truncf(step));
         if (diff<=fThresholdMin)
-            return;
+        {
+            fDimThreshold.Update(fThresholdMin);
+            return RateControl::State::kGlobalThresholdSet;
+        }
 
         if (fVerbose)
@@ -306,4 +313,6 @@
 
         fThresholdMin = diff;
+
+        return GetCurrentState();
     }
 
@@ -328,8 +337,6 @@
             return GetCurrentState();
 
-        if (!fTriggerOn && !fEnabled)
-            return GetCurrentState();
-
-        if (fDimRS.state()>=5)
+        if (GetCurrentState()<=RateControl::State::kConnected ||
+            GetCurrentState()==RateControl::State::kGlobalThresholdSet)
             return GetCurrentState();
 
@@ -340,5 +347,5 @@
 
         if (GetCurrentState()==RateControl::State::kSettingGlobalThreshold)
-            ProcessCamera(sdata);
+            return ProcessCamera(sdata);
 
         if (GetCurrentState()==RateControl::State::kInProgress)
@@ -363,5 +370,4 @@
         fThresholdMin = fThresholdReference;
         fTriggerRate  = -1;
-        fEnabled      = true;
         fCounter      = 0;
 
@@ -385,14 +391,4 @@
     }
 
-    int SetEnabled(const EventImp &evt)
-    {
-        if (!CheckEventSize(evt, 1))
-            return kSM_FatalError;
-
-        fEnabled = evt.GetBool();
-
-        return GetCurrentState();
-    }
-
     int SetMinThreshold(const EventImp &evt)
     {
@@ -438,11 +434,4 @@
     int Execute()
     {
-        // Dispatch (execute) at most one handler from the queue. In contrary
-        // to run_one(), it doesn't wait until a handler is available
-        // which can be dispatched, so poll_one() might return with 0
-        // handlers dispatched. The handlers are always dispatched/executed
-        // synchronously, i.e. within the call to poll_one()
-        //poll_one();
-
         if (!fDim.online())
             return RateControl::State::kDimNetworkNA;
@@ -453,21 +442,13 @@
 
         if (GetCurrentState()==RateControl::State::kSettingGlobalThreshold)
-        {
-            if (fTriggerRate<0 || fTriggerRate>fTargetRate)
-                return RateControl::State::kSettingGlobalThreshold;
-
+            return RateControl::State::kSettingGlobalThreshold;
+
+        if (GetCurrentState()==RateControl::State::kGlobalThresholdSet)
             return RateControl::State::kGlobalThresholdSet;
-        }
-
-        if (GetCurrentState()==RateControl::State::kGlobalThresholdSet)
-        {
-            if (!fTriggerOn)
-                return RateControl::State::kGlobalThresholdSet;
-            //return RateControl::State::kInProgress;
-        }
-
-        // At least one subsystem is not connected
-        //        if (fDimFTM.state()>=FTM::kConnected)
-        return fTriggerOn && fEnabled && fDimRS.state()<5 ? RateControl::State::kInProgress : RateControl::State::kConnected;
+
+        if (fEnabled && fTriggerOn && fDimRS.state()<RateScan::State::kConfiguring)
+            return RateControl::State::kInProgress;
+
+        return RateControl::State::kConnected;
     }
 
@@ -476,5 +457,6 @@
         fTriggerOn(false), fBlock(40),
         fDimFTM("FTM_CONTROL"),
-        fDimRS("RATE_SCAN")
+        fDimRS("RATE_SCAN"),
+        fDimThreshold("THRESHOLD", "S:1", "Resulting threshold after calibration|threshold[dac]:Resulting threshold from calibration")
     {
         // ba::io_service::work is a kind of keep_alive for the loop.
@@ -514,9 +496,9 @@
             ("");
 
-        AddEvent("START", "")
+        AddEvent("START", "", RateControl::State::kConnected, RateControl::State::kGlobalThresholdSet)
             (bind(&StateMachineRateControl::StartRC, this))
             ("");
 
-        AddEvent("STOP", "")
+        AddEvent("STOP", "", RateControl::State::kSettingGlobalThreshold, RateControl::State::kInProgress)
             (bind(&StateMachineRateControl::StopRC, this))
             ("");
