#!   /usr/bin/env   python
#    coding: utf8
##_________________________________________________________________________________________________
##                                                                                                |
##                                           |TDC PTS|                                            |
##                                                                                                |
##                                         CERN,BE/CO-HT                                          |
##________________________________________________________________________________________________|

##-------------------------------------------------------------------------------------------------
##                                                                                                |
##                                         TDC LEDs test                                          |
##                                                                                                |
##-------------------------------------------------------------------------------------------------
##                                                                                                |
## Description  Testing of the LEDs on the front panel of the TDC board.                          |
##                                                                                                |
##              Upon firmware loading the 6 LEDs on the TDC front panel start blinking one by one |
##              on a cyclic way;                                                                  |
##              This test needs the operator's input; when prompted, the operatior needs to report|
##              if all the LEDs are blinking as expected.                                         |
##                                                                                                |
##                                                                                                |
## FW to load   tdc_pts_leds.bit                                                                  |
## Authors      Evangelia Gousiou (Evangelia.Gousiou@cern.ch)                                     |
## Website      http://www.ohwr.org/projects/pts                                                  |
## Date         11/01/2013                                                                        |
##-------------------------------------------------------------------------------------------------

##-------------------------------------------------------------------------------------------------
##                               GNU LESSER GENERAL PUBLIC LICENSE                                |
##                              ------------------------------------                              |
## This source file is free software; you can redistribute it and/or modify it under the terms of |
## the GNU Lesser General Public License as published by the Free Software Foundation; either     |
## version 2.1 of the License, or (at your option) any later version.                             |
## This source is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;       |
## without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.      |
## See the GNU Lesser General Public License for more details.                                    |
## You should have received a copy of the GNU Lesser General Public License along with this       |
## source; if not, download it from http://www.gnu.org/licenses/lgpl-2.1.html                     |
##-------------------------------------------------------------------------------------------------

##-------------------------------------------------------------------------------------------------
##                                            Import                                             --
##-------------------------------------------------------------------------------------------------


# Import system modules
import sys
import time
import os
import math
from datetime import datetime

# Add common modules location tp path
sys.path.append('../../../../pts/')
sys.path.append('../../../../pts/gnurabbit/python/')
sys.path.append('../../../../pts/common/')

# Import common modules
from ptsexcept import *
import rr
import csr


def main (default_directory='.'):

    start_test_time = time.time()

    # Constants declaration
    FMC_TDC_ADDR            = '1a39:0004/1a39:0004@000B:0000'
    FMC_TDC_BITSTREAM_PATH  = '../firmwares/tdc_pts_leds.bit'
    FPGA_LOADER_PATH        = '../../../../pts/gnurabbit/user/fpga_loader'

    print "\n-------------------------------------------------------------------"
    print "----------------------------- TDC PTS -----------------------------"
    print "---------------------------- LEDs test ----------------------------\n"
    print ("Test of the front panel LEDs. For this test the operator’s\n"
           "intervention is needed; when prompted, the operator should visually\n"
           "inspect the LEDs.\n")


#-----> FPGA initializations  
    # SPEC object declaration
    spec = rr.Gennum()

    print "\n_________________________Initialisations_________________________\n"
    # Load FMC TDC firmware
    print "Loading FMC TDC firmware...",
    firmware_loader = os.path.join(default_directory, FPGA_LOADER_PATH)
    bitstream = os.path.join(default_directory, FMC_TDC_BITSTREAM_PATH)
    os.system(firmware_loader + ' ' + bitstream )
    time.sleep(1)
    print "Firmware loaded!"


#-----> LEDs inspection
    print "\n_________________________LEDs inspection_________________________\n"

    # Standard in/out temporary redirection
    tmp_stdout = sys.stdout;
    sys.stdout = sys.__stdout__;
    tmp_stdin = sys.stdin;
    sys.stdin = sys.__stdin__;

    ask = raw_input("--> Are all the TDC front panel LEDs blinking one by one [y/n]: ")
    ask = ask.upper()

    # Standard in/out redirection
    sys.stdout = tmp_stdout;
    sys.stdin  = tmp_stdin;

    if (ask == "N"):
        msg = ("ERROR: Front Panel LEDs: Not blinking as expected")
        os.close(spec.fd)
        print (msg)
        raise PtsError (msg)
    else:
        print ("LEDs inspected OK!")


    print "\n\n--------------------------------"
    end_test_time = time.time()
    print "Test elapsed time: %.2f seconds\n\n\n" % (end_test_time-start_test_time)


if __name__ == '__main__' :
    main()
