#!   /usr/bin/env   python
#    coding: utf8

# Copyright CERN, 2011
# Author: Matthieu Cattin <matthieu.cattin@cern.ch>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
# Last modifications: 4/6/2012

# Import system modules
import sys
import time
import os

# Add common modules and libraries location to path
sys.path.append('../../../')
sys.path.append('../../../gnurabbit/python/')
sys.path.append('../../../common/')

# Import common modules
from ptsexcept import *
import rr

# Import specific modules
from numpy import *
from pylab import *
from calibr_box import *
import find_usb_tty
from PAGE.Agilent33250A import *
from PAGE.SineWaveform import *


"""
test18: Test calibration box

"""


NB_CHANNELS = 4
AWG_SET_SLEEP = 0.3


def set_awg_freq(gen, sine, freq):
    sine.frequency = freq
    gen.play(sine)
    print('Sine frequency:%3.3fMHz')%(sine.frequency/1E6)
    time.sleep(AWG_SET_SLEEP)


def main (default_directory = '.'):

    # Constants declaration
    TEST_NB = 18

    # Calibration box vendor and device IDs
    BOX_USB_VENDOR_ID = 0x10c4 # Cygnal Integrated Products, Inc.
    BOX_USB_DEVICE_ID = 0xea60 # CP210x Composite Device
    # Agilent AWG serial access vendor and device IDs
    AWG_USB_VENDOR_ID = 0x0403 # Future Technology Devices International, Ltd
    AWG_USB_DEVICE_ID = 0x6001 # FT232 USB-Serial (UART) IC
    RS232_BAUD = 57600

    try:

        # Objects declaration
        usb_tty = find_usb_tty.CttyUSB()
        awg_tty = usb_tty.find_usb_tty(AWG_USB_VENDOR_ID, AWG_USB_DEVICE_ID)
        box_tty = usb_tty.find_usb_tty(BOX_USB_VENDOR_ID, BOX_USB_DEVICE_ID)
        gen = Agilent33250A(device=awg_tty[0], bauds=RS232_BAUD)
        sine = SineWaveform()
        box = CCalibr_box(box_tty[0])

        # Calibration box version 2
        for i in range(NB_CHANNELS):
            box.select_output_ch(i+1, '10V')
            print "Channel %d = AWG, others = 10V Vref"%(i+1)
            raw_input('Press ENTER to continue...')

        for i in range(NB_CHANNELS):
            box.select_output_ch(i+1, '1V')
            print "Channel %d = AWG, others = 1V Vref"%(i+1)
            raw_input('Press ENTER to continue...')

        for i in range(NB_CHANNELS):
            box.select_output_ch(i+1, '100mV')
            print "Channel %d = AWG, others = 100mV Vref"%(i+1)
            raw_input('Press ENTER to continue...')


    except(CalibrBoxOperationError) as e:
        raise PtsError("Test failed: %s" % e)



    # Calibration box version 1
    """
    print('\n=== 10V range ===')
    box.select_output('10V')
    print('Box gpio: %X')%box.get_config()
    raw_input('Press ENTER to continue...')
    print('\n=== 1V range ===')
    box.select_output('1V')
    print('Box gpio: %X')%box.get_config()
    raw_input('Press ENTER to continue...')
    print('\n=== 100mV range ===')
    box.select_output('100mV')
    print('Box gpio: %X')%box.get_config()
    raw_input('Press ENTER to continue...')
    print('\n=== AWG ===')
    box.select_output('AWG')
    print('Box gpio: %X')%box.get_config()
    """


if __name__ == '__main__' :
    main()
