File: ColorCal2.m

package info (click to toggle)
psychtoolbox-3 3.0.14.20170103%2Bgit6-g605ff5c.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 103,044 kB
  • ctags: 69,483
  • sloc: ansic: 167,371; cpp: 11,232; objc: 4,708; sh: 1,875; python: 383; php: 344; makefile: 207; java: 113
file content (373 lines) | stat: -rw-r--r-- 12,962 bytes parent folder | download | duplicates (2)
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
function varargout = ColorCal2(command, varargin)
% varargout = ColorCal2(command, [varargin])
%
% Description:
% Interface function to communicate with the ColorCal2 USB device.
%
% LINUX: If you want to use this function without the need to run
% Matlab or Octave as root user (i.e., without need for root login or the
% sudo command), please copy the file ...
% Psychtoolbox/PsychHardware/ColorCal2/60-cambridgeresearch-permissions.rules
% ... into the folder /etc/udev/rules.d/ on your system. This one time copy will
% require administrator privileges, but after that, any user should be able
% to use the ColorCal devices or other CRS Ltd. devices without special permissions.
%
%
% Required Input:
% command (string) - Command to send to the ColorCal2 device.  Commands are
%                    case insensitive.
%
% Optional Input:
% varargin - Argument(s) required for a subset of the ColorCal2
%            commands.  Varies depending on the command.
%
% Optional Output:
% varargout - Value(s) returned for a subset of the ColorCal2 commands.
%
% Command List:
% 'DeviceInfo' - Retrieves the following device information in a struct: firmware
%      version number, 8 digit serial number, and firmware build number.
%      The struct's fields are romVersion, serialNumber, buildNumber.
%
%      Example:
%      devInfo = ColorCal2('DeviceInfo');
% 'GetRawData' - Returns the raw data for all three light channels, the
%      contents of the zero correction array for all three channels, and
%      the current reading of the trigger ADC.  Returns a single struct
%      containing the following fields: Xdata, Xzero, Ydata, Yzero, Zzero,
%      Trigger.  All values are unformatted.
% 'LEDOn' - Turns the LED on.
% 'LEDOff' - Turns the LED off.
% 'MeasureXYZ' - Measures the tri-stimulus value of the current light.
%      Returns a struct with x, y, and z in floating point format.  These
%      values should be corrected by multiplying them against the calibration
%      matrix typically stored in the 1st calibration matrix in the device.
%
%      Example: Retrieve the xyz values and correct them with the 1st
%               calibration matrix.
%      cMatrix = ColorCal2('ReadColorMatrix');
%      s = ColorCal2('MeasureXYZ');
%      correctedValues = cMatrix(1:3,:) * [s.x s.y s.z]';
% 'ReadColorMatrix' or 'ReadColourMatrix' - Retrieves all 3 color
%      calibration matrices from the device and returns them as a 9x3 matrix.
%      Each set of 3 rows represents a single calibration matrix.  All
%      values will be in floating point format.
% 'SetLEDFunction' - Controls whether the LED is illuminated when the
%      trigger signal is generated.  This state is stored in non-volatile
%      memory and will survive a power cycle.  Takes 1 additional argument:
%      0 or 1.  0 = LED not active when triggered, 1 = LED active when
%      triggered.
% 'SetTriggerThreshold' - Sets the threshold which must be exceeded by the
%      first derivative of the trigger ADC before a trigger pulse is
%      generated.  It is stored in non-volatile memory and will survive a
%      power cycle.  Takes 1 additional argument which is the trigger
%      threshold value.
% 'StartBootloader' - Causes the ColorCal2 to start its internal bootloader
%      in preparation for a firmware upgrade.
% 'ZeroCalibration' - Removes small zero errors in the electronic system of
%      the ColorCal2 device.  It reads the current light level and stores
%      the readings in a zero correction array.  All subsequent light
%      readings have this value subtracted from them before being returned.
%      This command is intended to be issued when the ColorCal2 is in the
%      dark.  Returns 1 if the command succeeds, 0 if it fails.  This
%      command must be run after every power cycle of the device.

% *** These functions are for internal use only.  Using these functions may
% *** brick your device.  These functions are commented out below, so they
% *** must be uncommented before they can be run.
%
% 'ResetEEProm' - Resets the non-volatile memory on the ColorCal2 to its
%      default value.  ALL CALIBRATION DATA IS ERASED.
% 'SetColorMatrix' or 'SetColourMatrix' - Writes data into one of the
%      calibration matrices.  Takes 2 additional arguments: matrix number, 
%      and a color matrix.  The ColorCal2 has 3 calibration matrices
%      indexed in the range [0,2].  The color matrix must be 3x3 in
%      floating point format.  The values contained within the color matrix
%      will be converted to a Minolta format internally.
%
%      Example: Set the first color matrix to some random values.
%      ColorCal2('SetColorMatrix', 0, rand(3,3));

% History:
% 03.04.2009  Written (Chris Broussard).
% 18.08.2010  Make useBigEndian persistent. Reported by Ian A. ("tatooed.tentackle@yahoo.com")
% 19.01.2013  Make compatible with GNU/Octave-3 (Mario Kleiner).

persistent usbHandle;
persistent useBigEndian;

varargout = {};

if nargin == 0
	error('Usage: varargout = ColorCal2(command, [varargin])');
end

if ~ischar(command)
	error('command must be a string.');
end

% Connect to the ColorCal2 if we haven't already.
if isempty(usbHandle)
    LoadPsychHID;

	usbHandle = PsychHID('OpenUSBDevice', 2145, 4097);
	disp('- ColorCal2 connected');
	
	% Determine the byte order of the machine.
	x = typecast(uint32(1), 'uint8');
	if x(4) == 1
		useBigEndian = true;
	else
		useBigEndian = false;
	end
end

% Currently, we don't vary this value.
bRequest = 0;

switch lower(command)
	case {'close', 'cls'}
		PsychHID('CloseUSBDevice', usbHandle);
		disp('- ColorCal2 closed');
		usbHandle = [];
		
	case {'ledon', 'lon'}
		bmRequestType = hex2dec('40');
		wValue = 2;
		wIndex = 0;
		wLength = 0;
		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength);

	case {'ledoff', 'lof'}
		bmRequestType = hex2dec('40');
		wValue = 3;
		wIndex = 0;
		wLength = 0;
		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength);
		
	case {'measurexyz', 'mes'}
		bmRequestType = hex2dec('40');
		wValue = 1;
		wIndex = 0;
		wLength = 3;
		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength, uint8('MES'));
		bmRequestType = hex2dec('C0');
		wLength = 32;
		outString = char(PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength));
		
		% Parse the output string.
		a = sscanf(outString, 'OK00,%6f,%6f,%6f');
		s.x = a(1);
		s.y = a(2);
		s.z = a(3);
		
		varargout(1) = {s};
		
	case {'zerocalibration', 'uzc'}
		bmRequestType = hex2dec('40');
		wValue = 1;
		wLength = 3;
		wIndex = 0;
		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength, uint8('UZC'));
		bmRequestType = hex2dec('C0');
		wLength = 32;
		outString = char(PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength));
		
		% Parse the output string.
		switch outString(1:4)
			case 'OK00'
				varargout(1) = {true};
			case 'ER11'
				varargout(1) = {false};
			otherwise
				error('Failed to parse output string from the ColorCal2');
		end
		
	case {'getrawdata', 'grd'}
		bmRequestType = hex2dec('C0');
		wValue = 4;
		wLength = 28;
		wIndex = 0;
		outData = PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength);
		
		% Each 4 bytes represents one value, so we must concatenate each
		% group of 4 bytes to get number we want.  On big endian systems we
		% must swap the byte order because the USB bus works in little
		% endian mode.
		extractedData = [];
		for i = 1:4:28
			% Read 4 bytes of data and convert it into an uint32 value.
			y = typecast(outData(i:i+3), 'uint32');
			
			% Swap the byte order if on a big endian machine.
			if useBigEndian
				y = swapbytes(y);
			end
			
			extractedData(end+1) = double(y); %#ok<NASGU>
		end
		
		% Create a struct to hold the results.
		d.Xdata = extractedData(1);
		d.Xzero = extractedData(2);
		d.Ydata = extractedData(3);
		d.Yzero = extractedData(4);
		d.Zdata = extractedData(5);
		d.Zzero = extractedData(6);
		d.Trigger = extractedData(7);
		
		varargout(1) = {d};
		
	case {'readcolormatrix', 'readcolourmatrix', 'rcm'}
		colorMatrix = zeros(9, 3);
		
		% Get data for each row in the color matrices.  There are 9 rows, 3
		% per matrix.
		for i = 1:9
			bmRequestType = hex2dec('40');
			wValue = 1;
			wLength = 3;
			wIndex = 0;
			
			% Grab row 'i' of the color matrix.
			PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength, uint8(sprintf('r0%d', i)));
			bmRequestType = hex2dec('C0');
			wLength = 32;
			outString = char(PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength));

			% Parse the output string and store the values returned.
			colorMatrix(i, :) = sscanf(outString, 'OK00,%d,%d,%d')';
		end

		% Convert the matrix values from Minolta format to floating point.
		varargout(1) = {Minolta2Float(colorMatrix)};
		
	case {'deviceinfo', 'idr'}
		bmRequestType = hex2dec('40');
		wValue = 1;
		wLength = 3;
		wIndex = 0;
		
		% Get the ColorCal2 device info.
		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength, uint8('IDR'));
		bmRequestType = hex2dec('C0');
		wLength = 32;
		outString = char(PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength));
		
		% Parse the device info string.
		x = sscanf(outString, 'OK00,1,%d,100.10,%d,%d');
		
		% Set the output: rom version, serial number, and build number.
		dInfo.romVersion = x(1);
		dInfo.serialNumber = x(2);
		dInfo.buildNumber = x(3);
		varargout(1) = {dInfo};
		
% 	case {'reseteeprom', 'rse'}
% 		bmRequestType = hex2dec('40');
% 		wValue = 7;
% 		wLength = 0;
% 		wIndex = 0;
% 		
% 		% Send the reset command.
% 		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength);
		
	case {'startbootloader', 'sbl'}
		bmRequestType = hex2dec('40');
		wValue = 99;
		wLength = 0;
		wIndex = 0;
		
		% Send the start bootloader command.
		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength);
		
	case {'settriggerthreshold', 'stt'}
		bmRequestType = hex2dec('40');
		wValue = 8;
		wLength = 0;
		
		% Make sure a trigger value was passed.
		if nargin ~= 2
			error('Usage: ColorCal2(''SetTriggerThreshold'', triggerValue)');
		end
		
		% Make sure the trigger value is scalar.
		wIndex = varargin{1};
		if ~isscalar(wIndex)
			error('triggerValue must be scalar.');
		end
		
		% Send the set trigger threshold command.
		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength);
		
	case {'setledfunction', 'slf'}
		bmRequestType = hex2dec('40');
		wValue = 9;
		wLength = 0;
		
		% Make sure a LED function value was passed.
		if nargin ~= 2
			error('Usage: ColorCal2(''SetLEDFunction'', ledFunctionValue)');
		end
		
		% Make sure that the function value is 0 or 1.
		wIndex = varargin{1};
		if ~isscalar(wIndex)
			error('ledFunctionValue must be a scalar.');
		end
		if ~any(wIndex == [0 1])
			error('ledFunctionValue must be 0 or 1.');
		end
		
		% Send the new LED function value.
		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, wValue, wIndex, wLength);

% 	case {'setcolormatrix', 'setcolourmatrix', 'scm'}
% 		bmRequestType = hex2dec('40');
% 		wValue = 6;
% 		wLength = 18;
% 		
% 		% Check the number of input arguments.
% 		if nargin ~= 3
% 			error('Usage: ColorCal2(''SetColorMatrix'', matrixNumber, colorMatrix)');
% 		end
% 		
% 		% Make sure that the 2nd argument is a valid matrix number.
% 		matrixNumber = varargin{1};
% 		if isscalar(matrixNumber)
% 			if ~any(matrixNumber == [0 1 2])
% 				error('matrixNumber must be an integer in the range [0,2].');
% 			end
% 		else
% 			error('matrixNumber must be a scalar value in the range [0,2].');
% 		end
% 		wIndex = matrixNumber;
% 		
% 		% Make sure that argument 3 is a valid 3x3 color matrix.
% 		colorMatrix = varargin{2};
% 		if isnumeric(colorMatrix)
% 			if ndims(colorMatrix) ~= 2 || ~all(size(colorMatrix) == [3 3])
% 				error('colorMatrix must be a 3x3 matrix.'); 
% 			end
% 		else
% 			error('colorMatrix must be a numeric matrix');
% 		end
% 		
% 		% Convert the color matrix from floating point to Minolta format.
% 		colorMatrix = Float2Minolta(colorMatrix);
% 		
% 		% Repackage the color matrix into a 8 bit vector where each matrix
% 		% value is represented by 2 bytes.
% 		newColorMatrix = uint16([colorMatrix(1,:) colorMatrix(2,:) colorMatrix(3,:)]);
% 		if useBigEndian
% 			newColorMatrix = swapbytes(newColorMatrix);
% 		end
% 		newColorMatrix = typecast(newColorMatrix, 'uint8');
% 		
% 		% Send over the new color matrix.
% 		PsychHID('USBControlTransfer', usbHandle, bmRequestType, bRequest, ...
% 			wValue, wIndex, wLength, newColorMatrix);
		
	otherwise
		error('Invalid command: %s', command);
end