# -*- coding: utf-8 -*-
#import visa
import random
#import alarm_toolbox as alarm
import Tool

param={'HeLevel':'cm','HeLevel2':'cm'}

class Instrument(Tool.MeasInstr):  
    
    #a tag that will be initiallized with the same name than the module
    #const_label=''
    
    def __init__(self, resource_name, debug=False): 
        super(Instrument, self).__init__(resource_name,'CG500',debug)
        
#        print self.identify("Hello, this is ")
        #self.const_label='CG500'
        if self.debug == False:
            self.set_alarm(0)
            self.set_units('CM')
            self.set_mode('S')
            
    def __del__(self):
        super(Instrument, self).__del__()
#        if self.debug==False:
#            self.connexion.close()
#        else:
#            pass
#        print "a " + self.ID_name + " is dead"
#        

#------------------------------------------------------------------------------
    
    #I don't know if the values measured stack or not, if they do we should probably think about this function again
    def measure(self,channel='HeLevel'):
        if self.last_measure.has_key(channel): 
            if not self.debug:
                chan=self.get_channel()
                if channel=='HeLevel2':
                    answer=self.ask('MEAS? ' +chan)
                else:
                        answer=self.ask('MEAS ' +chan +';MEAS? ' +chan)
                answer=float(answer[:-3])
            else: 
                answer=100*random.random()
            self.last_measure[channel]=answer
        else:
            print "you are trying to measure a non existent channel : " +channel
            print "existing channels :", self.channels
            answer=None
        return answer
   
    def get_status(self):
        return self.ask('STAT?')    

    def get_type(self):
        chan_type=self.ask('TYPE? '+self.get_channel())
        if chan_type:
            print "The channel type is Liquid Nitrogen"
        else:
            print "The channel type is Liquid Helium"
        return chan_type    
    
    def get_channel(self):
        return self.ask('CHAN?')
        
    def set_channel(self,value_chan):
        return self.connexion.write('CHAN '+ str(value_chan) +';CHAN?')
        
    def get_sampling_interval(self):
        return self.ask('INTVL?')
    
    #format 'HH:MM:SS', if 0 is inputed then it measures continously    
    def set_sampling_interval(self,value_intvl):
        return self.write('INTVL '+ str(value_intvl) +';INTVL?')

    #'S' for sample/hold and 'C' for continuous        
    def get_mode(self):
        return self.ask('mode?')
    
    #'S' for sample/hold and 'C' for continuous    
    def set_mode(self,value_mode):
        return self.write('MODE '+ str(value_mode) +';MODE?')
        
    def set_error_mode(self,value_err_mode):
        return self.write('ERROR '+ str(value_err_mode) +';ERROR?')
    
    #'CM', 'IN', '%'
    def set_units(self,value_units):
        return self.write('UNITS '+ str(value_units) +';UNITS?')
    
    #0 will desactivate the alarm, a low value will not annoy people in the lab     
    def set_alarm(self,value_alarm):
        return self.write('ALARM '+ str(value_alarm) +';ALARM?')    
