# -*- coding: utf-8 -*-
"""

@author: David S. De Lorenzo

Import file for the HP 8648C 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):
        self.rm = visa.ResourceManager()
        self.my_instrument \
            = self.rm.open_resource('GPIB0::' + str(gpib_addr) + '::INSTR')
        self.my_instrument.control_ren(visa.constants.VI_GPIB_REN_ASSERT)

    def disconnect(self):
        self.my_instrument.control_ren(visa.constants.VI_GPIB_REN_DEASSERT)
        self.my_instrument.close()

    def set_freq_MHz(self, freq_MHz):
        self.my_instrument.write('FREQ:CW ' + str(freq_MHz) + ' MHZ')

    def get_freq_MHz(self):
        return float(self.my_instrument.query('FREQ:CW?'))/1E6

    def set_power_dBm(self, power_dBm):
        self.my_instrument.write('POW:AMPL ' + str(power_dBm) + ' DBM')

    def get_power_dBm(self):
        return float(self.my_instrument.query('POW:AMPL?'))

    def set_rf_on(self):
        self.my_instrument.write('OUTP:STAT ON')

    def set_rf_off(self):
        self.my_instruemt.write('OUTP:STAT OFF')

    def is_rf_on(self):
        return int(self.my_instrument.query('OUTP:STAT?'))
