from numarray import *

try:
    from gpib82357A import *
    print "Module gpib82357A Found!"
except:
    print "No Module gpib82357A installed!"


Ko=1024
LENGTH=8*Ko

class HP54111D:
    def __init__(self,name="GPIB0::3::INSTR"):
        self.GPIB=gpib82357A(name)
        self.YINC=".0"
        self.XINC=".0"
        self.XREF=".0"
        self.YREF="128"
        self.XORG=".0"
        self.Y1ORG=".0"
        self.Y2ORG=".0"
        
        self.POINTS="8192"
        self.XDIV ="100E-3"
        self.Y1DIV="1.0"
        self.Y2DIV="1.0"

# system command
#*****************
    def Reset(self):  # preset s to its default settings
        self.GPIB.write("RESET\n")

    def Stop(self): # stops acquire
        self.GPIB.write("KEY 42\n")
        
    def Run(self): # runs acquire
        self.GPIB.write("RUN\n")
        
    def SetLocal(self): # sets instrument in local mode
        self.GPIB.write("LOCAL\n")

    def Autoscale(self):
        self.GPIB.write("AUTOSCALE\n")
        
    def View(self,str) : # '1' to '4'
        if (str>'0') and (str<'5'):
           msg = "VIEW CH %s\n" %(str)
           self.GPIB.write(msg)
           st=self.GPIB.read() 
           return st
        else:
           return "error channel"
    
    def GetStatus(self) : 
        self.GPIB.write("STA?\n")
        st=self.GPIB.read() 
        return st
        
    def IsStopped(self) : 
        self.GPIB.write("KEY?\n")
        i=int (self.GPIB.read())          # 42 stop/single 0 no key
        if ( (i == 42) or (i ==0 ) ): 
            return 1
        else:
            return -1

    def GetID(self) : 
        self.GPIB.write("ID?\n")
        st=self.GPIB.read() 
        return st
    

#channel subsystem
#*****************
    def GetSensitivity(self,str) : # '1' to '4'
        if (str=='1'):
           return self.Y1DIV
        else:
           return self.Y2DIV
           
    def SetSensitivity(self, ch, str) : #  '1' "0.01"
        if (ch>'0') and (ch<'5'):
           msg = "CH %s SENS %s\n" %(ch, str)
           self.GPIB.write(msg)
           st=self.GPIB.read() 
           return st
        else:
           return "error channel"

    def GetOffset(self,str):
        if (str>'0') and (str<'5'):
           msg = "CH %s OFFS?\n" %(str)
           self.GPIB.write(msg)
           st=self.GPIB.read() 
           return st
        else:
           return "error channel"
     
    def SetOffset(self, ch, str):
        if (ch>'0') and (ch<'5'):
           msg = "CH %s OFFS %s\n" %(ch, str)
           self.GPIB.write(msg)
           st=self.GPIB.read() 
           return st
        else:
           return "error channel"

# Timbase subsystem 
#*****************
    def GetTimebase(self):
        return self.XDIV

    def SetTimebase(self,str):
        msg = "TIM SENS %s\n" %(str)
        self.GPIB.write(msg)
        st=self.GPIB.read()
        return st

    def GetDelay(self):  
        self.GPIB.write("TIM DEL?\n")
        st=self.GPIB.read()
        return st

    def SetDelay(self, str):  
        msg = "TIM DEL %s\n" %(str)
        self.GPIB.write(msg)
        st=self.GPIB.read()
        return st

    def GetReference(self):  
        self.GPIB.write("TIM REF?\n")
        st=self.GPIB.read()
        return st

    def SetReference(self, str):  # 'LEFT' 'CENTER' 'RIGHT'
        msg = "TIM REF %s\n" %(str)
        self.GPIB.write(msg)
        st=self.GPIB.read()
        return st

#  Waveform subsystem
#*****************
    def GetXref(self):  #0
        #self.GPIB.write("WAV XREFERENCE?\n")
        #st=self.GPIB.read()
        return self.XREF
        return st
        
    def GetYref(self):  #128
        #self.GPIB.write("WAV YREFERENCE?\n")
        #st=self.GPIB.read()
        return self.YREF
        return st
    
    def GetXinc(self):  #real 10ps to 20ms
        #self.GPIB.write("XINCREMENT?\n")
        #st=self.GPIB.read()
        return self.XINC
        return st

    def GetYinc(self):  #real
        #self.GPIB.write("WAV YINCREMENT?\n")
        #st=self.GPIB.read()
        return self.YINC
        return st

    def GetYorg(self,str):  #real
        #self.GPIB.write("WAV YORIGIN?\n")
        #st=self.GPIB.read()
        return self.YORG1
        return st
        
    def GetXorg(self):  #real
        #self.GPIB.write("WAV XORIGIN?\n")
        #st=self.GPIB.read()
        return self.XORG
        return st
    
    def WaveForm (self):  
        self.GPIB.write("WAV?\n")
        st=self.GPIB.read()
        return st
    
    def GetPreamble (self):  
        self.GPIB.write("WAV SRC MEM1 PRE?\n")
        st=self.GPIB.read()
        
        pos=st.find(',')+1
        st=st[pos:]
        pos=st.find(',')+1
        st=st[pos:]
        
        pos=st.find(',')
        self.POINTS=st[:pos]
        st=st[pos+1:]
        
        pos=st.find(',')+1
        st=st[pos:]
        
        pos=st.find(',')
        self.XINC=st[:pos]
        st=st[pos+1:]
        
        pos=st.find(',')
        self.XORG=st[:pos]
        st=st[pos+1:]
        
        pos=st.find(',')
        self.XREF=st[:pos]
        st=st[pos+1:]
        
        pos=st.find(',')
        self.YINC=st[:pos]
        st=st[pos+1:]
        
        pos=st.find(',')
        self.YORG1=self.YORG2=st[:pos]
        st=st[pos+1:]
        
        pos=st.find(',')
        self.YREF=st[:pos]
        st=st[pos+1:]

        self.GPIB.write("CH 1 SENS?\n")
        self.Y1DIV=self.GPIB.read() 
        
        self.GPIB.write("CH 2 SENS?\n")
        self.Y2DIV=self.GPIB.read()
        
        self.GPIB.write("TIM SENS?\n")
        self.XDIV=self.GPIB.read()
        return st

    def GetCoupling(self):  
        self.GPIB.write("WAV COUPLING?\n")
        st=self.GPIB.read()
        return st

    def SetCoupling(self):  # 'DC' 'AC' 'GND' 
        msg = "WAV COUPLING %s\n" %(str)
        self.GPIB.write(msg)
        st=self.GPIB.read()
        return st

    def GetValid(self):  #0
        self.GPIB.write("WAV VALID?\n")
        st=self.GPIB.read()
        return st

    def GetData(self,str,len=8*Ko):
        msg = "WAV SRC MEM%s FORMAT BYTE\n" %(str)
        self.GPIB.write(msg)
        self.GPIB.write("DATA?\n")
        self.GPIB.readbyte(4)  
        #print "#2A + len (2bytes)"
        YP=self.GPIB.readbyte(len)   #data
        data= zeros (len, Float)
        y1div=float(self.GetSensitivity('1'))
        y2div=float(self.GetSensitivity('2'))
        yref=float(self.YREF)
        yinc=float(self.YINC)
        yorg=float(self.GetYorg(str))
        if (str=='1'):
           for XP in range (0,len,1):
               data[XP]= ( (ord(YP[XP]) - yref)*yinc + yorg ) 
        else:
           for XP in range (0,len,1):
               data[XP]= ( (ord(YP[XP]) - yref)*yinc + yorg ) * y1div/ y2div
        return data
 

# Acquire subsystem
#*****************

    def Acquire (self,str):  
        msg='ACQ '+str+'\n'
        self.GPIB.write(msg)
        st=self.GPIB.read()
        return st

    def Digitize(self,str):
        msg = "DIG %s\n" %(str)
        self.GPIB.write(msg)

    def GetPoints(self): #integer  501 or 8192
        self.GPIB.write("ACQ POINTS?\n")
        st=self.GPIB.read()
        try:
          test=int(st)
        except:
          return "No acquisition made!\nFirst Acquire Data"
        return st

    def GetResolution(self): #integer OFF 6 7 or 8
        self.GPIB.write("ACQ RESO?\n")
        st=self.GPIB.read()
        try:
          test=int(st)
        except:
          return "No acquisition made!\nFirst Acquire Data"
        return st

    def SetResolution(self, str): # '6' '7' '8' or 'OFF'
        msg = "ACQ RESO %s\n" %(str)
        self.GPIB.write(msg)

# HardCopy subsystem
#*****************
    def Hardcopy(self,str="plot.hpgl"):    
        pass

    def close(self):
        self.GPIB.close()