Index: /fact/tools/PyDimCtrl/service.py
===================================================================
--- /fact/tools/PyDimCtrl/service.py	(revision 13805)
+++ /fact/tools/PyDimCtrl/service.py	(revision 13806)
@@ -257,4 +257,262 @@
     Take(-1, 1000, 'drs-time')
     Take(-1, 1000, 'drs-time-upshifted')
+
+
+
+
+def Connect( verbose=True ):
+
+    prepare_ftm_control()
+    prepare_fsc_control()
+    feedback.stop()
+    prepare_bias_control()
+    prepare_feedback()
+    prepare_data_logger()
+    
+
+def prepare_fsc_control( verbose = True, timeout = 10, delay = 0.2):
+    msg = MSG( verbose )
+    fsc = fsc_control
+    start_time = time.time()
+    if timeout == None:
+        timeout = float('inf')
+    if delay < 0.:
+        delay = 0.
+    
+    while not fsc.stn == 2: #Connected
+        if time.time() > start_time + timeout:
+            return False
+    
+        if (fsc.stn <= 0) or (fsc.stn >= 256):
+            msg.fail("FSC_CONTROL is in state "+fsc.sts)
+            msg.fail(prepare_fsc_control.__name__ + "does not know how to handle this")
+            return False
+        elif fsc.stn == 1: # Disconnected
+            fsc.reconnect()
+    return True
+    
+def prepare_ftm_control( verbose = True, timeout = 10, delay = 0.2):
+    msg = MSG( verbose )
+    ftm = ftm_control
+    start_time = time.time()
+    if timeout == None:
+        timeout = float('inf')
+    if delay < 0.:
+        delay = 0.
+    
+    while not ftm.stn == 3:
+        if time.time() > start_time + timeout:
+            return False
+    
+        if (ftm.stn <= 0) or (ftm.stn >= 256):
+            msg.fail("FMT_CONTROL is in state "+ftm.sts)
+            msg.fail("ftm_to_Idle() does not know how to handle this")
+            return False
+        elif ftm.stn == 1: # Disconnected
+            ftm.reconnect()
+        elif ftm.stn == 2: #Connected
+            # just wait a second
+            time.sleep(1)
+        elif ftm.stn == 4: #TriggerOn
+            ftm.enable_trigger(0) #switch trigger off
+        elif ftm.stn in [5,6,7]: # some Configure state
+            ftm.reset_configure()
+    
+    return True
+    
+def prepare_data_logger( verbose = True, timeout = 10, delay = 0.2):
+    msg = MSG(verbose)
+    dl = data_logger
+    if timeout == None:
+        timeout = float('inf')
+    if delay < 0.:
+        delay = 0.
+    start_time = time.time()
+    raise NotImplementedError('prepare_data_logger() is not yet implemented')
+
+def prepare_fad_control( verbose = True, timeout = 10, delay = 0.2):
+    msg = MSG(verbose)
+    fad = fad_control
+    ftm = ftm_control
+    if timeout == None:
+        timeout = float('inf')
+    if delay < 0.:
+        delay = 0.
+    start_time = time.time()
+
+    while not fad.stn == 4:
+        # Timeout
+        if time.time() > start_time + timeout:
+            msg.fail("Timeout in " + prepare_fad_control.__name__)
+            return False
+        # Strange States
+        if (fad.stn <= 0) or (fad.stn >= 256):
+            msg.fail("FAD_CONTROL is in state "+fad.sts)
+            msg.fail(prepare_fad_control.__name__ + "does not know how to handle this")
+            return False
+        elif fad.stn == 1: # Disengaged
+            fad.start()
+        elif fad.stn == 2: # Disconnected
+            fad.start()
+        elif fad.stn == 3: # Connecting
+            # it might just need time
+            if time.time() - fad.last_st_change < 5:
+                time.sleep(2)
+                timeout += 1
+            else:
+            # there might be a problem with one of the boards
+            conns = map( ord, fad.connections()[0] )
+            problems = {}
+            for fad,conn in enumerate(conns):
+                if conn < 255:
+                    print "FAD:", fad, "has a problem"
+                    if not fad/10 in problems:
+                        problems[fad/10] = []
+                    else:
+                        problems[fad/10].append( fad%10 )
+            
+            for fad in range(10):
+                for crate in problems.keys():
+                    fad.disconnect(crate*10+fad)
+                    ftm.toggle_ftu(crate*10+fad)
+            for crate in problems.keys():
+                time.sleep(1)
+                ftm.reset_crate(crate)
+                
+            for fad in range(10):
+                for crate in problems.keys():
+                    time.sleep(3.5)
+                    fad.connect(crate*10+fad)
+                    ftm.toggle_ftu(crate*10+fad)
+        
+        elif fad.stn == 5: # Configuring1
+            if time.time() - fad.last_st_change < 5:
+                time.sleep(1)
+            else:
+                msg.warn("FAD is in Configuring1 since more than 5sec")
+                
+        elif fad.stn == 6: # Configuring2 
+            if time.time() - fad.last_st_change < 5:
+                time.sleep(1)
+            else:
+                msg.warn("FAD is in Configuring2 since more than 5sec")
+        
+        elif fad.stn == 7: # Configured
+            if time.time() - fad.last_st_change < 5:
+                time.sleep(1)
+            else:
+                msg.warn("FAD is in Configuring2 since more than 5sec")
+        
+        elif fad.stn == 8: #WritingData
+            # I don't know how to get from WritingData to Connected
+            # well. .. one could wait for a timeout, but I hate that.
+            fad.close_open_files()
+        
+        
+        time.sleep(delay)
+    return True
+
+def prepare_bias_control( verbose = True, timeout = 10, delay = 0.2):
+    msg = MSG(verbose)
+    bias = bias_control
+    
+    if timeout == None:
+        timeout = float('inf')
+    if delay < 0.:
+        delay = 0.
+    start_time = time.time()
+
+    while (bias.stn != 4) and (bias.stn != 7): #Connected or VoltageOff
+        # Timeout
+        if time.time() > start_time + timeout:
+            msg.fail("Timeout in " + prepare_bias_control.__name__)
+            return False
+        # Strange States
+        if (bias.stn <= 0) or (bias.stn >= 256):
+            msg.fail("BIAS_CONTROL is in state "+bias.sts)
+            msg.fail(prepare_bias_control.__name__ + "does not know how to handle this")
+            return False
+        elif bias.stn == 1: # Disconnected
+            bias.reconnect()
+        elif bias.stn == 2: # Connecting
+            if time.time() - bias.last_st_change < 5:
+                time.sleep(1)
+            else:
+                msg.warn("BIAS_CONTROL is in Connecting since more than 5sec")
+        elif bias.stn == 3: # Initializing
+            if time.time() - bias.last_st_change < 5:
+                time.sleep(1)
+            else:
+                msg.warn("BIAS_CONTROL is in Initializing since more than 5sec")
+        elif bias.stn == 5: #Ramping
+            if time.time() - bias.last_st_change < 10:
+                time.sleep(1)
+            else:
+                msg.warn("BIAS_CONTROL is in Ramping since more than 10sec")
+                bias.stop()
+        elif bias.stn == 6: #OverCurrent
+            time.sleep(0.5)
+            bias.set_zero_voltage()
+            time.sleep(0.5)
+            bias.reset_over_current_status()
+            time.sleep(0.5)
+        elif bias.stn == 8: #NotReferenced
+            time.sleep(0.5)
+            bias.set_zero_voltage()
+            time.sleep(0.5)
+        elif bias.stn == 9: # VoltageOn
+            time.sleep(0.5)
+            bias.set_zero_voltage()
+            time.sleep(0.5)
+        elif bias.stn == 10: # ExpoertMode
+            msg.fail("BIAS is in ExportMode ... wtf")
+            return False
+        
+        time.sleep(delay)
+    return True
+    
+def prepare_feedback( verbose = True, timeout = 10, delay = 0.2):
+    msg = MSG(verbose)
+    fb = feedback
+    if timeout == None:
+        timeout = float('inf')
+    if delay < 0.:
+        delay = 0.
+    start_time = time.time()
+
+    while not fb.stn == 6: #Connected 
+        # Timeout
+        if time.time() > start_time + timeout:
+            msg.fail("Timeout in " + prepare_feedback.__name__)
+            return False
+        # Strange States
+        if (fb.stn <= 1) or (fb.stn >= 256):
+            msg.fail("FEEDBACK is in state "+fb.sts)
+            msg.fail(prepare_feedback.__name__ + "does not know how to handle this")
+            return False
+        elif fb.stn in [2,3,4,5]:
+            if time.time() - fb.last_st_change < 10:
+                time.sleep(1)
+            else:
+                msg.warn("BIAS_CONTROL is in "+fb.sts+" since more than 10sec")
+        
+        elif fb.stn in [7,8,9]:
+            fb.stop()
+
+        elif fb.stn in [10,11,12]:
+            fb.enable_output(0)
+            fb.stop()
+        
+        elif fb.stn == 13:
+            # maybe waiting helps
+            if time.time() - fb.last_st_change < 20:
+                time.sleep(1)
+            else:
+                msg.warn("FEEDBACK is in "+fb.sts+" since more than 20sec \n sending STOP")
+                fb.stop()
+        
+        time.sleep(delay)
+    return True
+
 
 
