function muxInputChangeCallback(hObj, hDlg, tag, desc)
% muxInputChangeCallback is called when the input to MUX changes
%it sets the MUX select edit field to the appropriate value
% Copyright 2020-23 The MathWorks, Inc.

cs = hObj.getConfigSet();
tagprefix = 'Tag_ConfigSet_CoderTarget_';
ud = hDlg.userData;
fieldName = ud.Storage;
if isempty(fieldName)
    fieldName = strrep(tag, tagprefix, '');
end

target_data = codertarget.targethardware.getTargetHardware(hObj);
if isequal(target_data.SubFamily, '2837xS') || isequal(target_data.SubFamily, '2837xD')
    fileLocation = 'codertarget.tic2000.pinmap.F2837xS';
else
    fileLocation = ['codertarget.tic2000.pinmap.F' target_data.SubFamily];
end

muxTag = strrep(fieldName,'_MUX',''); %gets MUX number


muxNo = codertarget.data.getParameterValue(cs, muxTag);
muxNo = str2double(strrep(muxNo,'MUX ',''));

if isequal(target_data.SubFamily, '28p65x') && ~contains(muxTag,'CLBXBAR')
 if muxNo <= 31
     muxValueTag = append(muxTag,'_MUXSELECT1');
 else
     muxValueTag = append(muxTag,'_MUXSELECT2');
     muxNo = muxNo -32;
 end
else
   muxValueTag = append(muxTag,'_MUXSELECT'); %field that needs to be modified
end

MuxVal = codertarget.data.getParameterValue(cs, muxValueTag);
muxInputVal = hDlg.value;


if contains(muxTag,'OXBAR')
    entryList = feval([fileLocation '.getMUXInputforOutputXbar'],hObj, muxTag);
elseif contains(muxTag,'EXBAR')
    entryList = feval([fileLocation '.getMUXInputforePWMXbar'],hObj, muxTag);
else
    entryList = feval([fileLocation '.getMUXInputforClbXbar'],hObj, muxTag);
end    
newVal = entryList{muxInputVal + 1};
codertarget.data.setParameterValue(cs, fieldName, newVal);

if ~isequal(muxInputVal,0)
    MuxVal([muxNo+1]) = num2str(newVal(1));
    codertarget.data.setParameterValue(cs, muxValueTag, MuxVal);
else
    MuxVal([muxNo+1]) = 'X';
    codertarget.data.setParameterValue(cs, muxValueTag, MuxVal);
end
end