Skip to content
Snippets Groups Projects
Commit e460f3e4 authored by Tim Stadtmann's avatar Tim Stadtmann
Browse files

Simplified mode/type test functions

They are now completely independent on the actual sensor type/mode handled.
If a new, unknown sensor shall be added, its type and modes now only
need to be specified in the corresponding enumerations and nowhere else.
This somehow solves issue #43.
parent d98f1396
No related branches found
No related tags found
No related merge requests found
......@@ -7,45 +7,18 @@ function mode = DeviceMode(type, modeNo)
end
try
switch type
case DeviceType.NXTTouch
mode = DeviceMode.NXTTouch(modeNo);
case DeviceType.NXTLight
mode = DeviceMode.NXTLight(modeNo);
case DeviceType.NXTSound
mode = DeviceMode.NXTSound(modeNo);
case DeviceType.NXTColor
mode = DeviceMode.NXTColor(modeNo);
case DeviceType.NXTUltraSonic
mode = DeviceMode.NXTUltraSonic(modeNo);
case DeviceType.NXTTemperature
mode = DeviceMode.NXTTemperature(modeNo);
case DeviceType.LargeMotor
mode = DeviceMode.Motor(modeNo);
case DeviceType.MediumMotor
mode = DeviceMode.Motor(modeNo);
case DeviceType.Touch
mode = DeviceMode.Touch(modeNo);
case DeviceType.Color
mode = DeviceMode.Color(modeNo);
case DeviceType.UltraSonic
mode = DeviceMode.UltraSonic(modeNo);
case DeviceType.Gyro
mode = DeviceMode.Gyro(modeNo);
case DeviceType.InfraRed
mode = DeviceMode.InfraRed(modeNo);
case DeviceType.HTColor
mode = DeviceMode.HTColor(modeNo);
case DeviceType.HTCompass
mode = DeviceMode.HTCompass(modeNo);
case DeviceType.HTAccelerometer
mode = DeviceMode.HTAccelerometer(modeNo);
otherwise
mode = DeviceMode.Default.Undefined; % Need to think about this...
% Motors are a special case where the mode name does not equal the type name
if type == DeviceType.LargeMotor || type == DeviceType.MediumMotor
mode = DeviceMode.Motor(modeNo);
else
mode = DeviceMode.(char(type))(modeNo);
end
catch ME
if strcmp(ME.identifier,'MATLAB:undefinedVarOrClass')
mode = DeviceMode.Default.Undefined; % Need to think about this...
else
error('ModeNo ''%d'' not valid for given type.', modeNo);
end
catch
error('MATLAB:RWTHMindstormsEV3:noclass:DeviceMode:invalidInputValue',...
'ModeNo ''%d'' not valid for given type.', modeNo);
end
end
function isValid = isModeValid(mode, type)
% Returns whether given mode is a valid mode in given type.
isValid = true;
if strcmp(class(mode), 'DeviceMode.Default')
return;
end
switch type
case DeviceType.NXTTouch
if ~strcmp(class(mode), 'DeviceMode.NXTTouch')
isValid = false;
end
case DeviceType.NXTLight
if ~strcmp(class(mode), 'DeviceMode.NXTLight')
isValid = false;
end
case DeviceType.NXTSound
if ~strcmp(class(mode), 'DeviceMode.NXTSound')
isValid = false;
end
case DeviceType.NXTColor
if ~strcmp(class(mode), 'DeviceMode.NXTColor')
isValid = false;
end
case DeviceType.NXTUltraSonic
if ~strcmp(class(mode), 'DeviceMode.NXTUltraSonic')
isValid = false;
end
case DeviceType.NXTTemperature
if ~strcmp(class(mode), 'DeviceMode.NXTTemperature')
isValid = false;
end
case DeviceType.LargeMotor
if ~strcmp(class(mode), 'DeviceMode.Motor')
isValid = false;
end
case DeviceType.MediumMotor
if ~strcmp(class(mode), 'DeviceMode.Motor')
isValid = false;
end
case DeviceType.Touch
if ~strcmp(class(mode), 'DeviceMode.Touch')
isValid = false;
end
case DeviceType.Color
if ~strcmp(class(mode), 'DeviceMode.Color')
isValid = false;
end
case DeviceType.UltraSonic
if ~strcmp(class(mode), 'DeviceMode.UltraSonic')
isValid = false;
end
case DeviceType.Gyro
if ~strcmp(class(mode), 'DeviceMode.Gyro')
isValid = false;
end
case DeviceType.InfraRed
if ~strcmp(class(mode), 'DeviceMode.InfraRed')
isValid = false;
end
case DeviceType.HTColor
if ~strcmp(class(mode), 'DeviceMode.HTColor')
isValid = false;
end
case DeviceType.HTCompass
if ~strcmp(class(mode), 'DeviceMode.HTCompass')
isValid = false;
end
case DeviceType.HTAccelerometer
if ~strcmp(class(mode), 'DeviceMode.HTAccelerometer')
isValid = false;
end
otherwise
isValid = false;
end
end
% A mode is valid for a given type if the name of the type (e.g. Color)
% equals the name of the enumeration corresponding to the mode (e.g. Color in
% DeviceMode.Color.Ambient)
isValid = strcmp(char(type), strrep(class(mode), 'DeviceMode.', ''));
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment