clear all;close all;clc;
%%
AI_0 = analoginput('mcc');
addchannel(AI_0, 0);
AI_1 = analoginput('mcc');
addchannel(AI_1, 1);

%%
max_len = 10;
T = [1,12,20, 30, 40, 50];
V = [7.9, 6.7, 6.1, 5.6, 5.3, 5.0];
V_temp = []; T_temp = [];
while (1),
    start(AI_0);
    V_in = mean(1000*getdata(AI_0));
    if length(T_temp)<=max_len,
        T_temp = [T_temp;Calc_temp(V_in)];
        V_temp = [V_temp;V_in];
    else
        T_temp(1:(end-1)) = T_temp(2:(end));T_temp(end) = Calc_temp(V_in);
        V_temp(1:(end-1)) = V_temp(2:end); V_temp(end) = V_in;
    end
    
    hold off;plot(V,T, '-*');hold on;scatter(mean(V_temp), mean(T_temp), 'r');
    s = sprintf(['T = ', num2str(round(10*mean(T_temp))/10), '%c'], char(176));
    title(s, 'fontsize', 25, 'fontweight', 'bold');
    
    drawnow;
end
%%
%T = [1,12,20, 30, 40, 50, 60, 70];
%V = [7.9, 6.7, 6.1, 5.6, 5.3, 5.0, 4.9, 4.9];
T = [1,12,20, 30, 40, 50];
V = [7.9, 6.7, 6.1, 5.6, 5.3, 5.0];

%%
V_in = [];
while (1),
   start(AI_1); 
   V_in = [V_in, mean(1000*getdata(AI_1))];
   plot(V_in);drawnow;
end

%%

AI_0 = analoginput('mcc');
AI_1 = analoginput('mcc');
AI_2 = analoginput('mcc');
AI_3 = analoginput('mcc');

A0 = analogoutput('mcc');
A1 = analogoutput('mcc');

addchannel(AI_0, 0);
addchannel(AI_1, 1);
addchannel(AI_2, 2);
addchannel(AI_3, 3);

addchannel(A0, 0);
addchannel(A1, 1);
%%
putdata(A0, .4);start(A0);stop(A0);
%%
putdata(A0, 0);start(A0);stop(A0);
%%
putdata(A1, 0); start(A1);stop(A1);
%%
putdata(A0, 0);
putdata(A1, 0);
start(A0);pause(.1);start(A1);

%% Degauss
n_steps = 800;
T_period = 100;
V_DEGAUSS_PULSE = sin(linspace(0, pi, T_period))';
V_DEGAUSS_MAG = linspace(2, 0, 2*n_steps+2);
for i=0:n_steps,
    putdata(A0, V_DEGAUSS_MAG(2*i+1)*V_DEGAUSS_PULSE);
    putdata(A1, V_DEGAUSS_MAG(2*i+2)*V_DEGAUSS_PULSE);
    start(A0);
    %pause(T_period/1000);
    wait(A0, T_period+3);stop(A0);
    start(A1);
    wait(A1, T_period+3);stop(A1);
end
%%
T_pulse_sec = 200;
T_n = T_pulse_sec*1000;
V_DEGAUSS = linspace(1, 0, T_n).*sin(linspace(0, T_n/(2*pi*100), T_n));
%V_DEGAUSS = 2*ones(1, T_n);V_DEGAUSS((end-10):end) = 0;
figure, plot(V_DEGAUSS);

%stop(A0);putdata(A0, V_DEGAUSS');start(A0);
%% Send output to channel 0

T_duration = 1000;
V_out_mag = 10;

T = linspace(0, 4, T_duration)';

V_out = 0.105*V_out_mag*ones(size(T));

putdata(A0, V_out);
start(A0);
wait(A0, 10);

% Zero the Voltage
V_out = 0;
putdata(A0, V_out);
start(A0);

%% Send output to channel 1

T_duration = 10000;
V_out_mag = 4;

T = linspace(0, 4, T_duration)';

%V_out = 0.0958*V_out_mag*ones(size(T));
V_out = 0.0958*V_out_mag*sin(2*T).^2;

putdata(A1, V_out);
start(A1);
wait(A1, 10);

% Zero the Voltage
putdata(A1, 0);
start(A1);

%% Send output to channel 1 and 2

T_duration = 6000;
V_out_mag_0 = 3;
V_out_mag_1 = -3;

T = linspace(0, 4, T_duration)';

V_out_1 = 0.105*V_out_mag_0*ones(size(T));
V_out_2 = 0.0958*V_out_mag_1*ones(size(T));
%%
putdata(A0, V_out_1);start(A0);
%%
putdata(A1, V_out_2);start(A1);
%%
% Zero the Voltage
stop(A0);putdata(A0, 0); start(A0);stop(A0);
%%
stop(A1);putdata(A1, 0); start(A1);stop(A1);

%%
AI_0 = analoginput('mcc');
AI_1 = analoginput('mcc');
AI_2 = analoginput('mcc');
AI_3 = analoginput('mcc');

addchannel(AI_0, 0);
addchannel(AI_1, 1);
addchannel(AI_2, 2);
addchannel(AI_3, 3);

%% 
Temp = @(X) 12.98*X - 61.4;
M = [];
for i=1:200,
    start(AI_0);
    T(i) = Temp(mean(1000*getdata(AI_0)));
    plot(T);
end

%%
start(AI_0);
M_V = getdata(AI_0);
close all,
figure, plot(M_V);
disp(mean(100*M_V));
%%
start(AI_1);
M_V = getdata(AI_1);
close all,
figure, plot(M_V);
%%
start(AI_2);
M_V = getdata(AI_2);
close all,
figure, plot(M_V);
%%
start(AI_3);
M_V = getdata(AI_3);
close all,
figure, plot(M_V);
%%
V_data = sin(linspace(2*pi*500, 8000))';
%V_data = linspace(0, 4, 10000);

%%
close all;
T = linspace(0, 4, 2000)';
F = (.46/5)*10;
V_out = F*ones(size(T));
%figure, plot(T, V_out);
putdata(AO, V_out);
start(AO);
wait(AO, 10);
%{
start(AI_0);
M_V = getdata(AI_0);
close all,
figure, plot(M_V);
%}
% Zero the Voltage
V_out = 0;
putdata(AO, V_out);
start(AO);