# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

Import file for the HP 8657B Signal Generator.

    NOTE:  In order to import this with GNURadio, you must first install
           the PyVISA module (pyvisa doesn't come with the default install).
           From a GNURadio Command Prompt:
               'python -m pip install pyvisa'

"""


import pyvisa as visa


class device_connect():

    def __init__(self, gpib_addr):
        print('Connecting:  ' + gpib_addr + '\n')
        self.rm = visa.ResourceManager()
        self.my_instrument = self.rm.open_resource(gpib_addr)
        self.my_instrument.control_ren(visa.constants.VI_GPIB_REN_ASSERT)
        self.my_instrument.write('R2')

    def disconnect(self):
        print('Disconnecting')
        self.my_instrument.control_ren(visa.constants.VI_GPIB_REN_DEASSERT)
        self.my_instrument.close()

    def fm_modulation_on(self, center_freq_MHz, power_dBm, modulation_kHz=400):
        print('  FM modulation ON:  '
              + str(center_freq_MHz) + ' MHz at ' + str(power_dBm) + ' dBm\n')
        self.my_instrument.write('FR' + str(center_freq_MHz) + 'MZ '
                                 + 'AP' + str(power_dBm) + 'DM '
                                 + 'AMS4 FMS3 '
                                 + 'FM' + str(modulation_kHz) + 'KZ'
                                 + 'R3')

    def cw_modulation_on(self, center_freq_MHz, power_dBm):
        print('  CW modulation ON:  '
              + str(center_freq_MHz) + ' MHz at ' + str(power_dBm) + ' dBm\n')
        self.my_instrument.write('FR' + str(center_freq_MHz) + 'MZ '
                                 + 'AP' + str(power_dBm) + 'DM '
                                 + 'AMS4 FMS4'
                                 + 'R3')

    def set_rf_on(self):
        print('  Turning RF ON')
        self.my_instrument.write('R3')

    def set_rf_off(self):
        print('  Turning RF OFF')
        self.my_instrument.write('R2')
