summaryrefslogtreecommitdiffstats
path: root/Kod/bilbana/IOFunctions/terminate.m
blob: 26a43f21523932730104aaa22c22388c5a1a467c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function [] = terminate(track)
%TERMINATE Stops all counters associated with specified track.
%   Stops all counters associated with a specified track, and clears the
%   resources. 
%
%   If both tracks are terminated as a result of a call on terminate() all
%   resources, including the global variables, will be cleared and the
%   PCI6602 card will be reset to original state.
%
%  Tobias Lindell 2013-02-12

global mytaskh
global lib

switch nargin
    case 1
        if isempty(mytaskh)
            disp('User needs to initialize counters before terminating!')
            clearvars -global mytaskh lib
            return
        end
        
        
        switch track
            case 1
                % Check if track 1 counter has been initialized, stop program if not
                if isfield(mytaskh,'ctr_0')
                    % Clear counters of selcted track
                    calllib(lib,'DAQmxClearTask',mytaskh.ctr_0);
                    calllib(lib,'DAQmxClearTask',mytaskh.ctr_1);
                    calllib(lib,'DAQmxClearTask',mytaskh.ctr_2);
                    calllib(lib,'DAQmxClearTask',mytaskh.ctr_3);
                    clear mytaskh.ctr_0
                    clear mytaskh.ctr_1
                    clear mytaskh.ctr_2
                    clear mytaskh.ctr_3
                    mytaskh = rmfield(mytaskh,'ctr_0');
                    mytaskh = rmfield(mytaskh,'ctr_1');
                    mytaskh = rmfield(mytaskh,'ctr_2');
                    mytaskh = rmfield(mytaskh,'ctr_3');
                    % Check to see if counters of other track are cleared, if so
                    % reset card to original state and clear global variables
                    if ~isfield(mytaskh,'ctr_4')
                        disp('Everything terminated. Device will reset!')
                        calllib(lib,'DAQmxResetDevice','Dev1');
                        clearvars -global mytaskh lib
                    end
                    
                else
                    disp(['User needs to initialize counters for track ',num2str(track),' before terminating!'])
                    return
                end
            case 2
                if isfield(mytaskh,'ctr_4')
                    calllib(lib,'DAQmxClearTask',mytaskh.ctr_4);
                    calllib(lib,'DAQmxClearTask',mytaskh.ctr_5);
                    calllib(lib,'DAQmxClearTask',mytaskh.ctr_6);
                    calllib(lib,'DAQmxClearTask',mytaskh.ctr_7);
                    clear mytaskh.ctr_4
                    clear mytaskh.ctr_5
                    clear mytaskh.ctr_6
                    clear mytaskh.ctr_7
                    mytaskh = rmfield(mytaskh,'ctr_4');
                    mytaskh = rmfield(mytaskh,'ctr_5');
                    mytaskh = rmfield(mytaskh,'ctr_6');
                    mytaskh = rmfield(mytaskh,'ctr_7');
                    if ~isfield(mytaskh,'ctr_0')
                        disp('Everything terminated. Device will reset!')
                        calllib(lib,'DAQmxResetDevice','Dev1');
                        clearvars -global mytaskh lib
                    end
                else
                    disp(['User needs to initialize counters for track ',num2str(track),' before terminating!'])
                    return
                end
            otherwise
                disp('Wrong track argument sent to terminate(track)! Should be 1 or 2!')
        end
    otherwise
        disp('Wrong number of arguments sent to terminate(track)! Should be 1!')
end
end