Index: trunk/FACT++/src/ftmctrl.cc
===================================================================
--- trunk/FACT++/src/ftmctrl.cc	(revision 10538)
+++ trunk/FACT++/src/ftmctrl.cc	(revision 10542)
@@ -132,5 +132,5 @@
             return;
 
-        Out() << endl << kBold << "Error list received:" << endl;
+        Out() << endl << kRed << "Error received:" << endl;
         Out() << fError;
         if (fIsHexOutput)
@@ -557,4 +557,26 @@
     }
 
+    bool SetThreshold(int32_t patch, int32_t value)
+    {
+
+        if (patch>159)
+            return false;
+
+        if (value<0 || value>0xffff)
+            return false;
+
+        if (patch<0)
+        {
+            for (int i=0; i<160; i++)
+                fStaticData[i/4].fDAC[i%4] = value;
+        }
+        else
+            fStaticData[patch/4].fDAC[patch%4] = value;
+
+        CmdSendStatDat();
+
+        return true;
+    }
+
     int GetState() const { return IsConnected() ? fState : (int)FTM::kDisconnected; }
 };
@@ -673,25 +695,23 @@
         kStateIdle         = FTM::kIdle,
         kStateTakingData   = FTM::kTakingData,
-/*
-        kCmdToggleLed,
-        kCmdPing,
-        kCmdReqDynData,
-        kCmdReqStatData,
-        kCmdReqRegister,
-        kCmdSetRegister,
-  */
+
         kCmdTest
     };
 
-    int SetRegister(const Event &evt)
-    {
-        if (evt.GetSize()!=8)
-        {
-            stringstream msg;
-            msg << "SetRegister - Received event has " << evt.GetSize() << " bytes, but expected 8.";
-            T::Fatal(msg);
-
+    bool CheckEventSize(size_t has, const char *name, size_t size)
+    {
+        if (has==size)
+            return true;
+
+        stringstream msg;
+        msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
+        T::Fatal(msg);
+        return false;
+    }
+
+    int SetRegister(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "SetRegister", 8))
             return T::kSM_FatalError;
-        }
 
         const unsigned int *dat = reinterpret_cast<const unsigned int*>(evt.GetData());
@@ -716,13 +736,8 @@
     }
 
-    int GetRegister(const Event &evt)
-    {
-        if (evt.GetSize()!=4)
-        {
-            stringstream msg;
-            msg << "GetRegister - Received event has " << evt.GetSize() << "bytes,  but expected 2.";
-            T::Fatal(msg);
+    int GetRegister(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "GetRegister", 4))
             return T::kSM_FatalError;
-        }
 
         const unsigned int addr = evt.GetInt();
@@ -737,14 +752,8 @@
     }
 
-    int TakeNevents(const Event &evt)
-    {
-        if (evt.GetSize()!=4)
-        {
-            stringstream msg;
-            msg << "TakeNevents - Received event has " << evt.GetSize() << " bytes, but expected 4.";
-            T::Fatal(msg);
-
+    int TakeNevents(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "TakeNevents", 4))
             return T::kSM_FatalError;
-        }
 
         const unsigned int dat = evt.GetUInt();
@@ -764,14 +773,8 @@
     }
 
-    int DisableReports(const Event &evt)
-    {
-        if (evt.GetSize()!=1)
-        {
-            stringstream msg;
-            msg << "DisableReports - Received event has " << evt.GetSize() << " bytes, but expected 1.";
-            T::Fatal(msg);
-
+    int DisableReports(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "DisableReports", 1))
             return T::kSM_FatalError;
-        }
 
         fFTM.CmdDisableReports(evt.GetText()[0]!=0);
@@ -780,14 +783,8 @@
     }
 
-    int SetVerbosity(const Event &evt)
-    {
-        if (evt.GetSize()!=1)
-        {
-            stringstream msg;
-            msg << "SetVerbosity - Received event has " << evt.GetSize() << " bytes, but expected 1.";
-            T::Fatal(msg);
-
+    int SetVerbosity(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
             return T::kSM_FatalError;
-        }
 
         fFTM.SetVerbose(evt.GetText()[0]!=0);
@@ -796,5 +793,5 @@
     }
 
-    int LoadStaticData(const Event &evt)
+    int LoadStaticData(const EventImp &evt)
     {
         if (fFTM.LoadStaticData(evt.GetString()))
@@ -814,5 +811,5 @@
     }
 
-    int SaveStaticData(const Event &evt)
+    int SaveStaticData(const EventImp &evt)
     {
         if (fFTM.SaveStaticData(evt.GetString()))
@@ -828,4 +825,16 @@
     }
 
+    int SetThreshold(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "SetThreshold", 8))
+            return T::kSM_FatalError;
+
+        const int32_t *data = reinterpret_cast<const int32_t*>(evt.GetData());
+
+        if (!fFTM.SetThreshold(data[0], data[1]))
+            T::Warn("SetThreshold - Maximum allowed patch number 159, valid value range 0-0xffff");
+
+        return T::GetCurrentState();
+    }
 
     int Disconnect()
@@ -843,5 +852,5 @@
     }
 
-    int Reconnect(const Event &evt)
+    int Reconnect(const EventImp &evt)
     {
         // Close all connections to supress the warning in SetEndpoint
@@ -957,4 +966,10 @@
              "|status[bool]:disable or enable that the FTM sends rate reports (yes/no)");
 
+        AddConfiguration("SET_THRESHOLD", "I:2", kStateIdle)
+            (boost::bind(&StateMachineFTM::SetThreshold, this, _1))
+            ("Set the comparator threshold"
+             "|Patch[idx]:Index of the patch"
+             "|Threshold[counts]:Threshold to be set in binary counts");
+
         T::AddConfiguration("SET_VERBOSE", "B")
             (boost::bind(&StateMachineFTM::SetVerbosity, this, _1))
@@ -990,14 +1005,9 @@
 
 
-        // RESET_THRESHOLD        val
-        // --> SetThreshold(-1, val)
-
         // SET_THRESHOLD          idx val
-        // ---> SetThreshold(idx, val)
-
-
-        // ENABLE_FTU             idx bool  (-1 for all)
-        // ---> EnableFtu(idx, bool)
-
+        // ---> SetThreshold(idx==-1, val)
+
+        // ENABLE_FTU             idx bool
+        // ---> EnableFtu(idx==-1, bool)
 
         // ENABLE_TRIGGER         bool
@@ -1012,5 +1022,4 @@
         // ---> SetTriggerSequence(val, val, val)
 
-
         // SET_TRIGGER_INTERVAL   val
         // SET_TRIGGER_DELAY      val
@@ -1019,10 +1028,6 @@
         // ---> SetXYZ(val)
 
-
         // SET_PRESCALING         idx val
-        // ---> SetPrescaling(-1, val)
-
-        // RESET_PRESCALING       val
-        // ---> SetPrescaling(idx, val)
+        // ---> SetPrescaling(idx==-1, val)
     }
 
