# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

Simple control script for the HP 4438C Arbitrary Waveform Generator.

Usage:  run this from a command prompt:

          > python simple_arb_control.py dme 17 -20
                                         ^   ^   ^
                                         |   |   |
                                   rfi type  |  power [dBm]
                                             |
                                       GPIB address

The interference types are:

    dme
    israeli
    iridium

"""


import sys
import time
import hp4438C


# 09/23/2024
#
# This is the net gain in the path from the output of
# the arbitrary waveform generator to the receiver RF
# input, when the MiniCircuits LNA is in the signal path.
NET_GAIN_IN_RF_PATH_dB = +1.9


def set(interference_type, gpib_addr, desired_pwr_dBm, on_time=-1):
    #
    # To calculate the receiver RF input power:
    #
    #     receiver RF input power [dBm] = output power from the ARB [dBm]
    #                                   + net gain from ARB to rcvr [dB]
    #
    # Rearranging to calculate the output power from the ARB:
    #
    #     output power from the ARB [dBm] = receiver RF input power   [dBm]
    #                                     - net gain from ARB to rcvr [dB]
    #
    rf_power_dBm = desired_pwr_dBm - NET_GAIN_IN_RF_PATH_dB

    # Connect to the arbitrary waveform generator
    arb = hp4438C.device_connect(gpib_addr)

    # Configure the desired jamming scenario
    arb.load_scenario(interference_type, rf_power_dBm)

    # Turn on the RF source
    arb.set_rf_on()

    if on_time < 0:
        time.sleep(1)
        input('\n\nPress <Enter> to halt jamming...')
    else:
        print('\nTURN ON FOR ', on_time, ' [SEC]\n')
        time.sleep(on_time)

    # Turn off the RF source
    arb.set_rf_off()

    # Disconnect from the arbitrary waveform generator
    arb.disconnect()

    print('\n\nTEST FINISHED!!!\n')


if __name__ == '__main__':
    if len(sys.argv) != 4:
        print(__doc__)
        print('Ack, not enough input arguments!!')
    else:
        #
        #        RFI type      GPIB address     desired power [dBm]
        #
        set(str(sys.argv[1]), int(sys.argv[2]), float(sys.argv[3]))
