#This script configures all the parameters of any waveform
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}")




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

#Extract number of channels and type of output (differential or single-ended form the model)
model = "AWG5062D"
mumber_of_markers = 1
number_of_channels = 0
differential = False
if model.endswith("D"):
    number_of_channels = int(model[-2])
    differential = True
else: number_of_channels = model[-1]

#Channel to set
channel = AFG_7000.Channel.Channel1

#Standard waveform parameters
amplitude: float = 1
unit = AFG_7000.VoltageUnit.VPP
offset: float = 0  #V
frequency: float = 1000000  #Hz
phase: float = 0  #°
shape = AFG_7000.Shape1.Arbitrary

#Ramp symmetry
symmetry: float = 50  #%

#Pulse
leading: float = 0.0000001  #s
trailing: float = 0.0000001  #s
duty_cycle: float = 50  #%
enable_set_period: bool = False
period: float = 0  #s
width = 0.0000005  #s

#Noise
noise_amplitude = 0.1  #V

#DC
dc_offset: float = 0.5  #V

#Arbitrary
sampling_frequency: float = 1000000000
number_of_samples: int = 16384

#Double pulse
amplitude1: float = 0.5  #V
amplitude2: float = 0.5  #V
width1: float = 0.0000002  #s
width2: float = 0.0000002  #s
delay1: float = 0  #s
delay2: float = 0.0000002  #s
leading1: float = 0.00000005  #s
leading2: float = 0.00000005  #s
trailing1: float = 0.00000005  #s
trailing2: float = 0.00000005  #s

#EFILe
file = "multitone_0175.txt"

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



try:
    AFG.Initialize()
    AFG.SetFunctionShape(channel, shape)
    if shape == AFG_7000.Shape1.Ramp: AFG.SetRampSymmetry(channel, symmetry)
    elif shape == AFG_7000.Shape1.Pulse: AFG.ConfigurePulseWaveform(channel, duty_cycle, frequency, enable_set_period, period, width, leading, trailing)
    elif shape == AFG_7000.Shape1.PR_Noise: AFG.ConfigureNoise(channel, noise_amplitude, offset)
    elif shape == AFG_7000.Shape1.DC: AFG.SetVoltageOffset(channel, dc_offset)
    elif shape == AFG_7000.Shape1.Double_Pulse: AFG.ConfigureDoublePulseWaveform(channel, frequency, offset, phase, amplitude1, amplitude2, width1, width2, delay1,
                                                                                 delay2, leading1, leading2, trailing1, trailing2)
    elif shape == AFG_7000.Shape1.EFILe: AFG.SetFunctionEFILe(channel, file)
    elif shape == AFG_7000.Shape1.Arbitrary: AFG.ConfigureArbitraryWaveform(channel, AFG_7000.ArbitraryWaveformSource.List, AFG_7000.BufferTarget.Carrier, [1, 2, 3, 4], "")
    else: AFG.ConfigureStandardWaveform(channel, amplitude, unit, offset, frequency, phase, shape, file)

    input("Press return to start")
    AFG.SetOutputState(channel, AFG_7000.State.ON)
    AFG.AFGStart()
    AFG.Close()
except Exception as e: print(f"{e}")