% Jon Newman
% Ming-fai Fong
% May 20 2011
% Periodic pulse protocol for opto-stim

% Periodic stim parameters
fid = 'orion363-5Hz-0V-periodic';
exp_length = 15; % min
reqV = 0; % volt, from 0, 1, 2, 3, 4, 5 volts
freq = 5; % Hz
pw = 500; % us

% Make the pulse sweep protocols
exp_length = exp_length * 60; % sec
reqV = round(reqV);
pws = pw/1e6;

% caclulate the on/off times
N = exp_length*freq;
t1 = cumsum((1/freq)*ones(N,1));
t0 = t1 + pws;

% translate to digital event times
t = sort([t1;t0]);

% create the integer numbers used to encode the stimulus
% on
b_trig = '1'; % LED Trigger encoded in bit 0 (this turns the LED on when its high)
b_type = '0'; % Type encoded in bit 1
b_freq = dec2bin(freq,7); % Freq encoded in bits 2-8
b_width = dec2bin(pw,14); % Width encoded in bits 9-22
b_contV = dec2bin(reqV,3);
b_str = [b_contV b_width b_freq b_type b_trig];
int_on = bin2dec(b_str);

% off
b_trig = '0';
b_type = '0';
b_freq = dec2bin(freq,7);
b_width = dec2bin(pw,14);
b_contV = dec2bin(reqV,3);
b_str = [b_contV b_width b_freq b_type b_trig];
int_off = bin2dec(b_str);

% Make state vector
p = zeros(length(t1) + length(t0),1);
p(1:2:end) = int_on;
p(2:2:end) = int_off;


% Make the analog porition
ta = [t(1);t(end)];
ca = ones(size(ta));
va = reqV*ones(size(ta));

%finish
figure()
hold on
stairs(t,p)
makedigfile(fid,p,t);
makeauxfile(fid,ta,ca,va);



