#!   /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 time
import os

import cp210x_gpio


"""
calibr_box: Access to calibration box via USB-UART bridge CP2103

"""

class CalibrBoxOperationError(Exception):
    def __init__(self, msg):
        self.msg = msg
    def __str__(self):
        return ("Calibration box: %s" %(self.msg))


class CCalibr_box:
	def __init__(self, device):
                self.cp210x = cp210x_gpio.CCP210x(device)
                # Select AWG as default output
#                self.cp210x.gpio_get();
                self.cp210x.gpio_set(0x0);

		self.state = 0;

        def select_output(self, out):
	
#		print("Cur %x" % self.state)
		self.state = self.state & 0x3;

		if(out == 1):
		    self.state = self.state | ( 0xc );
		elif(out == 2):
		    self.state = self.state | ( 0x8 );
		elif(out == 3):
		    self.state = self.state | ( 0x4 );
		elif(out == 4):
		    self.state = self.state | ( 0x0 );
                else:
                    raise CalibrBoxOperationError("Invalid output selection!")
		self.cp210x.gpio_set(self.state)


	def select_trigger_loopback(self, tloop):
	    if(tloop):
		self.state = self.state | 2;
	    else:
		self.state = self.state & ~2;

	    self.cp210x.gpio_set(self.state)

