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

# Copyright CERN, 2011 - 2013 (INCAA Computers BV)
# Author: Samuel Iglesias Gonsalvez <siglesia@cern.ch>
# Author: INCAA Computers BV (info@incaacomputers.com)
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org
# Website: http://www.incaacomputers.com
# Version: 2.0 (Last modifications: 13/06/2013)
#
# Description
#  This test checks the EEPROM of the GENNUM chip.

import sys
import rr
import time
import os

from eeprom import EEPROM_GENNUM

from ptsexcept import *

"""
test00: checks the EEPROM of the GENNUM chip.
"""

##-------------------------------------------------------------------------------------------------
##                                             main                                              --
##-------------------------------------------------------------------------------------------------

def main (default_directory='.'):
    # Load board library and open the corresponding device
    print "Loading hardware access library and opening devices\n"
    spec = rr.Gennum();
    eeprom = EEPROM_GENNUM(spec);

    GN4124_DIFF_FILE = default_directory + "/gn4124_diff.dat"
    EEPROM_TEST_FILE = default_directory + "/eeprom_test_A.dat"
    TMP_FILE = "/tmp_eeprom.dat"

    print "Test00 Start"
    init_test_time = time.time()
    ret_error = None

    eeprom.file_dump_to_gennum(GN4124_DIFF_FILE);

    eeprom.eeprom_dump_to_file(TMP_FILE); 

    eeprom.file_dump_to_eeprom(EEPROM_TEST_FILE);
    eeprom.eeprom_dump_to_screen();
    ret_error = eeprom.compare_eeprom_with_file(EEPROM_TEST_FILE);

    eeprom.file_dump_to_eeprom(TMP_FILE);
    eeprom.eeprom_dump_to_screen();
    ret_error = eeprom.compare_eeprom_with_file(TMP_FILE);

    end_test_time = time.time()
    print "End of Test00"
    print "RESULT: [{}]".format("FAIL" if ret_error else "OK")
    print 'Test00 elapsed time: {:.2f} seconds'.format(end_test_time-init_test_time)
    if ret_error:
        raise PtsError(ret_error)

if __name__ == '__main__' :
    main();
