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

% Last Modified by GUIDE v2.5 03-May-2006 13:16:06

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

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

%Initialisation Code
%Get the old band values
handles.OldCustomBands = varargin{1}.CustomBands;
%Get the value of the get choose bands toggle button
handles.OldCustomBandsTgl = varargin{1}.SetNotchTgl;
%Set the band values to the old band values the user had already entered
% (Kind of like a sticky form.)
set(handles.CustomFreqBands, 'String', num2str(handles.OldCustomBands));
%prepare the output function for detecting an ok click
handles.okclicked = false;

%Define Variables that will be used in Output_Fcn
handles.CustomBandsTglOut = [];    
handles.CustomBandsOut = [];
% Update handles structure
guidata(hObject, handles);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% make the figure visible here
set(gcf,'Visible','on');
% wait here until the Visible property is set to 'off'
waitfor(gcf,'Visible');
% Update handles structure
% guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = CustomFilters_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)

if handles.okclicked
    handles.output1 = handles.CustomBandsOut;  %Prepare to change the notch vals
    handles.output2 = (handles.CustomBandsTglOut); %Prepare to toggle the button
    
    %return the selected variables to FBA_int
    varargout{1} = handles.output1;
    varargout{2} = handles.output2;
    
    %delete the figure, which is not visible
    delete(hObject)
else
    handles.output1 = handles.CustomBandsOut; %Dont change the Notch values
    handles.output2 = (handles.CustomBandsOut);% UnToggle the Button in FBA_int
    %delete the figure, which is not visible
    varargout{1} = handles.output1;
    varargout{2} = handles.output2;
    delete(hObject)
end



function CustomFreqBands_Callback(hObject, eventdata, handles)
% hObject    handle to CustomFreqBands (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 CustomFreqBands as text
%        str2double(get(hObject,'String')) returns contents of CustomFreqBands as a double


% --- Executes during object creation, after setting all properties.
function CustomFreqBands_CreateFcn(hObject, eventdata, handles)
% hObject    handle to CustomFreqBands (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 on button press in OkBtn.
function OkBtn_Callback(hObject, eventdata, handles)
% hObject    handle to OkBtn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%Trim the whitespace off the ends, and call it S...
S = strtrim(get(handles.CustomFreqBands, 'String'));
% We first check that the user has entered a value into the box and warn
% them if they havent
if(length(S)>0)
    %If the string length is greater than zero without any white space...
    
    count = 0; %Start a count of how many items there are
    while length(S)>1
        count = count+1; %Increment the count
        [N, S] = strtok(S, ' '); %Store the string in N and the remainder in S 
        handles.CustomBandsOut(count) = str2num(N); %Store the Band value
    end %End of while

    if(mod(count, 2)>0) % If there's an odd number of values
        handles.CustomBandsTglOut= 0;   %Prepare to Untoggle the button
                                                %in FBA_int
        %Warn the user they've entered an odd number of limits
        errordlg('You must enter an even number of notch limits (Min + Max For each Notch)',...
            'Incorrect Number of Inputs','modal')
        handles.okclicked = false; %- We're not going to do anything
        guidata(hObject, handles);
    else %It is a multiple of 2 then set the notch filter values
        check = check_validity(handles);
        if check == 1
            handles.CustomBandsTglOut= 1;   %Make a Note to toggle the button
                                         %in FBA_int
            handles.okclicked = true; %tell handles ok was clicked for output fcn
            %Update Handles
            guidata(hObject, handles);
            %change the visibility to call the Output_Fcn
            set(gcf,'Visible', 'off');
        else
            %Tell the user they've made a mistake
            warndlg(strcat('You have made an error. Please check that your band',...
                    'frequncy values are of the type: low1 high1 low2 high2'),...
                    'Warning Incorrct Inputs','modal')
        end
          guidata(hObject, handles);
    end %end if mod(count, 2)>0
    
else %The User has not entered any inputs
       warndlg('You have not entered any notch filters',...
            'Warning No inputs','modal')
        handles.CustomBandsTglOut= 0;   %Prepare to Untoggle the button
                                                %in FBA_int
        handles.CustomBandsOut = []; %Prepare to set the Band Values as empty
        handles.okclicked = true; %- We're going to close the window
        guidata(hObject, handles);
        set(handles.figure1,'Visible', 'off');
end %End if length(S)>0
%
% We then check that the User has entered an even number of Band values
% (an upper and lower frequency for each band)
%


function  Output = check_validity(handles)
    G =[];%Define G;
    Output = 1; %Set the Boolean to True (Pass)
    for G = 1:2:length(handles.CustomBandsOut)
        %If low limmit > high limit then its wrong!
        if (handles.CustomBandsOut(G)>handles.CustomBandsOut(G+1))
            Output = 0; %Set the Boolean to false (Fail)
        end %if
    end %for
    

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


