import time
from struct import *
import ctypes
import array
import numpy as np
import matplotlib.pyplot as plt

from ADUCv2p1 import *


chip1 = ADUCv2p1(0x50,True)

chip1.set_channel(3) #ADC

chip1.set_auto_set_pga(1)
chip1.set_Vout(0,2500)  #DAC for offset, 2000 at channel 1
#chip1.set_pga_state(0x08)
#chip1.set_pga_state(0x18) #G=2
#chip1.set_pga_state(0x28) #G=4
#chip1.set_pga_state(0x38) #G=8
#chip1.set_pga_state(0x48) #G=16
#chip1.set_pga_state(0x58) #G=32
#chip1.set_pga_state(0x68) #G=64
#chip1.set_pga_state(0x78)
chip1.set_Vlearn(3250)
chip1.set_start(25)
chip1.set_stop(85)
chip1.set_start_gnd(110)
chip1.set_stop_gnd(140)
chip1.set_wf_len(140)
chip1.set_enab_gnd(1)
chip1.set_N(1)
chip1.set_Gain(11)
chip1.set_remote_trigg(0)
chip1.set_step_max(200)

time.sleep(2)
#chip1.set_auto_set_pga(0)
print(chip1.get_auto_set_pga())
print(chip1.get_Vout(0))
print(chip1.get_pga_state())
print('---')
time.sleep(2)

#print chip1.get_Vset()*0.61
#print chip1.get_trigg_cnt()


#chip1.set_Vout(0,1500)
npt = chip1.get_wf_len()

start = chip1.get_start()
stop = chip1.get_stop()
start_gnd = chip1.get_start_gnd()
stop_gnd = chip1.get_stop_gnd()


#print npt
    #print "start"
data = chip1.read_data()
    #print "stop"
#plt.plot(data)
#plt.show()

t = np.linspace(0,npt,npt)

fig, ax = plt.subplots(1, 1)
    #ax.set_aspect('equal')
    #ax.set_xlim(0, 255)
    #ax.set_ylim(0, 255)
    #ax.hold(True)
    #rw = randomwalk()
    #x, y = rw.next()

doblit = True
plt.show(False)
plt.draw()

if doblit:
    # cache the background
    background = fig.canvas.copy_from_bbox(ax.bbox)

gnd = sum(data[start_gnd:stop_gnd])/(stop_gnd-start_gnd)

points = ax.plot(t,data.astype(int),".")[0]
#points = ax.hist(data.astype(int))[0]
##ax.axvline(start)
##ax.axvline(stop)
##
##ax.axvline(start_gnd,color='red')
##ax.axvline(stop_gnd,color='red')

tic = time.time()
###
niter = 60000
###

std = 0
ave = np.zeros(niter,dtype = np.float)

#fig.canvas.draw()
total_data = np.zeros_like(data,dtype = np.float)
print(len(total_data))
print(chip1.get_Gain())

measurements = []

for ii in range(niter):
    #chip1.set_remote_trigg(1)
        # update the xy data
    data = chip1.read_data()
    #print total_data
        #total_data += data
        #points.set_data(t,total_data/(ii+1.0))
    gnd = sum(data[start_gnd:stop_gnd])/(stop_gnd-start_gnd)
    signal = sum(data[start:stop])/(stop-start)
    measurements.append(signal-gnd)
    
    print(chip1.get_Vout(1), chip1.get_Vout(2), signal, gnd, signal-gnd, np.std(data[start:stop]), np.std(data[start_gnd:stop_gnd]))
    print(np.std(measurements), chip1.get_Vout(0), chip1.get_pga_state())
    points.set_data(t,data.astype(int))
#    points.set_data(data.astype(int))


    #ave[ii]=np.mean(data[50::])
    
    av = np.mean(data)
    #for k in range(0,npt):
        #if abs(data[k]-av)>0.02*av:
            #data[k]=av

    points.set_data(t,data.astype(int))
    std = np.std(data)
    #print(std)
    #print(max(data)-min(data))
    #print(av)
    
    if doblit:
            # restore background
        fig.canvas.restore_region(background)

            # redraw just the points
        ax.draw_artist(points)

            # fill in the axes rectangle
        fig.canvas.blit(ax.bbox)

    else:
            # redraw everything
        fig.canvas.draw()
        
    time.sleep(10)



