#This script sets all the parameters of the analog modulation chosen by the user
from enum import Enum
import pyvisa
import AFG_7000

class AnalogModulation(Enum):
    AM = 1
    PM = 2
    FM = 3




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 = AFG_7000.Channel.Channel1
modulation_type : AnalogModulation = AnalogModulation.AM

#Carrier parameters
amplitude: float = 0.5
amplitude_unit: AFG_7000.VoltageUnit = AFG_7000.VoltageUnit.VPP
frequency: float = 2000000  #Hz
phase: float = 1 #°
offset: float = 0
shape: AFG_7000.Shape1 = AFG_7000.Shape1.Square  #Pulse, double pulse, noise and DC ar not valid for AM
file: str = ""  #Only for EFILe shape

#Modulation waveform
modulation_frequency: float = 10000  #Hz
AM_depth: float = 100  #%
FM_deviation: float = 10000  #Hz
PM_deviation: float = 30  #°
modulation_source = AFG_7000.ModulatingSource.Int
modulation_shape = AFG_7000.ModulatingShape.Sine
modulation_file : str = ""  #Only for EFILe shape

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

try:
    AFG.Initialize()
    
    AFG.ConfigureStandardWaveform(channel, amplitude, amplitude_unit, offset, frequency, phase, shape, file)
    if modulation_type == AnalogModulation.AM: AFG.ConfigureAmplitudeModulation(channel, modulation_shape, modulation_frequency, AM_depth, modulation_source,
                                                                                AFG_7000.State.ON, file, AFG_7000.ModulatingInput.Mod1)
    elif modulation_type == AnalogModulation.FM: AFG.ConfigureFrequencyModulation(channel, modulation_shape, modulation_frequency, FM_deviation, modulation_source,
                                                                                  AFG_7000.State.ON, file)
    else: AFG.ConfigurePhaseModulation(channel, modulation_shape, modulation_frequency, PM_deviation, modulation_source, AFG_7000.State.ON, file)

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