-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb1208FSPlusClass.m
309 lines (267 loc) · 10.8 KB
/
usb1208FSPlusClass.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
classdef usb1208FSPlusClass < singletonClass ...
& ttlHardwareAbstractClass ...
& daqHardwareAbstractClass ...
& storedPreferenceClass ...
& classThatThrowsWarnings
% USB-1208FS-Plus Interface
%
% Mac and Linux systems use libusb-1.0 to communicate with the USB
% device. Compiled version of multiple c++ mex files required, see
% README and Makefile for details.
%
properties (SetObservable,AbortSet)
strobeDelay % set to zero to use minimum latency strobe
AIChannelMode % Either SE or DIFF
end
properties (Hidden=true)
dev % the device, set by openInterface method
end
properties (Constant,Hidden=true)
singletonDesignatorKey = 'usb1208FSPlusClass';
prefFileNameStr = 'usb1208FSPlusClass';
archWarningID = 'usb1208FSPlusClass:ARCH';
statusWarningID = 'usb1208FSPlusClass:badStatus';
end
properties (Hidden=true)
% For error/warning messages
useGUI = true;
end
properties % AI Calibration and Conversion properties
nAI
AIMaxCount
range
slope
offset
end
properties (Constant)
supportedArchs = {'GLNX86','GLNXA64','MACI64'};
% Define control signal locations on DIO A
CTRLDIO = 0;
STARTBIT = 0;
STOPBIT = 1;
STROBEBIT = 2;
% Designate DIO B for event word
EVNTDIO = 1;
EVNTBITS = 8;
% 12 bit AO channels
AOMAXCOUNT = 2^12 - 1;
AORANGE = [0 5]; % Volts
end
methods (Static)
% Provide methods to build command strings to control the digital
% and analog ports
function cmdStr = makeDOSetValueCommand(port,bit,value)
% Format the command string to set a DIO port/bit
if isempty(bit)
cmdStr = sprintf('DIO{%i}:VALUE=%i',port,value);
else
cmdStr = sprintf('DIO{%i/%i}:VALUE=%i',port,bit,value);
end
end
function cmdStr = makeAOSetValueCommand(channel,value)
cmdStr = sprintf('AO{%i}:VALUE=%i',channel,value);
end
end
methods
function obj = usb1208FSPlusClass()
% Open the USB-1208FS-Plus interface - dev property will be
% empty the first time the constructor is called. In subsequent
% calls it will not be empty due to the singleton superclass
% constructor
if obj.singletonNeedsConstruction
obj.dev = 0;
obj.strobeDelay = ttlHardwareAbstractClass.strobeTime;
obj.preferencePropertyNames = {'strobeDelay',...
'AIChannelMode'};
switch computer
case obj.supportedArchs
obj.openInterface;
otherwise
ME = MException(obj.archWarningID,...
'Unsupported Computer');
errorMsg = sprintf('%s: %s is an unsupported computer archtecture',...
class(obj),computer);
handleError(ME,true,...
errorMsg);
end
obj.loadSavedPreferences;
obj.listenForPreferenceChanges;
end
end
function delete(obj)
if obj.isConstructed
obj.hideWarnings(false);
if obj.dev && obj.validateInterface
obj.closeInterface;
end
end
end
% TTL Interface methods
function startRecording(obj)
% sendMessage(obj,obj.makeDOSetValueCommand(obj.CTRLDIO,...
% obj.STARTBIT,1));
usb1208FSPlusStartRecording(obj.dev);
end
function stopRecording(obj)
% sendMessage(obj,obj.makeDOSetValueCommand(obj.CTRLDIO,...
% obj.STARTBIT,0));
usb1208FSPlusStopRecording(obj.dev);
end
function strobeEventWord(obj,value)
usb1208FSPlusSetEvent(obj.dev,value);
strobe(obj);
end
function setEventWord(obj,value)
usb1208FSPlusSetEvent(obj.dev,value);
end
function strobe(obj)
if obj.strobeDelay > 0
usb1208FSPlusSetStrobe(obj.dev,1);
WaitSecs(obj.strobeDelay);
usb1208FSPlusSetStrobe(obj.dev,0);
else
usb1208FSPlusStrobe(obj.dev,0);
end
end
function isOpen = openInterface(obj)
if obj.dev && obj.validateInterface
% If the dev is already open and valid, do nothing
isOpen = true;
else
% Open and validate the dev
obj.dev = usb1208FSPlusOpenInterface();
isOpen = obj.validateInterface();
end
end
function closeInterface(obj)
usb1208FSPlusCloseInterface(obj.dev);
obj.dev = 0;
end
function status = setAnalogLevel(obj,value)
if value > 1
errordlg('usb1208FSPlusClass.setAnalogLevel: max value 1');
end
status = usb1208FSPlusSetAnalog(obj.dev,obj.aoChannel,value);
end
function returnValue = sendMessage(obj,message)
[status,returnValue] = usb1208FSPlusSendMessage(obj.dev,message);
if status ~= 1
ME = MException(obj.statusWarningID,...
returnValue);
errStr = sprintf('sendMessage(''%s'') failed',...
message);
errStr = sprintf('%s.%s\n',class(obj),errStr);
handleWarning(ME,obj.useGUI,errStr);
end
end
function isValid = validateInterface(obj)
if obj.dev == 0
isValid = 0;
else
% Send a command and check to make sure that the DAQ
% responds as expected
[status,returnValue] = usb1208FSPlusSendMessage(obj.dev,...
'?DIO');
isValid = status == 1 && strcmp(returnValue,'DIO=2');
end
end
function hideWarnings(obj,trueOrFalse)
hideWarnings@classThatThrowsWarnings(obj,trueOrFalse);
obj.useGUI = ~obj.warningsAreSuppressed;
end
% -----------------------------------------------------------------
% DAQ Interface Methods
function status = writeAnalog(obj,channel,value)
if value > obj.AOMAXCOUNT
error('%s.writeAnalog: max value is %i',...
class(obj),obj.AOMAXCOUNT);
end
msg = obj.makeAOSetValueCommand(channel,value);
status = obj.sendMessage(msg);
end
function status = setAnalogOutVoltage(obj,channel,value)
countValue = floor(obj.AOMAXCOUNT * value / obj.AORANGE(2));
status = obj.writeAnalog(channel,countValue);
end
function value = readAnalog(obj,channel)
% cmdStr = sprintf('?AI{%i}:VALUE',channel);
% [~,rtrnValue] = obj.sendMessage(cmdStr);
% data = sscanf(rtrnValue,'AI{%i}:VALUE=%i');
% value = data(2);
[status,value,respStr] = usb1208FSPlusGetAI(obj.dev,channel);
if status ~= 1
if status ~= 1
ME = MException(obj.statusWarningID,respStr);
errStr = sprintf('%s.readAnalog failed\n',class(obj));
handleWarning(ME,obj.useGUI,errStr);
end
end
end
function scaleValue = readAnalogVoltage(obj,channel)
value = obj.readAnalog(channel);
calData = value * obj.slope(channel+1) + obj.offset(channel+1);
calData = max([calData 0]);
calData = min([calData obj.AIMaxCount]);
totalRange = diff(obj.range(channel+1,:));
scaleValue = calData * totalRange / (obj.AIMaxCount + 1);
% Note: following line is a hack that seems to work for DIFF
% mode. Not sure why. Also doesn't seem to work for SE mode.
scaleValue = scaleValue - totalRange / 2;
% fprintf('value = %d, scaled = %d\n',value,scaleValue);
end
function status = writeDigital(obj,port,bit,value)
msg = obj.makeDOSetValueCommand(port,bit,value);
status = obj.sendMessage(msg);
end
function data = readDigital(obj,varargin)
data = [];
end
function set.AIChannelMode(obj,mode)
switch mode
case {'DIFF' 'SE'}
obj.AIChannelMode = mode;
obj.configureAI;
otherwise
error('%s.AIChannelMode must be either SE or DIFF');
end
end
function triggerON(obj,port,bit)
cmd = obj.makeDOSetValueCommand(port,bit,1);
obj.sendMessage(cmd);
end
function triggerOFF(obj,port,bit)
cmd = obj.makeDOSetValueCommand(port,bit,0);
obj.sendMessage(cmd);
end
function configureAI(obj)
obj.sendMessage(sprintf('AI:CHMODE=%s',obj.AIChannelMode));
rtrnStr = obj.sendMessage('?AI:CHMODE');
chMode = sscanf(rtrnStr,'AI:CHMODE=%s');
switch chMode
case 'DIFF'
obj.AIMaxCount = 2^12 - 1; % 4 12-bit channels
case 'SE'
obj.AIMaxCount = 2^11 - 1; % 8 11-bit channels
end
rtrnStr = obj.sendMessage('?AI');
obj.nAI = sscanf(rtrnStr,'AI=%i');
obj.range = zeros(obj.nAI,2);
obj.slope = zeros(1,obj.nAI);
obj.offset = zeros(1,obj.nAI);
for iA = 0:obj.nAI-1
rtrnStr = sendMessage(obj,sprintf('?AI{%i}:RANGE',iA));
rangeStr = sscanf(rtrnStr,sprintf('AI{%i}:RANGE=%%s',iA));
switch rangeStr
case 'BIP10V'
obj.range(iA+1,:) = [-10 10];
case 'BIP20V'
obj.range(iA+1,:) = [-20 20];
end
rtrnStr = sendMessage(obj,sprintf('?AI{%i}:SLOPE',iA));
obj.slope(iA+1) = sscanf(rtrnStr,sprintf('AI{%i}:SLOPE=%%f',iA));
rtrnStr = sendMessage(obj,sprintf('?AI{%i}:OFFSET',iA));
obj.offset(iA+1) = sscanf(rtrnStr,sprintf('AI{%i}:OFFSET=%%f',iA));
end
end
end
end