~ubuntu-branches/ubuntu/trusty/psychtoolbox-3/trusty-proposed

« back to all changes in this revision

Viewing changes to Psychtoolbox/PsychHardware/NetStation.m

  • Committer: Package Import Robot
  • Author(s): Yaroslav Halchenko
  • Date: 2013-11-19 23:34:50 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20131119233450-f7nf92vb8qavjmk8
Tags: 3.0.11.20131017.dfsg1-3
Upload to unsable since fresh glew has arrived to sid!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
function [status error] = NetStation(varargin)
 
1
function [status, error] = NetStation(varargin)
2
2
%
3
3
% NetStation - Basic control of the EGI/NetStation EEG recording system via
4
4
% TCP/IP network connection. (See http://www.egi.com)
6
6
% This function was developed and contributed to Psychtoolbox by Gergely Csibra, 2006-2008.
7
7
% Code is based on Rick Gilmore's routines, 2005. Thanks!
8
8
% Code adapted to PCs (and Macs with Intel architecture) by Zhao Fan, 2008.
 
9
% This function was modified by Matt Mollison to accommodate sending more
 
10
%   than just int16s to Net Station (logical, int8, int16, int32, single,
 
11
%   double, and char).
9
12
%
10
13
%
11
14
% General syntax
52
55
%                       "duration"      The duration of the event IN SECONDS.
53
56
%                                               Default: 0.001.
54
57
%                       "keycode"       The 4-char code of a key (e.g., 'tria').
55
 
%                       "keyvalue"      The integer value of the key (>=-32767 <=32767)
 
58
%                       "keyvalue"      The value of the key (any number or string)
56
59
%                       The keycode-keyvalue pairs can be repeated arbitrary times.
57
60
%
58
61
%   Uses a modified version of the TCP/UDP/IP Toolbox 2.0.5, a third party GPL'ed
97
100
                if nargin > 2
98
101
                    port = varargin{3};
99
102
                end
100
 
                c = pnet( 'tcpconnect', NetStationHostName, port )
 
103
                c = pnet( 'tcpconnect', NetStationHostName, port );
101
104
                if(c < 0)
102
105
                    status = 3;
103
106
                else
165
168
                    status=0;
166
169
                    n=n+1;
167
170
                end
168
 
                if n>=100 warning('\nNetStation synchronization did not succeed within %.1f ms\nSynchronizatoin accuracy is %.1f ms\n',NSSynchLimit,df); end
 
171
                if n>=100
 
172
                    warning('\nNetStation synchronization did not succeed within %.1f ms\nSynchronizatoin accuracy is %.1f ms\n',NSSynchLimit,df);
 
173
                end
169
174
                %fprintf('synch: %.1f ms at the %ith attempt\n',df,n-1);
170
175
            end
171
176
        case 'startrecording'
214
219
                if isnumeric(duration)
215
220
                    if duration > 120
216
221
                        duration=.001;
217
 
                    else
218
 
                        duration= duration;
 
222
                    %else
 
223
                    %    duration= duration;
219
224
                    end
220
225
                end
221
226
 
225
230
                else
226
231
                    keyn=0;
227
232
                end
228
 
                send(NSIDENTIFIER,'D',uint16(15+keyn*12),int32(start*1000),uint32(duration*1000),event(1:4),int16(0),uint8(keyn));
229
 
                for k=1:keyn
230
 
                    id=[char(varargin{(k-1)*2+5}) '    '];
231
 
                    val=int16(varargin{k*2+4});
232
 
                    send(NSIDENTIFIER,id(1:4),'shor',uint16(2),val(1));
 
233
                
 
234
                nbytes_total = 0;
 
235
                for k=1:keyn
 
236
                    % Net Staiton GES Hardware Technical Manual Appendix G
 
237
                    val = cell2mat(varargin((k * 2) + 4));
 
238
                    
 
239
                    % I don't understand it, but these 10 extra bytes need
 
240
                    % to be counted
 
241
                    extraBytes = 10;
 
242
                    
 
243
                    if isa(val,'logical')
 
244
                        % will convert to int8 because NS expects 1-byte
 
245
                        % booleans but reads as int8
 
246
                        nbytes = 1 + extraBytes;
 
247
                    elseif isa(val,'int8')
 
248
                        % will convert to int16 because NS expects 2-byte
 
249
                        % short signed integers and you should use logical
 
250
                        % true/false if you meant to have boolean data
 
251
                        nbytes = 2 + extraBytes;
 
252
                    elseif isa(val,'int16')
 
253
                        nbytes = 2 + extraBytes;
 
254
                    elseif isa(val,'int32')
 
255
                        nbytes = 4 + extraBytes;
 
256
                    elseif isa(val,'single')
 
257
                        nbytes = 4 + extraBytes;
 
258
                    elseif isa(val,'double')
 
259
                        nbytes = 8 + extraBytes;
 
260
                    elseif isa(val,'char')
 
261
                        % strings get sent as single-byte characters, as
 
262
                        % explained in pnet.m
 
263
                        nbytes = length(val) + extraBytes;
 
264
                    end
 
265
                    nbytes_total = nbytes_total + nbytes;
 
266
                end
 
267
                send(NSIDENTIFIER,'D',uint16(15 + nbytes_total),int32(start*1000),uint32(duration*1000),event(1:4),int16(0),uint8(keyn));
 
268
                
 
269
                for k=1:keyn
 
270
                    id=[char(varargin{(k-1)*2+5}), '    '];
 
271
                    
 
272
                    % Net Staiton GES Hardware Technical Manual Appendix G
 
273
                    val = cell2mat(varargin((k * 2) + 4));
 
274
                    
 
275
                    if isa(val,'logical')
 
276
                        val = int8(val);
 
277
                        dtype = 'bool';
 
278
                        nbytes = 1;
 
279
                    elseif isa(val,'int8')
 
280
                        val = int16(val);
 
281
                        dtype = 'shor';
 
282
                        nbytes = 2;
 
283
                    elseif isa(val,'int16')
 
284
                        dtype = 'shor';
 
285
                        nbytes = 2;
 
286
                    elseif isa(val,'int32')
 
287
                        dtype = 'long';
 
288
                        nbytes = 4;
 
289
                    elseif isa(val,'single')
 
290
                        dtype = 'sing';
 
291
                        nbytes = 4;
 
292
                    elseif isa(val,'double')
 
293
                        dtype = 'doub';
 
294
                        nbytes = 8;
 
295
                    elseif isa(val,'char')
 
296
                        dtype = 'TEXT';
 
297
                        nbytes = length(val);
 
298
                    end
 
299
                    send(NSIDENTIFIER,id(1:4),dtype,uint16(nbytes),val);
233
300
                end
234
301
 
235
302
                rep=receive(NSIDENTIFIER,1); %#ok<NASGU>
283
350
    case 6
284
351
        errstr='NS event: Unsuccessful';
285
352
    case 7
286
 
        errstr='Unknown NetStation command'
 
353
        errstr='Unknown NetStation command';
287
354
    otherwise
288
355
        errstr='NS unknown error';
289
356
end