#This script configures all the coupling parameters using a double pulse wavefomr
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}")




#DOUBLE PULSE COUPLING PARAMETERS##############################################
#Coupling states
global_coupling_state= AFG_7000.State.ON
frequency_coupling_state = AFG_7000.State.ON
phase_coupling_state = AFG_7000.State.ON
offset_coupling_state = AFG_7000.State.ON
amplitude_coupling_state = [AFG_7000.State.ON, AFG_7000.State.ON]
delay_coupling_state = [AFG_7000.State.ON, AFG_7000.State.ON]
width_coupling_state = [AFG_7000.State.ON, AFG_7000.State.ON]
leading_coupling_state = [AFG_7000.State.ON, AFG_7000.State.ON]
trailing_coupling_state = [AFG_7000.State.ON, AFG_7000.State.ON]

#Coupling ratios
frequency_ratio = 1
phase_ratio = 1
offset_ratio = 1
amplitude_ratio = [1, 1]
delay_ratio = [1, 1]
width_ratio = [1, 1]
leading_ratio = [1, 1]
trailing_ratio = [1, 1]

#Coupling offsets
frequency_offset = 0
phase_offset = 1
offset_offset = 0
amplitude_offset = [0, 0.1]
delay_offset = [0, 0]
width_offset = [0, 0]
leading_offset = [0, 0]
trailing_offset = [0, 0]

#Channel to couple
channel = AFG_7000.Channel.Channel2
##############################################################################



try:
    AFG.SetFunctionShape(channel, AFG_7000.Shape1.Double_Pulse)
    AFG.SetCoupleState(channel, global_coupling_state)
    AFG.SetCoupleFrequencyState(channel, frequency_coupling_state)
    AFG.SetCouplePhaseState(channel, phase_coupling_state)
    AFG.SetCoupleFrequencyRatio(channel, frequency_ratio)
    AFG.SetCouplePhaseRatio(channel, phase_ratio)
    AFG.SetCoupleFrequencyOffset(channel, frequency_offset)
    AFG.SetCouplePhaseOffset(channel, phase_offset)

    for i in [0, 1]: AFG.SetCoupleDoublePulseAmplitudeState(channel, i+1, amplitude_coupling_state[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseDelayState(channel, i+1, delay_coupling_state[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseWidthState(channel, i+1, width_coupling_state[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseLeadingState(channel, i+1, leading_coupling_state[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseTrailingState(channel, i+1, trailing_coupling_state[i])

    for i in [0, 1]: AFG.SetCoupleDoublePulseAmplitudeRatio(channel, i+1, amplitude_ratio[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseDelayRatio(channel, i+1, delay_ratio[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseWidthRatio(channel, i+1, width_ratio[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseLeadingRatio(channel, i+1, leading_ratio[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseTrailingRatio(channel, i+1, trailing_ratio[i])

    for i in [0, 1]: AFG.SetCoupleDoublePulseAmplitudeOffset(channel, i+1, amplitude_offset[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseDelayOffset(channel, i+1, delay_offset[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseWidthOffset(channel, i+1, width_offset[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseLeadingOffset(channel, i+1, leading_offset[i])
    for i in [0, 1]: AFG.SetCoupleDoublePulseTrailingOffset(channel, i+1, trailing_offset[i])

except Exception as e: print(f"Error: {e}")