# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

Simple control script for the HP-8648C signal generator
and the DAT64L programmable digital step attenuator.

Usage:  run this from a command prompt:

          > python simple_test_cube_control.py 16 -20 1595.0
                                               ^   ^   ^
                                               |   |   |
                                     GPIB address  |  frequency [MHz]
                                                   |
                                             desired power
                                                [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

        From the NI-488.2 Help:
            How do I scan for GPIB instruments?
                To scan for instruments connected to your GPIB interface,
                complete the following steps:
                    - Make sure that your instrument is powered on and
                      connected to your GPIB interface.
                    - Start Measurement & Automation Explorer (MAX).
                    - In Measurement & Automation Explorer, expand Devices
                      and Interfaces and Locate Your GPIB Interface.
                    - Right-click on your GPIB interface and select Scan for
                      Instruments from the drop-down menu that appears.
                    - Connected instruments appear beneath the GPIB interface
                      in the Measurement & Automation Explorer configuration
                      tree.
"""


import sys
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 MiniCircuite LNA is in the signal path.
NET_GAIN_IN_RF_PATH_dB = -1.5


def set(gpib_addr, desired_pwr_dBm, freq_MHz):

    # 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
    atten_dB, actual_pwr_dBm \
        = dat64L.set(dat64_port, MAX_OUTPUT_POWER_dBm,
                     desired_pwr_dBm, NET_GAIN_IN_RF_PATH_dB)

    # Connect to the HP signal generator
    hp = hp8648C.device_connect(gpib_addr)

    # Over-ride the frequency command when desired power is "OFF"
    if (atten_dB > 60):
        # Send the sig. gen. way out of band
        freq_MHz = 1000.00

    # Set the frequency
    if hp.get_freq_MHz() != freq_MHz:
        print('\n  Setting sig. gen. frequency to {0:.2f} MHz'
              .format(freq_MHz))
        hp.set_freq_MHz(freq_MHz)

    # 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()

    # Report current configuration to screen
    print('\n  Current Configuration:')
    print('    HP Sig. Gen. Output =   {0:.1f} dBm @ {1:.2f} MHz'
          .format(MAX_OUTPUT_POWER_dBm, freq_MHz))
    print('    DAT64 Attenuator    =  {0:.1f} dB'.format(atten_dB))
    print('    Receiver Input Pwr  = {0:.1f} dBm'.format(actual_pwr_dBm))

    # Disconnect from the HP signal generator
    hp.disconnect()


if __name__ == '__main__':
    if len(sys.argv) != 4:
        print(__doc__)
        print('Ack, not enough input arguments!!')
    else:
        #
        #     GBIP address   desired power [dBm]   frequency [MHz]
        #
        set(int(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]))
