#This script configures all the parameters of the frequency sweep
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 = 1
unit = AFG_7000.VoltageUnit.VPP
offset: float = 0  #V
start_frequency: float = 1000000  #Hz
stop_frequency: float = 10000000  #Hz
phase: float = 0  #°
shape = AFG_7000.Shape1.Sine

#Sweep parameters
rise_time: float = 2   #s
fall_time: float = 0.5  #s
hold_time: float = 1  #s
trigger_mode: AFG_7000.SweepMode = AFG_7000.SweepMode.Triggered
sweep_shape: AFG_7000.SweepShape = AFG_7000.SweepShape.Linear  #Pulse, Noise and DC are not valid
file: str = ""  #Only for EFILe shape
step_number : int = 10

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

try:
    AFG.ConfigureStandardWaveform(channel, amplitude, unit, offset, start_frequency, phase, shape, file)
    AFG.ConfigureSweep(channel, trigger_mode, start_frequency, stop_frequency, rise_time, hold_time, fall_time, sweep_shape, trigger_mode, step_number, file, 10000000)

    input("Press return to start")
    AFG.SetOutputState(channel, AFG_7000.State.ON)

    if trigger_mode == AFG_7000.SweepMode.Triggered:
        while True:
            if input("Press t to send trigger, any other key to close ") == "t":
                AFG.SendTrigger()
                AFG.ReleaseTrigger()
                continue
            else: break
        AFG.Close()
except Exception as e: print(f"{e}")