# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

Simple random in-band CW jammer

Usage:  run this from a command prompt:

          > python quick_random_cw_jammer.py 16 -20 1590 1.5
                                             ^   ^   ^   ^
                                             |   |   |   |
                                   GPIB address  |   |  dwell [sec]
                                                 |   |
                                     desired power  band_center [MHz]
                                          [dBm]

Note: this script expects HP-8648C at GPIB #16 to connect to DAT64L on COM 4
                      and HP-8648C at GPIB #18 to connect to DAT64L on COM 22

"""


import sys
import time
import random
import dat64L
import hp8648C


# This is the maximum output power
# of the HP-8648C signal generator.
MAX_OUTPUT_POWER_dBm = 10.0


# 09/23/2024
#
# This is the net gain in the path from the output
# of the HP-8648C source to the receiver RF input,
# when the MiniCircuits LNA is in the signal path.
NET_GAIN_IN_RF_PATH_dB = -1.5


def set(gpib_addr, desired_pwr_dBm, center_freq_MHz, dwell_time):

    print('\nSTARTING TEST!!!\n')

    # Set the DAT64L port
    if gpib_addr == 16:
        dat64_port = 4
    elif gpib_addr == 18:
        dat64_port = 22
    else:
        raise ValueError(' *** UNSUPPORTED GPIB ADDRESS!! ***')

    # Control the DAT64L attenuator
    dat64L.set(dat64_port, MAX_OUTPUT_POWER_dBm,
               -999, NET_GAIN_IN_RF_PATH_dB)

    # Connect to the HP signal generator
    hp = hp8648C.device_connect(gpib_addr)

    # Set the power
    if hp.get_power_dBm() != MAX_OUTPUT_POWER_dBm:
        print('\n  Setting sig. gen. power to {0:.1f} dBm'
              .format(MAX_OUTPUT_POWER_dBm))
        hp.set_power_dBm(MAX_OUTPUT_POWER_dBm)

    # Make sure RF output is ON
    if hp.is_rf_on() != 1:
        print('\n  Setting sig. gen. RF Output to ON')
        hp.set_rf_on()

    for i in range(0, 99999):

        # Random float ~[-25,25]
        delta_freq = random.uniform(-25, 25)

        # Random float ~[-25,25] except within 2 MHz of GPS L1-C/A
        # while True:
        #     delta_freq = random.uniform(-25, 25)
        #     if abs(center_freq_MHz + delta_freq - 1575.42) >= 2.0:
        #         break

        # Add center frequency
        freq_MHz = center_freq_MHz + delta_freq

        # Control the DAT64L attenuator
        dat64L.set(dat64_port, MAX_OUTPUT_POWER_dBm,
                   desired_pwr_dBm, NET_GAIN_IN_RF_PATH_dB)

        # Set the frequency
        print('  Setting frequency to {0:.2f} MHz'.format(freq_MHz))
        hp.set_freq_MHz(freq_MHz)

        # Pause
        time.sleep(dwell_time)

        # When switching the frequency, first place the
        # signal generator in its "OFF" configuration
        dat64L.set(dat64_port, 0, -999, 0)
        hp.set_freq_MHz(1000.00)

        # Optional pause
        time.sleep(0.5)

    # Clean-up
    dat64L.set(dat64_port, MAX_OUTPUT_POWER_dBm, -999, NET_GAIN_IN_RF_PATH_dB)
    hp.set_freq_MHz(1000.00)

    # Disconnect from the HP signal generator
    hp.disconnect()

    print('\n\nTEST FINISHED!!!\n')


if __name__ == '__main__':
    if len(sys.argv) != 5:
        print(__doc__)
        print('Ack, not enough input arguments!!')
    else:
        #
        #     GBIP address   desired power [dBm]
        #
        set(int(sys.argv[1]), float(sys.argv[2]),
            float(sys.argv[3]), float(sys.argv[4]))
        #
        #    center freq [MHz]   dwell time [sec]
        #
