# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

Simple frequency-hopping CW jammer

Usage:  run this from a command prompt:

          > python quick_frequency_hopped_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 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):
        print('Sequence #' + str(i))

        # for freq in [1577.92, 1573.42, 1593.00, 1588.00,
        #              1563.00, 1581.00, 1605.00, -1]:
        for delta_freq in [-12.08, -16.58, 3.00, -2.00,
                           2.00, -9.00, 15.00, -9999]:

            # Add center frequency
            freq = center_freq_MHz + delta_freq

            # Power over-ride
            if freq < 0:
                pwr = -999
            else:
                pwr = desired_pwr_dBm

            # Control the DAT64L attenuator
            atten_dB, actual_pwr_dBm \
                = dat64L.set(dat64_port, MAX_OUTPUT_POWER_dBm,
                             pwr, NET_GAIN_IN_RF_PATH_dB)

            # Frequency over-ride
            if (atten_dB > 60):
                freq_MHz = 1000.00
            else:
                freq_MHz = freq

            # 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]
        #
