#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 14 10:26:30 2021

Simple driver for Single Photon
Drives the Piezo differentially. Piezo must be connected
between DAC0 and DAC1 outputs.

The APD output must go through the signal conditioning box
with the output connected between Gnd and FIO4.

@author: bcollett
"""
import numpy as np
import matplotlib.pyplot as plt
import u3
from time import sleep

# LJ Register numbers
AIN0 = 0
DAC0 = 5000
DAC1 = 5002


def scan(npoint, dwellTime=0.01, delay=0.03):
    d = u3.U3()
    d.configIO(EnableCounter0=True)
    vin = np.zeros(npoint)
    vout = np.linspace(0, 5, npoint)
    voutr = np.linspace(5, 0, npoint)
    for i in range(npoint):
        d.writeRegister(DAC0, vout[i])
        d.writeRegister(DAC1, voutr[i])
        print(vout[i], voutr[i])
        sleep(delay)
        d.getFeedback(u3.Counter(counter=0, Reset=True))
        sleep(dwellTime)
        res = d.getFeedback(u3.Counter(counter=0, Reset=True))
        vin[i] = res[0]
    d.writeRegister(DAC0, 2.5)
    d.writeRegister(DAC1, 2.5)
    d.close()
    plt.plot(vout, vin, '.')
    plt.xlabel("Voltage Applied")
    plt.ylabel("Signal")
    plt.show()
    np.savez('singlephoton_blocked',vout=vout,vin=vin)
    #return vout, vin


if __name__ == '__main__':
    scan(100)
