Changes

Jump to navigation Jump to search
3,160 bytes added ,  13:50, 7 August 2019
no edit summary
Line 1: Line 1:  +
{{Infobox tsg
 +
| name          = Buttonbox (2018)
 +
| image          = Buttonbox_2018_1.png
 +
| caption        = 2018 Buttonbox
 +
| downloads      = {{bulleted list
 +
      | [https://surfdrive.surf.nl/files/index.php/s/PPTKCyrjLkN4XUO Buttonbox 2018]
 +
      | [https://pypi.python.org/pypi/RuSocSci RuSocSci] (Python package)
 +
  }}
 +
}}
 
{{Infobox tsg
 
{{Infobox tsg
 
| name          = Buttonbox
 
| name          = Buttonbox
Line 4: Line 13:  
| caption        = 2013 Buttonbox
 
| caption        = 2013 Buttonbox
 
| downloads      = {{bulleted list
 
| downloads      = {{bulleted list
       | [[Buttonbox 2013 Hardware|Buttonbox 2013 Hardware Design]]
+
       | [https://surfdrive.surf.nl/files/index.php/s/72XEcu2XKSgzxjp Buttonbox 2015]
      | [[Media:BITSI_tempalte2015_duemilanove.zip|Code Template 2015 (Duemilanove)]]
   
       | [https://pypi.python.org/pypi/RuSocSci RuSocSci] (Python package)
 
       | [https://pypi.python.org/pypi/RuSocSci RuSocSci] (Python package)
 
   }}
 
   }}
Line 12: Line 20:  
The buttonbox is used for time accurate(1ms) button press registration. We use it to register buttonpresses, soundkey, voicekey signals and to send tone onset, analog output, triggers with the BITSI protocol. It is suitable for Behavioral, EEG, MEG, and fMRI experiments. The buttonbox is connected to a computer with a usb connection.
 
The buttonbox is used for time accurate(1ms) button press registration. We use it to register buttonpresses, soundkey, voicekey signals and to send tone onset, analog output, triggers with the BITSI protocol. It is suitable for Behavioral, EEG, MEG, and fMRI experiments. The buttonbox is connected to a computer with a usb connection.
   −
The output connector has two binary eight bit ports: input and output. The two ports can be used for responses (input) and stimulus triggers (output). Two 8 bits analog outputs and three 8 bits analog inputs. The output connector has a sound and voicekey which triggers when a amplitude reaches a threshold. By using the serial port, the BITSI can be used platform independently: it works on Windows, Linux and Mac OSX. Most programming environments and stimulus packages support serial communication.
+
The output connector has two binary eight bit ports: input and output. The two ports can be used for responses (input) and stimulus triggers (output). Two 12 bits analog outputs and three 12 bits analog inputs. The output connector has a sound and voicekey which triggers when a amplitude reaches a threshold. By using the serial port, the BITSI can be used platform independently: it works on Windows, Linux and Mac OSX. Most programming environments and stimulus packages support serial communication.
 +
 
 +
There is currently a [[Microsoft Windows driver issue]].
    
== BITSI Protocol ==
 
== BITSI Protocol ==
Line 45: Line 55:  
|-
 
|-
 
| 8 || H / h || 72 / 104
 
| 8 || H / h || 72 / 104
|-
  −
| Soundkey || S / s || 83 / 115
  −
|-
  −
| Voicekey || V / v || 86 / 118
   
|-
 
|-
 
|}
 
|}
Line 56: Line 62:  
===Output===
 
===Output===
   −
Output knows two protocols: '''BITSI simple''' or '''BITSI extended'''. To enter a certain protocol two buttons have to be pressed when the BITSIbox is powered. '''Press button H and A for simple mode and H and B for extended mode.'''
+
Output knows two protocols: '''BITSI simple''' or '''BITSI extended'''. To enter a certain protocol two buttons have to be pressed when the BITSIbox is powered or com port opens. '''[2015]Press button H and A for simple mode and H and B for extended mode. [2018]Press button E and A for simple mode and E and B for extended mode.'''
    
If no button is pressed when powered it boots the last known protocol. In the simple protocol every byte sent to the BITSI over the serial port, is represented at the 8 bit output.
 
If no button is pressed when powered it boots the last known protocol. In the simple protocol every byte sent to the BITSI over the serial port, is represented at the 8 bit output.
Line 113: Line 119:     
The 25 pins female connector has 8 inputs and 8 outputs, respectively 1-8 are inputs and 9-16 are outputs. Three analog input with an analog to digital convertor of 12 bit, pins 17,18,20 and 21. Two analog outputs with an digital to analog convertor of 12 bits on pins 22 and 23.
 
The 25 pins female connector has 8 inputs and 8 outputs, respectively 1-8 are inputs and 9-16 are outputs. Three analog input with an analog to digital convertor of 12 bit, pins 17,18,20 and 21. Two analog outputs with an digital to analog convertor of 12 bits on pins 22 and 23.
 +
 +
The inputs 1-8 will be pulled down from 5V to GND when the buttons are pressed. The outputs 9-16 will be pulled up from GND to 5V when the output is activated.
    
===Serial port===
 
===Serial port===
Line 176: Line 184:  
* Rate must be set 115200, Parity to None, Data Bits to 8 and Stop Bits to 1, Uncheck FIFO Interrupt.
 
* Rate must be set 115200, Parity to None, Data Bits to 8 and Stop Bits to 1, Uncheck FIFO Interrupt.
   −
[[File:Buttonbox1.png]]
+
[[File:Buttonbox2.png]]
   −
'''Adding Marker (output)'''
+
'''Testing Buttonbox'''
   −
[[file:output_buttonbox1.png | 800px]]
+
When pressing on the A button within the input channel tester. You will see the following ASCII code.
   −
'''Testing Buttonbox'''
+
[[file:testbuttonbox.png]]
   −
When pressing on the A button within the input channel tester. You will see the following output.
+
'''Adding Marker'''
   −
[[file:testbuttonbox.png]]
+
[[file:output_buttonbox1.png | 800px]]
    
'''Testing Markers (output)'''
 
'''Testing Markers (output)'''
Line 215: Line 223:  
=== Python/PsychoPy ===
 
=== Python/PsychoPy ===
   −
Download this site-package to use the buttonbox: [https://pypi.python.org/pypi/RuSocSci rusocsci]
+
Download this site-package to use the buttonbox: [https://pypi.python.org/pypi/RuSocSci rusocsci]  
 +
 
 +
or use in windows command 'pip install --upgrade rusocsci'
    
'''Example using buttons from the buttonbox in Python:'''
 
'''Example using buttons from the buttonbox in Python:'''
Line 248: Line 258:  
# send a marker
 
# send a marker
 
bb.sendMarker(val=100)    #This is your marker code, range code 1-255  
 
bb.sendMarker(val=100)    #This is your marker code, range code 1-255  
 +
</syntaxhighlight>
 +
 +
'''Example using BITSI extended in Python:'''
 +
 +
<syntaxhighlight lang="python" line>
 +
#!/usr/bin/env python
 +
 +
# import the rusocsci.buttonbox module
 +
from rusocsci import buttonbox
 +
 +
# make a buttonbox
 +
bb = buttonbox.Buttonbox()
 +
 +
# select a function
 +
bb.sendMarker(val=(ord(X)))    #select pulse time
 +
bb.sendMarker(val=2)          #set time of dureation pulse to 2ms
 +
 +
bb.sendMarker(val=(ord(M)))    #select marker out
 +
bb.sendMarker(val=115)          #set marker value 115
 +
</syntaxhighlight>
 +
 +
'''Example using BITSI extended analog read in Python:'''
 +
 +
<syntaxhighlight lang="python" line>
 +
#!/usr/bin/env python
 +
 +
# import the rusocsci.buttonbox module
 +
import serial
 +
 +
# make a buttonbox
 +
ser = serial.Serial("COM2", 115200, timeout = 0.10 )
 +
ser = serial.Serial("/dev/ttyUSB0", 115200, timeout = 0.10 )
 +
 +
while True:
 +
ser.write('A1')
 +
ser.flush()
 +
x = ser.readline()
 +
visual.TextStim(win, text=x).draw()
 +
 +
# black screen for 1000 ms
 +
win.flip()
 +
 +
key = event.getKeys()
 +
try:
 +
if key[0]=='escape':
 +
break
 +
except:
 +
continue
 
</syntaxhighlight>
 
</syntaxhighlight>
   Line 285: Line 343:     
<syntaxhighlight lang="matlab" line style="height:50em; overflow:auto;">
 
<syntaxhighlight lang="matlab" line style="height:50em; overflow:auto;">
function handle = buttonbox(cmd,varargin)
+
function ret = buttonbox(cmd,varargin)
% to initialize connection:
+
% to initialize connection: (omit 2nd argument if defaults apply)
 
%    define settings as structure with fields:
 
%    define settings as structure with fields:
 
%      bb.Device    = 'COM2';
 
%      bb.Device    = 'COM2';
Line 295: Line 353:  
% handle = buttonbox('open',bb)
 
% handle = buttonbox('open',bb)
 
%
 
%
% to run:
+
% to run: (receiving incoming data, check code for own purposes)
% buttonbox('run',handle);
+
% buttonbox('run');
 +
%
 +
% or
 +
%
 +
% to send a marker: (marker: a numeric value; Fs: sampling frequency device)
 +
% buttonbox(marker,Fs)
 +
%
 +
% or
 +
%
 +
% to wait for a buttonpress:
 +
% buttonbox('clear'); (make sure buttonbox buffer is emptied)
 +
% key = buttonbox('wait_keypress')
 
%
 
%
 
% to close the connection:
 
% to close the connection:
% buttonbox('close',handle);
+
% buttonbox('close');  
 
  −
persistent old_hdl  % keep handle to COM object persistent
      +
persistent old_hdl  % keep handle to COM object persistent
 
% set defaults
 
% set defaults
 
bb.Device    = 'COM2';
 
bb.Device    = 'COM2';
Line 308: Line 376:  
bb.DataBits  = 8;
 
bb.DataBits  = 8;
 
bb.StopBits  = 1;
 
bb.StopBits  = 1;
bb.Parity    = 'none';
+
bb.Parity    = 'none';  
    
if nargin < 1
 
if nargin < 1
    cmd = 'open';
+
  cmd = 'open';
 +
end
 +
if nargin > 1 && isstruct(varargin{1})
 +
  % user overwrites default settings
 +
  flds = fields(varargin{1});
 +
  for n = 1 : numel(flds)
 +
      bb.(flds{n}) = varargin{1}.(flds{n});
 +
  end
 +
end
 +
if nargin==1 && isnumeric(cmd)
 +
  error('Please also specify acquisition sampling frequency of device that receives the marker');
 
end
 
end
if nargin > 1
+
if nargin > 1 && isnumeric(cmd)
    % user overwrites default settings
+
  marker = cmd;
    flds = fields(varargin{1});
+
  cmd = 'marker';
    for n = 1 : numel(flds)
+
  Fs = varargin{1};
        bb.(flds{n}) = varargin{1}.(flds{n});
  −
    end
   
end
 
end
    +
if ~any(strcmp(cmd,{'open','close'}))
 +
  if isempty(old_hdl)
 +
      help serial_buttonbox_common
 +
      error('Buttonbox not yet initialized');
 +
  end
 +
  handle = old_hdl;
 +
end
    
switch cmd
 
switch cmd
    case 'open'
+
  case 'marker'
        % get handle to serial device
+
      fwrite(handle, uint8(marker));%IOPort('Write', handle, uint8(marker), 1); % last argument: blocking
        handle = open_buttonbox(bb);
+
      WaitSecs(2/Fs);
        return
+
      fwrite(handle, uint8(0));%IOPort('Write', handle, uint8(0), 0); % last argument: blocking
    case 'close'
+
      return
        if nargin > 0
+
  case 'clear'
            handle = varargin{1};
+
      while(handle.BytesAvailable)
        else
+
        fread(handle, 1);
            handle = old_hdl;
+
      end     
        end
+
      ret = []; % meaningless
        fclose(handle);
+
      return
        return
+
  case 'open'
    case 'run'
+
      % get handle to serial device
        % read incoming data
+
      handle = open_buttonbox(bb);
        if isempty(old_hdl)
+
      ret = handle;
             help serial_buttonbox_common
+
      return
             error('Buttonbox not yet initialized');
+
  case 'close'
        end
+
      if nargin > 1
        handle = old_hdl;
+
        handle = varargin{1};
    otherwise
+
      else
        fprintf('Unknown option %s\n',cmd);
+
        handle = old_hdl;
        return
+
      end
end
+
      fclose(handle);
 +
      delete(handle);
 +
      ret = [];
 +
      return
 +
  case 'run'
 +
      % read incoming data
 +
      % code proceeds below ....
 +
  case 'wait_keypress'
 +
      % start polling for characters (indicating start of scan)
 +
      while(1)
 +
        data = [];
 +
        while handle.BytesAvailable
 +
            navailable = handle.BytesAvailable;
 +
            % read incoming data
 +
            [newdata, cnt] = fread(handle, navailable);
 +
             % concatenate possible new data
 +
             if cnt
 +
              data = [data newdata(:)];
 +
            end
 +
        end
 +
        if ~isempty(data)
 +
            ret = data;
 +
            return
 +
        end
 +
      end
 +
  otherwise
 +
      fprintf('Unknown option %s\n',cmd);
 +
      ret = [];
 +
      return          
 +
end  
   −
% only gets here when cmd = 'run'
+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 +
% only gets here when cmd = 'run' %
 +
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% Initialize output figure
 
% Initialize output figure
 
win = list_output(' ',[]);
 
win = list_output(' ',[]);
 
while 1
 
while 1
    % Exit if user closed output figure
+
  % Exit if user closed output figure
    if ~ishandle(win)
+
  if ~ishandle(win)
        return
+
      return
    end
+
  end
    % start polling for characters (indicating start of scan)
+
  % start polling for characters (indicating start of scan)
    navailable = handle.BytesAvailable;
+
  navailable = handle.BytesAvailable;
    if navailable
+
  if navailable
        data = [];
+
      data = [];
        while navailable
+
      while navailable
            % read incoming data
+
        % read incoming data
            [newdata, cnt] = fread(handle, navailable);
+
        [newdata, cnt] = fread(handle, navailable);
            % concatenate possible new data  
+
        % concatenate possible new data
            if cnt
+
        if cnt
                data = [data newdata(:)];
+
            data = [data newdata(:)];
            end
+
        end
            % check if any more data left
+
        % check if any more data left
            navailable = handle.BytesAvailable;
+
        navailable = handle.BytesAvailable;
        end
+
      end
        % output info about which button was pressed  
+
      % output info about which button was pressed
        for n = 1 : numel(data)
+
      for n = 1 : numel(data)
            line = sprintf('incoming: %03d  %s',data(n),char(data(n)));
+
        line = sprintf('incoming: %03d  %s',data(n),char(data(n)));
            list_output(line,win);
+
        list_output(line,win);
        end
+
      end
    end
+
  end
    pause(0.01);
+
  pause(0.01);
end %while 1
+
end %while 1  
 +
 
   −
    function hdl = open_buttonbox(device)
  −
        % open handle to serial device (mini buttonbox)
  −
        try
  −
            hdl = serial(device.Device, 'Baudrate', device.BaudRate, 'DataBits', device.DataBits, 'StopBits', device.StopBits, 'Parity', device.Parity);
  −
            fopen(hdl);
  −
        catch
  −
            if ~isempty(old_hdl)
  −
                fclose(old_hdl);
  −
                delete(old_hdl);
  −
            end
  −
            hdl = serial(device.Device, 'Baudrate', device.BaudRate, 'DataBits', device.DataBits, 'StopBits', device.StopBits, 'Parity', device.Parity);
  −
            fopen(hdl);
  −
        end
  −
        old_hdl = hdl;
  −
       
  −
        fprintf('Wait for device buttonbox....\n');
  −
        tic
  −
        while hdl.BytesAvailable && toc<10
  −
            navailable = bbox.BytesAvailable;
  −
            % wait for welcome message device
  −
            fread(hdl, navailable);
  −
        end
  −
        pause(0.5);
  −
    end
     −
    function win = list_output(line,win)
+
  function hdl = open_buttonbox(device)
        persistent ptr
+
      % open handle to serial device (mini buttonbox)      WaitSecs(0.002); % just to load mex-file into memory
        persistent lines
+
      try
        persistent edt
+
        hdl = serial(device.Device, 'Baudrate', device.BaudRate, 'DataBits', device.DataBits, 'StopBits', device.StopBits, 'Parity', device.Parity);
        Maxlines = 40;  
+
        fopen(hdl);
       
+
      catch
        if isempty(win)
+
        if ~isempty(old_hdl)
             % initialize listbox output figure
+
             fclose(old_hdl);
             lines = cell(1,Maxlines);
+
             delete(old_hdl);
            [lines(1:end)]=deal({''});
+
        end
            ptr=Maxlines;
+
        hdl = serial(device.Device, 'Baudrate', device.BaudRate, 'DataBits', device.DataBits, 'StopBits', device.StopBits, 'Parity', device.Parity);
            lines(ptr) = {'Buttonbox output:'};
+
        fopen(hdl);
            idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1;
+
      end
 +
      old_hdl = hdl;     
 +
      fprintf('Wait for device buttonbox....\n');
 +
      tic
 +
      while hdl.BytesAvailable && toc<10
 +
        navailable = bbox.BytesAvailable;
 +
        % wait for welcome message device
 +
        fread(hdl, navailable);
 +
      end
 +
      pause(0.5);    
 +
  end
   −
            win = figure();
+
  function win = list_output(line,win)
            % initialize figure to hold output text
+
      persistent ptr
            edt = uicontrol('Parent',win,'Style','ListBox','HorizontalAlignment','left', ...
+
      persistent lines
 +
      persistent edt
 +
      Maxlines = 40;     
 +
      if isempty(win)
 +
        % initialize listbox output figure
 +
        lines = cell(1,Maxlines);
 +
        [lines(1:end)]=deal({''});
 +
        ptr=Maxlines;
 +
        lines(ptr) = {'Buttonbox output:'};
 +
        idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1;       
 +
        win = figure();
 +
        % initialize figure to hold output text
 +
        edt = uicontrol('Parent',win,'Style','ListBox','HorizontalAlignment','left', ...
 
             'Max',Maxlines,'BackgroundColor',[1 1 1],'Visible','on','String',lines(idxs), ...
 
             'Max',Maxlines,'BackgroundColor',[1 1 1],'Visible','on','String',lines(idxs), ...
 
             'FontSize',12,'Value',Maxlines);
 
             'FontSize',12,'Value',Maxlines);
            pos = get(win,'Position');
+
        pos = get(win,'Position');
            set(edt,'Position',[1 1 pos(3) pos(4)]);
+
        set(edt,'Position',[1 1 pos(3) pos(4)]);
        end
+
      end
        ptr = mod(ptr,Maxlines)+1; % start  
+
      ptr = mod(ptr,Maxlines)+1; % start
        lines{ptr} = line;
+
      lines{ptr} = line;
        idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1;
+
      idxs = mod(ptr:ptr+Maxlines-1,Maxlines)+1;
        set(edt,'String',lines(idxs),'Value',Maxlines);
+
      set(edt,'String',lines(idxs),'Value',Maxlines);
        drawnow;  
+
      drawnow;
    end
+
  end  
 
   
end
 
end
</syntaxhighlight>
+
end</syntaxhighlight>

Navigation menu