Index: trunk/FACT++/src/smartfact.cc
===================================================================
--- trunk/FACT++/src/smartfact.cc	(revision 13589)
+++ trunk/FACT++/src/smartfact.cc	(revision 13609)
@@ -55,7 +55,8 @@
 
     uint32_t fMcpConfigurationState;
-    uint64_t fMcpConfigurationMaxTime;
-    uint64_t fMcpConfigurationMaxEvents;
+     int64_t fMcpConfigurationMaxTime;
+     int64_t fMcpConfigurationMaxEvents;
     string   fMcpConfigurationName;
+    Time     fMcpConfigurationRunStart;
 
     enum weather_t { kWeatherBegin=0, kTemp = kWeatherBegin, kDew, kHum, kPress, kWind, kGusts, kDir, kWeatherEnd = kDir };
@@ -91,5 +92,5 @@
     public:
         DimState(const string &n, const string s="STATE") :
-            server(n), info(make_pair(Time(-1), -2)),
+            server(n), info(make_pair(Time(), -2)),
             dim((n+"/"+s).c_str(), (void*)NULL, 0, this) { }
 
@@ -248,4 +249,7 @@
         fMcpConfigurationMaxEvents = d.get<uint64_t>(8);
         fMcpConfigurationName      = d.ptr<char>(16);
+
+        if (d.qos==12)
+            fMcpConfigurationRunStart = Time();
     }
 
@@ -343,5 +347,5 @@
 
         // Simplified:
-        const double dev = cos(dzd) - sin(zd)*sin(zd)*(1.-cos(daz));
+        const double dev = cos(dzd) - sin(zd+dzd)*sin(zd)*(1.-cos(daz));
         fDriveControlTrackingDev = acos(dev) * 180 / M_PI * 3600;
         if (fDriveControlTrackingDev<0.01)
@@ -405,8 +409,16 @@
         fBiasControlVoltageMed = (v[159]+v[160])/2;
 
-        const char *ptr = d.ptr<char>();
-
-        ofstream fout("www/biascontrol-voltage.bin");
-        fout.write(ptr, 320*sizeof(float));
+        //const char *ptr = d.ptr<char>();
+        //ofstream fout("www/biascontrol-voltage.bin");
+        //fout.write(ptr, 320*sizeof(float));
+
+        vector<float> val(320, 0);
+        for (int i=0; i<320; i++)
+        {
+            const int idx = (fPixelMap.hv(i).hw()/9)*2+fPixelMap.hv(i).group();
+            val[i] = v[i];
+        }
+
+        WriteBinary("biascontrol-voltage", val, 75);
     }
 
@@ -427,7 +439,10 @@
 
         // Get the maximum of each patch
-        vector<float> val(160, 0);
-        for (int i=0; i<160; i++)
-            val[i] = max(v[i*2], v[i*2+1]);
+        vector<float> val(320, 0);
+        for (int i=0; i<320; i++)
+        {
+            const int idx = (fPixelMap.hv(i).hw()/9)*2+fPixelMap.hv(i).group();
+            val[idx] = v[i];//max(v[i*2], v[i*2+1]);
+        }
 
         // Write the 160 patch values to a file
@@ -465,12 +480,12 @@
             fFtmControlTriggerRateHist.pop_front();
 
+        // FIXME: Add statistics for all kind of rates
+
         WriteBinary("ftmcontrol-triggerrate-hist",
                     fFtmControlTriggerRateHist, 100);
         WriteBinary("ftmcontrol-boardrates",
-                    vector<float>(brates, brates+40), 50);
+                    vector<float>(brates, brates+40), 10);
         WriteBinary("ftmcontrol-patchrates",
                     vector<float>(prates, prates+160), 10);
-
-//        for (int i=0; i<160; i++) cout << prates[i] << endl;
 
         ostringstream out;
@@ -493,8 +508,11 @@
         //const float *pos = d.ptr<float>(1440*sizeof(float)*3);
 
-        vector<float> max(160, -2000);
+        vector<float> max(320, -2000);
         for (int i=0; i<1440; i++)
         {
-            const int idx = fPixelMap.index(i).hw()/9;
+            if (i%9==8)
+                continue;
+
+            const int idx = (fPixelMap.hw(i).hw()/9)*2+fPixelMap.hw(i).group();
             if (dat[i]>max[idx])
                 max[idx]=dat[i];
@@ -505,5 +523,5 @@
         case 0:  WriteBinary("fadcontrol-eventdata", max, 2000, 1000); break;
         case 1:  WriteBinary("fadcontrol-eventdata", max, 2000,    0); break;
-        default: WriteBinary("fadcontrol-eventdata", max, 1000,  500); break;
+        default: WriteBinary("fadcontrol-eventdata", max,  250,    0); break;
         }
     }
@@ -715,5 +733,7 @@
         {
             string col = kHtmlBlue;
-            if (fMcpConfigurationState!=5)
+            if (fMcpConfigurationState!= 5 &&
+                fMcpConfigurationState!=11 &&
+                fMcpConfigurationState!=12) // 9 e.g. Configuring3
                 col = kHtmlYellow;
             else
@@ -721,18 +741,37 @@
                     col = kHtmlGreen;
 
-            out << fMcpConfigurationName;
-            if (fMcpConfigurationMaxEvents>0 || fMcpConfigurationMaxTime>0)
+            out << col << '\t' << fMcpConfigurationName;
+
+            if (fMcpConfigurationMaxEvents>0 || fMcpConfigurationState==12)
                 out << " [";
             if (fMcpConfigurationMaxEvents>0)
                 out << fMcpConfigurationMaxEvents;
-            if (fMcpConfigurationMaxEvents>0 && fMcpConfigurationMaxTime>0)
-                out << "/";
+            if (fMcpConfigurationMaxEvents>0 || fMcpConfigurationState==12)
+                out << '/';
             if (fMcpConfigurationMaxTime>0)
-                out << fMcpConfigurationMaxTime << "s";
-            if (fMcpConfigurationMaxEvents>0 || fMcpConfigurationMaxTime>0)
-                out << "]";
+            {
+                if (fMcpConfigurationState==12)
+                    out << fMcpConfigurationMaxTime-(Time()-fMcpConfigurationRunStart).total_seconds() << 's';
+                else
+                    out << fMcpConfigurationMaxTime << 's';
+            }
+            else
+            {
+                if (fMcpConfigurationState==12)
+                {
+                    ostringstream d;
+                    d << Time()-fMcpConfigurationRunStart;
+                    out << d.str().substr(3, 5);
+                }
+            }
+
+            if (fMcpConfigurationMaxEvents>0 || fMcpConfigurationState==12)
+                out << ']';
+
+
         }
         else
-            out << kHtmlWhite << '\n';
+            out << kHtmlWhite;
+        out << '\n';
 
         // ------------------ Drive -----------------
@@ -740,5 +779,10 @@
         {
             const State rc = GetState(fDimDriveControl);
-            out << kHtmlWhite << '\t';
+            string col = kHtmlGreen;
+            if (rc.index==6) // Moving
+                col = kHtmlBlue;
+            if (rc.index==5) // Armed
+                col = kHtmlWhite;
+            out << col << '\t';
             out << rc.name << '\t';
             out << fDriveControlPointingZd  << '\t';
@@ -746,5 +790,7 @@
             if (fDimDriveControl.state()==7)
             {
+                out << setprecision(2);
                 out << fDriveControlTrackingDev << '\t';
+                out << setprecision(3);
                 out << fDriveControlSourceName  << '\n';
             }
@@ -818,8 +864,18 @@
                 col = kHtmlBlue;
 
-            out << col << '\t';
-            out << fBiasControlCurrentMed << '\t';
-            out << fBiasControlCurrentMax << '\t';
-            out << fBiasControlVoltageMed << '\n';
+            if (fDimFeedback.state()==13)
+            {
+                out << kHtmlBlue << '\t';
+                out << "***\t";
+                out << "***\t";
+                out << "---\n";
+            }
+            else
+            {
+                out << col << '\t';
+                out << fBiasControlCurrentMed << '\t';
+                out << fBiasControlCurrentMax << '\t';
+                out << fBiasControlVoltageMed << '\n';
+            }
         }
         else
@@ -895,4 +951,6 @@
         fDimFtmControlTriggerRates("FTM_CONTROL/TRIGGER_RATES",       (void*)NULL, 0, this),
         //-
+        fMcpConfigurationMaxEvents(0),
+        fMcpConfigurationMaxTime(0),
         fDimFadControlEventData(0)
     {
