# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

Simple control script for the 12V chirp jammer and
the DAT64L programmable digital step attenuator.

Usage:  run this from a command prompt:

          > python simple_chirp_jammer_control.py 21 -20
                                                  ^   ^
                                                  |   |
                                            COM port  |
                                                      |
                                                desired power
                                                   [dBm]

As provided by Ken Douglas:
    The 12V supply to the chirp jammer is controlled
    via a USB-to-serial adapter.  When the port is
    opened and the RTS line is set True, then the 12V
    supply will turn on; when the port is closed or
    the RTS line is set False, then the 12V supply
    will turn off.

Note:
    The serial port is closed when the context is left.
    https://pyserial.readthedocs.io/en/latest/pyserial_api.html

Note: this script expects the chirp jammer 12V supply to be on COM 21
           and that the chirp jammer connects to the DAT64L on COM 19

"""


import sys
import time
import serial
import dat64L


# This is the approximate output power
# of the 12V chirp jammer.
CHIRP_OUTPUT_POWER_dBm = 16.9


# 09/23/2024
#
# This is the net gain in the path from the output
# of the chirp jammer to the receiver RF input,
# when the MiniCircuits LNA is in the signal path.
NET_GAIN_IN_RF_PATH_dB = +0.5


def set(com_port, desired_pwr_dBm, on_time=-1):

    # Set the DAT64L port
    if com_port == 21:
        dat64_port = 19
    else:
        raise ValueError(' *** Only com_port = 21 is supported ***')

    print('\nSTARTING TEST!!!\n')

    # Control the DAT64L attenuator
    atten_dB, actual_pwr_dBm \
        = dat64L.set(dat64_port, CHIRP_OUTPUT_POWER_dBm,
                     desired_pwr_dBm, NET_GAIN_IN_RF_PATH_dB)

    if (atten_dB > 60):
        raise ValueError(' *** Chirp does not go this low!! ***')

    # Get a Serial instance
    ser = serial.Serial()

    # Configure but don't open the instance
    ser.port = 'COM' + str(com_port)
    ser.baudrate = 9600

    # Turn on the 12V supply to the chirp jammer
    ser.open()
    ser.rts = True

    print('  Current Configuration:')
    print('    Chirp Jammer Output =  {0:.1f} dBm'
          .format(CHIRP_OUTPUT_POWER_dBm))
    print('    DAT64 Attenuator    =  {0:.1f} dB'.format(atten_dB))
    print('    Receiver Input Pwr  = {0:.1f} dBm'.format(actual_pwr_dBm))

    if on_time < 0:
        time.sleep(1)
        input('\nPress <Enter> to halt chirp jamming...\n')
    else:
        print('\nTURN ON FOR ', on_time, ' [SEC]\n')
        time.sleep(on_time)

    # Turn off the 12V supply
    ser.rts = False
    ser.close()

    # Leave the DAT64L attenuator set to 60 dB
    dat64L.set_attenuation_dB(dat64_port, 60)

    print('\nTEST FINISHED!!!\n')


if __name__ == '__main__':
    if len(sys.argv) != 3:
        print(__doc__)
        print('Ack, not enough input arguments!!')
    else:
        #
        #        COM port     desired power [dBm]
        #
        set(int(sys.argv[1]), float(sys.argv[2]))
