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

# Copyright CERN, 2011
# Author: Matthieu Cattin <matthieu.cattin@cern.ch>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org

import sys
import rr
import time
import os
import fmc_adc_test_suite
from ptsexcept import *

import csr
import fcntl, struct, termios, os
import time
import array

"""
test01: Validates TestBox is working
"""


class cp210x_gpio:
	def __init__(self, usb_number):
		self.fd = open('/dev/ttyUSB' + str(usb_number), 'wb')

	def gpio_set(self, mask):
		fcntl.ioctl(self.fd.fileno(), 0x8001, mask)

	def gpio_get(self):
		f = array.array('I', [0])
		ret = fcntl.ioctl(self.fd.fileno(), 0x8000, f, 1)
		return f[0]


def main (default_directory='.'):
	gpio_wr = 0xF
	gpio = cp210x_gpio(0)
	gpio.gpio_set(gpio_wr)
	gpio_rd = gpio.gpio_get()
	
	print "Value written to test box : %X"%gpio_wr
	print "Value read back from testbox : %X"%gpio_rd
	
	if gpio_rd != gpio_wr:
		print " Problem with test box"
		print " Please check test box is attached properly"
		print " Check power LED on test box"
		raise PtsCritical("Problem with test box : Not all tests can be fully completed")
	

if __name__ == '__main__' :
    main()
