Index: /trunk/FACT++/src/biasctrl.cc
===================================================================
--- /trunk/FACT++/src/biasctrl.cc	(revision 11857)
+++ /trunk/FACT++/src/biasctrl.cc	(revision 11858)
@@ -62,4 +62,8 @@
 
     virtual void UpdateA()
+    {
+    }
+
+    virtual void UpdateV()
     {
     }
@@ -195,5 +199,5 @@
 
         SystemReset();
-        ReadAllChannelsStatus();
+        ReadAllChannels();
     }
 
@@ -258,10 +262,4 @@
 
         return data;
-    }
-
-    void GlobalSetDac(uint16_t dac)
-    {
-        PostMessage(GetCmd(0, kCmdGlobalSet, dac));
-        AsyncRead(ba::buffer(fBuffer), kCmdGlobalSet);
     }
 
@@ -315,18 +313,53 @@
     }
 
-    bool GlobalSet(double voltage)
-    {
-        const uint16_t dac = voltage*4096/90;
+    void SystemReset()
+    {
+        Message("Sending system reset.");
+        PostMessage(GetCmd(0, kCmdReset));
+        AsyncRead(ba::buffer(fBuffer), kCmdReset);
+    }
+
+    void ReadChannel(int ch)
+    {
+        PostMessage(GetCmd(ch, kCmdRead));
+        AsyncRead(ba::buffer(fBuffer), kCmdRead|(ch<<4));
+    }
+
+
+    void ReadAllChannels()
+    {
+        Message("Requesting full system status.");
+
+        // Prepare command to read all channels
+        for (int i=0; i<kNumChannels; i++)
+            ReadChannel(i);
+
+        //vector<char> buf;
+        //AsyncRead(ba::buffer(buf), kCmdPrint);
+    }
+
+    bool GlobalSetDac(uint16_t dac)
+    {
         if (dac>0xfff)
             return false;
 
-        GlobalSetDac(dac);
+        PostMessage(GetCmd(0, kCmdGlobalSet, dac));
+        AsyncRead(ba::buffer(fBuffer), kCmdGlobalSet);
+
+        ReadAllChannels();
+
+        fVolt.assign(kNumChannels, dac);
+        UpdateV();
 
         return true;
     }
 
-    bool ChannelSet(uint16_t ch, double voltage)
-    {
-        const uint16_t dac = voltage*4096/90;
+    bool GlobalSet(double voltage)
+    {
+        return GlobalSetDac(voltage*4096/90);
+    }
+
+    bool ChannelSetDac(uint16_t ch, uint16_t dac)
+    {
         if (dac>0xfff)
             return false;
@@ -335,24 +368,32 @@
             return false;
 
+        if (fVolt[ch]==dac)
+            return true;
+
         PostMessage(GetCmd(ch, kCmdChannelSet, dac));
         AsyncRead(ba::buffer(fBuffer), kCmdChannelSet|(ch<<4));
 
+        ReadChannel(ch);
+
+        fVolt[ch] = dac;
+        UpdateV();
+
         return true;
     }
 
-    void ReadAllChannelsStatus()
-    {
-        Message("Requesting full system status.");
-
-        // Prepare command to read all channels
-        for (int i=0; i<kNumChannels; i++)
-        {
-            const vector<char> cmd = GetCmd(i, kCmdRead);
-            PostMessage(cmd);
-            AsyncRead(ba::buffer(fBuffer), kCmdRead|(i<<4));
-        }
-
-        //vector<char> buf;
-        //AsyncRead(ba::buffer(buf), kCmdPrint);
+    bool ChannelSet(uint16_t ch, double voltage)
+    {
+        return ChannelSetDac(ch, voltage*4096/90);
+    }
+
+    void SetVoltage(int ch, int32_t dac)
+    {
+        if (dac<0)
+            dac = 0;
+
+        if (dac>0xfff)
+            dac = 0xfff;
+
+        ChannelSetDac(ch, dac);
     }
 
@@ -368,5 +409,4 @@
             // const double diffcur = (fRefCurrent[i]-fCurrent[i])*5000/4096
             //const int32_t diffcur = int32_t(fRefCurrent[i]-fCurrent[i])*5000;
-            const int32_t diffvolt = (fRefCurrent[i]-fCurrent[i])*5;
 
             // Calculate voltage difference
@@ -376,24 +416,6 @@
             // Calculate new vlaue by onverting voltage difference to DAC units
             //const int32_t dac = fRefVolt[i] + diffvolt*4096/90.0;
-            int32_t dac = fRefVolt[i] + diffvolt/90;
-
-            if (dac<0)
-                dac = 0;
-            if (dac>0xfff)
-                dac = 0xfff;
-
-            if (fVolt[i] == dac)
-                continue;
-
-            PostMessage(GetCmd(i, kCmdChannelSet, dac));
-            AsyncRead(ba::buffer(fBuffer), kCmdChannelSet|(i<<4));
-        }
-    }
-
-    void SystemReset()
-    {
-        Message("Sending system reset.");
-        PostMessage(GetCmd(0, kCmdReset));
-        AsyncRead(ba::buffer(fBuffer), kCmdReset);
+            SetVoltage(i, fRefVolt[i] + (fRefCurrent[i]-fCurrent[i])/18);
+        }
     }
 
@@ -457,18 +479,16 @@
     {
         fDimCurrent.Update(fCurrent);
-        ConnectionBias::UpdateA();
-    }
-
-    /*
+    }
+
     void UpdateV()
     {
-        fDimCurrent.Update(fRefVolt);
-    }*/
+        fDimVoltage.Update(fVolt);
+    }
 
 public:
     ConnectionDimBias(ba::io_service& ioservice, MessageImp &imp) :
         ConnectionBias(ioservice, imp),
-        fDimCurrent("BIAS_CONTROL/CURRENT", "F:416", ""),
-        fDimVoltage("BIAS_CONTROL/VOLTAGE", "F:416", "")
+        fDimCurrent("BIAS_CONTROL/CURRENT", "S:416", ""),
+        fDimVoltage("BIAS_CONTROL/VOLTAGE", "S:416", "")
     {
     }
@@ -524,4 +544,15 @@
     }
 
+    int SetGlobalDac(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "SetGlobalDac", 2))
+            return false;
+
+        if (!fBias.GlobalSetDac(evt.GetUShort()))
+            T::Error("Supplied voltage out of range (0-90)");
+
+        return T::GetCurrentState();
+    }
+
     int SetChannel(const EventImp &evt)
     {
@@ -530,4 +561,15 @@
 
         if (!fBias.ChannelSet(evt.GetUShort(), evt.Get<float>(2)))
+            T::Error("Value out of range");
+
+        return T::GetCurrentState();
+    }
+
+    int SetChannelDac(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "SetChannelDac", 4))
+            return false;
+
+        if (!fBias.ChannelSetDac(evt.Get<uint16_t>(), evt.Get<uint16_t>(2)))
             T::Error("Value out of range");
 
@@ -627,5 +669,5 @@
 
         AddEvent("REQUEST_STATUS", kStateConnected)
-            (Wrapper(bind(&ConnectionBias::ReadAllChannelsStatus, &fBias)))
+            (Wrapper(bind(&ConnectionBias::ReadAllChannels, &fBias)))
             ("");
 
@@ -634,6 +676,14 @@
             ("");
 
+        AddEvent("SET_GLOBAL_DAC", "S:1", kStateConnected)
+            (bind(&StateMachineBias::SetGlobalDac, this, _1))
+            ("");
+
         AddEvent("SET_CHANNEL_VOLTAGE", "S:1;F:1", kStateConnected)
             (bind(&StateMachineBias::SetChannel, this, _1))
+            ("");
+
+        AddEvent("SET_CHANNEL_DAC", "S:1;S:1", kStateConnected)
+            (bind(&StateMachineBias::SetChannelDac, this, _1))
             ("");
 
