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#

Functions#

close()

Shut BrainAccess Core down and discard its configuration.

config_enable_logging(enable)

Turn the library's log file on or off.

config_set_adapter_index(adapter_index)

Select the Bluetooth adapter used for scanning and connecting.

config_set_chunk_size(chunk_size)

Set the number of samples per data chunk.

config_set_log_level(log_level)

Set the log verbosity.

get_config()

Get the current library configuration.

get_version()

Get the version of the BrainAccess Core library.

init()

Initialize BrainAccess Core.

scan()

Scan for nearby BrainAccess headsets.

set_config_autoflush([enable])

Turn immediate writing of log lines on or off.

set_config_fields(**fields)

Change one or more library settings.

set_config_path(file_path[, append, buffer_size])

Choose the log file.

set_config_thread_id([enable])

Turn thread IDs in log lines on or off.

set_config_timestamp([enable])

Turn timestamps in log lines on or off.

set_config_update_path(file_path)

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 EEGManager first (leave its with block or call destroy()). Calling close() when the library is not initialized does nothing. The next init() starts from the default configuration again.

Raises:

BrainAccessException – If an EEGManager still 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) – True to log (the default state), False to turn all logging off.

Returns:

True when the setting was applied.

Return type:

bool

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_index selects hci<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:

True when the setting was applied.

Return type:

bool

Raises:

BrainAccessException – If adapter_index is 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:

True when the setting was applied.

Return type:

bool

Raises:

BrainAccessException – If chunk_size is 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:

True when the setting was applied.

Return type:

bool

Raises:

BrainAccessException – If log_level is not a LogLevel value, 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 with set_config_fields().

  • log_buffer_size (int): log buffer in bytes; default 512.

  • log_path (str): log file; default "bacore.log" in the current directory.

  • log_level (int): a LogLevel value; default 2 (INFO).

  • append_logs (bool): append to an existing log file instead of overwriting it; default False.

  • timestamps_enabled (bool): timestamp each log line; default True.

  • autoflush (bool): write each log line to disk at once; default False.

  • thread_ids_enabled (bool): add the thread ID to each log line; default True.

  • chunk_size (int): samples per data chunk; default 25.

  • enable_logs (bool): logging on or off; default True.

  • update_path (str): firmware file for start_update(); default "../updates/firmware_HALO.bin".

  • adapter_index (int): Bluetooth adapter; default 0.

Return type:

dict

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 brainaccess Python package. It works without init().

Returns:

Major, minor and patch numbers of the library.

Return type:

Version

brainaccess.core.init()[source]#

Initialize BrainAccess Core.

Call it once before scan(), get_config(), set_config_fields() or creating an EEGManager. Calling it again after a successful call does nothing and returns True.

Initialization starts the library with its default configuration (see get_config()). The log file bacore.log is created in the current directory when the first line is logged.

Returns:

True on success.

Return type:

bool

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 example BA HALO 042 or BA 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_address is the MAC address on Linux and Windows; macOS does not reveal MAC addresses, so it is the 36-character CoreBluetooth UUID there.

Return type:

list of ScannedDevice

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_index does not exist).

Notes

On Linux the adapter hci<adapter_index> is used (see set_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_size of set_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:

True when the setting was applied.

Return type:

bool

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 while autoflush is off; 0 selects 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 or LogLevel): log verbosity.

  • append_logs (bool): True appends to an existing file, False overwrites 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_size is 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): False turns all logging off.

  • update_path (str): firmware file for start_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 selects hci<adapter_index>; Windows and macOS use the system default adapter. The adapter is checked when you scan or connect, not here.

Returns:

True when the settings were applied.

Return type:

bool

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.log in 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; False overwrites 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 while autoflush is on; 0 selects the system default size.

Returns:

True when the settings were applied.

Return type:

bool

Raises:

BrainAccessException – If the path is longer than 199 UTF-8 bytes, buffer_size is 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, False omits it.

Returns:

True when the setting was applied.

Return type:

bool

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, False omits it.

Returns:

True when the setting was applied.

Return type:

bool

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:

True when the path was stored.

Return type:

bool

Raises:

BrainAccessException – If the path is too long or the core is not initialized. A missing file is reported by start_update().