-
Notifications
You must be signed in to change notification settings - Fork 1
/
NiPulseMultiOutput.m
296 lines (267 loc) · 7.89 KB
/
NiPulseMultiOutput.m
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
classdef NiPulseMultiOutput < hgsetget
% ---------------------------------------------------------------------
% NiPulseMultiOutput
% Han Lab
% 1/19/2013
% Mark Bucklin
% ---------------------------------------------------------------------
%
% This class provides an interface to the National Instruments
% Multifunction I/O board, NI USB-6259 (by default) for use with
% experimental instruments (e.g. nosepoke, LEDs, buzzers, speakers,
% etc.) As opposed to the NIDAQINTERFACE class, this class utilizes the
% session-based interface which affords compatibility with 64-bit
% versions of matlab.
%
% Note: Changed 12/2014 to also be more generally configured as an analog output.
%
% See Also TOUCHINTERFACE TOUCHDISPLAY NIDAQINTERFACE
properties % Base Properties (common to Analog & Digital types)
type % 'analog' or 'digital'
pulseTime % in seconds
deviceId = 1
channelId
default
end
properties % Digital Output
activeHigh % true if pulse goes high for pulseTime, then low until called again
portNumber
lineNumber
end
properties % Analog Output
aoNumber
pulseVal
end
properties (SetAccess = protected)
sessionObj
channelObj
deviceObj
subSystemObj
timerObj
isanalog
isrunning
isready
end
events
end
methods % Initialization
function obj = NiPulseMultiOutput(varargin)
% Assign input arguments to object properties
if nargin > 1
for k = 1:2:length(varargin)
obj.(varargin{k}) = varargin{k+1};
end
end
% Define Defaults
obj.default = struct(...
'type','digital',...
'deviceId',1,...
'pulseTime',.010,...
'activeHigh',true,...
'portNumber',0,...
'lineNumber',0,...
'aoNumber',0,...
'pulseVal',1);
obj.isready = false;
end
function setup(obj)
global ANALOGSESSION
global DIGITALSESSION
% Fill in Defaults
props = fields(obj.default);
for n=1:length(props)
thisprop = sprintf('%s',props{n});
if isempty(obj.(thisprop))
obj.(thisprop) = obj.default.(thisprop);
end
end
if strncmpi('analog', obj.type, 1)
obj.isanalog = true;
else
obj.isanalog = false;
end
% Check/Query Device Selection
obj.queryDevice();
% Check/Query Channel Selection
obj.queryChannel();
% Make Daq Session and Analog- or Digital-Output Channels the set Initial State
if obj.isanalog
% Configure as ANALOG OUTPUT
if isempty(ANALOGSESSION)
obj.sessionObj = daq.createSession('ni');
ANALOGSESSION = obj.sessionObj;
else
obj.sessionObj = ANALOGSESSION;
end
obj.channelObj = obj.sessionObj.addAnalogOutputChannel(obj.deviceId,...
obj.channelId, 'Voltage');%sprintf('ao%i',obj.aoNumber)
obj.sessionObj.outputSingleScan(zeros(1,numel(obj.sessionObj.Channels)));
else
% Configure as DIGITAL OUTPUT
% for n = 1:numel(obj.lineNumber) todo this part does not work
if isempty(DIGITALSESSION)
obj.sessionObj = daq.createSession('ni');
DIGITALSESSION = obj.sessionObj;
else
% DIGITALSESSION = DIGITALSESSION.addDigitalChannel('Dev1','port1/line0','OutputOnly'); % added by Mike
obj.sessionObj = DIGITALSESSION;
end
obj.channelObj = obj.sessionObj.addDigitalChannel(...
obj.deviceId,...
obj.channelId,...
'OutputOnly'); sprintf('port%g/line%g',obj.portNumber,obj.lineNumber);
if obj.activeHigh %start low if activeHigh is true
obj.sessionObj.outputSingleScan(zeros(1,numel(obj.sessionObj.Channels)));
else
obj.sessionObj.outputSingleScan(ones(1,numel(obj.sessionObj.Channels)));
end
end
% Ready
obj.isready = true;
end
function varargout = queryDevice(obj)
% Display available Device info
devs = daq.getDevices;
vendors = cat(1,devs.Vendor);
devs = devs(strncmp({vendors.ID}','ni',2));
if isempty(devs)
error('NiPulseMultiOutput:NoDevicesFound','No National-Instruments devices found')
end
devsel = strcmpi(obj.deviceId, {devs.ID});
if ~any(devsel)
if numel(devs) > 1
[devsel,ok] = listdlg('PromptString','Select a Data-Acquisition Device',...
'SelectionMode','single',...
'ListSize', [350 200],...
'ListString', {devs.Description}');
if ~ok, return, end
end
end
obj.deviceObj = devs(max(devsel,1));
obj.deviceId = obj.deviceObj.ID;
if nargout
varargout{1} = obj.deviceId;
end
end
function varargout = queryChannel(obj)
if isempty(obj.deviceObj)
queryDevice();
end
subsys = obj.deviceObj.Subsystems;
if obj.isanalog
obj.subSystemObj = subsys(strcmpi('analogoutput',{subsys.SubsystemType}));
chanNames = obj.subSystemObj.ChannelNames;
[aochans, count] = sscanf(strcat(chanNames{:}), 'ao%f');
chansel = (aochans==obj.aoNumber);
if ~any(chansel)
[chansel,ok] = listdlg('PromptString','Select an Analog-Output Channel (NiPulseMultiOutput)',...
'SelectionMode','single',...
'ListSize', [100 count*30],...
'ListString', chanNames);
if ok
obj.aoNumber = aochans(chansel);
else
return
end
end
else
obj.subSystemObj = subsys(strcmpi('digitalio',{subsys.SubsystemType}));
chanNames = obj.subSystemObj.ChannelNames;
[portlinepair, count] = sscanf(strcat(chanNames{:}), 'port%f/line%f');
ports = portlinepair(1:2:end);
lines = portlinepair(2:2:end);
chansel = (ports==obj.portNumber) & (lines==obj.lineNumber);
if ~any(chansel)
[~,portsort] = sort(100*(ports+1) + lines);
chanNames = chanNames(portsort);
[chansel,ok] = listdlg('PromptString','Select a Digital-Output Channel (NiPulseMultiOutput)',...
'SelectionMode','single',...
'ListSize', [150 count*30],...
'ListString', chanNames);
if ok
obj.portNumber = ports(portsort(chansel));
obj.lineNumber = lines(portsort(chansel));
else
return
end
end
end
obj.channelId = chanNames{chansel};
if nargout
varargout{1} = obj.channelId;
end
end
end
methods % Input/Output Callback Functions
function sendPulse(obj, pulseOnTime, nPulses, pulseOnVal)
% Using MATLAB timer to manually pulse signal on for a specified (or default) amount of time
% >> obj.sendPulse()
% >> obj.sendPulse(.5) - half-second pulse
% >> obj.sendPulse(.1, 5) - pulse train of five one-tenth-second pulses (1/5th second with
% 50% duty cycle)
% >> obj.sendPulse(.25, 1, 6) - quarter-second pulse to +6-volts (if signal is analog)
if nargin <= 1
pulseOnTime = obj.pulseTime;
end
if nargin <= 2
nPulses = 1;
end
if nargin <= 3
if obj.isanalog
pulseOnVal = obj.pulseVal;
else
pulseOnVal = obj.activeHigh;
end
end
if nPulses > 1
followFcn = @(~,~)sendPulse(obj, pulseOnTime, nPulses-.5, ~pulseOnVal);
else
followFcn = @deleteTimerFcn;
end
if isempty(obj.timerObj) || ~isvalid(obj.timerObj)
obj.timerObj = timer(...
'ExecutionMode', 'singleShot',...
'StartDelay', pulseOnTime,...
'TimerFcn', @(src,evnt)resetPulse(obj,src,evnt),...
'TasksToExecute', 1,...
'StopFcn', followFcn);
else
obj.timerObj.StartDelay = pulseOnTime;
obj.timerObj.TimerFcn = @(src,evnt)resetPulse(obj,src,evnt);
obj.timerObj.StopFcn = followFcn;
end
obj.sessionObj.outputSingleScan(pulseOnVal); % (set high)
start(obj.timerObj);
end
function resetPulse(obj,src,evnt)
obj.sessionObj.outputSingleScan(~obj.activeHigh); % (reset low)
end
function resetPulseAnalog(obj)
obj.sessionObj.outputSingleScan(0); % (reset low)
end
end
methods % Cleanup
function delete(obj)
global ANALOGSESSION
global DIGITALSESSION
if obj.isanalog
ANALOGSESSION = [];
else
DIGITALSESSION = [];
end
delete(obj.timerObj)
if isvalid(obj.sessionObj)
obj.sessionObj.release()
delete(obj.sessionObj);
end
end
end
methods (Static)
function storeCurrentState(id,state)
persistent currentState
end
end
end
function deleteTimerFcn(src,evnt)
delete(src);
end