% Jon Newman
% Multi-parameter activity assay for cultures transfected with
% optical exciters and silencers

% File name
fam = 'smiley';
mea = '22568';
no = '01';

fid = ['sine-wave_' datestr(now,29) '_hoss-r3_' fam '_' mea '_' no];

N = 50;
u = rand(N,1);
channel = 0;
dt = 1; % DAC sample res. (ms)

% Experiment parameters
t_pre = 600; % seconds
t_dead = 60; % seconds
prot_time = 60; % seconds
aoff = -0.5;

%% Make the protocol

% Storage vectors
T = [0;0];
A = [aoff;aoff];
C = [0;1];
T_D = [0;0];
S_D = [0;0];

% digital bits - all we need to encode is u because everything is
% dependent on that
b_trig = '0'; % one trigger bit
b_chan = '00'; % two bits for the A0 being used
b_u = '00000000000000000000000000000'; % 29 bits for encoding `u'
% start time at the end of the pre recording
tot = t_pre;

for i = 1:size(u,1)
    

        % make sure channel 1 is off
        T = [T; tot];
        C = [C; 1];
        A = [A; aoff];

        % Make the pulse sweep protocols
        uf = 10*u(i) + 10; %Hz
        ui = 5*u(i); % Volts (prop Amps) to achieve uniform distriubtion of irradiances

        % Create a sine wave
        t = (0:(dt/1000):prot_time)';
        a = ui * 0.5 * (sin(2*pi*uf*t - pi/2) + 1);
        c = channel*ones(size(a));
        a(end) = aoff;
        t = tot + t;
        tot = tot + prot_time + t_dead;

        % create the integer numbers used to encode the stimulus on
        b_chan= dec2bin(channel,length(b_chan)); % Freq encoded in bits 2-8
        b_u = dec2bin(10e6*u(i),length(b_u)); % 100e6 * u encoded in bits 2-31

        % Acutal encoding strings
        b_str = [b_u b_chan '1'];
        int_on = bin2dec(b_str);
        b_str = '0';
        int_off = bin2dec(b_str);

        % add to master list
        T = [T; t];
        C = [C; c];
        A = [A; a];

        T_D = [T_D; t(1); t(1) + 0.001];
        S_D = [S_D; int_on; int_off];

end

T = [T;tot;tot];
A = [A;aoff;aoff];
C = [C;1;0];

% Write the .oldig file
close all
stairs(T,A)
makeauxfile(fid,T,C,A);
makedigfile(fid,T_D,S_D);
