function varargout = controlui(varargin)
% CONTROLUI MATLAB code for controlui.fig
%      CONTROLUI, by itself, creates a new CONTROLUI or raises the existing
%      singleton*.
%
%      H = CONTROLUI returns the handle to a new CONTROLUI or the handle to
%      the existing singleton*.
%
%      CONTROLUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in CONTROLUI.M with the given input arguments.
%
%      CONTROLUI('Property','Value',...) creates a new CONTROLUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before controlui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to controlui_OpeningFcn via varargin.
%
%      *See GUI optionsmenu 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 controlui

% Last Modified by GUIDE v2.5 12-Jun-2013 15:56:19

% Begin initialization code - DO NOT EDIT
gui_Singleton = 0;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @controlui_OpeningFcn, ...
                   'gui_OutputFcn',  @controlui_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 controlui is made visible.
function controlui_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 controlui (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);


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


% --- Outputs from this function are returned to the command line.
function varargout = controlui_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 selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


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

% Hint: popupmenu 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 on button press in runbutton.
function runbutton_Callback(hObject, eventdata, handles)
% hObject    handle to runbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.collectbutton, 'Enable', 'off');
set(handles.pressurizebutton, 'Enable', 'off');
set(handles.flowbutton, 'Enable', 'off');
set(handles.flushbutton, 'Enable', 'off');

seq = get(handles.popupmenu1, 'Value');
ic = get(handles.figure1, 'UserData');
ic.startsequence(seq);

% --- Executes when selected object is changed in uipanel1.
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uipanel1 
% eventdata  structure with the following fields (see UIBUTTONGROUP)
%	EventName: string 'SelectionChanged' (read only)
%	OldValue: handle of the previously selected object or empty if none was selected
%	NewValue: handle of the currently selected object
% handles    structure with handles and user data (see GUIDATA)

ic = get(handles.figure1, 'UserData');

if eventdata.NewValue == handles.flowbutton
    x = str2double(get(handles.edit1, 'String'));
    ic.holdfcn('Flow', x);
elseif eventdata.NewValue == handles.flushbutton
    ic.holdfcn('Flush');
elseif eventdata.NewValue == handles.pressurizebutton
    ic.holdfcn('Pressurize');
elseif eventdata.NewValue == handles.collectbutton
    ic.holdfcn('Collect');
elseif eventdata.NewValue == handles.regenbutton
    x = str2double(get(handles.edit3, 'String'));
    ic.holdfcn('Regenerate', x);
elseif eventdata.NewValue == handles.stopbutton
    set(handles.collectbutton, 'Enable', 'on');
    set(handles.pressurizebutton, 'Enable', 'on');
    set(handles.flowbutton, 'Enable', 'on');
    set(handles.flushbutton, 'Enable', 'on');
    set(handles.regenbutton, 'Enable', 'on');
    ic.holdfcn('Stop');
else
    %What happened?
end


% --- Executes on button press in basebutton.
function basebutton_Callback(hObject, eventdata, handles)
% hObject    handle to basebutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ic = get(handles.figure1, 'UserData');

ic.baseline();


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
x = get(hObject, 'UserData');
delete(x);
delete(hObject);


% --------------------------------------------------------------------
function rawpixmenu_Callback(hObject, eventdata, handles)
% hObject    handle to rawpixmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ic = get(handles.figure1, 'UserData');
ic.startrawviewer();


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



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double
ic = get(handles.figure1, 'UserData');
if get(handles.uipanel1, 'SelectedObject') == handles.flowbutton
    x = str2double(get(handles.edit1, 'String'));
    ic.holdfcn('Flow', x);
end


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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


% --------------------------------------------------------------------
function fluidicsprogrammenu_Callback(hObject, eventdata, handles)
% hObject    handle to fluidicsprogrammenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ic = get(handles.figure1, 'UserData');
ic.startfluidicsprogramingviewer();


% --- Executes on selection change in msgbox.
function msgbox_Callback(hObject, eventdata, handles)
% hObject    handle to msgbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns msgbox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from msgbox


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

% Hint: listbox 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 on selection change in warnbox.
function warnbox_Callback(hObject, eventdata, handles)
% hObject    handle to warnbox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns warnbox contents as cell array
%        contents{get(hObject,'Value')} returns selected item from warnbox


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

% Hint: listbox 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


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


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


% --------------------------------------------------------------------
function serialmenu_Callback(hObject, eventdata, handles)
% hObject    handle to serialmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.changeserial();


% --------------------------------------------------------------------
function dataoptionsmenu_Callback(hObject, eventdata, handles)
% hObject    handle to dataoptionsmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.dataui();


% --------------------------------------------------------------------
function connectmenu_Callback(hObject, eventdata, handles)
% hObject    handle to connectmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.connectmenu, 'Enable', 'off');
set(handles.disconnectmenu, 'Enable', 'on');
% set(handles.loadmenu, 'Enable', 'off');
x = get(handles.figure1, 'UserData');
x.serialcommand('Connect');

% --------------------------------------------------------------------
function disconnectmenu_Callback(hObject, eventdata, handles)
% hObject    handle to disconnectmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.connectmenu, 'Enable', 'on');
set(handles.disconnectmenu, 'Enable', 'off');
set(handles.reconnectmenu, 'Enable', 'on');
x = get(handles.figure1, 'UserData');
x.serialcommand('Disconnect');


% --------------------------------------------------------------------
function loadmenu_Callback(hObject, eventdata, handles)
% hObject    handle to loadmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.connectmenu, 'Enable', 'on');
set(handles.disconnectmenu, 'Enable', 'off');
set(handles.reconnectmenu, 'Enable', 'off');
[file, dir] = uigetfile({'*.spr;*.txt', 'SPR Data Files.'});
x = get(handles.figure1, 'UserData');
if ~isempty(file)
    x.loadfile([dir file]);
end
    
% --------------------------------------------------------------------
function reconnectmenu_Callback(hObject, eventdata, handles)
% hObject    handle to reconnectmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.reconnectmenu, 'Enable', 'off');
set(handles.disconnectmenu, 'Enable', 'on');
set(handles.connectmenu, 'Enable', 'off');
% set(handles.loadmenu, 'Enable', 'off');
x = get(handles.figure1, 'UserData');
x.serialcommand('Reconnect');


% --------------------------------------------------------------------
function datafilemenu_Callback(hObject, eventdata, handles)
% hObject    handle to datafilemenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
oldfile = x.getdatadir();
newfolder = uigetdir(oldfile);
x.updatesetting('filedir', newfolder);


% --------------------------------------------------------------------
function resetmenu_Callback(hObject, eventdata, handles)
% hObject    handle to resetmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.resetdata();


% --------------------------------------------------------------------
function refmenu_Callback(hObject, eventdata, handles)
% hObject    handle to refmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.refui();


% --------------------------------------------------------------------
function tempmenu_Callback(hObject, eventdata, handles)
% hObject    handle to tempmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.tempui();


% --- Executes on button press in plotbox1.
function plotbox1_Callback(hObject, eventdata, handles)
% hObject    handle to plotbox1 (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 plotbox1
x = get(handles.figure1, 'UserData');
if get(hObject, 'Value')
    x.showplot(1);
else
    x.hideplot(1);
end

% --- Executes on button press in plotbox2.
function plotbox2_Callback(hObject, eventdata, handles)
% hObject    handle to plotbox2 (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 plotbox2
x = get(handles.figure1, 'UserData');
if get(hObject, 'Value')
    x.showplot(2);
else
    x.hideplot(2);
end


% --- Executes on button press in plotbox3.
function plotbox3_Callback(hObject, eventdata, handles)
% hObject    handle to plotbox3 (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 plotbox3
x = get(handles.figure1, 'UserData');
if get(hObject, 'Value')
    x.showplot(3);
else
    x.hideplot(3);
end


% --- Executes on button press in plotbox4.
function plotbox4_Callback(hObject, eventdata, handles)
% hObject    handle to plotbox4 (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 plotbox4
x = get(handles.figure1, 'UserData');
if get(hObject, 'Value')
    x.showplot(4);
else
    x.hideplot(4);
end


% --------------------------------------------------------------------
function calibratemenu_Callback(hObject, eventdata, handles)
% hObject    handle to calibratemenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.calibrateui();


% --- Executes on button press in pausebox.
function pausebox_Callback(hObject, eventdata, handles)
% hObject    handle to pausebox (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 pausebox
x = get(handles.figure1, 'UserData');
x.pause(get(hObject, 'Value'));


% --- Executes on mouse press over figure background.
function figure1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function preferencesmenu_Callback(hObject, eventdata, handles)
% hObject    handle to preferencesmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
x = get(handles.figure1, 'UserData');
x.settingsui();



function edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double
ic = get(handles.figure1, 'UserData');
if get(handles.uipanel1, 'SelectedObject') == handles.regenbutton
    x = str2double(get(handles.edit1, 'String'));
    ic.holdfcn('Regenerate', x);
end


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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


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