import pyvisa
import AWG_7000

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

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

    #Instrument TCP/IP address
    address_listed = resources[1]
    address_not_listed = "TCPIP0::192.168.0.55::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
    AWG = AWG_7000.AWG7000(master)
except Exception as e: print(f"The following exception has occured while connecting with the instrument: {e}")




##################################################################################
format = AWG_7000.Format.Amplitude_offset
analog_waveform_names = [["SINE", "SINE"],
                  ["RAMP", "RAMP"],
                  ["SQUARE", "SQUARE"]]
amplitudes = [[1, 1.5],
               [0.75, 0.75],
               [1, 1.2]]
offsets = [[0, 0],
            [0, 0.3],
            [0, 0]]
decreasing_strategies = [[AWG_7000.DecreasingStrategy.Decimation, AWG_7000.DecreasingStrategy.Decimation],
                         [AWG_7000.DecreasingStrategy.Decimation, AWG_7000.DecreasingStrategy.Decimation],
                         AWG_7000.DecreasingStrategy.Decimation, AWG_7000.DecreasingStrategy.Decimation]
increasing_strategies = [[AWG_7000.IncreasingStrategy.Interpolation, AWG_7000.IncreasingStrategy.Interpolation],
                         [AWG_7000.IncreasingStrategy.Interpolation, AWG_7000.IncreasingStrategy.Interpolation],
                         [AWG_7000.IncreasingStrategy.Interpolation, AWG_7000.IncreasingStrategy.Interpolation]]
sub_lengths = [[16384, 16384],
               [16384, 16384],
               [16384, 16384]]
delays = [[0, 0],
          [0, 0],
          [0, 0]]
output_formats = [[AWG_7000.OutputFormat.Waveform, AWG_7000.OutputFormat.Waveform],
                  [AWG_7000.OutputFormat.Waveform, AWG_7000.OutputFormat.Waveform],
                  [AWG_7000.OutputFormat.Waveform, AWG_7000.OutputFormat.Waveform]]
entry_lengths = [16384, 16384, 16384]
repetitions = [1, 1, 1]
sequencer_length = len(analog_waveform_names)
digital_waveforms = ["CLOCK", "COUNTER", "COUNTER"]
marker_custom_pattern = ["", "", ""]

#Burst parameters
burst_count = 10
wait_trigger_on = AWG_7000.WaitTriggerState.First_sample
##################################################################################




try:
    AWG.Initialize()

    for channel in AWG.channels:
        AWG.ConfigureChannelSettings(channel, 100, 0, 0, 0, 0, AWG_7000.KeepLastMode.KeepLast, AWG_7000.Polarity.Inverted, 0, 0,AWG_7000.ChannelOptimization.High_Linearity, 0,
                                     AWG_7000.State.OFF, AWG_7000.KeepLastMode.KeepLast, 0)
        
    AWG.ConfigureGeneralSettings(AWG_7000.OperatingMode.Full_rate, AWG_7000.IncreasingStrategy.Interpolation, AWG_7000.DecreasingStrategy.Decimation,
                                 AWG_7000.RunMode.SingleBurst, wait_trigger_on, burst_count, AWG_7000.BurstSelection.NumberCycles,
                                 AWG_7000.JumpMode.AfterRepetitions)
    
    AWG.SetSequencerLength(sequencer_length, AWG_7000.SetValue.PRESENT)

     #Program all wanted sequencer entries
    for i in range(sequencer_length):
        entry = AWG_7000.SequencerEntry(entry_number=i+1, analog_waveform_names=analog_waveform_names[i], amplitudes=amplitudes[i], offsets=offsets[i],
                                        decreasing_strategies=decreasing_strategies[i], increasing_strategies=increasing_strategies[i], sub_lengths=sub_lengths[i],
                                        delays=delays[i], digital_waveform_name=digital_waveforms[i], custom_pattern=marker_custom_pattern[i], length=entry_lengths[i],
                                        repetitions=repetitions[i], output_formats=output_formats[i])
        AWG.ConfigureSequencerElement(entry)

    #Turn all the channels on    
    for channel in AWG.channels: AWG.SetOutputState(channel, AWG_7000.State.ON)
    
    AWG.AWGStart()




    #HMI
    while(input("Press enter to send trigger; press any other key + enter to finish: ") == ""):
        AWG.SendTrigger()  
    AWG.Close()
except Exception as e: print(f"The following excepotion has occured: {e}")