function varargout = helperBLEChannelHopSelectionUI(varargin)
%helperBLEChannelHopSelectionUI MATLAB code for helperBLEChannelHopSelectionUI.fig
%   H = helperBLEChannelHopSelectionUI returns the handle to a new GUI or
%   the handle to the existing singleton*. helperBLEChannelHopSelectionUI,
%   opens up a GUI or raises the existing singleton* that can be used for
%   configuring a channel selection algorithm and analyzing the same.
%
%   See also: GUIDE, GUIDATA, GUIHANDLES

%   Last Modified by GUIDE v2.5 16-Oct-2018 14:39:50

%   Copyright 2018 The MathWorks, Inc.

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
    'gui_Singleton', gui_Singleton, ...
    'gui_OpeningFcn', @helperBLEChannelHopSelectionUI_OpeningFcn, ...
    'gui_OutputFcn', @helperBLEChannelHopSelectionUI_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 helperBLEChannelHopSelectionUI is made visible.
function helperBLEChannelHopSelectionUI_OpeningFcn(hObject, eventdata, handles, varargin) %#ok<*INUSL>
% 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 helperBLEChannelHopSelectionUI (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes helperBLEChannelHopSelectionUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = helperBLEChannelHopSelectionUI_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 popupmenu_algorithm.
function popupmenu_algorithm_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_algorithm (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
%        popupmenu_algorithm contents as cell array
%        contents{get(hObject,'Value')} returns selected item from
%        popupmenu_algorithm
contents = cellstr(get(hObject,'String'));
algorithm = contents{get(hObject,'Value')};

switch algorithm
    case 'Channel Selection Algorithm #1'
        handles.accessAddressEdit.Enable = 'off';
        handles.accessAddressEdit.Visible = 'off';
        handles.accessAddressEdit.BackgroundColor = [0.8 0.8 0.8];
        handles.accessAddressEdit.ForegroundColor = [0.65 0.65 0.65];
        
        handles.text5.String = 'Hop Increment :';
        handles.popupmenu_hopIncrement.Enable = 'on';
        handles.popupmenu_hopIncrement.Visible = 'on';
        handles.popupmenu_hopIncrement.BackgroundColor = [1 1 1];
        handles.popupmenu_hopIncrement.ForegroundColor = [0 0 0];
        
    case 'Channel Selection Algorithm #2'
        handles.popupmenu_hopIncrement.Enable = 'off';
        handles.popupmenu_hopIncrement.Visible = 'off';
        handles.popupmenu_hopIncrement.BackgroundColor = [0.8 0.8 0.8];
        handles.popupmenu_hopIncrement.ForegroundColor = [0.65 0.65 0.65];
        
        handles.text5.String = 'Access Address :';
        handles.accessAddressEdit.Enable = 'on';
        handles.accessAddressEdit.Visible = 'on';
        handles.accessAddressEdit.BackgroundColor = [1 1 1];
        handles.accessAddressEdit.ForegroundColor = [0 0 0];
end
resetStatusTag(handles);

% --- Executes during object creation, after setting all properties.
function popupmenu_algorithm_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_algorithm (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 selection change in popupmenu_hopIncrement.
function popupmenu_hopIncrement_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_hopIncrement (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
%        popupmenu_hopIncrement contents as cell array
%        contents{get(hObject,'Value')} returns selected item from
%        popupmenu_hopIncrement

resetStatusTag(handles);

% --- Executes during object creation, after setting all properties.
function popupmenu_hopIncrement_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_hopIncrement (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 selection change in accessAddressEdit.
function accessAddressEdit_Callback(hObject, eventdata, handles)
% hObject    handle to accessAddressEdit (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 accessAddressEdit as
%        text str2double(get(hObject,'String')) returns contents of
%        accessAddressEdit as a double
accessAddress = get(hObject,'String');
validateAccessAddress(handles, accessAddress);

resetStatusTag(handles);

% --- Executes during object creation, after setting all properties.
function accessAddressEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to accessAddressEdit (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 togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (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 togglebutton1
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton2.
function togglebutton2_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton2 (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 togglebutton2
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton3.
function togglebutton3_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton3 (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 togglebutton3
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton4.
function togglebutton4_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton4 (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 togglebutton4
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton5.
function togglebutton5_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton5 (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 togglebutton5
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton6.
function togglebutton6_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton6 (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 togglebutton6
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton7.
function togglebutton7_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton7 (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 togglebutton7
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton8.
function togglebutton8_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton8 (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 togglebutton8
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton9.
function togglebutton9_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton9 (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 togglebutton9
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton10.
function togglebutton10_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton10 (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 togglebutton10
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton11.
function togglebutton11_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton11 (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 togglebutton11
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton12.
function togglebutton12_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton12 (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 togglebutton12
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton13.
function togglebutton13_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton13 (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 togglebutton13
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton14.
function togglebutton14_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton14 (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 togglebutton14
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton15.
function togglebutton15_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton15 (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 togglebutton15
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton16.
function togglebutton16_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton16 (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 togglebutton16
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton17.
function togglebutton17_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton17 (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 togglebutton17
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton18.
function togglebutton18_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton18 (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 togglebutton18
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton19.
function togglebutton19_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton19 (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 togglebutton19
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton20.
function togglebutton20_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton20 (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 togglebutton20
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton21.
function togglebutton21_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton21 (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 togglebutton21
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton22.
function togglebutton22_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton22 (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 togglebutton22
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton23.
function togglebutton23_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton23 (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 togglebutton23
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton24.
function togglebutton24_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton24 (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 togglebutton24
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton25.
function togglebutton25_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton25 (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 togglebutton25
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton26.
function togglebutton26_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton26 (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 togglebutton26
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton27.
function togglebutton27_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton27 (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 togglebutton27
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton28.
function togglebutton28_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton28 (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 togglebutton28
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton29.
function togglebutton29_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton29 (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 togglebutton29
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton30.
function togglebutton30_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton30 (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 togglebutton30
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton31.
function togglebutton31_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton31 (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 togglebutton31
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton32.
function togglebutton32_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton32 (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 togglebutton32
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton33.
function togglebutton33_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton33 (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 togglebutton33
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton34.
function togglebutton34_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton34 (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 togglebutton34
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton35.
function togglebutton35_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton35 (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 togglebutton35
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton36.
function togglebutton36_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton36 (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 togglebutton36
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on button press in togglebutton37.
function togglebutton37_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton37 (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 togglebutton37
setToggleColor(handles, hObject);
uncheckSelectAll(handles)

% --- Executes on selection change in nHopsEdit.
function nHopsEdit_Callback(hObject, eventdata, handles)
% hObject    handle to nHopsEdit (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 nHopsEdit as text
%        str2double(get(hObject,'String')) returns contents of nHopsEdit as
%        a double

value = str2double(get(hObject,'String'));
validateNumberOfHops(handles, value);
resetStatusTag(handles);

% --- Executes during object creation, after setting all properties.
function nHopsEdit_CreateFcn(hObject, eventdata, handles) %#ok<*INUSD,*DEFNU>
% hObject    handle to nHopsEdit (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 to get all toggle buttons
function obj = getToggleButtons(handles)
obj = cell(1, 37);
obj{1} = handles.togglebutton1;
obj{2} = handles.togglebutton2;
obj{3} = handles.togglebutton3;
obj{4} = handles.togglebutton4;
obj{5} = handles.togglebutton5;
obj{6} = handles.togglebutton6;
obj{7} = handles.togglebutton7;
obj{8} = handles.togglebutton8;
obj{9} = handles.togglebutton9;
obj{10} = handles.togglebutton10;
obj{11} = handles.togglebutton11;
obj{12} = handles.togglebutton12;
obj{13} = handles.togglebutton13;
obj{14} = handles.togglebutton14;
obj{15} = handles.togglebutton15;
obj{16} = handles.togglebutton16;
obj{17} = handles.togglebutton17;
obj{18} = handles.togglebutton18;
obj{19} = handles.togglebutton19;
obj{20} = handles.togglebutton20;
obj{21} = handles.togglebutton21;
obj{22} = handles.togglebutton22;
obj{23} = handles.togglebutton23;
obj{24} = handles.togglebutton24;
obj{25} = handles.togglebutton25;
obj{26} = handles.togglebutton26;
obj{27} = handles.togglebutton27;
obj{28} = handles.togglebutton28;
obj{29} = handles.togglebutton29;
obj{30} = handles.togglebutton30;
obj{31} = handles.togglebutton31;
obj{32} = handles.togglebutton32;
obj{33} = handles.togglebutton33;
obj{34} = handles.togglebutton34;
obj{35} = handles.togglebutton35;
obj{36} = handles.togglebutton36;
obj{37} = handles.togglebutton37;

% --- Executes on button press in selectAll.
function selectAll_Callback(hObject, eventdata, handles)
% hObject    handle to selectAll (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 selectAll

obj = getToggleButtons(handles);
selectAll = get(hObject,'Value');
noOfDataChannels = 37;

if selectAll
    % Number of data channels
    for i = 1:noOfDataChannels
        set(obj{i}, 'Value', 1);
        setToggleColor(handles, obj{i});
    end
else
    for i = 1:noOfDataChannels
        set(obj{i}, 'Value', 0);
        setToggleColor(handles, obj{i});
    end
end

resetStatusTag(handles);

% --- Executes on button press in visualize_checkbox.
function visualize_checkbox_Callback(hObject, eventdata, handles)
% hObject    handle to visualize_checkbox (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 visualize_checkbox

resetStatusTag(handles);

% Function to validate access address
function isValid = validateAccessAddress(handles, str)
accessAddressObj = handles.accessAddressEdit;

try
    validateattributes(str, {'char'}, {'row', 'numel', 8}, 'Access Address');
catch
    message = 'Access Address must be an 8-character long hexadecimal value (Ex. 8E89BED6)';
    setErrorTag(handles, message);
    setValidColor(accessAddressObj, false);
    isValid = false;
    return
end

for i = 1:numel(str)
    if nnz(str(i) == [(65:70) (97:102) (48:57)])
        continue
    else
        message = 'Access Address must contain only hexadecimal digits';
        setErrorTag(handles, message);
        setValidColor(accessAddressObj, false);
        isValid = false;
        return
    end
end

setValidColor(accessAddressObj, true)
resetErrorTag(handles);
isValid = true;

% Function to validate number of hops
function isValid = validateNumberOfHops(handles, value)
obj = handles.nHopsEdit;
try
    validateattributes(value, {'numeric'}, {'scalar', 'integer', ...
        '>=', 1, '<=', 65535}, 'Number of Hops');
    resetErrorTag(handles);
    setValidColor(obj, true);
    isValid = true;
catch
    message = 'Number of hops should be between [1 - 65535]';
    setErrorTag(handles, message);
    setValidColor(obj, false);
    isValid = false;
end

function setValidColor(obj, flag)
if flag
    obj.BackgroundColor = [1 1 1];
else
    obj.BackgroundColor = [0.8510    0.3490    0.4118];
end

function setErrorTag(handles, message)
errorObj = handles.errorTag;
set(errorObj, 'String', message);

function resetErrorTag(handles)
errorObj = handles.errorTag;
set(errorObj, 'String', '');

function setToggleColor(handles, hObject)
toggleState = get(hObject,'Value');

if toggleState
    hObject.BackgroundColor = [0.3020 0.7490 0.8510];
else
    hObject.BackgroundColor = [0.8 0.8 0.8];
end
resetStatusTag(handles);

function resetStatusTag(handles)
tag = handles.statusTag;
tag.String = '';

function uncheckSelectAll(handles)
obj = handles.selectAll;
obj.Value = 0;

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

obj = handles.popupmenu_algorithm;
contents = cellstr(get(obj,'String'));
algorithm = contents{get(obj,'Value')};

channelMap = (0:36);
obj = getToggleButtons(handles);
for i = 1:37
    channelMap(i) = get(obj{i},'Value');
end

channelMapLogical = logical(channelMap);
usedChannels = find(channelMapLogical) - 1;

if numel(usedChannels) < 2
    setErrorTag(handles, 'At least 2 channels must be selected as good channels');
    return
end
resetErrorTag(handles);

switch algorithm
    case 'Channel Selection Algorithm #1'
        obj = handles.popupmenu_hopIncrement;
        contents = cellstr(get(obj,'String'));
        hopIncrement = str2double(contents{get(obj,'Value')});
        
        alg = bleChannelSelection('Algorithm', 1, ...
            'HopIncrement', hopIncrement, ...
            'UsedChannels', usedChannels);
        
    case 'Channel Selection Algorithm #2'
        obj = handles.accessAddressEdit;
        accessAddress = get(obj,'String');
        isValid = validateAccessAddress(handles, accessAddress);
        if ~isValid
            return
        end
        
        alg = bleChannelSelection('Algorithm', 2, ...
            'AccessAddress', accessAddress, ...
            'UsedChannels', usedChannels);
end

obj = handles.nHopsEdit;
n = str2double(get(obj,'String'));
isValid = validateNumberOfHops(handles, n);
if ~isValid
    return
end

obj = handles.visualize_checkbox;
showHopping = get(obj,'Value');

tag = handles.statusTag;
tag.String = 'Simulation in progress....';
pause(0.1);

hopSequence = zeros(1, n);
for i = 1:n
    hopSequence(i) = alg();
end

handles.plotIndex.Value = handles.plotIndex.Value + 1;

try
    helperBLEPlotChannelHopSequence(alg, hopSequence, ...
        'PlotIndex', handles.plotIndex.Value, 'ShowHopping', showHopping);
catch ME
    errordlg(ME.message, 'Simulation failed');
    tag = handles.statusTag;
    tag.String = 'Simulation failed to run !';
    return
end

tag = handles.statusTag;
tag.String = 'Simulation completed successfully!';
