APIs: Difference between revisions
Appearance
Wengelhardt (talk | contribs) Created page with "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 bo..." |
Wengelhardt (talk | contribs) First draft of parallel versions, not edited yet to make them exactly parallel |
||
| Line 28: | Line 28: | ||
|[[Contributions:BCI2000RemoteNET#Examples|Examples]] | |[[Contributions:BCI2000RemoteNET#Examples|Examples]] | ||
|} | |} | ||
=Parallel Examples= | |||
<div class="noresize"> | |||
{|class="wikitable" | |||
|'''Operator Scripting''' | |||
<syntaxhighlight> | |||
#! ../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 --LogKeyboard=1 --SpinningWheel=1 --ShowDisplayStatistics=1 | |||
Start executable P3SignalProcessing --local | |||
Start executable StimulusPresentation --local | |||
Wait for Connected | |||
Load parameterfile "../parms/examples/StimulusPresentation_SignalGenerator.prm" | |||
</syntaxhighlight> | |||
|'''BCI2000RemoteNET''' | |||
<syntaxhighlight lang="c#"> | |||
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/StimulusPresentation_SignalGenerator.prm"); | |||
bci.StartupModules(new Dictionary<string, IEnumerable<string>?> { | |||
{"SignalGenerator", new() {"LogKeyboard=1", "SpinningWheel=1", "ShowDisplayStatistics=1"}}, | |||
{"P3SignalProcessing", null}, | |||
{"StimulusPresentation", null} | |||
}); | |||
</syntaxhighlight> | |||
|- | |||
|'''Python''' | |||
<syntaxhighlight lang="python"> | |||
bci = BCI2000Remote() | |||
print("Operator path: " + bci.OperatorPath) | |||
bci.WindowVisible = True | |||
bci.WindowTitle = "Python controlled" | |||
bci.SubjectID = "pysub" | |||
bci.Connect() | |||
bci.Execute("cd ${BCI2000LAUNCHDIR}") | |||
bci.StartupModules(("SignalGenerator", "ARSignalProcessing", "CursorTask")) | |||
bci.LoadParametersRemote("../parms/examples/CursorTask_SignalGenerator.prm") | |||
bci.SetConfig() | |||
print("SubjectName parameter: " + bci.GetParameter("SubjectName")) | |||
bci.Start() | |||
bci.Execute("Wait for Suspended 5") | |||
if bci.Result != "":print("Result: " + bci.Result) | |||
bci.Stop() | |||
del bci | |||
</syntaxhighlight> | |||
|'''Matlab''' | |||
<syntaxhighlight lang="matlab"> | |||
clc;clear; | |||
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', 'SpectralSignalProcessing', 'CursorTask'}); | |||
calllib('bci', 'BCI2000Remote_StartupModules2', bciHandle, modules, 3); | |||
calllib('bci', 'BCI2000Remote_LoadParametersRemote', bciHandle, '../parms/examples/CursorTask_SignalGenerator.prm'); | |||
%% | |||
%add states | |||
calllib('bci', 'BCI2000Remote_AddStateVariable', bciHandle,'matlab',8, 0); | |||
calllib('bci', 'BCI2000Remote_SetConfig', bciHandle); | |||
calllib('bci', 'BCI2000Remote_Execute', bciHandle,'Show window watches',0); | |||
calllib('bci', 'BCI2000Remote_Execute', bciHandle,'visualize watch matlab',0); | |||
%start | |||
calllib('bci', 'BCI2000Remote_Start', bciHandle); | |||
pause(5); | |||
%% send the behavior data to BCI2000 | |||
for i = 1:10 | |||
calllib('bci', 'BCI2000Remote_SetStateVariable', bciHandle,'matlab', i); | |||
pause(1); | |||
end | |||
calllib('bci', 'BCI2000Remote_Delete', bciHandle); | |||
</syntaxhighlight> | |||
|- | |||
|colspan="2"|'''C++''' | |||
<syntaxhighlight lang="cpp"> | |||
#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 | |||
const char* modules[] = { "SignalGenerator --LogMouse=1", "ARSignalProcessing", "CursorTask" }; | |||
std::vector<std::string> vModules( &modules[0], &modules[0] + sizeof( modules ) / sizeof( *modules ) ); | |||
if( !bci.StartupModules( vModules ) ) | |||
{ | |||
std::cerr << bci.Result(); | |||
return -1; | |||
} | |||
// Load a parameter file, and set subject information | |||
bci.LoadParametersRemote( "../parms/examples/CursorTask_SignalGenerator.prm" ); | |||
bci.SubjectID( "SUB" ); | |||
// Start a run | |||
if( !bci.Start() ) | |||
{ | |||
std::cerr << bci.Result(); | |||
return -1; | |||
} | |||
// Print feedback signal | |||
std::string state; | |||
while( bci.GetSystemState( state ) && state == "Running" ) | |||
{ | |||
double value = 0; | |||
bci.GetControlSignal( 1, 1, value ); | |||
std::cout << "Control signal: " << value << ", press Enter to proceed" << std::flush; | |||
std::string line; | |||
std::getline( std::cin, line ); | |||
} | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||
|} | |||
</div> | |||
Revision as of 17:44, 18 April 2025
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 |
| Windows (C#, .NET, JScript) | Documentation | Examples |
| Unity | Documentation | Examples |
Parallel Examples
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 --LogKeyboard=1 --SpinningWheel=1 --ShowDisplayStatistics=1
Start executable P3SignalProcessing --local
Start executable StimulusPresentation --local
Wait for Connected
Load parameterfile "../parms/examples/StimulusPresentation_SignalGenerator.prm" |
BCI2000RemoteNET
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/StimulusPresentation_SignalGenerator.prm");
bci.StartupModules(new Dictionary<string, IEnumerable<string>?> {
{"SignalGenerator", new() {"LogKeyboard=1", "SpinningWheel=1", "ShowDisplayStatistics=1"}},
{"P3SignalProcessing", null},
{"StimulusPresentation", null}
});
|
Python
bci = BCI2000Remote()
print("Operator path: " + bci.OperatorPath)
bci.WindowVisible = True
bci.WindowTitle = "Python controlled"
bci.SubjectID = "pysub"
bci.Connect()
bci.Execute("cd ${BCI2000LAUNCHDIR}")
bci.StartupModules(("SignalGenerator", "ARSignalProcessing", "CursorTask"))
bci.LoadParametersRemote("../parms/examples/CursorTask_SignalGenerator.prm")
bci.SetConfig()
print("SubjectName parameter: " + bci.GetParameter("SubjectName"))
bci.Start()
bci.Execute("Wait for Suspended 5")
if bci.Result != "":print("Result: " + bci.Result)
bci.Stop()
del bci
|
Matlab
clc;clear;
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', 'SpectralSignalProcessing', 'CursorTask'});
calllib('bci', 'BCI2000Remote_StartupModules2', bciHandle, modules, 3);
calllib('bci', 'BCI2000Remote_LoadParametersRemote', bciHandle, '../parms/examples/CursorTask_SignalGenerator.prm');
%%
%add states
calllib('bci', 'BCI2000Remote_AddStateVariable', bciHandle,'matlab',8, 0);
calllib('bci', 'BCI2000Remote_SetConfig', bciHandle);
calllib('bci', 'BCI2000Remote_Execute', bciHandle,'Show window watches',0);
calllib('bci', 'BCI2000Remote_Execute', bciHandle,'visualize watch matlab',0);
%start
calllib('bci', 'BCI2000Remote_Start', bciHandle);
pause(5);
%% send the behavior data to BCI2000
for i = 1:10
calllib('bci', 'BCI2000Remote_SetStateVariable', bciHandle,'matlab', i);
pause(1);
end
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
const char* modules[] = { "SignalGenerator --LogMouse=1", "ARSignalProcessing", "CursorTask" };
std::vector<std::string> vModules( &modules[0], &modules[0] + sizeof( modules ) / sizeof( *modules ) );
if( !bci.StartupModules( vModules ) )
{
std::cerr << bci.Result();
return -1;
}
// Load a parameter file, and set subject information
bci.LoadParametersRemote( "../parms/examples/CursorTask_SignalGenerator.prm" );
bci.SubjectID( "SUB" );
// Start a run
if( !bci.Start() )
{
std::cerr << bci.Result();
return -1;
}
// Print feedback signal
std::string state;
while( bci.GetSystemState( state ) && state == "Running" )
{
double value = 0;
bci.GetControlSignal( 1, 1, value );
std::cout << "Control signal: " << value << ", press Enter to proceed" << std::flush;
std::string line;
std::getline( std::cin, line );
}
return 0;
}
| |