#This script configures all the parameters of the channel coupling
import pyvisa
import AFG_7000

try:
    # Initialize VISA
    rm = pyvisa.ResourceManager()

    # List connected resources
    resources = rm.list_resources()
    print(f"\n\n\nConnected devices: {resources}")

    #Intrumetn TCP/IP address
    address_listed = resources[1]
    address_not_listed = "TCPIP0::192.168.0.59::inst0::INSTR"

    #If you use NI MAX or similar, you can also use one of the adresses in the list 
    master = rm.open_resource(address_not_listed)

    #Master Encoding: Necessary if you use non-ascii characters, like accented vocals.
    master.encoding = "latin-1"

    #Initialize AWG_5062
    AFG = AFG_7000.AFG7000(master)
except Exception as e: print(f"The following exception has occured while connecting with the instrument: {e}")




##################################################################################################################

channel = AFG_7000.Channel.Channel1

#Carrier parameters
amplitude: float = 0.5
unit = AFG_7000.VoltageUnit.VPP
offset: float = 0  #V
frequency: float = 1  #Hz
phase: float = 0  #°
shape = AFG_7000.Shape1.Double_Pulse
file: str = ""

#Coupling parameters: each row in the matrix below lists the coupling state, ratio and offset each couplable parameter 

channel_to_couple = AFG_7000.Channel.Channel2
couple = [[AFG_7000.State.ON, 1.1, 0],  #Amplitude
          [AFG_7000.State.ON, 1.1, 0],  #phase
          [AFG_7000.State.ON, 1.1, 0],  #offset
          [AFG_7000.State.ON, 1.1, 0]]  #frequency

couple_symmetry = [AFG_7000.State.ON, 1.1, 0]   #Only for ramp

#Only for pulse
couple_pulse = [[AFG_7000.State.ON, 1.1, 0],  #Rise time
                [AFG_7000.State.ON, 1.1, 0],  #Fall time
                [AFG_7000.State.ON, 1.1, 0]]  #Duty cycle

##################################################################################################################

try:
    if shape != AFG_7000.Shape1.DC and shape != AFG_7000.Shape1.PR_Noise: AFG.ConfigureStandardWaveform(channel, amplitude, unit, offset, frequency, phase, shape, file)
    else:
        AFG.SetFunctionShape(channel, shape)
        AFG.SetVoltageOffset(channel, offset)

    AFG.SetFunctionShape(channel_to_couple, shape)
    if shape == AFG_7000.Shape1.Ramp: AFG.ConfigureChannelCouplingRamp(channel_to_couple, couple_symmetry[0], couple_symmetry[1], couple_symmetry[2])

    elif shape == AFG_7000.Shape1.Pulse: AFG.ConfigureChannelCouplingPulse(channel_to_couple,
                                                                           couple_pulse[0][0], couple_pulse[1][0], couple_pulse[2][0],
                                                                           couple_pulse[0][1], couple_pulse[1][1], couple_pulse[2][1],
                                                                           couple_pulse[0][2], couple_pulse[1][2], couple_pulse[2][2])
        
    elif shape == AFG_7000.Shape1.DC or shape == AFG_7000.Shape1.PR_Noise: AFG.ConfigureChannelCouplingNoiseOrDC(channel_to_couple, couple[2][0], couple[2][1], couple[2][2])

    elif shape == AFG_7000.Shape1.Double_Pulse: raise Exception("To couple double pulse waveform see the dedicated file named SetCoupleDoublePulse5000")
  
    else: AFG.ConfigureChannelCouplingStandardWaveform(channel_to_couple, AFG_7000.State.ON, 
                                                couple[0][0], couple[1][0], couple[2][0], couple[3][0],
                                                couple[0][1], couple[1][1], couple[2][1], couple[3][1],
                                                couple[0][2], couple[1][2], couple[2][2], couple[3][2])
        
except Exception as e: print(f"{e}")