Index: trunk/FACT++/src/ratescan.cc
===================================================================
--- trunk/FACT++/src/ratescan.cc	(revision 13892)
+++ trunk/FACT++/src/ratescan.cc	(revision 13893)
@@ -38,4 +38,5 @@
         kStateConnecting,
         kStateConnected,
+        kStateConfiguring,
         kStateInProgress,
     };
@@ -103,5 +104,5 @@
             return GetCurrentState();
 
-        if (fThreshold<0/* || fDimFTM.state()!=FTM::kTakingData*/)
+        if (GetCurrentState()!=kStateInProgress)
             return GetCurrentState();
 
@@ -192,6 +193,5 @@
         {
             Message("Rate scan stopped due to timeout.");
-            fThreshold=-1;
-            return GetCurrentState();
+            return kStateConnected;
         }
 
@@ -199,6 +199,5 @@
         {
             Message("Rate scan finished.");
-            fThreshold = -1;
-            return GetCurrentState();
+            return kStateConnected;
         }
 
@@ -245,4 +244,29 @@
 
         UpdateProc();
+
+        const string data = "\0\0\0\0\0\0\0\0ratescan";
+
+        Dim::SendCommand("FAD_CONTROL/SET_FILE_FORMAT", uint16_t(0));
+        Dim::SendCommand("FTM_CONTROL/CONFIGURE", data);
+
+        Message("Configuration for ratescan started.");
+
+        return kStateConfiguring;
+    }
+
+    int HandleFtmStateChange(/*const EventImp &evt*/)
+    {
+        if (GetCurrentState()!=kStateConfiguring)
+            return GetCurrentState();
+
+        if (fDimFTM.state()!=FTM::kConfigured)
+            return GetCurrentState();
+
+        const int32_t data[2] = { -1, fThresholdMin };
+
+        Dim::SendCommand(fCommand, data);
+
+        fThreshold = fThresholdMin;
+        fCounter = -2;
 
         const Time now;
@@ -262,16 +286,4 @@
         fout << "# -----" << endl;
 
-        Dim::SendCommand("FAD_CONTROL/SET_FILE_FORMAT", uint16_t(0));
-
-        const int32_t data[2] = { -1, fThresholdMin };
-
-        //Message("Starting Trigger (FTM)");
-        //Dim::SendCommand("FTM_CONTROL/SET_PRESCALING", int32_t(20));
-        Dim::SendCommand(fCommand, data);
-        //Dim::SendCommand("FTM_CONTROL/STOP_TRIGGER");
-
-        fThreshold = fThresholdMin;
-        fCounter = -2;
-
         ostringstream msg;
         msg << "Rate scan " << now << "(" << fStartTime << ") from " << fThresholdMin << " to ";
@@ -280,19 +292,15 @@
         Message(msg);
 
-        return GetCurrentState();
+        return kStateInProgress;
     }
 
     int StopRateScan()
     {
-        fThreshold = -1;
+        if (GetCurrentState()!=kStateConfiguring && GetCurrentState()!=kStateInProgress)
+            return GetCurrentState();
+
         Message("Rate scan manually stopped.");
 
-        //if (fDimFTM.state()==FTM::kTakingData)
-	{
-            //Message("Stopping FTM");
-	    //Dim::SendCommand("FTM_CONTROL/STOP_TRIGGER");
-        }
-
-        return GetCurrentState();
+        return kStateConnected;
     }
 
@@ -366,11 +374,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 kStateDimNetworkNA;
@@ -380,7 +381,5 @@
             return kStateDisconnected;
 
-        // At least one subsystem is not connected
-        //        if (fDimFTM.state()>=FTM::kConnected)
-        return fThreshold<0 ? kStateConnected : kStateInProgress;
+        return GetCurrentState();
     }
 
@@ -412,4 +411,5 @@
         fDim.Subscribe(*this);
         fDimFTM.Subscribe(*this);
+        fDimFTM.SetCallback(bind(&StateMachineRateScan::HandleFtmStateChange, this));
 
         Subscribe("FTM_CONTROL/TRIGGER_RATES")
@@ -453,5 +453,5 @@
              "|max[int]:Limiting value in DAC counts");
 
-        AddEvent("STOP", kStateInProgress)
+        AddEvent("STOP", kStateConfiguring, kStateInProgress)
             (bind(&StateMachineRateScan::StopRateScan, this))
             ("Stop a ratescan in progress");
