function varargout = sf_rf24network_init_cb(varargin)
% sf_rf24network_init_cb
    
% Copyright 2015 - 2016 The MathWorks, Inc.
    
    if nargout == 0
        feval(varargin{:});
    else
        [varargout{1:nargout}] = feval(varargin{:});
    end
end

function [ds, nodeAddress] = init(blk, pOctalAddress) %#ok<DEFNU>
    nodeAddress = rf24.oct2dec(pOctalAddress);
    setNetworkChannel = strcmp('on', get_param(blk, 'pChooseNetworkChannel'));
    if setNetworkChannel
        nwStr = sprintf('Channel: #%s', get_param(blk, 'pNetworkChannel'));
    else
        nwStr = 'Channel: <default>';
    end
    ds=sprintf('RF24Network Init\nAddr: %s (%d)\n%s', ...
        get_param(blk, 'pOctalAddress'), nodeAddress, nwStr);
    %     set_param(blk, 'MaskDisplay', dstr);
end

function choose_network_ch(blk) %#ok<DEFNU>
    setNetworkChannel = strcmp('on', get_param(blk, 'pChooseNetworkChannel'));
    idx = getIdxForName(blk, 'pNetworkChannel');
    enabs = get_param(blk, 'MaskEnables');
    needsSet = false;
    if setNetworkChannel
        if strcmp('off', enabs{idx});
            enabs{idx} = 'on';
            needsSet = true;
        end
    else
        if strcmp('on', enabs{idx});
            enabs{idx} = 'off';
            needsSet = true;
        end        
    end
    if needsSet
        set_param(blk, 'MaskEnables', enabs);
    end
    
end

function idx = getIdxForName(blk, name)
   names = get_param(blk, 'MaskNames');
   idx = find(strcmp(name, names), 1);
end