Index: /trunk/FACT++/src/ftmctrl.cc
===================================================================
--- /trunk/FACT++/src/ftmctrl.cc	(revision 11776)
+++ /trunk/FACT++/src/ftmctrl.cc	(revision 11777)
@@ -708,4 +708,59 @@
 
         /*
+          if (data[patch/4].fDAC[patch%4] == value)
+             return true;
+          */
+ 
+        // Calculate offset in static data block
+        const uint16_t addr = (uintptr_t(&fStaticData[patch/4].fDAC[patch%4])-uintptr_t(&fStaticData))/2;
+
+        // From CmdSetRegister
+        const array<uint16_t, 2> data = {{ addr, uint16_t(value) }};
+        PostCmd(data, FTM::kCmdWrite, FTM::kCmdRegister);
+
+        reinterpret_cast<uint16_t*>(&fBufStaticData)[addr] = value;
+
+        // Now execute change before the static data is requested back
+        PostCmd(FTM::kCmdConfigFTU, (patch/40) | (((patch/4)%10)<<8));
+
+        //CmdGetRegister(addr);
+        CmdReqStatDat();
+
+        return true;
+    }
+
+    bool SetNoutof4(int32_t patch, int32_t value)
+    {
+        if (patch>=FTM::StaticData::kMaxMultiplicity)
+            return false;
+
+        if (value<0 || value>FTM::StaticData::kMaxDAC)
+            return false;
+
+        if (patch<0)
+        {
+            FTM::StaticData data(fStaticData);
+
+            bool ident = true;
+            for (int i=0; i<FTM::StaticData::kMaxMultiplicity; i++)
+                if (data[i].fDAC[4] != value)
+                {
+                    ident = false;
+                    break;
+                }
+
+            if (ident)
+                return true;
+
+            for (int i=0; i<=FTM::StaticData::kMaxMultiplicity; i++)
+                data[i].fDAC[4] = value;
+
+            // Maybe move to a "COMMIT" command?
+            CmdSendStatDat(data);
+
+            return true;
+        }
+
+        /*
          if (data[patch/4].fDAC[patch%4] == value)
             return true;
@@ -718,5 +773,5 @@
 
         // Calculate offset in static data block
-        const uint16_t addr = (uintptr_t(&fStaticData[patch/4].fDAC[patch%4])-uintptr_t(&fStaticData))/2;
+        const uint16_t addr = (uintptr_t(&fStaticData[patch].fDAC[4])-uintptr_t(&fStaticData))/2;
 
         // From CmdSetRegister
@@ -1425,4 +1480,17 @@
     }
 
+    int SetNoutof4(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "SetNoutof4", 8))
+            return T::kSM_FatalError;
+
+        const int32_t *data = evt.Ptr<int32_t>();
+
+        if (!fFTM.SetNoutof4(data[0], data[1]))
+            T::Warn("SetNoutof4 - Maximum allowed board number 39, valid value range 0-0xffff");
+
+        return T::GetCurrentState();
+    }
+
     int EnableFTU(const EventImp &evt)
     {
@@ -1921,8 +1989,14 @@
              "|status[bool]:disable or enable that the FTM sends rate reports (yes/no)");
 
-       T::AddEvent("SET_THRESHOLD", "I:2", FTM::kIdle, FTM::kTakingData)
+        T::AddEvent("SET_THRESHOLD", "I:2", FTM::kIdle, FTM::kTakingData)
             (bind(&StateMachineFTM::SetThreshold, this, placeholders::_1))
             ("Set the comparator threshold"
              "|Patch[idx]:Index of the patch (0-159), -1 for all"
+             "|Threshold[counts]:Threshold to be set in binary counts");
+
+        T::AddEvent("SET_N_OUT_OF_4", "I:2", FTM::kIdle, FTM::kTakingData)
+            (bind(&StateMachineFTM::SetNoutof4, this, placeholders::_1))
+            ("Set the comparator threshold"
+             "|Board[idx]:Index of the board (0-39), -1 for all"
              "|Threshold[counts]:Threshold to be set in binary counts");
 
@@ -2065,5 +2139,5 @@
         // A new configure will first stop the FTM this means
         // we can allow it in idle _and_ taking data
-        T::AddEvent("CONFIGURE", "C", FTM::kIdle, FTM::kTakingData)
+        T::AddEvent("CONFIGURE", "C", FTM::kIdle, FTM::kConfiguring1, FTM::kConfiguring2, FTM::kConfigured, FTM::kTakingData)
             (bind(&StateMachineFTM::ConfigureFTM, this, placeholders::_1))
             ("");
@@ -2262,6 +2336,6 @@
                 !CheckConfigVal<uint16_t>(conf, data.kMaxIntensity,       "light-pulser.external.intensity.",     *it) ||
                 !CheckConfigVal<uint16_t>(conf, data.kMaxIntensity,       "light-pulser.internal.intensity.",     *it) ||
-                !CheckConfigVal<uint16_t>(conf, data.kMaxDAC,             "trigger.threshold.pixel.",             *it) ||
-                !CheckConfigVal<uint16_t>(conf, data.kMaxDAC,             "trigger.threshold.patch.",            *it) ||
+                !CheckConfigVal<uint16_t>(conf, data.kMaxDAC,             "trigger.threshold.patch.",             *it) ||
+                !CheckConfigVal<uint16_t>(conf, data.kMaxDAC,             "trigger.threshold.logic.",             *it) ||
                 0)
                 return 2;
@@ -2336,6 +2410,6 @@
             }
 
-            const uint16_t th0 = conf.GetDef<uint16_t>("trigger.threshold.pixel.", *it);
-            const uint16_t th1 = conf.GetDef<uint16_t>("trigger.threshold.patch.", *it);
+            const uint16_t th0 = conf.GetDef<uint16_t>("trigger.threshold.patch.", *it);
+            const uint16_t th1 = conf.GetDef<uint16_t>("trigger.threshold.logic.", *it);
 
             for (int i=0; i<40; i++)
@@ -2433,6 +2507,6 @@
         ("trigger.disable-pixel.*",              vars<uint16_t>(), "")
         ("trigger.disable-patch.*",              vars<uint16_t>(), "")
-        ("trigger.threshold.pixel.*",            var<uint16_t>(),  "")
         ("trigger.threshold.patch.*",            var<uint16_t>(),  "")
+        ("trigger.threshold.logic.*",            var<uint16_t>(),  "")
         ("ftu-report-interval.*",                var<uint16_t>(),  "")
         ("light-pulser.external.enable-group1.*", var<bool>(),     "Enable LED group 1 of external light pulser")
