#!   /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: 30/5/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 fmc_adc_spec import *
from fmc_adc import *
from numpy import *
from pylab import *

"""
test42: Tests sampling frequency counter
"""


def main (default_directory='.'):

    # Constants declaration
    TEST_NB = 42
    FMC_ADC_BITSTREAM = '../firmwares/spec_fmcadc100m14b4cha.bin'
    FMC_ADC_BITSTREAM = os.path.join(default_directory, FMC_ADC_BITSTREAM)
    EXPECTED_BITSTREAM_TYPE = 0x1

    start_test_time = time.time()
    print "================================================================================"
    print "Test%02d start\n" % TEST_NB

    # SPEC object declaration
    print "Loading hardware access library and opening device.\n"
    spec = rr.Gennum()

    # Load FMC ADC firmware
    #print "Loading FMC ADC firmware: %s\n" % FMC_ADC_BITSTREAM
    #if(os.path.isfile(FMC_ADC_BITSTREAM)):
    #    spec.load_firmware(FMC_ADC_BITSTREAM)
    #    time.sleep(2)
    #else:
    #    raise PtsCritical("Firmware file \"%s\" is missing, test stopped." % FMC_ADC_BITSTREAM)

    # Carrier object declaration (SPEC board specific part)
    # Used to check that the firmware is loaded.
    try:
        carrier = CFmcAdc100mSpec(spec, EXPECTED_BITSTREAM_TYPE)
    except FmcAdc100mSpecOperationError as e:
        raise PtsCritical("Carrier init failed, test stopped: %s" % e)

    carrier.print_unique_id()
    carrier.print_temp()

    # Mezzanine object declaration (FmcAdc100m14b4cha board specific part)
    try:
        fmc = CFmcAdc100m(spec)
    except FmcAdc100mOperationError as e:
        raise PtsCritical("Mezzanine init failed, test stopped: %s" % e)


    try:
        expect_samp_freq = 100E6

        samp_freqs = []
        for i in range(20):
            time.sleep(2)
            samp_freq = fmc.get_samp_freq()
            samp_freqs.append(samp_freq)
            diff = abs(samp_freq - expect_samp_freq)
            print("Sampling frequency: %6d Hz, diff: %6d Hz"%(samp_freq, diff))

        freq_mean = mean(samp_freqs)
        freq_dev  = std(samp_freqs)
        print("Measured sampling frequency, mean: %f std_dev:%f"%(freq_mean, freq_dev))

    except(FmcAdc100mSpecOperationError, FmcAdc100mOperationError) as e:
        raise PtsError("Test failed: %s" % e)


    print ""
    print "==> End of test%02d" % TEST_NB
    print "================================================================================"
    end_test_time = time.time()
    print "Test%02d elapsed time: %.2f seconds\n" % (TEST_NB, end_test_time-start_test_time)


if __name__ == '__main__' :
    main()
