Skip to content

Commit 10dc661

Browse files
committed
More flags for addRigboxPaths
1 parent 0b38541 commit 10dc661

File tree

3 files changed

+164
-65
lines changed

3 files changed

+164
-65
lines changed

addRigboxPaths.m

Lines changed: 81 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,87 @@
1-
function addRigboxPaths(savePaths)
1+
function addRigboxPaths(varargin)
22
%ADDRIGBOXPATHS Adds the required paths for using Rigbox
3+
% addRigboxPaths([savePaths, interactive, strict]) or
4+
% addRigboxPaths('SavePaths', true, 'Interactive', true, 'Strict', true)
35
%
4-
% Part of the Rigging toolbox
6+
% Inputs (Optional):
7+
% savePaths (logical): If true, added paths are saved between sessions
8+
% interactive (logical): If true, user may be prompted for input
9+
% strict (logical): Assert toolbox & system requirments are all met
10+
%
11+
% Part of the Rigging toolbox
512
%
613
% 2014-01 CB
714
% 2017-02 MW Updated to work with 2016b
815

9-
% Flag for perminantly saving paths
10-
if nargin < 1; savePaths = true; end
16+
%%% Input validation %%%
17+
% Allow positional or Name-Value pairs
18+
p = inputParser;
19+
p.addOptional('savePaths', true)
20+
p.addOptional('interactive', true)
21+
p.addOptional('strict', true)
22+
p.parse(varargin{:});
23+
p = p.Results;
1124

1225
%%% MATLAB version and toolbox validation %%%
13-
% MATLAB must be running on Windows
14-
assert(ispc, 'Rigbox currently only works on Windows 7 or later')
15-
16-
% Microsoft Visual C++ Redistributable for Visual Studio 2015 must be
17-
% installed, check for runtime dll file in system32 folder
18-
sys32 = dir('C:\Windows\System32');
19-
assert(any(strcmpi('VCRuntime140.dll',{sys32.name})), 'Rigbox:setup:libraryRequired',...
20-
['Requires Microsoft Visual C++ Redistributable for Visual Studio 2015. ',...
21-
'Click <a href="matlab:web(''%s'',''-browser'')">here</a> to install.'],...
22-
'https://www.microsoft.com/en-us/download/details.aspx?id=48145')
23-
24-
% Check MATLAB 2017b is running
25-
assert(~verLessThan('matlab', '9.3'), 'Requires MATLAB 2017b or later')
26-
27-
% Check essential toolboxes are installed (common to both master and
28-
% stimulus computers)
29-
toolboxes = ver;
30-
requiredMissing = setdiff(...
31-
{'Data Acquisition Toolbox', ...
32-
'Signal Processing Toolbox', ...
33-
'Instrument Control Toolbox', ...
34-
'Statistics and Machine Learning Toolbox'},...
35-
{toolboxes.Name});
36-
37-
assert(isempty(requiredMissing),'Rigbox:setup:toolboxRequired',...
38-
'Please install the following toolboxes before proceeding: \n%s',...
39-
strjoin(requiredMissing, '\n'))
40-
41-
% Check that GUI Layout Toolbox is installed (required for the master
42-
% computer only)
43-
isInstalled = strcmp('GUI Layout Toolbox', {toolboxes.Name});
44-
if ~any(isInstalled) ||...
45-
str2double(strrep(toolboxes(isInstalled).Version,'.', '')) < 230
46-
warning('Rigbox:setup:toolboxRequired',...
47-
['MC requires GUI Layout Toolbox v2.3 or higher to be installed. '...
26+
if p.strict
27+
% MATLAB must be running on Windows
28+
assert(ispc, 'Rigbox currently only works on Windows 7 or later')
29+
30+
% Microsoft Visual C++ Redistributable for Visual Studio 2015 must be
31+
% installed, check for runtime dll file in system32 folder
32+
sys32 = dir('C:\Windows\System32');
33+
assert(any(strcmpi('VCRuntime140.dll',{sys32.name})), 'Rigbox:setup:libraryRequired',...
34+
['Requires Microsoft Visual C++ Redistributable for Visual Studio 2015. ',...
4835
'Click <a href="matlab:web(''%s'',''-browser'')">here</a> to install.'],...
49-
'https://uk.mathworks.com/matlabcentral/fileexchange/47982-gui-layout-toolbox')
50-
end
51-
52-
% Check that the Psychophisics Toolbox is installed (required for the
53-
% stimulus computer only)
54-
isInstalled = strcmp('Psychtoolbox', {toolboxes.Name});
55-
if ~any(isInstalled) || str2double(toolboxes(isInstalled).Version(1)) < 3
56-
warning('Rigbox:setup:toolboxRequired',...
57-
['The stimulus computer requires Psychtoolbox v3.0 or higher to be installed. '...
58-
'Click <a href="matlab:web(''%s'',''-browser'')">here</a> to install.'],...
59-
'https://github.com/Psychtoolbox-3/Psychtoolbox-3/releases')
60-
end
61-
62-
% Check that the NI DAQ support package is installed (required for the
63-
% stimulus computer only)
64-
info = matlabshared.supportpkg.getInstalled;
65-
if isempty(info) || ~any(contains({info.Name}, 'NI-DAQmx'))
66-
warning('Rigbox:setup:toolboxRequired',...
67-
['The stimulus computer requires the National Instruments support package to be installed. '...
68-
'Click <a href="matlab:web(''%s'',''-browser'')">here</a> to install.'],...
69-
'https://www.mathworks.com/hardware-support/nidaqmx.html')
36+
'https://www.microsoft.com/en-us/download/details.aspx?id=48145')
37+
38+
% Check MATLAB 2017b is running
39+
assert(~verLessThan('matlab', '9.3'), 'Requires MATLAB 2017b or later')
40+
41+
% Check essential toolboxes are installed (common to both master and
42+
% stimulus computers)
43+
toolboxes = ver;
44+
requiredMissing = setdiff(...
45+
{'Data Acquisition Toolbox', ...
46+
'Signal Processing Toolbox', ...
47+
'Instrument Control Toolbox', ...
48+
'Statistics and Machine Learning Toolbox'},...
49+
{toolboxes.Name});
50+
51+
assert(isempty(requiredMissing),'Rigbox:setup:toolboxRequired',...
52+
'Please install the following toolboxes before proceeding: \n%s',...
53+
strjoin(requiredMissing, '\n'))
54+
55+
% Check that GUI Layout Toolbox is installed (required for the master
56+
% computer only)
57+
isInstalled = strcmp('GUI Layout Toolbox', {toolboxes.Name});
58+
if ~any(isInstalled) ||...
59+
str2double(strrep(toolboxes(isInstalled).Version,'.', '')) < 230
60+
warning('Rigbox:setup:toolboxRequired',...
61+
['MC requires GUI Layout Toolbox v2.3 or higher to be installed. '...
62+
'Click <a href="matlab:web(''%s'',''-browser'')">here</a> to install.'],...
63+
'https://uk.mathworks.com/matlabcentral/fileexchange/47982-gui-layout-toolbox')
64+
end
65+
66+
% Check that the Psychophisics Toolbox is installed (required for the
67+
% stimulus computer only)
68+
isInstalled = strcmp('Psychtoolbox', {toolboxes.Name});
69+
if ~any(isInstalled) || str2double(toolboxes(isInstalled).Version(1)) < 3
70+
warning('Rigbox:setup:toolboxRequired',...
71+
['The stimulus computer requires Psychtoolbox v3.0 or higher to be installed. '...
72+
'Click <a href="matlab:web(''%s'',''-browser'')">here</a> to install.'],...
73+
'https://github.com/Psychtoolbox-3/Psychtoolbox-3/releases')
74+
end
75+
76+
% Check that the NI DAQ support package is installed (required for the
77+
% stimulus computer only)
78+
info = matlabshared.supportpkg.getInstalled;
79+
if isempty(info) || ~any(contains({info.Name}, 'NI-DAQmx'))
80+
warning('Rigbox:setup:toolboxRequired',...
81+
['The stimulus computer requires the National Instruments support package to be installed. '...
82+
'Click <a href="matlab:web(''%s'',''-browser'')">here</a> to install.'],...
83+
'https://www.mathworks.com/hardware-support/nidaqmx.html')
84+
end
7085
end
7186

7287
%%% Paths for adding
@@ -133,7 +148,7 @@ function addRigboxPaths(savePaths)
133148
end
134149

135150
%%% Validate that paths saved correctly %%%
136-
if savePaths
151+
if p.savePaths
137152
assert(savepath == 0, 'Failed to save changes to MATLAB path');
138153
if ~cbtoolsInJavaPath
139154
fseek(fid, 0, 'eof');
@@ -152,11 +167,13 @@ function addRigboxPaths(savePaths)
152167
fileExists = any(strcmp('msvcr120.dll',{sys32.name}));
153168
copied = false;
154169
if isWindowsAdmin % If user has admin privileges, attempt to copy dll file
155-
if fileExists % If there's already a dll file there prompt use to make backup
170+
if fileExists && p.interactive % If there's already a dll file there prompt use to make backup
156171
prompt = sprintf(['For signals to work propery, it is nessisary to copy ',...
157-
'the file \n<strong>', strrep(fileName, '\', '\\'), '</strong> to ',...
158-
'<strong>C:\\Windows\\System32</strong>.\n',...
172+
'the file \n<strong>', strrep(fileName, '\', '\\\\'), '</strong> to ',...
173+
'<strong>C:\\\\Windows\\\\System32</strong>.\n',...
159174
'You may want to make a backup of your existing dll file before continuing.\n\n',...
175+
'Alternatively this file is installed with ',...
176+
'Visual C++ Redistributable Packages for Visual Studio 2013\n\n',...
160177
'Do you want to proceed? Y/N [Y]: ']);
161178
str = input(prompt,'s'); if isempty(str); str = 'y'; end
162179
if strcmpi(str, 'n'); return; end % Return without copying
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
classdef (SharedTestFixtures={ % add 'fixtures' folder as test fixture
2+
matlab.unittest.fixtures.PathFixture(['..' filesep 'fixtures'])})...
3+
setScalePort_test < matlab.unittest.TestCase
4+
5+
methods (TestClassSetup)
6+
function setupFolder(testCase)
7+
% SETUPFOLDER Set up hardware scale objects
8+
% Creates a few folders for saving hardware. Adds teardowns for
9+
% deletion of these folders via ReposFixture.
10+
11+
% Set INTEST flag to true
12+
setTestFlag(true);
13+
testCase.addTeardown(@setTestFlag, false)
14+
15+
% Ensure we're using the correct test paths and add teardowns to
16+
% remove any folders we create
17+
testCase.applyFixture(ReposFixture)
18+
19+
% Now create a couple of hardware files (the rigConfig folder is
20+
% already created in the ReposFixture setup)
21+
scale = hw.WeighingScale;
22+
hwPaths = pick(dat.paths, {'rigConfig', 'globalConfig'});
23+
hwPaths{2} = [hwPaths{2} filesep hostname];
24+
assert(mkdir(hwPaths{2}), 'Failed to create extra config path')
25+
26+
for i = 1:length(hwPaths) % Save scale object into hardware files
27+
save(fullfile(hwPaths{i}, 'hardware'), 'scale');
28+
end
29+
30+
addTeardown(testCase, @ClearTestCache) % Remove folders on teardown
31+
end
32+
end
33+
34+
methods (Test)
35+
function test_setPort(testCase)
36+
% Test setting the COM port of the current rig with various input
37+
% types.
38+
39+
% Test as full string
40+
port = 'COM3';
41+
s = hw.setScalePort(port);
42+
scale = testCase.loadScale(hostname);
43+
testCase.verifyEqual(s, scale, 'Failed to return saved scale obj')
44+
testCase.verifyEqual(scale.ComPort, port, ...
45+
'Failed to set COM port as full string')
46+
47+
% Test as single char
48+
port = 'COM4';
49+
hw.setScalePort(port(end));
50+
scale = testCase.loadScale(hostname);
51+
testCase.verifyEqual(scale.ComPort, port, ...
52+
'Failed to set COM port as single char')
53+
54+
% Test as numerical
55+
port = 'COM6';
56+
hw.setScalePort(str2double(port(end)));
57+
scale = testCase.loadScale(hostname);
58+
testCase.verifyEqual(scale.ComPort, port, ...
59+
'Failed to set COM port as double')
60+
end
61+
62+
function test_rigNameInput(testCase)
63+
% Test setting the COM port of a specific rig with lower case COM
64+
% port char array
65+
port = 'COM6';
66+
[~, name] = fileparts(getOr(dat.paths, 'rigConfig'));
67+
hw.setScalePort(lower(port), name);
68+
scale = testCase.loadScale(name);
69+
testCase.verifyEqual(scale.ComPort, port, ...
70+
'Failed to set COM port as double')
71+
end
72+
end
73+
74+
methods (Static)
75+
function scale = loadScale(rigName)
76+
% LOADSCALE Load the scale object from the hardware file for rigName
77+
if nargin == 0, rigName = hostname; end
78+
hwPath = fullfile(getOr(dat.paths, 'globalConfig'), rigName, 'hardware');
79+
scale = pick(load(hwPath), 'scale');
80+
end
81+
end
82+
end

tests/fixtures/ReposFixture.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
function setup(fixture)
99
% SETUP Ensure correct test paths and create rig config folder
1010
import matlab.unittest.fixtures.PathFixture
11-
fixture.applyFixture(PathFixture('fixtures'))
11+
fixture.applyFixture(PathFixture(fileparts(mfilename('fullpath'))))
1212

1313
% Check paths file
1414
assert(endsWith(which('dat.paths'),...

0 commit comments

Comments
 (0)