function SoftStimulator(varargin)
%Physiological stimulator and oscilloscope

%global fig

% By default make the GUI
%from the command line with no parameters

if (nargin == 0)
    fcn = 'makeGUI';
elseif nargin == 3 & strcmp(varargin{3},'stimaction')  %stim event
    fcn='stimaction';
    fig=findobj('tag','BRLstim');
else
    fcn=varargin{1};
    fig=gcbf;
end

%This is the main decision point of the function.
%The switch statement is executed once-per-fuction call
switch fcn

    %This code is executed ONCE when the function enters with
    %no arguments
    case 'makeGUI'

        %delete(daqfind);

        %store the name of this executing file
        data.myname = mfilename;

        %feature('javafigures',0);
        %=============================================
        %==Stimulator controls
        %============================================

        % ===Create main figure==========================
        fig = figure('Position',centerfig(800,200),...
            'Resize','off',...
            'NumberTitle','off',...
            'Name','Soft Stimulator',...
            'Interruptible','off',...
            'tag','BRLstim',...
            'doublebuffer','on',...
            'HandleVisibility','on',...
            'Color',[.7 .7 .7]);

        %===Header text===================================
        uicontrol(gcf,'Style','text', ...
            'String','Neurobiology Soft Stimulator',...
            'fontsize',14, ...
            'HorizontalAlignment','Center',...
            'Position',[5,170,300,25],...
            'BackgroundColor',[0.8 .8 0.8]);

        %==frame for stim=================================
%         uicontrol('style','frame',...
%             'Position',[5 60 795 90],...
%             'backgroundcolor',[.7 .7 .7]);

        % ===Create axes=================================
        data.sax = axes('Units','pixels',...
            'Position',[10 10 780 50],...
            'Box','on');

        % ===The quit button===============================
        uicontrol('Style','pushbutton',...
            'Position',[750 170 45 20],...
            'String','Quit',...
            'Interruptible','off',...
            'BusyAction','cancel',...
            'tag','stim',...
            'Callback',[data.myname,' quit']);

        %==Mode Chooser=========================
        x=10 ; y=100 ; w=140 ; h=20 ;
        data.mode='Single';
        data.mode1=uicontrol(gcf,'Style','radiobutton', ...
            'String','Single',...
            'Position',[x,y,w,h],...
            'BackgroundColor',[0.8,0.8,0.8], ...
            'tag','modegrp',...
            'value',1,...
            'CallBack',[data.myname,' mode'] );
        data.mode2=uicontrol(gcf,'Style','radiobutton', ...
            'String','Continuous',...
            'Position',[x,y-h,w,h],...
            'BackgroundColor',[0.8,0.8,0.8], ...
            'tag','modegrp',...
            'CallBack',[data.myname,' mode'] );
        data.mode3=uicontrol(gcf,'Style','radiobutton', ...
            'String','External',...
            'Position',[x,y-2*h,w,h],...
            'BackgroundColor',[0.8,0.8,0.8], ...
            'tag','modegrp',...
            'enable','off',...
            'CallBack',[data.myname,' mode'] );
        uicontrol(gcf,'style','text',...
            'Position',[x,y+h,w,h],...
            'BackgroundColor',[0.8,0.8,0.8],...
            'string','Mode');
        data.start=uicontrol(gcf,'Style','Pushbutton', ...
            'String','Start',...
            'Position',[x+80,y-20,60,h],...
            'BackgroundColor',[0.8,0.8,0.8], ...
            'enable','off',...
            'CallBack',[data.myname,' start'] );
        data.trigger=uicontrol(gcf,'Style','Pushbutton', ...
            'String','Pulse',...
            'Position',[x+80,y,60,h],...
            'BackgroundColor',[0.8,0.8,0.8], ...
            'enable','on',...
            'CallBack',[data.myname,' trigger'] );


        %==repeat time slider===========================
        x=400 ; y=120 ; w=140 ; h=20 ;

        data.repeattime=10;
        data.repeattime1 = uicontrol(gcf,'Style','text', ...
            'String','Repeat interval',...
            'Position',[x,y,100,20],...
            'BackgroundColor',[0.8,0.8,0.8]);
        data.repeattime3 = uicontrol(gcf,...
            'Style','slider',...
            'Min' ,-1,'Max',1, ...
            'Position',[x+100,y,80,20], ...
            'Value', 0,...
            'SliderStep',[0.0084 0.2], ...
            'BackgroundColor',[0.8,0.8,0.8],...
            'tag','stim',...
            'CallBack', [data.myname,' setrepeattime']);
        data.repeattime2 = uicontrol(gcf,'Style','edit',...
            'String',num2str(data.repeattime),...
            'Position',[x+115,y,50,20],...
            'BackgroundColor',[0.8,0.8,0.8],...
            'tag','stim',...
            'CallBack', [data.myname,' setrepeattime']);
        data.repeattime4=uicontrol(gcf,'Style','text',...
            'String',num2str(1/data.repeattime,'f=%6.3f'),...
            'Position',[x+180,y,50,20],...
            'BackgroundColor',[0.8,0.8,0.8]...
            );


        %==Waveform Chooser=========================
        x=170 ; y=100 ; w=80 ; h=20 ;
        data.wave='One pulse';
        data.wave1=uicontrol(gcf,'Style','radiobutton', ...
            'String','One pulse',...
            'Position',[x,y,w,h],...
            'BackgroundColor',[0.8,0.8,0.8], ...
            'tag','wavegrp',...
            'value',1,...
            'CallBack',[data.myname,' wave'] );
        data.wave2=uicontrol(gcf,'Style','radiobutton', ...
            'String','Two pulse',...
            'Position',[x,y-h,w,h],...
            'BackgroundColor',[0.8,0.8,0.8], ...
            'tag','wavegrp',...
            'CallBack',[data.myname,' wave'] );
        data.wave3=uicontrol(gcf,'Style','radiobutton', ...
            'String','Tetanic',...
            'Position',[x,y-2*h,w,h],...
            'BackgroundColor',[0.8,0.8,0.8], ...
            'tag','wavegrp',...
            'enable','on',...
            'CallBack',[data.myname,' wave'] );
        uicontrol(gcf,'style','text',...
            'Position',[x,y+h,w,h],...
            'BackgroundColor',[0.8,0.8,0.8],...
            'string','Waveform');

        %==Pulse width slider=================
        x=240 ; y=120 ; w=60 ; h=20 ;

        data.width=0.001;
        data.width1 = uicontrol(gcf,'Style','text', ...
            'String','Pulse dur',...
            'Position',[x,y,w,20],...
            'BackgroundColor',[0.8,0.8,0.8]);
        data.width3 = uicontrol(gcf,...
            'Style','slider',...
            'Min' ,-1,'Max',1, ...
            'Position',[x+w,y,80,20], ...
            'Value', 0,...
            'SliderStep',[0.1 0.2], ...
            'BackgroundColor',[1 .6 .6],...
            'tag','stim',...
            'CallBack', [data.myname,' setwidth']);
        data.width2 = uicontrol(gcf,'Style','edit',...
            'String',num2str(data.width),...
            'Position',[x+w+15,y,50,20],...
            'BackgroundColor',[1 .6 .6],...
            'tag','stim',...
            'CallBack', [data.myname,' setwidth']);

        %==Tetanic burst duration slider=================
        x=400 ; y=95 ; w=140 ; h=20 ;

        data.burst=0.01;
        data.burst1 = uicontrol(gcf,'Style','text', ...
            'String','Tetanic duration',...
            'Position',[x,y,100,20],...
            'BackgroundColor',[0.8,0.8,0.8]);
        data.burst3 = uicontrol(gcf,...
            'Style','slider',...
            'Min' ,-1,'Max',1, ...
            'Position',[x+100,y,80,20], ...
            'Value', 0,...
            'SliderStep',[0.1 0.2], ...
            'BackgroundColor',[.6 .6 .2],...
            'tag','stim',...
            'CallBack', [data.myname,' setburst']);
        data.burst2 = uicontrol(gcf,'Style','edit',...
            'String',num2str(data.burst),...
            'Position',[x+115,y,50,20],...
            'BackgroundColor',[.9 .9 .4],...
            'tag','stim',...
            'CallBack', [data.myname,' setburst']);

        %==Tetanic burst interval slider========
        x=400 ; y=70 ; w=140 ; h=20 ;

        data.burstrepeat=0.002;
        data.burstrepeat1 = uicontrol(gcf,'Style','text', ...
            'String','Tetanic interval',...
            'Position',[x,y,100,20],...
            'BackgroundColor',[0.8,0.8,0.8]);
        data.burstrepeat3 = uicontrol(gcf,...
            'Style','slider',...
            'Min' ,-1,'Max',1, ...
            'Position',[x+100,y,80,20], ...
            'Value', 0,...
            'SliderStep',[0.0105 0.25], ...
            'BackgroundColor',[.8 .6 .2],...
            'tag','stim',...
            'CallBack', [data.myname,' setburstrepeat']);
        data.burstrepeat2 = uicontrol(gcf,'Style','edit',...
            'String',num2str(data.burstrepeat),...
            'Position',[x+115,y,50,20],...
            'BackgroundColor',[1 .8 .4],...
            'tag','stim',...
            'CallBack', [data.myname,' setburstrepeat']);
        data.burstrepeat4=uicontrol(gcf,'Style','text',...
            'String',num2str(1/data.burstrepeat,'f=%5.2f'),...
            'Position',[x+180,y,50,20],...
            'BackgroundColor',[0.8,0.8,0.8]...
            );

        %==Delay 1 slider=================
        x=250 ; y=100 ; w=50 ; h=20 ;

        data.delay1=0.002;
        data.delay11 = uicontrol(gcf,'Style','text', ...
            'String','Delay 1',...
            'Position',[x,y,w,20],...
            'BackgroundColor',[0.8,0.8,0.8]);
        data.delay13 = uicontrol(gcf,...
            'Style','slider',...
            'Min' ,-1,'Max',1, ...
            'Position',[x+w,y,80,20], ...
            'Value', 0,...
            'SliderStep',[0.1 0.2], ...
            'BackgroundColor',[0.6,1,0.6],...
            'tag','stim',...
            'CallBack', [data.myname,' setdelay1']);
        data.delay12 = uicontrol(gcf,'Style','edit',...
            'String',num2str(data.delay1),...
            'Position',[x+w+15,y,50,20],...
            'BackgroundColor',[0.6,1,0.6],...
            'tag','stim',...
            'CallBack', [data.myname,' setdelay1']);

        %==Delay 2 slider=================
        x=250 ; y=80 ; w=50 ; h=20 ;

        data.delay2=0.003;
        data.delay21 = uicontrol(gcf,'Style','text', ...
            'String','Delay 2',...
            'Position',[x,y,w,20],...
            'BackgroundColor',[0.8,0.8,0.8]);
        data.delay23 = uicontrol(gcf,...
            'Style','slider',...
            'Min' ,-1,'Max',1, ...
            'Position',[x+w,y,80,20], ...
            'Value', 0,...
            'SliderStep',[0.1 0.2], ...
            'BackgroundColor',[.8 .8 1],...
            'tag','stim',...
            'CallBack', [data.myname,' setdelay2']);
        data.delay22 = uicontrol(gcf,'Style','edit',...
            'String',num2str(data.delay2),...
            'Position',[x+w+15,y,50,20],...
            'BackgroundColor',[.8 .8 1],...
            'tag','stim',...
            'CallBack', [data.myname,' setdelay2']);

        %==Delay 3 slider=================
        x=250 ; y=60 ; w=50 ; h=20 ;

        data.delay3=0.003;
        data.delay31 = uicontrol(gcf,'Style','text', ...
            'String','Delay 3',...
            'Position',[x,y,w,20],...
            'BackgroundColor',[0.8,0.8,0.8]);
        data.delay33 = uicontrol(gcf,...
            'Style','slider',...
            'Min' ,-1,'Max',1, ...
            'Position',[x+w,y,80,20], ...
            'Value', 0,...
            'SliderStep',[0.1 0.2], ...
            'BackgroundColor',[.3 .7 .7],...
            'tag','stim',...
            'CallBack', [data.myname,' setdelay3']);
        data.delay32 = uicontrol(gcf,'Style','edit',...
            'String',num2str(data.delay3),...
            'Position',[x+w+15,y,50,20],...
            'BackgroundColor',[.5 1 1],...
            'tag','stim',...
            'CallBack', [data.myname,' setdelay3']);

        %=== set up the analog output ========================================
        daqregister('nidaq');
        [adaptorStrArray, aiParams, CHnames] = daqgate('privateGetAdaptor', 'analogoutput');
        [sel,ok] = listdlg('PromptString','Select an analog output:',...
            'SelectionMode','single',...
            'ListSize', [200 80], ...
            'Name', 'Choose Adaptor', ...
            'ListString',adaptorStrArray);
        if ok %the user actually chose something
            %parse the adaptor info
            adaptorStr = adaptorStrArray{sel};
            index = find(adaptorStr == ' ');
            adaptor = adaptorStr(1:index(1)-1);
            id = adaptorStr(index(1)+1:end);

        else %no choice made
            errordlg('You must choose a adaptor')
            close gcf
            return
        end
        %use the adaptor info to build the output object
        data.adaptor = adaptor ; %'nidaq';
        data.id = id ; %'Dev1';
        data.ao = analogoutput(data.adaptor, data.id);
        %data.Fs = get(data.ao,'SampleRate');
        if ~strcmp(lower(adaptor), 'winsound') %nidaq
            data.cho = addchannel(data.ao, 0:1);
            data.Fs = 10000; %assumes a fairly fast DAC
            set(data.ao, 'SampleRate', data.Fs);
        else %winsound
            data.cho = addchannel(data.ao, 1:2);
            data.Fs = 44100;
            set(data.ao, 'SampleRate', data.Fs);
        end
        
        %== Sample rate edit field ========
        x=700 ; y=70 ; w=70 ; h=20 ;
        setFs = uicontrol(gcf,'Style','edit', ...
            'String',num2str(data.Fs),...
            'Position',[x,y,w,h],...
            'callback', [data.myname,' setrate'], ...
            'BackgroundColor',[0.8,0.8,0.8]);
        y=y+h;
        uicontrol(gcf,'Style','text', ...
            'String','Sample Rate',...
            'Position',[x,y,w,h],...
            'BackgroundColor',[0.8,0.8,0.8]);

        %put all the variables in a safe place (the figure's data area)
        set(fig,'UserData',data);

        buildwave; %and make the output waveforms

        %======================================================
        %UIcontrol callbacks for the Stimulator
        %======================================================
    case 'start'
        data=get(gcf,'UserData');

        %determine if starting or stopping
        if strcmp(get(gco,'string'),'Start')
            set(gco,'string','Stop')
            set(data.mode1,'enable','off')
            set(findobj('tag','wavegrp'),'enable','off')
            set(findobj('tag','stim'),'enable','off');
            stop(data.ao)
            %set(data.ao,'bufferingconfig',[50000,2])
            putdata(data.ao,[data.trigwave data.waveform]);
            set(data.ao,'repeatoutput',inf);
            set(data.ao,'triggertype','manual');
            start(data.ao);
            trigger(data.ao);
        elseif strcmp(get(gco,'string'),'Stop')
            set(gco,'string','Start')
            set(data.mode1,'enable','on')
            set(findobj('tag','wavegrp'),'enable','on')
            set(findobj('tag','stim'),'enable','on')
            stop(data.ao);
            set(data.ao,'repeatoutput',0)
        end
        set(gcf,'UserData',data);

    case 'stimaction'
        %data=get(gcf,'UserData');
        %stop(data.ao)
        %putdata(data.ao,[data.trigwave data.waveform]);
        %start(data.ao);
        %'stim repeat'
        %trigger(data.ao);
        %set(gcf,'UserData',data);

    case 'quit'
        data=get(gcf,'UserData');
        close(gcf);
        try
            stop(data.ao)
        end
        delete(data.ao);
        clear data
        %end

    case 'trigger'
        data=get(gcf,'UserData');
        stop(data.ao);
        %set(data.ao,'bufferingconfig',[50000,2])
        set(data.ao,'triggertype','manual');
        putdata(data.ao,[data.trigwave data.waveform]);
        start(data.ao);
        trigger(data.ao);

    case 'mode'
        data=get(gcf,'UserData');
        objhandle=findobj('tag','modegrp');
        clrindex=find(objhandle ~= gco);
        set(objhandle(clrindex),'value',0);
        set(gco,'value',1);
        data.mode=get(gco,'string');
        if strcmp(data.mode,'Continuous')
            set(data.start,'enable','on')
            set(data.trigger,'enable','off')
            set(data.wave3,'enable','off')
        elseif strcmp(data.mode,'Single')
            set(data.start,'enable','off')
            set(data.start,'string','Start');
            set(data.trigger,'enable','on')
            set(data.wave3,'enable','on')
        end
        set(gcf,'UserData',data);

    case 'setrepeattime'
        data=get(gcf,'UserData');

        if strcmp(get(gco,'style'),'slider')
            if get(gco,'value')>0
                data.repeattime=...
                    min(10,data.repeattime*2);
            else
                data.repeattime=...
                    max(.0001,data.repeattime/2);
            end
            set(gco,'value',0);
            set(data.repeattime2,...
                'string',num2str(data.repeattime));
            set(data.repeattime4,...
                'string',num2str(1/data.repeattime,'f=%6.3f'));
        elseif strcmp(get(gco,'style'),'edit')
            data.repeattime=str2num(get(gco,'string'));
            set(data.repeattime4,...
                'string',num2str(1/data.repeattime,'f=%6.3f'));
        end

        set(gcf,'UserData',data);
        buildwave

    case 'setwidth'
        data=get(gcf,'UserData');

        if strcmp(get(gco,'style'),'slider')
            if get(gco,'value')>0
                data.width=...
                    min(10,data.width*2);
            else
                data.width=...
                    max(.0001,data.width/2);
            end
            set(gco,'value',0);
            set(data.width2,...
                'string',num2str(data.width));
        elseif strcmp(get(gco,'style'),'edit')
            data.width=str2num(get(gco,'string'));
        end

        set(gcf,'UserData',data);
        buildwave;

    case 'setburst'
        data=get(gcf,'UserData');

        if strcmp(get(gco,'style'),'slider')
            if get(gco,'value')>0
                data.burst=...
                    min(10,data.burst*2);
            else
                data.burst=...
                    max(.0001,data.burst/2);
            end
            set(gco,'value',0);
            set(data.burst2,...
                'string',num2str(data.burst));
        elseif strcmp(get(gco,'style'),'edit')
            data.burst=str2num(get(gco,'string'));
        end

        set(gcf,'UserData',data);
        buildwave;

    case 'setburstrepeat'
        data=get(gcf,'UserData');

        if strcmp(get(gco,'style'),'slider')
            if get(gco,'value')>0
                data.burstrepeat=...
                    min(10,data.burstrepeat*2);
            else
                data.burstrepeat=...
                    max(.0001,data.burstrepeat/2);
            end
            set(gco,'value',0);
            set(data.burstrepeat2,...
                'string',num2str(data.burstrepeat));
            set(data.burstrepeat4,...
                'string',num2str(1/data.burstrepeat,'f=%5.2f'));
        elseif strcmp(get(gco,'style'),'edit')
            data.burstrepeat=str2num(get(gco,'string'));
            set(data.burstrepeat4,...
                'string',num2str(1/data.burstrepeat,'f=%5.2f'));
        end

        set(gcf,'UserData',data);
        buildwave;


    case 'setdelay1'
        data=get(gcf,'UserData');

        if strcmp(get(gco,'style'),'slider')
            if get(gco,'value')>0
                data.delay1=...
                    min(10,data.delay1*2);
            else
                data.delay1=...
                    max(.0001,data.delay1/2);
            end
            set(gco,'value',0);
            set(data.delay12,...
                'string',num2str(data.delay1));
        elseif strcmp(get(gco,'style'),'edit')
            data.delay1=str2num(get(gco,'string'));
        end
        set(gcf,'UserData',data);
        buildwave

    case 'setdelay2'
        data=get(gcf,'UserData');

        if strcmp(get(gco,'style'),'slider')
            if get(gco,'value')>0
                data.delay2=...
                    min(10,data.delay2*2);
            else
                data.delay2=...
                    max(.0001,data.delay2/2);
            end
            set(gco,'value',0);
            set(data.delay22,...
                'string',num2str(data.delay2));
        elseif strcmp(get(gco,'style'),'edit')
            data.delay2=str2num(get(gco,'string'));
        end
        set(gcf,'UserData',data);
        buildwave

    case 'setdelay3'
        data=get(gcf,'UserData');

        if strcmp(get(gco,'style'),'slider')
            if get(gco,'value')>0
                data.delay3=...
                    min(10,data.delay3*2);
            else
                data.delay3=...
                    max(.0001,data.delay3/2);
            end
            set(gco,'value',0);
            set(data.delay32,...
                'string',num2str(data.delay3));
        elseif strcmp(get(gco,'style'),'edit')
            data.delay3=str2num(get(gco,'string'));
        end
        set(gcf,'UserData',data);
        buildwave

    case 'setrate'
        data=get(fig,'UserData');
        data.Fs=str2num(get(gco,'string'));
        if strcmp(lower(data.adaptor), 'winsound')
            %8.000 kHz, 11.025 kHz, 22.050 kHz, and 44.100 kHz
            freqs = [8000, 11025, 22050, 44100];
            [mf,p]=min(abs(data.Fs-freqs));
            data.Fs = freqs(p);
            set(gco,'string',num2str(data.Fs));
        end
        set(data.ao, 'SampleRate', data.Fs);
        set(fig,'UserData',data);
        buildwave

    case 'wave'
        data=get(gcf,'UserData');

        objhandle=findobj('tag','wavegrp');
        clrindex=find(objhandle ~= gco);
        set(objhandle(clrindex),'value',0);
        set(gco,'value',1);
        data.wave=get(gco,'string');
        if strcmp(data.wave,'One pulse')
            set(data.mode2,'enable','on')

        elseif strcmp(data.wave,'Two pulse')
            set(data.mode2,'enable','on')

        elseif strcmp(data.wave,'Tetanic')
            set(data.mode2,'enable','off')
        end

        set(gcf,'UserData',data);
        buildwave

end %of main case statement

%===========================================
%===various subroutines=====================
%============================================================
%===A utility to center the window on the screen============
%===========================================================
function pos = centerfig(width,height)
% Find the screen size in pixels
screen_s = get(0,'ScreenSize');
pos = [screen_s(3)/2 - width/2, screen_s(4)/2 - height/2, width, height];
return

%============================================================
%===Build the stimulator output waveforms
%============================================================
function buildwave
data=get(gcf,'UserData');

set(gcf,'pointer','watch');

Fs=data.Fs; %sample rate

%convert all the times to sample numbers
dur=fix(data.width*Fs);
tet=fix(data.burst*Fs);
tetrep=fix(data.burstrepeat*Fs);
d1=fix(data.delay1*Fs);
d2=fix(data.delay2*Fs);
d3=fix(data.delay3*Fs);
rpt=fix(data.repeattime*Fs);
wave=data.wave;

numpulse=1+(tet-dur)/tetrep; %tetanic burst pulse number

%now just enumerate all the pulses
if strcmp(wave,'One pulse')
    waveform=zeros(d1+1,1);
    waveform=[waveform; ones(dur,1)];
elseif strcmp(wave,'Two pulse')
    waveform=zeros(d1+1,1);
    waveform=[waveform; ones(dur,1)];
    waveform=[waveform; zeros(d2-dur,1)];
    waveform=[waveform; ones(dur,1)];
elseif strcmp(wave,'Tetanic')
    waveform=zeros(d1+1,1);
    waveform=[waveform; ones(dur,1)];
    waveform=[waveform; zeros(d2-dur,1)];
    for i=1:numpulse-1
        waveform=[waveform; ones(dur,1)];
        waveform=[waveform; zeros(tetrep-dur,1)];
    end
    waveform=[waveform; ones(dur,1)]; %last tetanic pulse
    endburst=length(waveform); %need this for drawing
    waveform=[waveform; zeros(d3-dur,1)];
    waveform=[waveform; ones(dur,1); ];
end

wavelength=length(waveform);

%now pad for repetition rate
waveform=[waveform; zeros(rpt-length(waveform),1)];

%and copy into global data structure
data.waveform=5*waveform;

%trigger waveform must be same length
data.trigwave=[0; 5; zeros(length(waveform)-2,1)];

set(gcf,'UserData',data);

%build the actual plot
%depending on whether the plot has been padded
x=linspace(0,length(waveform)/Fs,length(waveform));
set(gcf,'CurrentAxes',data.sax);
if  rpt/Fs<11.0
    plot(x(1:wavelength+1),[waveform(1:wavelength);0],...
        'k','linewidth',2);
else
    plot(x,waveform,'k','linewidth',2);
end


text(0,.2,'Stimulator waveform','color','white');
curlim=get(gca,'xlim');
text(curlim(2)*.9,.2,...
    num2str(curlim(2),'time=%5.3f'),...
    'color','white');
set(gca,'ylim',[0 1.3], 'tickdir','in');
set(gca,'yticklabel',[],'color',[.6 .6 .6])
set(gca,'xticklabel',[]);

%lines to show various parameters
line([0 d1/Fs], [.5 .5], ...
    'color', [.6 1 .6],'linewidth',4)

line([d1/Fs (d1+dur)/Fs], [.8 .8], ...
    'color', [1 .6 .6],'linewidth',4)

if strcmp(wave,'Two pulse') | strcmp(wave,'Tetanic')
    line([d1/Fs d1/Fs+d2/Fs], [.5 .5], ...
        'color', [.8 .8 1],'linewidth',4)
end

if  strcmp(wave,'Tetanic')
    line([(endburst-dur)/Fs (endburst-dur)/Fs+d3/Fs], [.5 .5], ...
        'color', [.5 1 1],'linewidth',4)
    line([(d1+d2)/Fs (endburst)/Fs], [1.1 1.1], ...
        'color', [.9 .9 .4],'linewidth',4)
    line([(d1+d2)/Fs (d1+d2+tetrep)/Fs], [.8 .8], ...
        'color', [1 .8 .4],'linewidth',4)
end

%compute the violation function
if (dur>=d2 & strcmp(wave,'Two pulse')) ...
        | (dur>=d2 & strcmp(wave,'Tetanic')) ...
        | (dur>=d3 & strcmp(wave,'Tetanic')) ...
        | rpt<=wavelength ...
        | (dur>=tetrep & strcmp(wave,'Tetanic'))
    curlim=get(gca,'xlim');
    text(curlim(2)/3,.5,...
        'Violation','color','red','fontsize',30)
end

set(gcf,'pointer','arrow');

return

%=== end =========================================================

