#This script configures all the parameters of any digital modulation (FSK, PSK, PWM)
from enum import Enum
import pyvisa
import AFG_7000

class DigitalModulation(Enum):
    FSK = 1
    PSK = 2
    PWM = 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}")




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

channel = AFG_7000.Channel.Channel1
modulation_type : DigitalModulation = DigitalModulation.PWM

#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

#Pulse parameters for PWM
duty_cycle = 50  #%
enable_set_period: bool = False
period: float = 0.000001 #s
width: float = 0.0000005 #s
leading: float = 0.000000080  #s
trailing: float = 0.000000080  #s

#Modulation parameters
source = AFG_7000.ModulatingSource.Int
modulation_frequency: float = 15000 #Hz
FSK_hop_frequency: float = 1000  #Hz
PSK_hop_phase: float = 1  #°
PWM_shape = AFG_7000.ModulatingShape.Square
PWM_deviation: float = 30  #%

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

try:
    AFG.Initialize()
    if modulation_type == DigitalModulation.FSK:
        AFG.ConfigureStandardWaveform(channel, amplitude, amplitude_unit, offset, frequency, phase, shape, file)
        AFG.ConfigureFSKModulation(channel, source, modulation_frequency, FSK_hop_frequency, AFG_7000.State.ON, AFG_7000.ModulatingInput.Mod1)
    elif modulation_type == DigitalModulation.PSK:
        AFG.ConfigureStandardWaveform(channel, amplitude, amplitude_unit, offset, frequency, phase, shape, file)
        AFG.ConfigurePSKModulation(channel, source, modulation_frequency, PSK_hop_phase, AFG_7000.State.ON, AFG_7000.ModulatingInput.Mod1)
    else:
        AFG.ConfigurePulseWaveform(channel, duty_cycle, modulation_frequency, enable_set_period, period, width, leading, trailing)
        AFG.ConfigurePulseWidthModulation(channel, AFG_7000.State.ON, source, PWM_shape, frequency, PWM_deviation, file, AFG_7000.ModulatingInput.Mod1)
        input("Press return to start")
    AFG.SetOutputState(channel, AFG_7000.State.ON)
    AFG.Close()
except Exception as e: print(f"{e}")