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

% File name
fam = 'venus';
mea = '20708';
no = '01';

fid = ['multi-u_dead-time_' datestr(now,29) '_hoss-r3_' fam '_' mea '_' no];

N = 100;
u = rand(N,1);
channels = round(rand(size(u)));

% 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 stim freq
% start time at the end of the pre recording
tot = t_pre;

for i = 1:size(u,1)
    
    channel = channels(i);
    
    if channel == 0
        
        % make sure channel 1 is off
        T = [T; tot];
        C = [C; 1];
        A = [A; aoff];

        % Make the pulse sweep protocols
        up = 5*u(i); %msec
        uf = 10*u(i) + 10; %Hz
        ui = 5*u(i); % Volts (prop Amps) to achieve uniform distriubtion of irradiances
    
        % get parameter triplet
        p = [];
        p(1) = uf;
        p(2) = up*1e3;
        p(3) = ui;

        % caclulate the on/off times
        N = floor(prot_time*p(1));
        t1 = tot + cumsum((1/p(1))*ones(N,1))-(1/p(1));
        t0 = t1 + p(2)/1e6;
        tot = tot + prot_time + t_dead;

        % translate to digital event times
        t = zeros(length(t1) + length(t0),1);
        t(1:2:end) = t1;
        t(2:2:end) = t0;
        c = channel*ones(size(t));
        a = zeros(size(t));
        a(1:2:end)  = a(1:2:end) + p(3);
        a(2:2:end)  = a(2:2:end) + aoff;

        % 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);

        int = zeros(length(t1) + length(t0),1);
        int(1:2:end) = int_on;
        int(2:2:end) = int_off;

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

        T_D = [T_D; t];
        S_D = [S_D; int];
        
    else
        
        % light power
        lightpower = 5*u(i);
        
        % translate to digital event times
        t = tot;
        a = lightpower;
        c = channel;
        tot = tot + prot_time;

        % 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(round(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);

        % add to master list
        T = [T; t];
        C = [C; c];
        A = [A; a];
        
        % make sure channel 1 is off
        T = [T; tot];
        C = [C; 1];
        A = [A; aoff];
        tot = tot + t_dead;

        T_D = [T_D; t];
        S_D = [S_D; int_on];
        
        
    end
    
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);
