#!/usr/bin/python
#===============================================================================
# CERN (BE-CO-HT)
# Set converter board status register
#===============================================================================
# author: Theodor Stana (t.stana@cern.ch)
#
# date of creation: 2014-05-02
#
# version: 1.0
#
# description:
#   Set the converter board status register, typically for clearing set error
#   bits.
#
# dependencies:
#   ../ei2c/ei2c.py
#   ../ei2c/ei2cdefine.py
#
#===============================================================================
# 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:
#    2014-05-02    Theodor Stana     File created
#===============================================================================
#  TODO: -
#===============================================================================


import sys
sys.path.append("../ei2c")
from ei2c import *
import ei2cdefine


if __name__ == "__main__":

    # Get the IP, user and password for the ELMA crate from ei2cdefine.py
    ip   = ei2cdefine.HNAME
    user = ei2cdefine.USER
    pwd  = ei2cdefine.PWD
    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])

    # Get status register value, set and re-print
    v = testelma.read(slot, 0x4)
    print("SR (pre-set)  : 0x%08x" % v)
    print("==========================")
    v = raw_input("Value to set  : 0x")
    v = int(v, 16)
    testelma.write(slot, 0x4, v)
    print("==========================")
    v = testelma.read(slot, 0x4)
    print("SR (post-set) : 0x%08x" % v)

    testelma.close()
