示例#1
0
文件: Go.py 项目: normansaez/lotuce2
class Go(GObject.GObject):
    def __init__(self):
        GObject.GObject.__init__(self)
        self.counter = 0
        GObject.timeout_add_seconds(1, self._cb_counter)

        path, fil = os.path.split(os.path.abspath(os.path.realpath(__file__)))
        self.builder = Gtk.Builder()
        self.builder.add_from_file(path+"/glade/Go.glade")
        self.window = self.builder.get_object ("window1")
        self.window.set_events(self.window.get_events())
        
        self.DarcAravis = None
        self.darc_running = False

        self.configfile=path+'/../../conf/config.cfg'
        self.config = None
        self.config = ConfigParser.ConfigParser()
        self.config.read(self.configfile)

        if self.window:
            self.window.connect("destroy", self.quit)#Gtk.main_quit)

        self.button_play = self.builder.get_object("play")
        self.calibration = self.builder.get_object("calibration")
        self.acquisition = self.builder.get_object("acquisition")
       
        self.calibration.set_active(True)
        self.button_play.connect("clicked", self._cb_play, "play")
        self.calibration.connect("clicked", self._cb_check_cal, "calibration")
        self.acquisition.connect("clicked", self._cb_check_acq, "acquisition")

        dic = { 
            "on_buttonQuit_clicked" : self.quit,
        }
        
        self.builder.connect_signals( dic )
    def _cmd(self, cmd, wait=False):
        print cmd
        process = Popen(cmd , stdout=sys.stdout , stderr=sys.stderr , shell=True)
        if wait is True:
            process.wait()
        return

    def _darc_start(self, filename, prefix):
        try:
            self.DarcAravis = DarcAravis()
            self.darc_running = True
        except:
            print "DARC is already running"
        self.darc_running = False
        if not self.darc_running:
            cmd = 'darccontrol -o %s --prefix=%s' % (filename, prefix)
            self._cmd(cmd)
            time.sleep(20)

    def _darc_stop(self):
        cmd = "ps aux|grep %s |awk '{print $2}'|xargs kill -9" % (self.config.get('bbb','calGUI'))
        self._cmd(cmd, wait=True)
        cmd = "ps aux|grep %s |awk '{print $2}'|xargs kill -9" % (self.config.get('bbb','acqGUI'))
        self._cmd(cmd, wait=True)
        cmd = 'darcmagic stop -c  --prefix=all'
        self._cmd(cmd, wait=True)
        cmd = "ps aux|grep darcmain|awk '{print $2}'|xargs kill -9"
        self._cmd(cmd, wait=True)
        cmd = "ps aux|grep darccontrol|awk '{print $2}'|xargs kill -9"
        self._cmd(cmd, wait=True)

    def _darc_cal(self):
        print GREEN+"Calibrating ..."
        freq = self.config.get('bbb', 'frequency')
        cmd = '/bin/set_frecuency %s' % freq
        self._cmd(cmd)
        print "Setup ..."
        if self.DarcAravis is None:
            self.DarcAravis = DarcAravis()
        for i in range(0,4):
            camera = 'cam%d' % i
            print "\n\nReading configuration for %s ... " % camera
            trigger = self.config.getboolean(camera, 'trigger')
            print "Trigger: %s" % trigger
            print "\nReading current configuration from HW : %s" % camera
            self.DarcAravis.get(i, 'TriggerSource') 
            print "\nSET configuration readed from file, for  %s" % camera
            if trigger is True:
                value = 'Line1'
            else:
                value = 'Freerun'
            print value
#            self.DarcAravis.set(i, 'TriggerSource', value) 
#            exptime = self.config.get(camera, 'exptime')
#            self.DarcAravis.set(i, 'ExposureTimeAbs', exptime)
        for i in range(0,4):
            camera = 'cam%d' % i
            exptime = self.config.get(camera, 'exptime')
            print "%s => exptime : %s" % (camera, exptime)
#            self.DarcAravis.set(i, 'ExposureTimeAbs', exptime)
        print "end" + NO_COLOR
        cmd = "python %s" % (self.config.get('bbb','calGUI'))
        self._cmd(cmd)

    def _darc_acq(self):
        print "Acquiring ..."
        if self.DarcAravis is None:
            self.DarcAravis = DarcAravis()
        for i in range(0,4):
            camera = 'cam%d' % i
            print "\n\nReading configuration for %s ... " % camera
            offset_x = self.config.getint(camera, 'offset_x')
            offset_y = self.config.getint(camera, 'offset_y')
            trigger = self.config.get(camera, 'trigger')
            exptime = self.config.getint(camera, 'exptime')
            print "OffsetX: %d" % offset_x 
            print "OffsetY: %d" % offset_y 
            print "Trigger: %s" % trigger
            print "exptime: %d" % exptime
            print "\nReading current configuration from HW : %s" % camera
            self.DarcAravis.get(i, 'OffsetX') 
            self.DarcAravis.get(i, 'OffsetY') 
            self.DarcAravis.get(i, 'ExposureTimeAbs')
            self.DarcAravis.get(i, 'TriggerSource') 
            print "\nSET configuration readed from file, for  %s" % camera
            self.DarcAravis.set(i, 'OffsetX', offset_x) 
            self.DarcAravis.set(i, 'OffsetY', offset_y) 
            self.DarcAravis.set(i, 'ExposureTimeAbs', exptime)
            if trigger.__contains__('True'):
                value = 'Line1'
            else:
                value = 'Freerun'
#            self.DarcAravis.set(i, 'TriggerSource', value) 
        freq = self.config.get('bbb', 'frequency')
        cmd = '/bin/set_frecuency %s' % freq
        self._cmd(cmd)
        cmd = "python %s" % (self.config.get('bbb','acqGUI'))
        self._cmd(cmd)

    def _cb_counter(self):
        self.counter += 1
        return True

    def _cb_play(self, widget, data=None):
        print "%s: %s" % (data, ("STOP", "PLAY")[widget.get_active()])
        image=Gtk.Image()
        #This happend when PLAY is pressed
        if widget.get_active() is True:
            image.set_from_stock(Gtk.STOCK_MEDIA_STOP, Gtk.IconSize.BUTTON)
            self.button_play.set_image(image)
            self.button_play.set_label("Stop")
            self.calibration.set_sensitive(False)
            self.acquisition.set_sensitive(False)
            if self.calibration.get_active() is True:
                filename = self.config.get('bbb','cfgdarcCAL')
                prefix   = self.config.get('bbb','prefix')
                self._darc_start(filename, prefix)
                self._darc_cal()
            if self.acquisition.get_active() is True:
                filename = self.config.get('bbb','cfgdarcACQ')
                prefix   = self.config.get('bbb','prefix')
                self._darc_start(filename, prefix)
                self._darc_acq()
        else:
            #This happend when STOP is pressed
            image.set_from_stock(Gtk.STOCK_MEDIA_PLAY, Gtk.IconSize.BUTTON)
            self.button_play.set_image(image)
            self.button_play.set_label("Play")
            self._darc_stop()
            self.calibration.set_sensitive(True)
            self.acquisition.set_sensitive(True)
            
    def _cb_check_cal(self, widget, data=None):
        self.calibration.set_active(True)
        if self.acquisition.get_active() is True:
            self.acquisition.set_active(False)

    def _cb_check_acq(self, widget, data=None):
        self.acquisition.set_active(True)
        if self.calibration.get_active() is True:
            self.calibration.set_active(False)
        
    def quit(self, widget):
        '''
        quit
        '''
        self._darc_stop()
        sys.exit(0)
示例#2
0
class OffSetGui:


    def __init__( self ):
        path, fil = os.path.split(os.path.abspath(__file__))

        self.builder = Gtk.Builder()
        self.builder.add_from_file(path+"/glade/offset.glade")
        self.window = self.builder.get_object ("window1")
        self.window.set_events(self.window.get_events())
        
        self.DarcAravis = DarcAravis()

        #step
        self.__step = 10

        if self.window:
            self.window.connect("destroy", Gtk.main_quit)

        self.camera = 0
        #Offset x,y
        self.offset_x = self.builder.get_object("offset_x")
        offset_x = self.DarcAravis.get(self.camera, 'OffsetX')
        self.offset_x.set_text("%s pixel(s)"% offset_x)

        self.offset_y = self.builder.get_object("offset_y")
        offset_y = self.DarcAravis.get(self.camera, 'OffsetY')
        self.offset_y.set_text("%s pixel(s)"% offset_y)

        #Toggle button to connect to cam0
        self.togglebutton_cam0 = self.builder.get_object ("togglebutton0")
        self.togglebutton_cam0.connect("toggled", self.callback, "0")

        #Toggle button to connect to cam1
        self.togglebutton_cam1 = self.builder.get_object ("togglebutton1")
        self.togglebutton_cam1.connect("toggled", self.callback, "1")

        #Toggle button to connect to cam2
        self.togglebutton_cam2 = self.builder.get_object ("togglebutton2")
        self.togglebutton_cam2.connect("toggled", self.callback, "2")

        #Toggle button to connect to cam3
        self.togglebutton_cam3 = self.builder.get_object ("togglebutton3")
        self.togglebutton_cam3.connect("toggled", self.callback, "3")
        
        #Default cam0:
        self.togglebutton_cam0.set_active(True)

        #cross to put available offset
        # up = up
        # do = down
        # le = left
        # ri = right
        self.button_up = self.builder.get_object("button_up")
        self.button_do = self.builder.get_object("button_do")
        self.button_le = self.builder.get_object("button_le")
        self.button_ri = self.builder.get_object("button_ri")
        
        self.button_up.connect("clicked", self.offset_callback, "up")
        self.button_do.connect("clicked", self.offset_callback, "do")
        self.button_le.connect("clicked", self.offset_callback, "le")
        self.button_ri.connect("clicked", self.offset_callback, "ri")

        #apply_step
        self.button_apply_step = self.builder.get_object("apply_step")
        self.button_apply_step.connect("clicked", self.step_callback, "step")

        self.step = self.builder.get_object("step")
        self.current_step = self.builder.get_object("current_step")
        self.current_step.set_text("-- pixel(s)")
        self.step.set_text("10")

        #button brute force offset
        self.button_brute_offset = self.builder.get_object("brute_offset")
        self.button_brute_offset.connect("clicked", self.offset_callback, "first try")

        dic = { 
            "on_buttonQuit_clicked" : self.quit,
        }
        
        self.builder.connect_signals( dic )

    def callback(self, widget, data=None):
        '''
        callback
        '''
        print "%s: %s" % (data, ("disconnecting", "connecting")[widget.get_active()])
        #CONN
        if widget.get_active() is True:
            if data == "0":
#                self.togglebutton_cam0.set_active(False)
                self.togglebutton_cam1.set_active(False)
                self.togglebutton_cam2.set_active(False)
                self.togglebutton_cam3.set_active(False)

            if data == "1":
                self.togglebutton_cam0.set_active(False)
#                self.togglebutton_cam1.set_active(False)
                self.togglebutton_cam2.set_active(False)
                self.togglebutton_cam3.set_active(False)

            if data == "2":
                self.togglebutton_cam0.set_active(False)
                self.togglebutton_cam1.set_active(False)
#                self.togglebutton_cam2.set_active(False)
                self.togglebutton_cam3.set_active(False)

            if data == "3":
                self.togglebutton_cam0.set_active(False)
                self.togglebutton_cam1.set_active(False)
                self.togglebutton_cam2.set_active(False)
#                self.togglebutton_cam3.set_active(False)

            self.camera = int(data)
            offset_y = self.DarcAravis.get(self.camera, 'OffsetY')
            self.offset_y.set_text("%d pixel(s)"% int(offset_y)) 
            offset_x = self.DarcAravis.get(self.camera, 'OffsetX')
            self.offset_x.set_text("%d pixel(s)"% int(offset_x))


#        if widget.get_active() is False:
#            print "OFF"

    def offset_callback(self, widget, data=None):
        '''
        offset callback

        The offset is taking as reference darcplot gui.  Therefore the offset
        cross follows that darcplot axis references.
        '''
        print "offset: %s" % (data)
        offset_y = self.DarcAravis.get(self.camera, 'OffsetY')
        offset_x = self.DarcAravis.get(self.camera, 'OffsetX')
        print "before apply step(%d)\noffset(%d,%d)" % (self.__step, offset_x, offset_y)

        if data == 'up':
            val = offset_y + self.__step
            print "to be applied: %d" % val
            self.DarcAravis.set(self.camera, 'OffsetY', val)
        if data == 'do':
            val = offset_y - self.__step
            print "to be applied: %d" % val
            self.DarcAravis.set(self.camera, 'OffsetY', val)
        if data == 'le':
            val = offset_x + self.__step
            print "to be applied: %d" % val
            self.DarcAravis.set(self.camera, 'OffsetX', val)
        if data == 'ri':
            val = offset_x - self.__step
            print "to be applied: %d" % val
            self.DarcAravis.set(self.camera, 'OffsetX', val)

        if data == 'first try':
            x = self.DarcAravis.get(self.camera, 'Width')
            y = self.DarcAravis.get(self.camera, 'Height')
            offsetX = int((656 - x )/2.0)
            offsetY = int((492 - y)/2.0)
            self.DarcAravis.set(self.camera, 'OffsetX', offsetX)
            self.DarcAravis.set(self.camera, 'OffsetY', offsetY)

        offset_y = self.DarcAravis.get(self.camera, 'OffsetY')
        offset_x = self.DarcAravis.get(self.camera, 'OffsetX')
        if data =='up' or data =='do' or data == 'first try':
            self.offset_y.set_text("%d pixel(s)"% int(offset_y))
        if data =='le' or data =='ri' or data == 'first try':
            self.offset_x.set_text("%d pixel(s)"% int(offset_x))
        print "after apply: offset(%d,%d)" % (offset_x, offset_y)
        


    def step_callback(self, widget, data=None):
        '''
        step_callback
        '''
        step = self.step.get_text()
        print "step %s" % step
        if step == "":
            step = "10"
        self.__step = int(step)
        print "self.__step %d" % self.__step
        self.current_step.set_text("%s pixel(s)"% step)
        self.step.set_text("")

    def quit(self, widget):
        '''
        quit
        '''
        sys.exit(0)