ButtonBox: Difference between revisions

Jump to navigation Jump to search
improved examples
nowiki -> source
Line 411: Line 411:


== Buttonbox in Python and PsychoPy ==
== Buttonbox in Python and PsychoPy ==
Example using the buttonbox in Python:
 
<nowiki>
Example using the buttonbox in Python:  
<source lang="python>
#!/usr/bin/env python
#!/usr/bin/env python


Line 426: Line 427:
# print the button pressed
# print the button pressed
print("b: {}".format(b))
print("b: {}".format(b))
</nowiki>
</source>


Using the Buttonbox in PsychoPy
Using the Buttonbox in PsychoPy:
<nowiki>
<source lang="python>
#!/usr/bin/env python
#!/usr/bin/env python


Line 455: Line 456:
##Cleanup Section
##Cleanup Section
core.quit()
core.quit()
 
</source>
== Matlab Settings ==
== Matlab Settings ==


Line 463: Line 464:
  % to initialize connection:
  % to initialize connection:
  % handle = serial_buttonbox('open',se)
  % handle = serial_buttonbox('open',se)
  %   settings (se):
  % settings (se):
  %       define settings as a structure, i.e.:
  % define settings as a structure, i.e.:
  %       se.Device   = 'COM1';
  % se.Device = 'COM1';
  %       se.BaudRate = 115200;
  % se.BaudRate = 115200;
  %       se.DataBits = 8;
  % se.DataBits = 8;
  %       se.StopBits = 1;
  % se.StopBits = 1;
  %       se.Parity   = 0;
  % se.Parity = 0;
  %       se.PTBPath   = 'c:\MyToolboxes\PsychToolbox'
  % se.PTBPath = 'c:\MyToolboxes\PsychToolbox'
  %
  %
  % to close the connection:
  % to close the connection:
Line 478: Line 479:
   
   
  % set defaults
  % set defaults
  se.Device   = 'COM1';
  se.Device = 'COM1';
  se.BaudRate = 115200;
  se.BaudRate = 115200;
  se.DataBits = 8;
  se.DataBits = 8;
  se.StopBits = 1;
  se.StopBits = 1;
  se.Parity   = 0;
  se.Parity = 0;
  se.PTBPath   = 'c:\Pgrogram Files\PsychToolbox';
  se.PTBPath = 'c:\Pgrogram Files\PsychToolbox';
   
   
  if nargin < 1
  if nargin < 1
    cmd = 'open';
cmd = 'open';
  end
  end
  if nargin > 1
  if nargin > 1
    % user overwrites default settings
% user overwrites default settings
    flds = fields(varargin{1});
flds = fields(varargin{1});
    for n = 1 : numel(flds)
for n = 1 : numel(flds)
      se.(flds{n}) = varargin{1}.(flds{n});
se.(flds{n}) = varargin{1}.(flds{n});
    end
end
  end
  end
   
   
   
   
  switch cmd
  switch cmd
    case 'open'
case 'open'
      addpath(genpath(se.PTBPath));
addpath(genpath(se.PTBPath));
      % get handle to serial device
% get handle to serial device
      handle = open_buttonbox(se.Device);
handle = open_buttonbox(se.Device);
      return
return
    case 'close'
case 'close'
      handle = varargin{1};
handle = varargin{1};
      IOPort('close',handle);
IOPort('close',handle);
      return
return
    case 'run'
case 'run'
      % read incoming data
% read incoming data
      if isempty(old_hdl)
if isempty(old_hdl)
          help serial_buttonbox_common
help serial_buttonbox_common
          error('Buttonbox not yet initialized');
error('Buttonbox not yet initialized');
      end
end
      handle = old_hdl;
handle = old_hdl;
    otherwise
otherwise
      fprintf('Unknown option %s\n',cmd);
fprintf('Unknown option %s\n',cmd);
      return
return
  end
  end
   
   
  % only gets here when cmd = 'run'
  % only gets here when cmd = 'run'
  while 1   
  while 1  
    % start polling for characters (indicating start of scan)
  % start polling for characters (indicating start of scan)
    navailable = IOPort('BytesAvailable', handle);
navailable = IOPort('BytesAvailable', handle);
    if navailable
if navailable
      data = [];
data = [];
      while navailable
while navailable
          % read incoming data
% read incoming data
          [newdata, ~, err] = IOPort('Read', handle, 0, navailable);
[newdata, ~, err] = IOPort('Read', handle, 0, navailable);
          if ~isempty(err), disp(err); end
if ~isempty(err), disp(err); end
          data = [data newdata];
data = [data newdata];
          %pause(0.001); % if possible just add a small pause to not claim entire core
%pause(0.001); % if possible just add a small pause to not claim entire core
          navailable = IOPort('BytesAvailable', handle);
navailable = IOPort('BytesAvailable', handle);
      end
end
      if numel(data)>1
if numel(data)>1
          fprintf('\nReceived characters: %d\n',numel(data));
fprintf('\nReceived characters: %d\n',numel(data));
      end
end
      for n = 1 : numel(data)
for n = 1 : numel(data)
  %       disp(char(data(n)));
  % disp(char(data(n)));
          fprintf('incoming: %d\t%s\n',data(n),char(data(n)));
fprintf('incoming: %d\t%s\n',data(n),char(data(n)));
      end
end
    end
end
 
 
  end %while 1
  end %while 1
   
   
    function hdl = open_buttonbox(device)
function hdl = open_buttonbox(device)
      % open handle to serial device (mini buttonbox)
% open handle to serial device (mini buttonbox)
      try
try
          hdl = IOPort('OpenSerialPort',device,['BaudRate=' num2str(se.BaudRate)]);
hdl = IOPort('OpenSerialPort',device,['BaudRate=' num2str(se.BaudRate)]);
      catch
catch
          if ~isempty(old_hdl)
if ~isempty(old_hdl)
            IOPort('close',old_hdl);
IOPort('close',old_hdl);
          end
end
          hdl = IOPort('OpenSerialPort',device,['BaudRate=' num2str(se.BaudRate)]);
hdl = IOPort('OpenSerialPort',device,['BaudRate=' num2str(se.BaudRate)]);
      end
end
      old_hdl = hdl;
old_hdl = hdl;
     
      fprintf('Wait for device buttonbox....\n');
fprintf('Wait for device buttonbox....\n');
      tic
tic
      while ~IOPort('BytesAvailable', hdl) && toc<10
while ~IOPort('BytesAvailable', hdl) && toc<10
          % wait for welcome message device
% wait for welcome message device
      end
end
      pause(0.5);
pause(0.5);
% clear buffer
%IOPort('flush', hdl);
IOPort('purge', hdl);
end
   
   
      % clear buffer
      %IOPort('flush', hdl);
      IOPort('purge', hdl);
    end
 
  end</nowiki>
  end</nowiki>