#!   /usr/bin/env   python

# Copyright Creotech SA, 2016
# Author: Marek Guminski <marek.guminski@creotech.pl>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org


import spec
import fmcmasterfip as fmc
import utilities as util
import ptsexcept
import time


ledlist = ['out_of_sync', 'rx_err', 'rx_act', 'sync', 'tx_act', 'tx_err' ]

def test_led_all( board ):
    
    util.section_msg("Testing all LED's")
    
    board.fipcore.write_regname( "led", 0 )
    if not util.ask_user("Are all LEDs disabled?"):
        util.err_msg("Some LEDs might be broken")
        return 0
    
    board.fipcore.write_regname( "led", 0xff )
    if not util.ask_user("Are all LEDs enabled?"):
        util.err_msg("Some LEDs might be broken")
        return 0
    
    board.fipcore.write_regname( "led", 0 )
    if not util.ask_user("Are all LEDs disabled?"):
        util.err_msg("Some LEDs might be broken")
        return 0
    
    util.info_msg("All LEDs verified successfully!")
    return 1
    
def test_led_single( board, led ):
    
    
    
    regname = ( 'led.%s' % led )
    
    board.fipcore.write_regname( regname, 0 )
    if not util.ask_user("Is led %s disabled?" % led):
        util.err_msg("LED %s might be broken" % led)
        return 0
    
    board.fipcore.write_regname( regname, 1 )
    if not util.ask_user("Is led %s enabled?" % led):
        util.err_msg("LED %s might be broken" % led)
        return 0
    
    board.fipcore.write_regname( regname, 0 )
    if not util.ask_user("Is led %s disabled?" % led):
        util.err_msg("LED %s might be broken" % led)
        return 0
        
    util.info_msg("LED %s verified successfully!" % led)
    
    return 1

def test_running_led( dut ):
    
    util.section_msg("Testing LED's")
    
    util.user_msg("Please observe the FMC masterFIP front panel")
    util.user_msg("Please make sure that no more than one LED is enabled at a time")
    util.ask_user("Are you ready to start a test?" )

    for j in xrange(2):
        dut.fipcore.write_regname( 'led', 1 << 1 )
        time.sleep(0.7)
        dut.fipcore.write_regname( 'led', 1 << 2 )
        time.sleep(0.7)
        dut.fipcore.write_regname( 'led', 1 << 4 )
        time.sleep(0.7)
        dut.fipcore.write_regname( 'led', 1 << 5 )
        time.sleep(0.7)
        dut.fipcore.write_regname( 'led', 1 << 0 )
        time.sleep(0.7)
        dut.fipcore.write_regname( 'led', 1 << 3 )
        time.sleep(0.7)

        #for i in xrange(6):
        #    dut.fipcore.write_regname( 'led', 1 << i )
        #    time.sleep(0.7)
    
    dut.fipcore.write_regname( 'led', 0 )
    
    if not util.ask_user("Was there no more than one led enabled at a time?"):
        util.err_msg("LED's verification failed!")
        return 0
    
    else :
        util.info_msg("LED's verified successfully!")
        return 1

def main (card=None, default_directory='.',suite=None, serial=""):
    
    testname= "Test01: LED Connectivity"
    util.header_msg( testname, [ "LED connectivity"] )
    
    box = util.calibration_box_init()
    
    ###############################################################################
    ############################ initialization ###################################
    ###############################################################################
    
    util.section_msg("Hardware initialization")
    abspath = util.find_prj_topdir(util.TOPDIRNAME);
    carrier = spec.spec( abspath );
        
    # initialize test list assuming negative result    
    test_results={}
    
    # FPGA bitstream and FMC presence are verified during initialization
    dut = fmc.fmcmasterfip(carrier, abspath, util.FIRMWARE_PATH)

    ###############################################################################
    ############################ actual test ######################################
    ###############################################################################
    
    # negative return value means error
#     test_results['All leds'] = test_led_all( dut )
    test_results['Running led'] = test_running_led( dut )
    
    for led in ledlist:
        # if there was an error in test of all LEDs
        # we have to check which LED is broken
        if ( test_results['Running led'] == 0 ):
            test_results[led] = test_led_single(dut, led)
         
        # if all LEDs test was successful individual tests is not needed
        else:
            test_results["LED: " + led] = 1
    
    ###############################################################################
    ########################## result processing ##################################
    ###############################################################################
    
    errors = util.summarise_test_results( testname, test_results)

        
if __name__ == '__main__' :
    main()