brainaccess.core#
Low-level control of BrainAccess headsets.
Start with init(), find headsets with scan(), and talk to a headset
through EEGManager. This module also holds
the library configuration (get_config(), set_config_fields() and the
helpers built on them) and the library version (get_version()). Call
close() when you are done.
For recording EEG without handling the connection yourself, use
brainaccess.utils.acquisition.EEG.
Submodules#
- brainaccess.core.annotation
- brainaccess.core.battery_info
- brainaccess.core.ble_device
- brainaccess.core.charging_settings
- brainaccess.core.device_features
- brainaccess.core.device_info
- brainaccess.core.device_model
- brainaccess.core.eeg_channel
- brainaccess.core.eeg_manager
- brainaccess.core.full_battery_info
- brainaccess.core.gain_mode
- brainaccess.core.impedance_measurement_mode
- brainaccess.core.log_level
- brainaccess.core.output_mode
- brainaccess.core.stream_rate
- brainaccess.core.time_sync_info
- brainaccess.core.version
Functions#
|
Shut BrainAccess Core down and discard its configuration. |
|
Turn the library's log file on or off. |
|
Select the Bluetooth adapter used for scanning and connecting. |
|
Set the number of samples per data chunk. |
|
Set the log verbosity. |
Get the current library configuration. |
|
Get the version of the BrainAccess Core library. |
|
|
Initialize BrainAccess Core. |
|
Scan for nearby BrainAccess headsets. |
|
Turn immediate writing of log lines on or off. |
|
Change one or more library settings. |
|
Choose the log file. |
|
Turn thread IDs in log lines on or off. |
|
Turn timestamps in log lines on or off. |
|
Set the firmware file used by a headset update. |
Package Contents#
- brainaccess.core.close()[source]#
Shut BrainAccess Core down and discard its configuration.
Destroy every
EEGManagerfirst (leave itswithblock or calldestroy()). Callingclose()when the library is not initialized does nothing. The nextinit()starts from the default configuration again.- Raises:
BrainAccessException – If an
EEGManagerstill exists, or the library fails to shut down.- Return type:
None
- brainaccess.core.config_enable_logging(enable)[source]#
Turn the library’s log file on or off.
- Parameters:
enable (bool) –
Trueto log (the default state),Falseto turn all logging off.- Returns:
Truewhen the setting was applied.- Return type:
- Raises:
BrainAccessException – If the core is not initialized.
- brainaccess.core.config_set_adapter_index(adapter_index)[source]#
Select the Bluetooth adapter used for scanning and connecting.
Only Linux can choose an adapter:
adapter_indexselectshci<N>. Windows and macOS ignore it and use the system default adapter. The adapter is checked when you scan or connect, not here.close()resets the index to 0.- Parameters:
adapter_index (int) – Adapter number, 0 to 255 (default
0).- Returns:
Truewhen the setting was applied.- Return type:
- Raises:
BrainAccessException – If
adapter_indexis not an integer from 0 to 255, or the core is not initialized.
- brainaccess.core.config_set_chunk_size(chunk_size)[source]#
Set the number of samples per data chunk.
A chunk is what the callback set with
set_callback_chunk()receives. The value takes effect at the next stream start.- Parameters:
chunk_size (int) – Samples per chunk, 1 to 100 (default
25).- Returns:
Truewhen the setting was applied.- Return type:
- Raises:
BrainAccessException – If
chunk_sizeis not an integer from 1 to 100, or the core is not initialized.
- brainaccess.core.config_set_log_level(log_level)[source]#
Set the log verbosity.
- Parameters:
log_level (LogLevel) – Lowest severity to log; more severe messages are logged too.
- Returns:
Truewhen the setting was applied.- Return type:
- Raises:
BrainAccessException – If
log_levelis not aLogLevelvalue, or the core is not initialized.
- brainaccess.core.get_config()[source]#
Get the current library configuration.
- Returns:
The settings below, with the values they have after
init(). Change them withset_config_fields().log_buffer_size(int): log buffer in bytes; default512.log_path(str): log file; default"bacore.log"in the current directory.log_level(int): aLogLevelvalue; default2(INFO).append_logs(bool): append to an existing log file instead of overwriting it; defaultFalse.timestamps_enabled(bool): timestamp each log line; defaultTrue.autoflush(bool): write each log line to disk at once; defaultFalse.thread_ids_enabled(bool): add the thread ID to each log line; defaultTrue.chunk_size(int): samples per data chunk; default25.enable_logs(bool): logging on or off; defaultTrue.update_path(str): firmware file forstart_update(); default"../updates/firmware_HALO.bin".adapter_index(int): Bluetooth adapter; default0.
- Return type:
- Raises:
BrainAccessException – If the core is not initialized.
- brainaccess.core.get_version()[source]#
Get the version of the BrainAccess Core library.
This is the version of the bundled native library, not the version of the
brainaccessPython package. It works withoutinit().- Returns:
Major, minor and patch numbers of the library.
- Return type:
- brainaccess.core.init()[source]#
Initialize BrainAccess Core.
Call it once before
scan(),get_config(),set_config_fields()or creating anEEGManager. Calling it again after a successful call does nothing and returnsTrue.Initialization starts the library with its default configuration (see
get_config()). The log filebacore.logis created in the current directory when the first line is logged.- Returns:
Trueon success.- Return type:
- Raises:
BrainAccessException – If the library cannot allocate its resources. It then stays uninitialized and the call can be repeated.
- brainaccess.core.scan()[source]#
Scan for nearby BrainAccess headsets.
The call blocks for about 5 seconds. Call
init()first.Only devices advertising a BrainAccess name (
BA <MODEL> <number>, for exampleBA HALO 042orBA EXG8 003) are returned; a renamed device is not listed. A headset that is already connected, from this or another computer, usually stops advertising and is then not found.- Returns:
The headsets found, empty if there are none.
mac_addressis the MAC address on Linux and Windows; macOS does not reveal MAC addresses, so it is the 36-character CoreBluetooth UUID there.- Return type:
- Raises:
BrainAccessException – If the library is not initialized, or Bluetooth cannot be used (no adapter, the system Bluetooth service is not reachable, or the adapter selected with
adapter_indexdoes not exist).
Notes
On Linux the adapter
hci<adapter_index>is used (seeset_config_fields()); Windows and macOS always use the system default adapter.
- brainaccess.core.set_config_autoflush(enable=True)[source]#
Turn immediate writing of log lines on or off.
With autoflush on, each line reaches the file at once, so nothing is lost if the program crashes, at some cost in speed. With it off, lines are buffered (see
buffer_sizeofset_config_path()).- Parameters:
enable (bool, optional) –
True(default here) writes every line at once. The library’s own default, without calling this function, is off.- Returns:
Truewhen the setting was applied.- Return type:
- Raises:
BrainAccessException – If the core is not initialized.
- brainaccess.core.set_config_fields(**fields)[source]#
Change one or more library settings.
Settings you do not pass keep their value. The names, types and defaults are those listed in
get_config().- Parameters:
**fields –
Any of the following keyword arguments:
log_buffer_size(int): log buffer in bytes, 0 or more. Used only whileautoflushis off;0selects the system default size. Buffered lines are written when the buffer fills or the core is closed.log_path(str): log file, at most 199 UTF-8 bytes. The file is created (or overwritten) when the first line is logged.log_level(int orLogLevel): log verbosity.append_logs(bool):Trueappends to an existing file,Falseoverwrites it.timestamps_enabled(bool): timestamp each log line.autoflush(bool): write each line to disk at once. Slower, but nothing is lost if the program crashes;log_buffer_sizeis then ignored.thread_ids_enabled(bool): add the thread ID to each log line.chunk_size(int): samples per data chunk, 1 to 100. Takes effect at the next stream start.enable_logs(bool):Falseturns all logging off.update_path(str): firmware file forstart_update(), at most 199 UTF-8 bytes. A missing file is reported when the update starts.adapter_index(int): Bluetooth adapter, 0 to 255. Used on Linux only, where it selectshci<adapter_index>; Windows and macOS use the system default adapter. The adapter is checked when you scan or connect, not here.
- Returns:
Truewhen the settings were applied.- Return type:
- Raises:
BrainAccessException – If a name is unknown, a value has the wrong type or is out of range, a string is too long, or the core is not initialized. Nothing is changed when an exception is raised.
Examples
Log everything to a file of your own:
>>> from brainaccess.core import set_config_fields >>> from brainaccess.core.log_level import LogLevel >>> set_config_fields( ... log_level=LogLevel.DEBUG, ... log_path="logs/session.log", ... append_logs=False, ... ) True
- brainaccess.core.set_config_path(file_path, append=True, buffer_size=512)[source]#
Choose the log file.
By default the library logs to
bacore.login the current directory and overwrites it. The file is created when the first line is logged.- Parameters:
file_path (str) – Absolute or relative path of the log file, at most 199 UTF-8 bytes.
append (bool, optional) –
True(default here) appends to an existing file;Falseoverwrites it. The library’s own default, without calling this function, is to overwrite.buffer_size (int, optional) – Log buffer in bytes (default
512). Lines are written when the buffer fills or the core is closed, so a smaller value keeps the file more current. Ignored whileautoflushis on;0selects the system default size.
- Returns:
Truewhen the settings were applied.- Return type:
- Raises:
BrainAccessException – If the path is longer than 199 UTF-8 bytes,
buffer_sizeis negative, or the core is not initialized. The path is not checked for existence or write access.
- brainaccess.core.set_config_thread_id(enable=True)[source]#
Turn thread IDs in log lines on or off.
Useful when debugging a multi-threaded program.
- Parameters:
enable (bool, optional) –
True(default) adds the thread ID to each line,Falseomits it.- Returns:
Truewhen the setting was applied.- Return type:
- Raises:
BrainAccessException – If the core is not initialized.
- brainaccess.core.set_config_timestamp(enable=True)[source]#
Turn timestamps in log lines on or off.
- Parameters:
enable (bool, optional) –
True(default) adds a timestamp to each line,Falseomits it.- Returns:
Truewhen the setting was applied.- Return type:
- Raises:
BrainAccessException – If the core is not initialized.
- brainaccess.core.set_config_update_path(file_path)[source]#
Set the firmware file used by a headset update.
- Parameters:
file_path (str) – Path of the firmware file, at most 199 UTF-8 bytes.
- Returns:
Truewhen the path was stored.- Return type:
- Raises:
BrainAccessException – If the path is too long or the core is not initialized. A missing file is reported by
start_update().