#!/usr/bin/python
#===============================================================================
# CERN (BE-CO-HT)
# Set the timestamp counter value
#===============================================================================
# author: Theodor Stana (t.stana@cern.ch)
#
# date of creation:
#
# version: 1.0
#
# description:
#   This script writes the TVLR and TVHR registers to set the new internal
#   timestamp counter value.
#
#===============================================================================
# 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
#===============================================================================
# last changes:
#===============================================================================
#  TODO: - 
#===============================================================================
import sys
sys.path.append("../../ei2c")
import time
from ei2c import *
import ei2cdefine


if __name__ == "__main__":

    # Get the IP, user and password for the ELMA crate from either user input,
    # or ei2cdefine.py
    if (ei2cdefine.ENABLED):
        ip   = ei2cdefine.HNAME
        user = ei2cdefine.USER
        pwd  = ei2cdefine.PWD
    else:
        ip   = raw_input("ELMA crate IP or hostname : ")
        user = raw_input("ELMA crate user name      : ")
        pwd  = raw_input("ELMA crate password       : ")
    testelma = EI2C(ip, user, pwd)
    testelma.open()

    # Ask for slot number
    while 1:
        try:
            slot = raw_input("Slot no.: ")
            slot = int(slot)
            break
        except TypeError as e:
            print("Please input a decimal slot number.")
        except SlotError as e:
            print(e.strerror)
        except KeyboardInterrupt:
            sys.exit();
        except:
            print("Unexpected error: ", sys.exc_info()[0])

    # Print which board is detected in the selected slot
    bid = testelma.get_bid(slot)

    # Set time stamp, close and exit
    s = int(raw_input("Number of seconds: "))
    testelma.write(slot, 0x24, s & 0xffffffff)
    if s > 2**32-1:
        testelma.write(slot, 0x28, s >> 32)

    tail = testelma.read(slot, 0x24)
    taih = testelma.read(slot, 0x28)

    print("TAI counter value: %d" % ((taih << 32) | tail))

    testelma.close()

