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.83::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
    AWG = AWG_7000.AWG7000(master)
except Exception as e: print(f"The following exception has occured while connecting with the instrument: {e}")




##################################################################################
#Sequencer parameters
#Sequencer parameters
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)
wait_event = AWG_7000.JumpEvent.Button
jump_event = AWG_7000.JumpEvent.NONE
jump_to_mode = AWG_7000.Mode.Next
jump_to_entry = 1
pattern_code = 1
pattern_mode = AWG_7000.Mode.Next
pattern_jump_to_entry = 1
go_to_mode = AWG_7000.Mode.Next
go_to_entry = 1

#General settings
wait_trigger_on = AWG_7000.WaitTriggerState.First_sample
jump_mode = AWG_7000.JumpMode.ASAP
timer_interval = 1

#Trigger and general settings
trigger_impedance = AWG_7000.Impedance.High
trigger_edge = AWG_7000.Slope.Positive
trigger_source = AWG_7000.TriggerInSource.Timer
trigger_threshold = 1
trigger_timing_mode = AWG_7000.TriggerTiming.Fast
trigger_delay_adjust = 0

#Digital parameters
digital_channels = AWG_7000.ChannelsNumber.Eight
digital_pod = AWG_7000.Pod.PodA
skew = 0
digital_voltage_level = 1
digital_waveforms = ["COUNTER", "COUNTER", "COUNTER"]

#Marker custom pattern
marker_custom_pattern = ["", "", ""]

#External forced jumps settings
enable = AWG_7000.State.ON
polarity = AWG_7000.Polarity.Normal
forced_jumps = [[1, 3, AWG_7000.State.ON],  #Pattern, entry, enable
                [2, 9, AWG_7000.State.OFF],
                [3, 10, AWG_7000.State.OFF]]
#################################################################################




try:
    AWG.Initialize(True)

    AWG.ConfigureGeneralSettings(AWG_7000.OperatingMode.Full_rate, AWG_7000.IncreasingStrategy.Interpolation, AWG_7000.DecreasingStrategy.Decimation, AWG_7000.RunMode.Advanced,
                                 wait_trigger_on, 1, AWG_7000.BurstSelection.NumberCycles, jump_mode)

    for channel in AWG.channels:
        AWG.ConfigureChannelSettings(channel, 100, 0, 0, 0, 0, AWG_7000.KeepLastMode.CustomOnStop, AWG_7000.Polarity.Normal, 0, 0,
                                     AWG_7000.ChannelOptimization.High_Linearity, 0, AWG_7000.State.OFF, AWG_7000.KeepLastMode.CustomOnStop, 0)
        
    AWG.SetSequencerLength(sequencer_length, AWG_7000.SetValue.PRESENT)
    
    for name in digital_waveforms:
        if name != "": AWG.ConfigureDigitalChannels(digital_channels, digital_pod, digital_voltage_level, AWG_7000.SetValue.PRESENT, skew, AWG_7000.SetValue.PRESENT)
        AWG.SetDigitalsState(AWG_7000.State.ON)

    #Program all wanted sequencer entries
    for i in range(sequencer_length):
        entry = AWG_7000.AdvancedSequencerEntry(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], wait_event=wait_event, jump_event=jump_event, jump_to_mode=jump_to_mode,
                                        jump_to_entry_item=jump_to_entry, pattern_code=pattern_code, pattern_mode=pattern_mode, pattern_jump_to_entry=pattern_jump_to_entry,
                                        go_to_mode=go_to_mode, go_to_entry_item=go_to_entry)
        AWG.ConfigureAdvancedSequencerElement(format, entry)
        
    AWG.ConfigureTriggerIn(trigger_source, 0,AWG_7000.SetValue.PRESENT, trigger_threshold, trigger_edge, trigger_impedance, trigger_timing_mode, trigger_delay_adjust)

    
    for i in range(len(forced_jumps)): AWG.ConfigureExternalForceJumps(enable, polarity, forced_jumps[i][0], forced_jumps[i][2], forced_jumps[i][1])
    
    for channel in AWG.channels: AWG.SetOutputState(channel, AWG_7000.State.ON)

    while(True):
        if input("Press enter to start") == "":
            AWG.AWGStart()
            break
    
    while(True):
        if input("Press enter to stop") == "":
            AWG.AWGStop()
            break

    AWG.Close()
except Exception as e: print(f"The following exception has occured: {e}")