function varargout = ScanGUI_6110_SessionBased(varargin)
% ScanGUI_6110_SessionBased M-file for ScanGUI_6110_SessionBased.fig
%      ScanGUI_6110_SessionBased, by itself, creates a new ScanGUI_6110_SessionBased or raises the existing
%      singleton*.
%
%      H = ScanGUI_6110_SessionBased returns the handle to a new ScanGUI_6110_SessionBased or the handle to
%      the existing singleton*.
%
%      ScanGUI_6110_SessionBased('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ScanGUI_6110_SessionBased.M with the given input arguments.
%
%      ScanGUI_6110_SessionBased('Property','Value',...) creates a new ScanGUI_6110_SessionBased or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ScanGUI_6110_SessionBased_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ScanGUI_6110_SessionBased_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ScanGUI_6110_SessionBased

% Last Modified by GUIDE v2.5 03-May-2017 04:11:17

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ScanGUI_6110_SessionBased_OpeningFcn, ...
                   'gui_OutputFcn',  @ScanGUI_6110_SessionBased_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ScanGUI_6110_SessionBased is made visible.
function ScanGUI_6110_SessionBased_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ScanGUI_6110_SessionBased (see VARARGIN)

% Choose default command line output for ScanGUI_6110_SessionBased
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ScanGUI_6110_SessionBased wait for user response (see UIRESUME)
% uiwait(handles.figure1);



%%%%%%%%
%MY MY CODE
%%%%%%%%
%Written by Phil Tsai 07/26/2011
%Last modified 02/11/2016

gds.numFrames = 50;            % Number of frames to aquire
gds.clockRateHz = 20000;         % Pixel clock rate [Hz]
gds.xImageSize = 64;           % Horizontal size [pixels] of image scan (32-512)
gds.yImageSize = 64;            % Vertical size [pixels] of image scan (32-512)
gds.xScanAmplitude = 0.4;         % Maximum scan voltage to x-scan mirror (0-4)
gds.yScanAmplitude = 0.4;         % Maximum scan voltage to y-scan mirror (0-4)
gds.inputVoltageRange = 10;       % +/- range of input voltages to digitize
gds.displayAutoContrastFlag = true;                % Turn on or off autoContrast [yes/no] 
gds.manualDisplayMinBrightness = -10;        % For Manual Contrast, min Itensity Value
gds.manualDisplayMaxBrightness = 10;                % For Manual Contrast, max Intensity Value
gds.saveMovieToFileFlag = 0;                     %Flag to save resultant movie to disk after run (yes/no)
gds.imageHorizontalOffset = 0;%70;                     %Shift pixels horizontally to account for scanner lag (0-512)
gds.imageCropHorizontalPixelsFromLeft =0;% 85;         %Crop pixels on right to avoid diplay of scanner refresh

daq.reset
vSession = daq.createSession('ni');
addAnalogInputChannel(vSession,'Dev1','ai0','Voltage');
addAnalogInputChannel(vSession,'Dev1','ai1','Voltage');
addAnalogOutputChannel(vSession,'Dev1','ao0','Voltage');
addAnalogOutputChannel(vSession,'Dev1','ao1','Voltage');
% vSession.Channels(1).TerminalConfig = 'PseudoDifferential';
% % % vSession.Channels(1).TerminalConfig = 'Differential';
vSession.Channels(1).TerminalConfig = 'SingleEnded';
vSession.Channels(1).Coupling = 'DC';
gds.vSession = vSession;

set(handles.numFramesEdit,'String',num2str(gds.numFrames,'%0.2f'));
set(handles.clockRateEdit,'String',num2str(gds.clockRateHz,'%0.2f'));
set(handles.xScanAmpEdit,'String',num2str(gds.xScanAmplitude,'%0.2f'));
set(handles.yScanAmpEdit,'String',num2str(gds.yScanAmplitude,'%0.2f'));
set(handles.autoContrastCheckbox,'Value',gds.displayAutoContrastFlag);
set(handles.minIntensityEdit,'String',num2str(gds.manualDisplayMinBrightness,'%0.2f'));
set(handles.maxIntensityEdit,'String',num2str(gds.manualDisplayMaxBrightness,'%0.2f'));
set(handles.xImageSizeEdit,'String',num2str(gds.xImageSize,'%0.2f'));
set(handles.yImageSizeEdit,'String',num2str(gds.yImageSize,'%0.2f'));
set(handles.pixelShiftEdit,'String',num2str(gds.imageHorizontalOffset,'%0.2f'));
set(handles.leftCropEdit,'String',num2str(gds.imageCropHorizontalPixelsFromLeft,'%0.2f'));
set(handles.minIntensitySlider,'Value',gds.manualDisplayMinBrightness);
set(handles.maxIntensitySlider,'Value',gds.manualDisplayMaxBrightness);
set(handles.scanIsRunningText,'Visible','off');

handles.gds = gds;
guidata(hObject,handles);




function RunScan(hObject, eventdata, handles)
gds = handles.gds;
xScanAmplitude = gds.xScanAmplitude;
yScanAmplitude = gds.yScanAmplitude;
numFrames = gds.numFrames;
imageCropHorizontalPixelsFromLeft = gds.imageCropHorizontalPixelsFromLeft;
yImageSize = gds.yImageSize;
xImageSize = gds.xImageSize;
inputVoltageRange = gds.inputVoltageRange;
vSession = gds.vSession;
clockRateHz = gds.clockRateHz;
%gds.dataBlock; %accessed directly to minimize memory usage;
stopFlag = gds.stopFlag;
%----------
%Generate Raster Patterns
fastRamp = repmat(linspace(-xScanAmplitude,xScanAmplitude,xImageSize)',[yImageSize,1]);
slowRamp = imresize(linspace(-yScanAmplitude,yScanAmplitude,yImageSize)',[xImageSize*yImageSize,1],'nearest');
dataBlock = zeros([yImageSize,xImageSize-imageCropHorizontalPixelsFromLeft,numFrames],'uint16');



vSession.Rate = clockRateHz;
vSession.Channels(1).Range = [-inputVoltageRange inputVoltageRange];
% set(ai,'SamplesPerTrigger',yImageSize*xImageSize)

set(handles.scanIsRunningText,'Visible','on');
gds.dataBlock = zeros(yImageSize,xImageSize,numFrames);
for frameIter = 1:numFrames,
    
    
    %Raster Mirrors and Acquire Image Data
    queueOutputData(vSession,[fastRamp,slowRamp]);
    vData = startForeground(vSession);
    
    intensityData = vData(:,1);
    fastScanFeedbackSignal = vData(:,2);
   
    diffSignal = abs(diff([0;fastScanFeedbackSignal]));
    bigDiffIndexList = find(diffSignal>mean(diffSignal));
    diffOffsetList = mod(bigDiffIndexList,xImageSize);
    detectedOffset = round(median(diffOffsetList));
    
    %Perform Image Alignment
    imageHorizontalOffset = -detectedOffset;
    
    offsetData = circshift(intensityData,imageHorizontalOffset);
    %Display Image FrameizontalOffset);
    fullFrame = reshape(offsetData(1:numel(intensityData)),xImageSize,yImageSize)';
    shiftedFrame = circshift(fullFrame,[0,imageHorizontalOffset]);
    croppedImageFrame = shiftedFrame(:,imageCropHorizontalPixelsFromLeft+1:end);
    bins = linspace(min(intensityData(:)),max(intensityData(:)),200);
    datahist=histc(intensityData, bins);
    plot(handles.histogramAxes,bins,datahist);
    
    lastDisplayedImageData = croppedImageFrame;
    handles.gds.lastDisplayedImageData = lastDisplayedImageData;
    tempHObject = guidata(gcf);
    stopFlag = tempHObject.gds.stopFlag;
    guidata(hObject,handles);
    RefreshImage(hObject, eventdata, handles)
    
    gds.dataBlock(:,:,frameIter) = croppedImageFrame;
    
    if stopFlag == 1,
        break%break out of frameIter loop
    end%if stopFlag
end%for frameIter = 1:gds.numFrames,

%Reset Mirrors to Center Position
resetRamp = zeros(1000,1);
queueOutputData(vSession,[resetRamp,resetRamp]);
startForeground(vSession);
stopFlag = 0;
set(handles.scanIsRunningText,'Visible','off');

%Save Image Data
switch gds.saveMovieToFileFlag,
    case {'Yes','yes','Y','y','YES','save','Save','SAVE',1},
        [fileName,pathName] = uiputfile('*.tif','Save image stack as:');
        %maketiff(gds.dataBlock,fullfile(pathName,fileName));
        a=squeeze(mean(double(gds.dataBlock),3));
        a=a-min(a(:)); a=uint16(a/max(a(:))*32767);
        imwrite(a,fullfile(pathName,fileName),'tif');
end%switch gds.saveMovieToFileFlag,
%----------
gds.fastRamp = fastRamp;
gds.slowRamp = slowRamp;
gds.dataBlock = dataBlock;
gds.vSession = vSession;
gds.lastDisplayedImageData = lastDisplayedImageData;
% gds.blockData; %modified directly to minimize memory usage;
gds.stopFlag = stopFlag;
handles.gds = gds;
guidata(hObject,handles);




function RefreshImage(hObject, eventdata, handles)
gds = handles.gds;
lastDisplayedImageData = gds.lastDisplayedImageData;
displayAutoContrastFlag = gds.displayAutoContrastFlag;
manualDisplayMinBrightness = gds.manualDisplayMinBrightness;
manualDisplayMaxBrightness = gds.manualDisplayMaxBrightness;

%----------
croppedImageFrame = lastDisplayedImageData;
figure(handles.figure1);
axes(handles.displayAxes);
colormap(gray)
minImageIntensity = min(croppedImageFrame(:));
meanImageIntensity = mean(croppedImageFrame(:));
maxImageIntensity = max(croppedImageFrame(:));
imageStats = [minImageIntensity, meanImageIntensity, maxImageIntensity];
switch displayAutoContrastFlag,
    case {'Yes','yes','YES','Y','y',1}
    imagesc(croppedImageFrame);
    otherwise
        croppedImageFrame = max(manualDisplayMinBrightness,croppedImageFrame);
        croppedImageFrame = min(manualDisplayMaxBrightness,croppedImageFrame);
        croppedImageFrame = croppedImageFrame - manualDisplayMinBrightness;
        myRange = manualDisplayMaxBrightness-manualDisplayMinBrightness;
        croppedImageFrame = uint16(100*double(croppedImageFrame/myRange));
        image(croppedImageFrame)
        title(['Min,Mean,Max Intensity] = [ ',num2str(imageStats,'%0.2f '),']']);
   
end%switch displayAutoContrastFlag,
%----------
handles.gds = gds;
guidata(hObject,handles);






















% --- Outputs from this function are returned to the command line.
function varargout = ScanGUI_6110_SessionBased_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;




% --- Executes on button press in startBiutton.
function startBiutton_Callback(hObject, eventdata, handles)
% hObject    handle to startBiutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
gds = handles.gds;
%----------
gds.stopFlag = 0;
%----------
handles.gds = gds;
guidata(hObject,handles);
RunScan(hObject, eventdata, handles);





% --- Executes on button press in stopButton.
function stopButton_Callback(hObject, eventdata, handles)
% hObject    handle to stopButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
gds = handles.gds;
gds.stopFlag = 1
%----------
handles.gds = gds;
guidata(hObject,handles);



function numFramesEdit_Callback(hObject, eventdata, handles)
% hObject    handle to numFramesEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of numFramesEdit as text
%        str2double(get(hObject,'String')) returns contents of numFramesEdit as a double
gds = handles.gds;
numFrames = gds.numFrames;
%----------
minVal = 0;
maxVal = inf;
validEntryFlag = 0;
tempString = get(handles.numFramesEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    numFrames = tempNum;
else%if validEntryFlag == 1,
    set(handles.numFramesEdit,'String',num2str(numFrames,'%0.2f'));
end%if validFlag == 1
%----------
gds.numFrames = numFrames;
handles.gds = gds;
guidata(hObject,handles);




function clockRateEdit_Callback(hObject, eventdata, handles)
% hObject    handle to clockRateEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of clockRateEdit as text
%        str2double(get(hObject,'String')) returns contents of clockRateEdit as a double
gds = handles.gds;
clockRateHz = gds.clockRateHz;
%----------
minVal = 0;
maxVal = 100000;
validEntryFlag = 0;
tempString = get(handles.clockRateEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    clockRateHz = tempNum;
else%if validEntryFlag == 1,
    set(handles.clockRateEdit,'String',num2str(clockRateHz,'%0.2f'));
end%if validFlag == 1
%----------
gds.clockRateHz = clockRateHz;
handles.gds = gds;
guidata(hObject,handles);





function xScanAmpEdit_Callback(hObject, eventdata, handles)
% hObject    handle to xScanAmpEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of xScanAmpEdit as text
%        str2double(get(hObject,'String')) returns contents of xScanAmpEdit as a double
gds = handles.gds;
xScanAmplitude = gds.xScanAmplitude
%----------
minVal = 0;
maxVal = 1;
validEntryFlag = 0;
tempString = get(handles.xScanAmpEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    xScanAmplitude = tempNum;
else%if validEntryFlag == 1,
    set(handles.xScanAmpEdit,'String',num2str(xScanAmplitude,'%0.2f'));
end%if validFlag == 1
%----------
gds.xScanAmplitude = xScanAmplitude;
handles.gds = gds;
guidata(hObject,handles);




function yScanAmpEdit_Callback(hObject, eventdata, handles)
% hObject    handle to xScanAmpEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of xScanAmpEdit as text
%        str2double(get(hObject,'String')) returns contents of xScanAmpEdit as a double
gds = handles.gds;
yScanAmplitude = gds.yScanAmplitude;
%----------
minVal = 0;
maxVal = 1;
validEntryFlag = 0;
tempString = get(handles.yScanAmpEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    yScanAmplitude = tempNum;
else%if validEntryFlag == 1,
    set(handles.yScanAmpEdit,'String',num2str(yScanAmplitude,'%0.2f'));
end%if validFlag == 1
%----------
gds.yScanAmplitude = yScanAmplitude;
handles.gds = gds;
guidata(hObject,handles);




function xImageSizeEdit_Callback(hObject, eventdata, handles)
% hObject    handle to xImageSizeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of xImageSizeEdit as text
%        str2double(get(hObject,'String')) returns contents of xImageSizeEdit as a double
gds = handles.gds;
xImageSize = gds.xImageSize;
%----------
minVal = 32;
maxVal = 512;
validEntryFlag = 0;
tempString = get(handles.xImageSizeEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    xImageSize = tempNum;
else%if validEntryFlag == 1,
    set(hanldes.xImageSizeEdit,'String',num2str(xImageSize,'%0.2f'));
end%if validFlag == 1
%----------
gds.xImageSize = xImageSize;
handles.gds = gds;
guidata(hObject,handles);




function yImageSizeEdit_Callback(hObject, eventdata, handles)
% hObject    handle to yImageSizeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of yImageSizeEdit as text
%        str2double(get(hObject,'String')) returns contents of yImageSizeEdit as a double
gds = handles.gds;
yImageSize = gds.yImageSize;

%----------
minVal = 32;
maxVal = 512;
validEntryFlag = 0;
tempString = get(handles.yImageSizeEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    yImageSize = tempNum;
else%if validEntryFlag == 1,
    set(handles.yImageSizeEdit,'String',num2str(yImageSize,'%0.2f'));
end%if validFlag == 1
%----------
gds.yImageSize = yImageSize;
handles.gds = gds;
guidata(hObject,handles);




% --- Executes on button press in autoContrastCheckbox.
function autoContrastCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to autoContrastCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of autoContrastCheckbox
gds = handles.gds;
gds.displayAutoContrastFlag = get(handles.autoContrastCheckbox,'Value');
%----------
handles.gds = gds;
guidata(hObject,handles);




% --- Executes on button press in saveImageCheckbox.
function saveImageCheckbox_Callback(hObject, eventdata, handles)
% hObject    handle to saveImageCheckbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of saveImageCheckbox
gds = handles.gds;
gds.saveMovieToFileFlag = get(handles.saveImageCheckbox,'Value');
%----------
handles.gds = gds;
guidata(hObject,handles);






function pixelShiftEdit_Callback(hObject, eventdata, handles)
% hObject    handle to pixelShiftEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of pixelShiftEdit as text
%        str2double(get(hObject,'String')) returns contents of pixelShiftEdit as a double
gds = handles.gds;
imageHorizontalOffset = gds.imageHorizontalOffset;
%----------
minVal = 0;
maxVal = inf;
validEntryFlag = 0;
tempString = get(handles.pixelShiftEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    imageHorizontalOffset = tempNum;
else%if validEntryFlag == 1,
    set(handles.pixelShiftEdit,'String',num2str(imageHorizontalOffset,'%0.2f'));
end%if validFlag == 1
%----------
gds.imageHorizontalOffset = imageHorizontalOffset;
handles.gds = gds;
guidata(hObject,handles);
RefreshImage(hObject, eventdata, handles);





function leftCropEdit_Callback(hObject, eventdata, handles)
% hObject    handle to leftCropEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of leftCropEdit as text
%        str2double(get(hObject,'String')) returns contents of leftCropEdit as a double
gds = handles.gds;
imageCropHorizontalPixelsFromLeft = gds.imageCropHorizontalPixelsFromLeft;
%----------
minVal = 0;
maxVal = inf;
validEntryFlag = 0;
tempString = get(handles.leftCropEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    imageCropHorizontalPixelsFromLeft = tempNum;
else%if validEntryFlag == 1,
    set(handles.leftCropEdit,'String',num2str(imageCropHorizontalPixelsFromLeft,'%0.2f'));
end%if validFlag == 1
%----------
gds.imageCropHorizontalPixelsFromLeft = imageCropHorizontalPixelsFromLeft;
handles.gds = gds;
guidata(hObject,handles);
RefreshImage(hObject, eventdata, handles);





% --- Executes on slider movement.
function maxIntensitySlider_Callback(hObject, eventdata, handles)
% hObject    handle to maxIntensitySlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
gds = handles.gds;
ao = gds.ao;
manualDisplayMaxBrightness = gds.manualDisplayMaxBrightness;
%----------
sliderVal = get(handles.maxIntensitySlider,'Value');
manualDisplayMaxBrightness = sliderVal;
set(handles.maxIntensityEdit,'String',num2str(manualDisplayMaxBrightness,'%0.2f'));
stop([ao]);
%----------
gds.manualDisplayMaxBrightness = manualDisplayMaxBrightness;
gds.ao = ao;
handles.gds = gds;
guidata(hObject,handles);
RefreshImage(hObject, eventdata, handles);






function maxIntensityEdit_Callback(hObject, eventdata, handles)
% hObject    handle to maxIntensityEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxIntensityEdit as text
%        str2double(get(hObject,'String')) returns contents of maxIntensityEdit as a double
gds = handles.gds;
manualDisplayMaxBrightness = gds.manualDisplayMaxBrightness;
%----------
minVal = 0;
maxVal = inf;
validEntryFlag = 0;
tempString = get(handles.maxIntensityEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    manualDisplayMaxBrightness = tempNum;
    sliderVal = tempNum;
    set(handles.maxIntensitySlider,'Value',sliderVal);
else%if validEntryFlag == 1,
    set(handles.maxIntensityEdit,'String',num2str(manualDisplayMaxBrightness,'%0.2f'));
end%if validFlag == 1
%----------
gds.manualDisplayMaxBrightness = manualDisplayMaxBrightness;
handles.gds = gds;
guidata(hObject,handles);
RefreshImage(hObject, eventdata, handles);





% --- Executes on slider movement.
function minIntensitySlider_Callback(hObject, eventdata, handles)
% hObject    handle to minIntensitySlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
gds = handles.gds;
%----------
sliderVal = get(handles.minIntensitySlider,'Value');
gds.manualDisplayMinBrightness = sliderVal;
set(handles.minIntensityEdit,'String',num2str(gds.manualDisplayMinBrightness,'%0.2f'));
stop([gds.ao]);
%----------
handles.gds = gds;
guidata(hObject,handles);
RefreshImage(hObject, eventdata, handles);





function minIntensityEdit_Callback(hObject, eventdata, handles)
% hObject    handle to minIntensityEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of minIntensityEdit as text
%        str2double(get(hObject,'String')) returns contents of minIntensityEdit as a double
gds = handles.gds;
manualDisplayMinBrightness = gds.manualDisplayMinBrightness;
%----------
minVal = 0;
maxVal = inf;
validEntryFlag = 0;
tempString = get(handles.minIntensityEdit,'String');
tempNum = str2double(tempString);
if ~isnan(tempNum),
    if tempNum >= minVal && tempNum <= maxVal
        validEntryFlag = 1;
    end%if tempNum >= ...
end%if ~isnan(tempNum);
%---
if validEntryFlag == 1,
    manualDisplayMinBrightness = tempNum;
    sliderVal = tempNum;
    set(handles.minIntensitySlider,'Value',sliderVal);
else%if validEntryFlag == 1,
    set(handles.minIntensityEdit,'String',num2str(manualDisplayMinBrightness,'%0.2f'));
end%if validFlag == 1
%----------
gds.manualDisplayMinBrightness = manualDisplayMinBrightness;
handles.gds = gds;
guidata(hObject,handles);
RefreshImage(hObject, eventdata, handles);


















% --- Executes during object creation, after setting all properties.
function numFramesEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numFramesEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function clockRateEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to clockRateEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes during object creation, after setting all properties.
function xScanAmpEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xScanAmpEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function yScanAmpEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xScanAmpEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





% --- Executes during object creation, after setting all properties.
function xImageSizeEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xImageSizeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function yImageSizeEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yImageSizeEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes during object creation, after setting all properties.
function pixelShiftEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pixelShiftEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes during object creation, after setting all properties.
function leftCropEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to leftCropEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function maxIntensitySlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxIntensitySlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes during object creation, after setting all properties.
function maxIntensityEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxIntensityEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function minIntensitySlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minIntensitySlider (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes during object creation, after setting all properties.
function minIntensityEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minIntensityEdit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function scanIsRunningText_CreateFcn(hObject, eventdata, handles)
% hObject    handle to scanIsRunningText (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
