# -*- coding: utf-8 -*-
"""

Created on Wed Mar 28 17:22:21 2018

@author: David S. De Lorenzo

         based on templates provided by Colin Madigan

Import file for the HP 4438C Arbitrary Waveform Generator.

    NOTE:  In order to import this with GNURadio, you must first install
           the PyVISA module (pyvisa doesn't come with the default install).
           From a GNURadio Command Prompt:
               'python -m pip install pyvisa'

"""


import time
import pyvisa as visa


class device_connect():

    def __init__(self, gpib_addr):
        self.rm = visa.ResourceManager()
        self.my_instrument \
            = self.rm.open_resource('GPIB0::' + str(gpib_addr) + '::INSTR')
        self.my_instrument.write(":output:state off")

    def disconnect(self):
        self.my_instrument.close()

    def load_scenario(self, interference_type, rf_power_dBm):
        if interference_type == 'dme':
            print('\nSTARTING DME JAMMER!!!\n')
            self.set_dme_jammer(rf_power_dBm)
        elif interference_type == 'israeli':
            print('\nSTARTING ISRAELI JAMMER!!!\n')
            self.set_israeli_jammer(rf_power_dBm)
        elif interference_type == 'iridium':
            print('\nSTARTING IRIDIUM JAMMER!!!\n')
            self.set_iridium_jammer(rf_power_dBm)
        else:
            raise ValueError(' *** INVALID INTERFERENCE TYPE!! ***')

    def set_dme_jammer(self, power_dBm):
        # Recall correct instrument state
        self.my_instrument.write("*RCL 20")     # Recall register #20 settings
        self.my_instrument.write(":output:state off")
        self.my_instrument.write(":SOURce:FREQuency:FIXed 1088000000")
        self.my_instrument.write(":SOURce:POWer:LEVel:IMMediate:AMPLitude " +
                                 str(power_dBm))

        # Load and configure correct list sweep
        self.my_instrument.write(':MEMory:LOAD:LIST "DME_JAM"')
        self.my_instrument.write(":SOURce:LIST:DIRection UP")
        self.my_instrument.write(":SOURce:LIST:TYPE LIST")
        self.my_instrument.write(":SOURce:FREQuency:MODE LIST")
        self.my_instrument.write(":SOURce:LIST:TYPE LIST")
        self.my_instrument.write(":SOURce:LIST:TRIGger:Source IMMediate")
        self.my_instrument.write(":TRIGger:SEQuence:SOURce: IMMediate")
        self.my_instrument.write(":TRIGger:OUTPut:POLarity: NEGative")

        # Load and configure ARB generator
        self.my_instrument.write('RADio:ARB:SCLock:RATE 2000000')  # 2 MHz
        self.my_instrument.write('RADio:ARB:WAVeform "DME_JAM"')
        time.sleep(7)                           # Sleep: large waveform file!!
        self.my_instrument.write('RADio:ARB:MPOLarity:MARKer1 NEGative')
        self.my_instrument.write('RADio:ARB:MDEStination:PULSe M1')
        self.my_instrument.write('RADio:ARB:TRIGger:TYPE SINGle')
        self.my_instrument.write('RADio:ARB:RETRigger IMMediate')
        self.my_instrument.write('RADio:ARB:TRIGger:SOURce EXT')
        self.my_instrument.write('RADio:ARB:TRIGger:SOURce:EXT:SLOPe NEGative')

    def set_israeli_jammer(self, power_dBm):
        # Recall correct instrument state
        self.my_instrument.write("*RCL 18")     # Recall register #18 settings
        self.my_instrument.write(":output:state off")
        self.my_instrument.write(":SOURce:FREQuency:FIXed 1590000000")
        self.my_instrument.write(":SOURce:POWer:LEVel:IMMediate:AMPLitude " +
                                 str(power_dBm))

        # Load and configure ARB generator
        self.my_instrument.write('RADio:ARB:SCLock:RATE 100000000')  # 100 MHz
        self.my_instrument.write('RADio:ARB:WAVeform "ISRAELI_JAMMER"')
        time.sleep(7)                           # Sleep: large waveform file!!
        self.my_instrument.write(":RADio:ARB:STATe ON")

    def set_iridium_jammer(self, power_dBm):
        # Recall correct instrument state
        self.my_instrument.write("*RCL 19")     # Recall register #19 settings
        self.my_instrument.write(":output:state off")
        self.my_instrument.write(":SOURce:FREQuency:FIXed 1621000000")
        self.my_instrument.write(":SOURce:POWer:LEVel:IMMediate:AMPLitude " +
                                 str(power_dBm))

        # Load and configure correct list sweep
        self.my_instrument.write(':MEMory:LOAD:LIST "IRIDIUM"')
        self.my_instrument.write(":SOURce:LIST:DIRection UP")
        self.my_instrument.write(":SOURce:LIST:TYPE LIST")
        self.my_instrument.write(":SOURce:FREQuency:MODE LIST")
        self.my_instrument.write(":SOURce:LIST:TYPE LIST")
        self.my_instrument.write(":SOURce:LIST:TRIGger:Source EXTernal")
        self.my_instrument.write(":TRIGger:SEQuence:SOURce: EXTernal")

        # Load and configure ARB generator
        self.my_instrument.write('RADio:ARB:SCLock:RATE 100000000')  # 100 MHz
        self.my_instrument.write('RADio:ARB:WAVeform "IRIDIUM"')
        time.sleep(7)                           # Sleep: large waveform file!!
        self.my_instrument.write('RADio:ARB:MPOLarity:MARKer1 NEGative')
        self.my_instrument.write('RADio:ARB:MDEStination:PULSe M1')
        self.my_instrument.write('RADio:ARB:SCLock:RATE 100000')
        self.my_instrument.write(":RADio:ARB:STATe ON")

    def set_rf_on(self):
        self.my_instrument.write(":output:state on")

    def set_rf_off(self):
        self.my_instrument.write(":output:state off")
