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

import sys
import rr
import random
import time
import spi
import csr
import i2c
import gn4124
import fmc_adc
from   pylab import *

if __name__ == '__main__':

    """
    Channel order test:
    Forces ADC channel values digitally to their channel number, i.e. 1 - 16
    Does acquisitions and checks all the channel orders are correct
    """


    GN4124_CSR = 0x0 

    test_data =0  
    error_position =[]

    # Objects declaration
    spec = rr.Gennum() # bind to the SPEC board
    gn4124 = gn4124.CGN4124(spec, GN4124_CSR)
    fmc = fmc_adc.CFmcAdc100ks(spec)
    
    # Set up memory pages
    pages = gn4124.get_physical_addr()
 
    chan = 1
    post_trig = 9
    pre_trig = 10
    fmc.channel_order_en()
    
    iteration_number = int(raw_input(" Number of iterations : "))
    
    for iteration in range(iteration_number):
    	print "Iteration Number : %s"%iteration
    	channel_data = fmc.do_acq(1, "camera_link" ,"N", post_trig ,pre_trig,0)
        
    	for i in range(len(channel_data)):
    		for j in range(len(channel_data[0])): 
    			if channel_data[i][j]!= i+1:
    				print "Error : Expected %d... %d found"%( i+1, channel_data[i][j]) 
	    			raw_input(" Error Found : Press enter to continue ")
    
    	
    fmc.channel_order_dis()
    print '\nBye bye ...'
    sys.exit()
