% display of an incoming analog input signal.
% Hacked by BRL from the Mathworks FFT demo program. 
% ===8 Oct 2000===
%
%    scope1 creates an analog input object associated with the 
%    winsound adaptor with device identification 0.  The incoming 
%    signal of the created analog input object is plotted.
%

% Based on function, call the appropriate 
% local function.

% ***********************************************************************   
% Create the DAQ object and get the data
clear all;
clf;

% Initialize variables.
data = [];

adaptor = 'winsound';
id = 0;
chan = 1;

% Object Configuration.
% Create an analog input object with one channel.
ai = analoginput(adaptor, id);
ch = addchannel(ai, chan);

% Configure the analog input object.
set(ai, 'SampleRate', 44100);

% Configure the analog input object to trigger manually.
set(ai, 'SamplesPerTrigger', 4410);
set(ai, 'TriggerRepeat', 1);
set(ai, 'TriggerType', 'Manual');

start(ai);
trigger(ai);

%make sure we actually got all the data
while get(ai,'SamplesPerTrigger') ...
      ~= get(ai,'SamplesAcquired')
   drawnow
end

y = getdata(ai, ai.SamplesPerTrigger); 
stop(ai);

x = linspace(0, ...
         get(ai,'SamplesPerTrigger')/get(ai,'SampleRate'),...
         get(ai,'SamplesPerTrigger'));

% ***********************************************************************   
% Create the display

% Initialize variables.
btnColor=get(0,'DefaultUIControlBackgroundColor');

% Position the GUI in the middle of the screen
screenUnits=get(0,'Units');
set(0,'Units','pixels');
screenSize=get(0,'ScreenSize');
set(0,'Units',screenUnits);
figWidth=600;
figHeight=300;
figPos=[(screenSize(3)-figWidth)/2 ...
      (screenSize(4)-figHeight)/2  ...
      figWidth ...
   figHeight];

% Create the figure window.
hFig=figure(...                    
   'Color'             ,btnColor                 ,...
   'IntegerHandle'     ,'off'                    ,...
   'DoubleBuffer'      ,'on'                     ,...
   'HandleVisibility'  ,'on'                     ,...
   'Name'              ,'Winsound one-channel scope'  ,...
   'Tag'               ,'One Channel scope'  ,...
   'NumberTitle'       ,'off'                    ,...
   'Units'             ,'pixels'                 ,...
   'Position'          ,figPos                   ,...
   'UserData'          ,[]                       ,...
   'Colormap'          ,[]                       ,...
   'Pointer'           ,'arrow'                  ,...
   'Visible'           ,'on'                     ...
   );
%'MenuBar'           ,'none'                   ,...

% Create Data subplot.
hAxes(1) = axes(...
   'Units'             ,'normalized', ...
   'Position'          , [0.08 0.1 0.5 .85],...
   'fontsize'          ,8, ...
   'Parent'            , hFig...
    );

% Plot the data.
hLine(1) = plot(y,'w');
set(hAxes(1), 'XLim', [0 ...
      get(ai,'SamplesPerTrigger')/get(ai,'Samplerate')]);
set(hAxes(1), 'YLim', [-1 1]);

set(hAxes(1), 'color', [.1 .4 .2]);
grid on

% Label the plot.
xlabel('Time (sec)');
ylabel('Winsound.0 (Volts)');
%title('Analog Data Acquisition');

%Frames==================================
%frame for trigger stuff
uicontrol( ...
   'style', 'frame', ...
   'Units','normalized', ...
   'position', [.86 .1 .13 .9], ...
   'backgroundcolor', [.8,.8,.8] ...
   );

%frame for vertical stuff
uicontrol( ...
   'style', 'frame', ...
   'Units','normalized', ...
   'position', [.6 .1 .13 .9], ...
   'backgroundcolor', [.8,.8,.8] ...
   );

%frame for horizontal stuff
uicontrol( ...
   'style', 'frame', ...
   'Units','normalized', ...
   'position', [.73 .1 .13 .9], ...
   'backgroundcolor', [.8,.8,.8] ...
   );

%Trig controls==================================
% Create trigger radio buttons.
hsingle = uicontrol(...
   'Parent'          , hFig,...
   'Style'           , 'radiobutton',...
   'Units'           , 'normalized',...
   'Position'        , [0.87 0.9 0.1 0.06],...
   'Value'           , 0,...
   'String'          , 'Single',...
   'Callback'        , 'fcn=''single'';');

hfree = uicontrol(...
   'Parent'          , hFig,...
   'Style'           , 'radiobutton',...
   'Units'           , 'normalized',...
   'Position'        , [0.87 0.85 0.1 0.06],...
   'Value'           , 1,...
   'String'          , 'Freerun',...
   'Callback'        , 'fcn=''freerun'';');

%create trigger button
htrig = uicontrol(...
   'Parent'          , hFig,...
   'Style'           , 'pushbutton',...
   'Units'           , 'normalized',...
   'Position'        , [0.87 0.75 0.1 0.06],...
   'Value'           , 1,...
   'enable'          , 'off', ... %disabled in freerun mode
   'String'          , 'Trigger',...
   'Callback'        , 'fcn=''trigger'';');

%Vert controls==================================
hvolt = uicontrol(...
   'style', 'listbox', ...
   'Units','normalized', ...
   'position', [.61 .5 .11 .2], ...
   'string','0.1|0.2|0.5|1.0', ...
   'value', 4, ...  %initially 1.0 fullscale
   'callback', 'fcn=''setVolts'';'...
   );

uicontrol(...
   'style', 'text',...
   'string','V fullscale',...
   'Units','normalized', ...
   'position', [.61 .7 .11 .06]...
   );

%Horizontal controls======================
htime = uicontrol(...
   'style', 'listbox', ...
   'Units','normalized', ...
   'position', [.74 .5 .11 .3], ...
   'string','0.002|0.005|0.01|0.02|0.05|0.10', ...
   'value', 6, ...  %initially 0.10 fullscale time
   'callback', 'fcn=''setTime'';'...
   );

uicontrol(...
   'style', 'text',...
   'string','Timebase',...
   'Units','normalized', ...
   'position', [.74 .8 .11 .06]...
   );

%Menu==================================
%A close menu item
hmenu = uimenu(gcf, 'Label', 'Close');
hmenu1 = uimenu(hmenu,...
   'Label', 'Close',...
   'Callback', 'fcn=''close'';');

% *****************************************************
%main pgm loop
% *****************************************************
%free running, no trigger
triggerRun = 0;

%no initial function
fcn='';

while (1)
   
   drawnow; %make sure controls are active
   
   switch fcn
      
      %--------------------------------------------   
   case 'close' 
      % Stop the device if it is running 
      %and delete the object.
      if isvalid(ai)
         if strcmp(get(ai, 'Running'), 'On')
            stop(ai);
         end
         delete(ai);
      end
      % Close the figure window.
      delete(hFig);
      %and stop the loop
      break; 
      
      %--------------------------------------------   
   case 'single'
      fcn='';
      %radio button exclusion
      set(hfree, 'Value', 0); 
      
      %enable trigger button
      set(htrig, 'enable', 'on');  
      
      % Stop the device.
      stop(ai);
      %Turn off auto refresh
      %set(data.ai, 'TimerAction', '');
      
      %trigger > software (voltage level on ch 0)
      set(ai, 'TriggerChannel', ch);
      set(ai, 'TriggerType', 'Software');
      set(ai, 'TriggerCondition', 'rising');
      set(ai, 'TriggerConditionValue', 0.2);
      set(ai, 'TriggerRepeat', 0);  
      
      set(ai, 'Timeout', 2)
      
      % set state to 'trigger mode'
      triggerRun = 1;
      
      
      %--------------------------------------------   
   case 'freerun'
      fcn='';
      %radio button exclusion
      set(hsingle, 'Value', 0);  
      
      %disable trigger button
      set(htrig, 'enable', 'off');  
      
      %trigger > manual (trigger on call)
      set(ai, 'TriggerType', 'manual');
      set(ai, 'TriggerRepeat', 1);
      
      % Store the new state (free run).
      triggerRun = 0;
            
      %--------------------------------------------   
   case 'trigger'
      fcn='';
            
      % Get one trace
      start(ai);
      
      %while strcmp(obj.Running, 'On')
      %end
      
      y=[];
                  
      %make sure we actually got all the data
      while get(ai,'SamplesPerTrigger') ...
            ~= get(ai,'SamplesAcquired')
         drawnow
      end
      
      stop(ai);
      
      y = getdata(ai, ai.SamplesPerTrigger);
      % Update the plot.
      set(hLine(1), 'YData', y, 'XData', x);
      
      drawnow;
      
      %--------------------------------------------   
   case 'setVolts'
      fcn='';    
      
      switch get(hvolt, 'value');        
      case 1
         set(hAxes(1), 'YLim', [-.1 .1]);
      case 2
         set(hAxes(1), 'YLim', [-.2 .2]);
      case 3
         set(hAxes(1), 'YLim', [-.5 .5]);
      case 4
         set(hAxes(1), 'YLim', [-1 1]);
      end
      
      %--------------------------------------------   
   case 'setTime'
      fcn='';
            
            
      switch get(htime, 'value');
         
      case 1
         set(hAxes(1), 'XLim', [0 .002]);
         set(ai, 'SamplesPerTrigger', 175);
      case 2
         set(hAxes(1), 'XLim', [0 .005]);
         set(ai, 'SamplesPerTrigger', 220);
      case 3
         set(hAxes(1), 'XLim', [0 .01]);
         set(ai, 'SamplesPerTrigger', 441);
      case 4
         set(hAxes(1), 'XLim', [0 .02]);
         set(ai, 'SamplesPerTrigger', 882);
      case 5
         set(hAxes(1), 'XLim', [0 .05]);
         set(ai, 'SamplesPerTrigger', 2205);
      case 6
         set(hAxes(1), 'XLim', [0 .1]);
         set(ai, 'SamplesPerTrigger', 4410);
      end
      
      x = linspace(0, ...
         get(ai,'SamplesPerTrigger')/get(ai,'SampleRate'),...
         get(ai,'SamplesPerTrigger'));
      
   end %switch fcn
   
   %--------------------------------------------
   %refresh the display if we are free running
   if triggerRun==0
            
      % Start the device.
      start(ai);
      trigger(ai);

      % Execute a peekdata and 
      %make sure we got the right amount of data
      y=[];
            
      %make sure we actually got all the data
      %while get(obj,'SamplesPerTrigger') ...
       %     ~= get(obj,'SamplesAcquired')
       %  drawnow
      %end
      
      y = getdata(ai, ai.SamplesPerTrigger); 
      stop(ai);
      
      % Update the plot.
      set(hLine(1), 'YData', y, 'XData', x);
      
      drawnow;
   end
   %--------------------------------------------
   
end %while loop



