#!   /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: 29/5/2012

# Import system modules
import sys
import time
import os

# Import specific modules
from fmc_adc_spec import *
from fmc_adc import *

# Import common modules
from ptsexcept import *
import rr

"""
test03: Test mezzanine front-panel LEDs

Note: Requires test00.py to run first to load the gateware!
"""


def main (default_directory='.'):

    # Constants declaration
    TEST_NB = 3
    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()

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

    # 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:
        # Standard in/out temporary redirection
        tmp_stdout = sys.stdout;
        sys.stdout = sys.__stdout__;
        tmp_stdin = sys.stdin;
        sys.stdin = sys.__stdin__;

        # Initialize error variable, disable triggers and make sure no acquisition is running
        error = [0, 0]
        fmc.set_trig_config(1, 0, 0, 0, 1, 0, 0)
        fmc.stop_acq()

        #==================================================
        # Test LEDs ON
        fmc.trig_led(1)
        fmc.acq_led(1)

        ask = "";
        while ((ask != "Y") and (ask != "N")) :
            print "-------------------------------------------------------------"
            ask = raw_input("Are the front panel LEDs (TRIG and ACQ) switched ON? [y,n]")
            ask = ask.upper()
	    #print "-------------------------------------------------------------"
            print " "

        if (ask == "N"):
            # LED ON fail
            error[0] += 1

        #==================================================
        # Test LEDs OFF
        fmc.trig_led(0)
        fmc.acq_led(0)

        ask = "";
        while ((ask != "Y") and (ask != "N")) :
            print "-------------------------------------------------------------"
            ask = raw_input("Are the front panel LEDs (TRIG and ACQ) switched OFF? [y,n]")
            ask = ask.upper()
	    #print "-------------------------------------------------------------"
            print " "

        if (ask == "N"):
            # LED OFF fail
            error[1] += 1

        # Restore standard in/out
        sys.stdout = tmp_stdout;
        sys.stdin = tmp_stdin;

        # Check if an error occured during LED test
        if (error != [0]*2):
            if(error[0] != 0):
                print("One or both LEDs not switching ON. Check for soldering or connection problem, grounded control line.")
            if(error[1] != 0):
                print("One or both LEDs not switching OFF. Check for short-cut.")
        else:
            print('LEDs are working ok.')

    except 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)

    # Check if an error occured during LED test
    if (error != [0]*2):
        raise PtsError('An error occured during LEDs test, check log for details.')


if __name__ == '__main__' :
    main()
