function relay = mccUsbErb24(arg1)
% Constructor for the class representing an Measurement Computing relay
% card, USB-ERB24. This class uses the DAQ object's UserData to store
% information about this object that isn't already embedded in the DAQ
% object.
%
%
% Behavior:
%
% relayCard = mccUsbErb24(); % object is initialized with the first
% % constructor from
% % daqhwinfo('mcc','ObjectConstructorName')
% % whose BoardName is 'USB-ERB24'
% % same as relayCard = mccUsbErb24(1);
%
% relayCard = mccUsbErb24(i); % returns an initialized relay object, the
% % non-empty constructor from daqhwinfo
% % indexed by the input number whose
% % BoardName is also 'USB-ERB24'.
%
% relayCard = mccUsbErb24(daqObj); % creates the object with the given
% % relay card w/re-initialized lines.
% % Throws an error if it's not a
% % USB-ERB24.
%
% on(relayCard,1:24); % turns on all relays
% off(relayCard,2:2:24); % turns off even-numbered relays
% off(relayCard,1); % turns off first relay
% off(relayCard,1:24); % turns off all relays
%
% clear relayCard % Removes var from workspace, but retains hardware
% and all information in this session. An identical object can be created
% by calling this constructor on the daq object directly, provided the
% object's UserData hasn't been externally modified.
%
% TODO: for the first two uses, check daqfind() for valid cards before
% constructing them from scratch.
boardName = 'USB-ERB24';
%relay.devObj = [];
switch nargin
case 0
relay = mccUsbErb24(1);
return
case 1
if isnumeric(arg1)
idx = arg1; % it's an index into the constructors
if idx < 1
error('Relay card index must be an integer >= 1')
end
relay = mccUsbErb24(initMccUsbErb24(idx));
return
elseif isa(arg1,'mccUsbErb24')
relay = arg1;
return
elseif isa(arg1,'digitalio') && strcmp(daqhwinfo(arg1, 'DeviceName'), boardName)
relay.devObj = initLines(arg1);
else
error('mccUsbErb24: first argument must be a number or digitalio object of ''USB-ERB24''')
end
otherwise
error('Constructor expects 0 or 1 argument. %d were given.', nargin)
end
% TODO: maybe inherit from the DAQ object instead of just embedding it?
% relay = class(relay, 'mccUsbErb24', relay.devObj)
relay = class(relay, 'mccUsbErb24');
off(relay,1:24);
%% initMccUsbErb24()
function relayCard = initMccUsbErb24(idx)
% Initializes the DAQ hardware
mccHardware = daqhwinfo('mcc');
constructorIdx = strcmp(mccHardware.BoardNames, 'USB-ERB24');
constructors = mccHardware.ObjectConstructorName(constructorIdx, :); % get only valid cards
emptyRowIdx = all(cellfun('isempty',constructors),2);
constructors = constructors(~emptyRowIdx, :); % eliminate rows that are entirely empty
if isempty(constructors)
error('Could not find any USB-ERB24 relay cards. Check hardware is installed and restart MATLAB.')
elseif idx > size(constructors,1)
error('Only %d USB-ERB24 relay card(s) are installed. The index %d is too high.', size(constructors,1),idx)
end
% Now construct it
constructor = [constructors{idx,:}];
relayCard = eval(constructor);
relayCard = initLines(relayCard);
%% initLines()
function relayCard = initLines(relayCard)
% If lines exist, then delete them
if length(relayCard.Line) > 0
delete(relayCard.Line)
end
% Now setup the lines to make sense
addline(relayCard, 0:7, 2, 'out'); % configure relays 1-8 as outputs
addline(relayCard, 0:7, 3, 'out'); % configure relays 9-16 as outputs
addline(relayCard, 0:3, 0, 'out'); % configure relays 17-20 as outputs
addline(relayCard, 0:3, 1, 'out'); % configure relays 12-24 as outputs