Jump to content

APIs

From BCI2000 Wiki

BCI2000 can be interacted with through external commands, called Application Program Interface (API) commands. BCI2000 provides its own extensive scripting language, called Operator Module Scripting. These commands can then be called with various languages, such as Matlab, Python, C++, JavaScript, and more.

This page provides links to each language's documentation page, with the specific commands that can be used. Examples for each language will then be linked at the bottom.

API Documentation and Examples

Operator Scripting Documentation Examples
Matlab Documentation Examples
Python Documentation Examples
C++ Documentation Examples
C# (.NET) Documentation Examples

Rosetta Stone

Operator Scripting
#! ../prog/BCI2000Shell
@cls & ..\prog\BCI2000Shell %0 %* #! && exit /b 0 || exit /b 1\n
Change directory $BCI2000LAUNCHDIR
Show window; Set title ${Extract file base $0}
Reset system
Startup system localhost
Start executable SignalGenerator --local --LogMouse=1 --LogKeyboard=1
Start executable SpectralSignalProcessing --local
Start executable CursorTask --local
Wait for Connected
Load parameterfile "../parms/examples/CursorTask_SignalGenerator.prm"

Set Config
Start
C# (.NET, Unity)
using BCI2000RemoteNET;
using System.RuntimeInformation.InteropServices;
BCI2000Remote bci = new(new BCI2000Connection());
bool isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
bci.connection.StartOperator("../prog/Operator" + isWindows ? ".exe" : ""); //Add file extension if on windows 
bci.connection.Connect();
bci.LoadParameters("../parms/examples/CursorTask_SignalGenerator.prm");
bci.StartupModules(new Dictionary<string, IEnumerable<string>?> {
	{"SignalGenerator", new() {"LogMouse=1", "LogKeyboard=1"}},
	{"SpectralSignalProcessing", null},
	{"CursorTask", null}
	});

bci.SetConfig();
bci.Start();
Python
bci = BCI2000Remote()
print("Operator path: " + bci.OperatorPath)
bci.WindowVisible = True
bci.WindowTitle = "Python controlled"
bci.Connect()
bci.Execute("cd ${BCI2000LAUNCHDIR}")
bci.StartupModules(("SignalGenerator --LogMouse=1 --LogKeyboard=1", "SpectralSignalProcessing", "CursorTask"))
bci.LoadParametersRemote("../parms/examples/CursorTask_SignalGenerator.prm")

bci.SetConfig()
bci.Start()

del bci


Matlab
if not(libisloaded('bci'))
    loadlibrary('C:\BCI2000.x64\prog\BCI2000RemoteLib64','C:\BCI2000.x64\src\core\Operator\BCI2000Remote\BCI2000RemoteLib.h', 'alias', 'bci')
end
libfunctions('bci')
%need to call BCI2000Remote_Delete to recover the memory
bciHandle = calllib('bci', 'BCI2000Remote_New');
calllib('bci', 'BCI2000Remote_SetOperatorPath', bciHandle,'C:/BCI2000.x64/prog/Operator');
if calllib('bci', 'BCI2000Remote_Connect', bciHandle) ~= 1
    fprintf('Could not connect to BCI2000, aborting.')
    calllib('bci', 'BCI2000Remote_Delete', bciHandle);
    return
end
calllib('bci', 'BCI2000Remote_Execute', bciHandle,'Change directory $BCI2000LAUNCHDIR',0);

calllib('bci', 'BCI2000Remote_SetWindowVisible', bciHandle,1);
modules = libpointer('stringPtrPtr', {'SignalGenerator --local --LogMouse=1 --LogKeyboard=1', 'SpectralSignalProcessing', 'CursorTask'});
calllib('bci', 'BCI2000Remote_StartupModules2', bciHandle, modules, 3);
calllib('bci', 'BCI2000Remote_LoadParametersRemote', bciHandle, '../parms/examples/CursorTask_SignalGenerator.prm');

calllib('bci', 'BCI2000Remote_SetConfig', bciHandle);
calllib('bci', 'BCI2000Remote_Start', bciHandle);

calllib('bci', 'BCI2000Remote_Delete', bciHandle);
C++
#include "BCI2000Remote.h"
#include <string>
#include <vector>
#include <iostream>

int main( int argc, char* argv[] )
{
  // Instantiate a BCI2000Remote object
  BCI2000Remote bci;
  // Assume that Operator executable resides in the same directory as this program.
  std::string path = ( argc > 0 ) ? argv[0] : "";
  size_t pos = path.find_last_of( "\\/" );
  path = ( pos != std::string::npos ) ? path.substr( 0, pos + 1 ) : "";
  // Start the Operator module, and connect
  bci.OperatorPath( path + "Operator" );
  if( !bci.Connect() )
  {
    std::cerr << bci.Result();
    return -1;
  }
  // Startup modules
  std::vector<std::string> modules{ "SignalGenerator --LogMouse=1 --LogKeyboard=1", "SpectralSignalProcessing", "CursorTask" };
  if( !bci.StartupModules( modules ) )
  {
    std::cerr << bci.Result();
    return -1;
  }
  // Load a parameter file, and set subject information
  bci.LoadParametersRemote( "../parms/examples/CursorTask_SignalGenerator.prm" );

  bci.SetConfig()
  bci.Start()

  return 0;
}