#!/usr/bin/env	python
#coding: utf8
# Copyright CERN, 2011
# Author: Ross Millar <ross.millar@cern.ch>
# Licence: GPL v2 or later.
# Website: http://www.ohwr.org

import sys
import rr
import random
import time
import spi
import csr
import i2c
import gn4124
import fmc_adc_test_suite
import fmc_adc
import cp210x_gpio
from pylab import *
#from ptsexcept import *
import fcntl, struct, termios, os
import time
import array







def main (default_directory='.'):
	"""
	path_fpga_loader = '../firmwares/fpga_loader';
    	path_firmware = '../main_firmware_8ch_ref.bin';
    	firmware_loader = os.path.join(default_directory, path_fpga_loader)
    	bitstream = os.path.join(default_directory, path_firmware)
    	os.system( firmware_loader + ' ' + bitstream)
    	
    	time.sleep(2);
	#main_firmware_8ch_ref.bin
	
	#Requires sinewave input
	#Does an acquisition with a threshold trigger,
	#checks the acquired data to see if it matches the result expected.
	"""
	
	
	
	hw_en =1
	sw_en =0
	chan_for_thresh = 0xB  	# check that this is masked in the fmc class
	int_thresh = 0xF000   	# 16 bit in twos comp
	slope = 1		# 1 for positive, 0 for negative
	delay = 0		# Trigger delay
	edge_detect = 1		# Trig on Rising or falling edges, depends on slope

	pre_trig_val = 100
	post_trig_val = 500

	thresh_negative = 0
	thresh_positive = 0
	error=0
	
	print int(int_thresh)
	

	# Object Declaration
	spec = rr.Gennum() # bind to the SPEC board
	fmc = fmc_adc.CFmcAdc100ks(spec)
	
	
	# Set trigger Register
	fmc.set_trig_config(hw_en,sw_en,chan_for_thresh,int_thresh,slope,delay,edge_detect)
	
	#print bin(fmc.get_trig_config())
	
	
	# Do acq on the same channel as selected channel for thresh trig
	result =  fmc.do_acq(chan_for_thresh, "chan_select" ,"n",post_trig_val,pre_trig_val,1)
	
	#print bin(fmc.get_trig_config())
	
	
	#print result
	
	
	
	
	if (int_thresh & 0x8000) >> 15 ==0x1:
		int_thresh = -0x10000 + int_thresh
		print "converted int_thresh", int_thresh
		thresh_negative = 1
	else:
		thresh_positive = 1
	
	
	if thresh_positive and (slope == 0):
		print "Thresh positive and slope positive"
		
		if result[pre_trig_val-1]>int_thresh:
			error = 1
		if result[pre_trig_val]<=int_thresh:
			error = 1
	
	elif thresh_positive and (slope == 1):
		if result[pre_trig_val-1]<int_thresh:
			error = 1
		if result[pre_trig_val]>=int_thresh:
			error = 1
	  
	
	elif thresh_negative and (slope == 0):
		if result[pre_trig_val-1]>int_thresh:
			error = 1
		if result[pre_trig_val]<=int_thresh:
			error = 1
	
	elif thresh_negative and (slope == 1):
		if result[pre_trig_val-1]<int_thresh:
			error = 1
		if result[pre_trig_val]>=int_thresh:
			error = 1
	
	if error == 1:
		print "Error with threshold trigger"
	else:
		print "Thresh Trig as expected"
		
	
	for i in range(len(result)):
		result[i] = result[i]*0.000152
	
	time_base = arange(0,len(result),1)	
	
	#Change to Voltage level (depends on 'range setting in adc')	
	int_thresh=int_thresh*0.000152		
	
		
	 # WRITE TO FILE
        file = open("adc_100k_acq.txt", 'w')
        for i in range(len(result)):        
		file.write("%5d, %5f, %5f\n" % (time_base[i], int_thresh, result[i]))
		        	
							
if __name__ == '__main__':
    main()        
    
