Exemplo n.º 1
0
class Device:

    
    def __init__(self,name):
        #print("making device")
        # Get all the stuff from the constructor.
        # Has a the device made an appearance, this is so we dont alert the
        # user more than once if a device dissapears.
        self.foundDevice = False
        # self.cxn = cxn
        self.name = name
        # Nicknames of settings (the ones that show up on th Gui)
        self.nicknames=[]           
        # Device's frame
        # The device's labrad server        
        #self.serverName = serverName   
        # List of settings that the user wants run on their device
        settings=[]             
        # The actual names of the settings
        self.settingNames = []
        # Stores the actual reference to the labrad server      
        deviceServer = None 
        # True if device is functioning correctly
        self.isDevice = False           
        # Used for device.select_device(selectedDevice) setting
        self.selectedDevice = 0
        # stores the setting to select device (almost always 'select_device')
        self.setDeviceCmd = None    
        # Stores the buttons along with their parameters
        buttons = [[]]              
        # Arguments that should be passed to settings if necessary
        self.settingArgs =[]    
        self.settingResultIndices = []
        #print name, ":", yLabel
        self.frame = MFrame()
        self.frame.setYLabel(None)
        # Store the graph
        # self.plots = []
        # Determine which buttons get messages
#       if(buttonMessages is not None):
        self.buttonMessages = []
        # Setup all buttons
        #if(buttonNames is not None):
        self.buttonNames = []
        self.buttonSettings = []
            #print(buttonArgs)
        self.buttons = []
        # new datachest wrapper
        self.datachest = dataChestWrapper(self)
        # Tells thread to keep going
        self.keepGoing = True
        atexit.register(self.stop)
    def stop(self):
       
        keepGoing = False
        
    def setServerName(self, name):
        self.serverName = name
        
    def addParameter(self, parameter, setting, arg = None, index = None):
        #if(index is None):
            #index = len(self.nicknames)
        self.settingNames.append(setting)
        self.settingResultIndices.append(index)
        self.nicknames.append(parameter)
        self.settingArgs.append(arg)
        #print self.settingResultIndices
        
    def connection(self, cxn):
        self.cxn = cxn
        self.ctx = cxn.context()
    def addButton(self, name, msg, setting, arg=None):
        self.buttons.append([])
        i = len(self.buttons)-1
        self.buttons[i].append(name)
        self.buttons[i].append(setting)
        self.buttons[i].append(msg)
        self.buttons[i].append(arg)
        self.frame.setButtons(self.buttons)
        
    def setYLabel(self, yLbl, units = ''):
        self.frame.setYLabel(yLbl, units)
    
        
    def selectDeviceCommand(self, cmd, arg):
        self.selectedDevice = arg   
        self.setDeviceCmd = cmd 
    
    def begin(self):
        self.frame.setTitle(self.name)
        self.frame.setNicknames(self.nicknames)
        self.frame.setReadingIndices(self.settingResultIndices)
        # Connect to the device's server
        #self.connect()
        # Each device NEEDS to run on a different thread 
        # than the main thread (which ALWAYS runs the gui)
        # This thread is responsible for querying the devices
        self.deviceThread = threading.Thread(target = self.Query, args=[])
        # If the main thread stops, stop the child thread
        self.deviceThread.daemon = True
        # Start the thread
        self.deviceThread.start()
    def setRefreshRate(self, period):
        #self.refreshRateSec = period
        self.frame.setRefreshRate(period)
    def setPlotRefreshRate(self, period):
        self.frame.setPlotRefreshRate(period)
    def addPlot(self, length = None):
        self.frame.addPlot(length)
        # Datalogging must be enabled if we want to plot data
        self.frame.enableDataLogging(True)
        #print self.frame.getPlot()
        return self.frame.getPlot()
    def connect(self):  
        '''Connect to the device'''
        #self.deviceServer = getattr(self.cxn, self.serverName)()
        try:
            # Attempt to connect to the server given the connection 
            # and the server name.
            self.deviceServer = getattr(self.cxn, self.serverName)()    
            # If the select device command is not none, run it.
            if(self.setDeviceCmd is not None):
                getattr(self.deviceServer, self.setDeviceCmd)(
                    self.selectedDevice, context = self.ctx)
            # True means successfully connected
            self.foundDevice= False
            print ("Found device: "+self.serverName)
            return True
        except labrad.client.NotFoundError, AttributeError:
            if( not self.foundDevice):
                self.foundDevice = True
                print("Unable to find device: "+self.serverName)
            self.frame.raiseError("Labrad issue")
        except: